From fa8accd9e28e13933c3f06b5b33ec41d7c078720 Mon Sep 17 00:00:00 2001 From: gavine99 Date: Wed, 12 Mar 2025 18:52:26 +1100 Subject: [PATCH] fix no creation of a new conversation when no existing conversation to member of a multi-recipient sms not sent as group --- .../QKSMS/feature/compose/ComposeViewModel.kt | 169 ++++++++++-------- 1 file changed, 91 insertions(+), 78 deletions(-) diff --git a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt index 5f80f98a..dde7fd75 100644 --- a/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt +++ b/presentation/src/main/java/com/moez/QKSMS/feature/compose/ComposeViewModel.kt @@ -999,85 +999,97 @@ class ComposeViewModel @Inject constructor( // Send a message when the send button is clicked, and disable editing mode if it's enabled view.sendIntent - .withLatestFrom(view.textChangedIntent) { _, body -> body.toString() } - .withLatestFrom(state, conversation, selectedChips) { body, state, conversation, chips -> - if (!permissionManager.isDefaultSms()) { - view.requestDefaultSms() - return@withLatestFrom - } - - if (!permissionManager.hasSendSms()) { - view.requestSmsPermission() - return@withLatestFrom - } - - val delay = when (prefs.sendDelay.get()) { - Preferences.SEND_DELAY_SHORT -> 3000 - Preferences.SEND_DELAY_MEDIUM -> 5000 - Preferences.SEND_DELAY_LONG -> 10000 - else -> 0 - } - - if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) { - navigator.showExactAlarmsSettings() - return@withLatestFrom - } - - val subId = state.subscription?.subscriptionId ?: -1 - val addresses = when (conversation.recipients.isNotEmpty()) { - true -> conversation.recipients.map { it.address } - false -> chips.map { chip -> chip.address } - } - val sendAsGroup = !state.editingMode || state.sendAsGroup - - when { - // Scheduling a message - state.scheduled != 0L -> { - newState { copy(scheduled = 0) } - val uris = state.attachments.map { it.uri.toString() } - val params = AddScheduledMessage - .Params(state.scheduled, subId, addresses, sendAsGroup, body, uris) - addScheduledMessage.execute(params) - context.makeToast(R.string.compose_scheduled_toast) - } - - // Sending a group message - sendAsGroup -> { - sendMessage.execute(SendMessage - .Params(subId, conversation.id, addresses, body, state.attachments, delay)) - } - - // Sending a message to an existing conversation with one recipient - conversation.recipients.size == 1 -> { - val address = conversation.recipients.map { it.address } - sendMessage.execute(SendMessage.Params(subId, threadId, address, body, state.attachments, delay)) - } - - // Create a new conversation with one address - addresses.size == 1 -> { - sendMessage.execute(SendMessage - .Params(subId, threadId, addresses, body, state.attachments, delay)) - } - - // Send a message to multiple addresses - else -> { - addresses.forEach { addr -> - val threadId = tryOrNull(false) { - TelephonyCompat.getOrCreateThreadId(context, addr) - } ?: 0 - val address = listOf(conversationRepo - .getConversation(threadId)?.recipients?.firstOrNull()?.address ?: addr) - sendMessage.execute(SendMessage - .Params(subId, threadId, address, body, state.attachments, delay)) - } - } - } - - // clear the current message ready for new message composition - view.clearCurrentMessageIntent.onNext(Unit) + .observeOn(Schedulers.io()) + .withLatestFrom(view.textChangedIntent) { _, body -> body.toString() } + .withLatestFrom(state, conversation, selectedChips) { body, state, conversation, chips -> + if (!permissionManager.isDefaultSms()) { + view.requestDefaultSms() + return@withLatestFrom } - .autoDisposable(view.scope()) - .subscribe() + + if (!permissionManager.hasSendSms()) { + view.requestSmsPermission() + return@withLatestFrom + } + + val delay = when (prefs.sendDelay.get()) { + Preferences.SEND_DELAY_SHORT -> 3000 + Preferences.SEND_DELAY_MEDIUM -> 5000 + Preferences.SEND_DELAY_LONG -> 10000 + else -> 0 + } + + if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) { + navigator.showExactAlarmsSettings() + return@withLatestFrom + } + + val subId = state.subscription?.subscriptionId ?: -1 + val addresses = when (conversation.recipients.isNotEmpty()) { + true -> conversation.recipients.map { it.address } + false -> chips.map { chip -> chip.address } + } + val sendAsGroup = !state.editingMode || state.sendAsGroup + + when { + // Scheduling a message + state.scheduled != 0L -> { + newState { copy(scheduled = 0) } + val uris = state.attachments.map { it.uri.toString() } + val params = AddScheduledMessage + .Params(state.scheduled, subId, addresses, sendAsGroup, body, uris) + addScheduledMessage.execute(params) + context.makeToast(R.string.compose_scheduled_toast) + } + + // Sending a group message + sendAsGroup -> { + sendMessage.execute(SendMessage + .Params(subId, conversation.id, addresses, body, state.attachments, delay)) + } + + // Sending a message to an existing conversation with one recipient + conversation.recipients.size == 1 -> { + val address = conversation.recipients.map { it.address } + sendMessage.execute(SendMessage.Params(subId, threadId, address, body, state.attachments, delay)) + } + + // Create a new conversation with one address + addresses.size == 1 -> { + sendMessage.execute(SendMessage + .Params(subId, threadId, addresses, body, state.attachments, delay)) + } + + // Send a message to multiple addresses + else -> { + addresses.forEach { + val threadId = tryOrNull(false) { + TelephonyCompat.getOrCreateThreadId(context, it) + } ?: 0 + val address = conversationRepo.getOrCreateConversation(it) + ?.recipients + ?.firstOrNull() + ?.address + ?: it + sendMessage.execute( + SendMessage.Params( + subId, + threadId, + listOf(address), + body, + state.attachments, + delay + ) + ) + } + } + } + + // clear the current message ready for new message composition + view.clearCurrentMessageIntent.onNext(Unit) + } + .autoDisposable(view.scope()) + .subscribe() // View QKSMS+ view.viewQksmsPlusIntent @@ -1113,6 +1125,7 @@ class ComposeViewModel @Inject constructor( // clear the current message schedule, text and attachments view.clearCurrentMessageIntent .withLatestFrom(state) { _, state -> state } + .observeOn(AndroidSchedulers.mainThread()) .autoDisposable(view.scope()) .subscribe { view.setDraft("")