From 01a86336c07a8c62ee38fd4c0f719153befcc5f2 Mon Sep 17 00:00:00 2001 From: JRoberts <8711996+jr-simplex@users.noreply.github.com> Date: Sat, 3 Dec 2022 16:19:13 +0400 Subject: [PATCH] android: simplify logic for allowing voice messages on alert; fix non exhaustive `when` in SendMsgView (#1484) --- .../java/chat/simplex/app/model/SimpleXAPI.kt | 5 +++++ .../chat/simplex/app/views/chat/ComposeView.kt | 11 ++--------- .../chat/simplex/app/views/chat/SendMsgView.kt | 17 +++++++---------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index cd8725e174..3451881701 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -1983,6 +1983,11 @@ data class ContactUserPreferences( val fullDelete: ContactUserPreference, val voice: ContactUserPreference, ) { + fun toPreferences(): ChatPreferences = ChatPreferences( + fullDelete = fullDelete.userPreference.pref, + voice = voice.userPreference.pref + ) + companion object { val sampleData = ContactUserPreferences( fullDelete = ContactUserPreference( diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt index f859dd68e3..b5decc4909 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ComposeView.kt @@ -457,9 +457,8 @@ fun ComposeView( fun allowVoiceToContact() { val contact = (chat.chatInfo as ChatInfo.Direct?)?.contact ?: return - val featuresAllowed = contactUserPrefsToFeaturesAllowed(contact.mergedPreferences) + val prefs = contact.mergedPreferences.toPreferences().copy(voice = ChatPreference(allow = FeatureAllowed.YES)) withApi { - val prefs = contactFeaturesAllowedToPrefs(featuresAllowed).copy(voice = ChatPreference(FeatureAllowed.YES)) val toContact = chatModel.controller.apiSetContactPrefs(contact.contactId, prefs) if (toContact != null) { chatModel.updateContact(toContact) @@ -576,13 +575,7 @@ fun ComposeView( .clip(CircleShape) ) } - val allowedVoiceByPrefs = remember(chat.chatInfo) { - when (chat.chatInfo) { - is ChatInfo.Direct -> chat.chatInfo.contact.mergedPreferences.voice.enabled.forUser - is ChatInfo.Group -> chat.chatInfo.groupInfo.fullGroupPreferences.voice.enable == GroupFeatureEnabled.ON - else -> false - } - } + val allowedVoiceByPrefs = remember(chat.chatInfo) { chat.chatInfo.voiceMessageAllowed } val needToAllowVoiceToContact = remember(chat.chatInfo) { when (chat.chatInfo) { is ChatInfo.Direct -> with(chat.chatInfo.contact.mergedPreferences.voice) { diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt index 6d27046e0f..246356aa8e 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/SendMsgView.kt @@ -237,16 +237,13 @@ private fun NativeKeyboard( var showKeyboard by remember { mutableStateOf(false) } LaunchedEffect(cs.contextItem) { - when (cs.contextItem) { - is ComposeContextItem.QuotedItem -> { - delay(100) - showKeyboard = true - } - is ComposeContextItem.EditingItem -> { - // Keyboard will not show up if we try to show it too fast - delay(300) - showKeyboard = true - } + if (cs.contextItem is ComposeContextItem.QuotedItem) { + delay(100) + showKeyboard = true + } else if (cs.contextItem is ComposeContextItem.EditingItem) { + // Keyboard will not show up if we try to show it too fast + delay(300) + showKeyboard = true } }