fix no creation of a new conversation when no existing conversation to member of a multi-recipient sms not sent as group

This commit is contained in:
gavine99
2025-03-12 18:52:26 +11:00
parent da3de1e67d
commit 714a3337f2
@@ -999,29 +999,30 @@ class ComposeViewModel @Inject constructor(
// Send a message when the send button is clicked, and disable editing mode if it's enabled // Send a message when the send button is clicked, and disable editing mode if it's enabled
view.sendIntent view.sendIntent
.withLatestFrom(view.textChangedIntent) { _, body -> body.toString() } .observeOn(Schedulers.io())
.withLatestFrom(state, conversation, selectedChips) { body, state, conversation, chips -> .withLatestFrom(view.textChangedIntent) { _, body -> body.toString() }
if (!permissionManager.isDefaultSms()) { .withLatestFrom(state, conversation, selectedChips) { body, state, conversation, chips ->
view.requestDefaultSms() if (!permissionManager.isDefaultSms()) {
return@withLatestFrom view.requestDefaultSms()
} return@withLatestFrom
}
if (!permissionManager.hasSendSms()) { if (!permissionManager.hasSendSms()) {
view.requestSmsPermission() view.requestSmsPermission()
return@withLatestFrom return@withLatestFrom
} }
val delay = when (prefs.sendDelay.get()) { val delay = when (prefs.sendDelay.get()) {
Preferences.SEND_DELAY_SHORT -> 3000 Preferences.SEND_DELAY_SHORT -> 3000
Preferences.SEND_DELAY_MEDIUM -> 5000 Preferences.SEND_DELAY_MEDIUM -> 5000
Preferences.SEND_DELAY_LONG -> 10000 Preferences.SEND_DELAY_LONG -> 10000
else -> 0 else -> 0
} }
if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) { if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) {
navigator.showExactAlarmsSettings() navigator.showExactAlarmsSettings()
return@withLatestFrom return@withLatestFrom
} }
val subId = state.subscription?.subscriptionId ?: -1 val subId = state.subscription?.subscriptionId ?: -1
val addresses = when (conversation.recipients.isNotEmpty()) { val addresses = when (conversation.recipients.isNotEmpty()) {
@@ -1032,55 +1033,66 @@ class ComposeViewModel @Inject constructor(
(!state.editingMode || // and is not a new convo (!state.editingMode || // and is not a new convo
state.sendAsGroup)) // or (is a new convo and) send as group is selected state.sendAsGroup)) // or (is a new convo and) send as group is selected
when { when {
// Scheduling a message // Scheduling a message
state.scheduled != 0L -> { state.scheduled != 0L -> {
newState { copy(scheduled = 0) } newState { copy(scheduled = 0) }
val uris = state.attachments.map { it.uri.toString() } val uris = state.attachments.map { it.uri.toString() }
val params = AddScheduledMessage val params = AddScheduledMessage
.Params(state.scheduled, subId, addresses, sendAsGroup, body, uris) .Params(state.scheduled, subId, addresses, sendAsGroup, body, uris)
addScheduledMessage.execute(params) addScheduledMessage.execute(params)
context.makeToast(R.string.compose_scheduled_toast) 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 (or finish() // Sending a group message
// compose activity) sendAsGroup -> {
view.clearCurrentMessageIntent.onNext( sendMessage.execute(SendMessage
((addresses.size > 1) && // if more than one address to send to .Params(subId, conversation.id, addresses, body, state.attachments, delay))
state.editingMode && // and is a new convo }
!state.sendAsGroup) // and is *not* sent as a group
// 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 (or finish()
// compose activity)
view.clearCurrentMessageIntent.onNext(
((addresses.size > 1) && // if more than one address to send to
state.editingMode && // and is a new convo
!state.sendAsGroup) // and is *not* sent as a group
) )
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -1115,6 +1127,7 @@ class ComposeViewModel @Inject constructor(
// clear the current message schedule, text and attachments // clear the current message schedule, text and attachments
view.clearCurrentMessageIntent view.clearCurrentMessageIntent
.observeOn(AndroidSchedulers.mainThread())
.autoDisposable(view.scope()) .autoDisposable(view.scope())
.subscribe { .subscribe {
view.setDraft("") view.setDraft("")