Merge pull request #345 from gavine99/send-to-multiple-addresses-doesn't-start-new-conversations

fix new conversation not created for non-send-as-group multi-recipient message
This commit is contained in:
octoshrimpy
2025-03-15 17:36:59 -04:00
committed by GitHub
@@ -1001,88 +1001,100 @@ 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()) {
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 = ((addresses.size > 1) && // if more than one address to send to
(!state.editingMode || // and is not a new convo
state.sendAsGroup)) // or (is a new convo and) send as group is selected
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)
} }
if (!permissionManager.hasSendSms()) { // Sending a group message
view.requestSmsPermission() sendAsGroup -> {
return@withLatestFrom sendMessage.execute(SendMessage
.Params(subId, conversation.id, addresses, body, state.attachments, delay))
} }
val delay = when (prefs.sendDelay.get()) { // Sending a message to an existing conversation with one recipient
Preferences.SEND_DELAY_SHORT -> 3000 conversation.recipients.size == 1 -> {
Preferences.SEND_DELAY_MEDIUM -> 5000 val address = conversation.recipients.map { it.address }
Preferences.SEND_DELAY_LONG -> 10000 sendMessage.execute(SendMessage.Params(subId, threadId, address, body, state.attachments, delay))
else -> 0
} }
if ((delay != 0 || state.scheduled != 0L) && !permissionManager.hasExactAlarms()) { // Create a new conversation with one address
navigator.showExactAlarmsSettings() addresses.size == 1 -> {
return@withLatestFrom sendMessage.execute(SendMessage
.Params(subId, threadId, addresses, body, state.attachments, delay))
} }
val subId = state.subscription?.subscriptionId ?: -1 // Send a message to multiple addresses
val addresses = when (conversation.recipients.isNotEmpty()) { else -> {
true -> conversation.recipients.map { it.address } addresses.forEach {
false -> chips.map { chip -> chip.address } val threadId = tryOrNull(false) {
} TelephonyCompat.getOrCreateThreadId(context, it)
val sendAsGroup = ((addresses.size > 1) && // if more than one address to send to } ?: 0
(!state.editingMode || // and is not a new convo val address = conversationRepo.getOrCreateConversation(it)
state.sendAsGroup)) // or (is a new convo and) send as group is selected ?.recipients
?.firstOrNull()
when { ?.address
// Scheduling a message ?: it
state.scheduled != 0L -> { sendMessage.execute(
newState { copy(scheduled = 0) } SendMessage.Params(
val uris = state.attachments.map { it.uri.toString() } subId,
val params = AddScheduledMessage threadId,
.Params(state.scheduled, subId, addresses, sendAsGroup, body, uris) listOf(address),
addScheduledMessage.execute(params) body,
context.makeToast(R.string.compose_scheduled_toast) state.attachments,
} delay
)
// 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() // clear the current message ready for new message composition (or finish()
// compose activity) // compose activity)
view.clearCurrentMessageIntent.onNext( view.clearCurrentMessageIntent.onNext(
((addresses.size > 1) && // if more than one address to send to ((addresses.size > 1) && // if more than one address to send to
state.editingMode && // and is a new convo state.editingMode && // and is a new convo
!state.sendAsGroup) // and is *not* sent as a group !state.sendAsGroup) // and is *not* sent as a group
) )
} }
.autoDisposable(view.scope()) .autoDisposable(view.scope())
@@ -1117,6 +1129,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("")