From 2fc686ce73e86fe703a99735ccf7345023bb04b8 Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 18 Jun 2024 21:10:44 +0930 Subject: [PATCH] Refactor conversation.start --- .../newmessage/EnterPublicKeyFragment.kt | 99 ------------------- .../start/NewConversationFragment.kt | 2 +- .../{ => start}/newmessage/Callbacks.kt | 4 +- .../{ => start}/newmessage/NewMessage.kt | 2 +- .../newmessage/NewMessageFragment.kt | 5 +- .../newmessage/NewMessageViewModel.kt | 5 +- 6 files changed, 6 insertions(+), 111 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/EnterPublicKeyFragment.kt rename app/src/main/java/org/thoughtcrime/securesms/conversation/{ => start}/newmessage/Callbacks.kt (51%) rename app/src/main/java/org/thoughtcrime/securesms/conversation/{ => start}/newmessage/NewMessage.kt (98%) rename app/src/main/java/org/thoughtcrime/securesms/conversation/{ => start}/newmessage/NewMessageFragment.kt (90%) rename app/src/main/java/org/thoughtcrime/securesms/conversation/{ => start}/newmessage/NewMessageViewModel.kt (95%) diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/EnterPublicKeyFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/EnterPublicKeyFragment.kt deleted file mode 100644 index c327d93e4a..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/EnterPublicKeyFragment.kt +++ /dev/null @@ -1,99 +0,0 @@ -package org.thoughtcrime.securesms.conversation.new - -import android.content.ClipData -import android.content.ClipboardManager -import android.content.Context -import android.content.Intent -import android.os.Bundle -import android.text.InputType -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.view.inputmethod.EditorInfo -import android.widget.Toast -import androidx.core.view.isVisible -import androidx.core.widget.addTextChangedListener -import androidx.fragment.app.Fragment -import network.loki.messenger.R -import network.loki.messenger.databinding.FragmentEnterPublicKeyBinding -import org.session.libsession.utilities.TextSecurePreferences -import org.thoughtcrime.securesms.util.QRCodeUtilities -import org.thoughtcrime.securesms.util.hideKeyboard -import org.thoughtcrime.securesms.util.toPx - -class EnterPublicKeyFragment : Fragment() { - private lateinit var binding: FragmentEnterPublicKeyBinding - - var delegate: EnterPublicKeyDelegate? = null - - private val hexEncodedPublicKey: String - get() = TextSecurePreferences.getLocalNumber(requireContext())!! - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - binding = FragmentEnterPublicKeyBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - with(binding) { - publicKeyEditText.imeOptions = EditorInfo.IME_ACTION_DONE or 16777216 // Always use incognito keyboard - publicKeyEditText.setRawInputType(InputType.TYPE_CLASS_TEXT) - publicKeyEditText.setOnEditorActionListener { v, actionID, _ -> - if (actionID == EditorInfo.IME_ACTION_DONE) { - v.hideKeyboard() - handlePublicKeyEntered() - true - } else { - false - } - } - publicKeyEditText.addTextChangedListener { text -> createPrivateChatButton.isVisible = !text.isNullOrBlank() } - publicKeyEditText.setOnFocusChangeListener { _, hasFocus -> optionalContentContainer.isVisible = !hasFocus } - mainContainer.setOnTouchListener { _, _ -> - binding.optionalContentContainer.isVisible = true - publicKeyEditText.clearFocus() - publicKeyEditText.hideKeyboard() - true - } - val size = toPx(228, resources) - val qrCode = QRCodeUtilities.encode(hexEncodedPublicKey, size, isInverted = false, hasTransparentBackground = false) - qrCodeImageView.setImageBitmap(qrCode) - publicKeyTextView.text = hexEncodedPublicKey - publicKeyTextView.setOnCreateContextMenuListener { contextMenu, view, _ -> - contextMenu.add(0, view.id, 0, R.string.copy).setOnMenuItemClickListener { - copyPublicKey() - true - } - } - copyButton.setOnClickListener { copyPublicKey() } - shareButton.setOnClickListener { sharePublicKey() } - createPrivateChatButton.setOnClickListener { handlePublicKeyEntered(); publicKeyEditText.hideKeyboard() } - } - } - - private fun copyPublicKey() { - val clipboard = requireActivity().getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager - val clip = ClipData.newPlainText("Session ID", hexEncodedPublicKey) - clipboard.setPrimaryClip(clip) - Toast.makeText(requireContext(), R.string.copied_to_clipboard, Toast.LENGTH_SHORT).show() - } - - private fun sharePublicKey() { - val intent = Intent() - intent.action = Intent.ACTION_SEND - intent.putExtra(Intent.EXTRA_TEXT, hexEncodedPublicKey) - intent.type = "text/plain" - startActivity(intent) - } - - private fun handlePublicKeyEntered() { - val hexEncodedPublicKey = binding.publicKeyEditText.text?.trim()?.toString() - if (hexEncodedPublicKey.isNullOrEmpty()) return - delegate?.handlePublicKeyEntered(hexEncodedPublicKey) - } -} - -fun interface EnterPublicKeyDelegate { - fun handlePublicKeyEntered(publicKey: String) -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt index e02ea59870..c5873834bf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/NewConversationFragment.kt @@ -17,9 +17,9 @@ import dagger.hilt.android.AndroidEntryPoint import network.loki.messenger.R import org.session.libsession.utilities.Address import org.session.libsession.utilities.modifyLayoutParams -import org.thoughtcrime.securesms.conversation.new.NewMessageFragment import org.thoughtcrime.securesms.conversation.start.home.NewConversationHomeFragment import org.thoughtcrime.securesms.conversation.start.invitefriend.InviteFriendFragment +import org.thoughtcrime.securesms.conversation.start.newmessage.NewMessageFragment import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 import org.thoughtcrime.securesms.groups.CreateGroupFragment import org.thoughtcrime.securesms.groups.JoinCommunityFragment diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/Callbacks.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/Callbacks.kt similarity index 51% rename from app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/Callbacks.kt rename to app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/Callbacks.kt index d03f0c9962..02d39b1327 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/Callbacks.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/Callbacks.kt @@ -1,6 +1,6 @@ -package org.thoughtcrime.securesms.conversation.newmessage +package org.thoughtcrime.securesms.conversation.start.newmessage -interface Callbacks { +internal interface Callbacks { fun onChange(value: String) {} fun onContinue() {} fun onScanQrCode(value: String) {} diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessage.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessage.kt similarity index 98% rename from app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessage.kt rename to app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessage.kt index 065bf16cda..20465d3230 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessage.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessage.kt @@ -1,4 +1,4 @@ -package org.thoughtcrime.securesms.conversation.newmessage +package org.thoughtcrime.securesms.conversation.start.newmessage import androidx.compose.animation.animateContentSize import androidx.compose.foundation.ExperimentalFoundationApi diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageFragment.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageFragment.kt similarity index 90% rename from app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageFragment.kt rename to app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageFragment.kt index 3dd72f3528..a128d2ff83 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageFragment.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageFragment.kt @@ -1,4 +1,4 @@ -package org.thoughtcrime.securesms.conversation.new +package org.thoughtcrime.securesms.conversation.start.newmessage import android.content.Intent import android.os.Bundle @@ -13,9 +13,6 @@ import androidx.lifecycle.lifecycleScope import kotlinx.coroutines.launch import org.session.libsession.utilities.Address import org.session.libsession.utilities.recipients.Recipient -import org.thoughtcrime.securesms.conversation.newmessage.NewMessage -import org.thoughtcrime.securesms.conversation.newmessage.NewMessageViewModel -import org.thoughtcrime.securesms.conversation.newmessage.State import org.thoughtcrime.securesms.conversation.start.NewConversationDelegate import org.thoughtcrime.securesms.conversation.v2.ConversationActivityV2 import org.thoughtcrime.securesms.dependencies.DatabaseComponent diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageViewModel.kt similarity index 95% rename from app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt rename to app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageViewModel.kt index f61cc7d1a1..891e09d95c 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/newmessage/NewMessageViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/start/newmessage/NewMessageViewModel.kt @@ -1,4 +1,4 @@ -package org.thoughtcrime.securesms.conversation.newmessage +package org.thoughtcrime.securesms.conversation.start.newmessage import android.app.Application import androidx.lifecycle.AndroidViewModel @@ -7,13 +7,10 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.TimeoutCancellationException -import kotlinx.coroutines.channels.Channel -import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.asStateFlow -import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeout