From da6354c9a16c1ce4ed581be0a328e5fa29106da1 Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 18 Nov 2022 01:05:19 +0530 Subject: [PATCH] Update the Ui ASAP when sending a message --- .../smsmessenger/activities/ThreadActivity.kt | 66 ++++++++++++------- .../smsmessenger/extensions/Context.kt | 10 ++- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt index 1674c2b6..9e4262b7 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt @@ -1075,22 +1075,18 @@ class ThreadActivity : SimpleActivity() { threadId = message.threadId createTemporaryThread(message, message.threadId) } - messagesDB.insertOrUpdate(message) val conversation = conversationsDB.getConversationWithThreadId(threadId) if (conversation != null) { val nowSeconds = (System.currentTimeMillis() / 1000).toInt() conversationsDB.insertOrUpdate(conversation.copy(date = nowSeconds)) } scheduleMessage(message) + insertOrUpdateMessage(message) runOnUiThread { clearCurrentMessage() hideScheduleSendUi() scheduledMessage = null - - if (!refreshedSinceSent) { - refreshMessages() - } } } } catch (e: Exception) { @@ -1105,11 +1101,16 @@ class ThreadActivity : SimpleActivity() { try { refreshedSinceSent = false sendMessage(text, addresses, subscriptionId, attachments) + ensureBackgroundThread { + val messageIds = messages.map { it.id } + val message = getMessages(threadId, getImageResolutions = true, limit = 1).firstOrNull { it.id !in messageIds } + if (message != null) { + maybeUpdateMessageSubId(message) + insertOrUpdateMessage(message) + } + } clearCurrentMessage() - if (!refreshedSinceSent) { - refreshMessages() - } } catch (e: Exception) { showErrorToast(e) } catch (e: Error) { @@ -1123,6 +1124,24 @@ class ThreadActivity : SimpleActivity() { checkSendMessageAvailability() } + private fun insertOrUpdateMessage(message: Message) { + if (messages.map { it.id }.contains(message.id)) { + val messageToReplace = messages.find { it.id == message.id } + messages[messages.indexOf(messageToReplace)] = message + } else { + messages.add(message) + } + + val newItems = getThreadItems() + runOnUiThread { + getOrCreateThreadAdapter().updateMessages(newItems) + if (!refreshedSinceSent) { + refreshMessages() + } + } + messagesDB.insertOrUpdate(message) + } + // show selected contacts, properly split to new lines when appropriate // based on https://stackoverflow.com/a/13505029/1967672 private fun showSelectedContact(views: ArrayList) { @@ -1249,7 +1268,6 @@ class ThreadActivity : SimpleActivity() { } } - @SuppressLint("MissingPermission") @Subscribe(threadMode = ThreadMode.ASYNC) fun refreshMessages(event: Events.RefreshMessages) { refreshedSinceSent = true @@ -1260,6 +1278,7 @@ class ThreadActivity : SimpleActivity() { notificationManager.cancel(threadId.hashCode()) } + val lastMaxId = messages.filterNot { it.isScheduled }.maxByOrNull { it.id }?.id ?: 0L val newThreadId = getThreadId(participants.getAddresses().toSet()) val newMessages = getMessages(newThreadId, false) messages = if (messages.all { it.isScheduled } && newMessages.isNotEmpty()) { @@ -1271,18 +1290,8 @@ class ThreadActivity : SimpleActivity() { getMessages(threadId, true) } - val lastMaxId = messages.filterNot { it.isScheduled }.maxByOrNull { it.id }?.id ?: 0L - messages.filter { !it.isReceivedMessage() && it.id > lastMaxId }.forEach { latestMessage -> - // subscriptionIds seem to be not filled out at sending with multiple SIM cards, so fill it manually - if ((subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1) { - val subscriptionId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId - if (subscriptionId != null) { - updateMessageSubscriptionId(latestMessage.id, subscriptionId) - latestMessage.subscriptionId = subscriptionId - } - } - + maybeUpdateMessageSubId(latestMessage) messagesDB.insertOrIgnore(latestMessage) } @@ -1292,6 +1301,18 @@ class ThreadActivity : SimpleActivity() { } } + @SuppressLint("MissingPermission") + private fun maybeUpdateMessageSubId(message: Message) { + // subscriptionIds seem to be not filled out at sending with multiple SIM cards, so fill it manually + if ((subscriptionManagerCompat().activeSubscriptionInfoList?.size ?: 0) > 1) { + val subscriptionId = availableSIMCards.getOrNull(currentSIMCardIndex)?.subscriptionId + if (subscriptionId != null) { + updateMessageSubscriptionId(message.id, subscriptionId) + message.subscriptionId = subscriptionId + } + } + } + private fun isMmsMessage(text: String): Boolean { val isGroupMms = participants.size > 1 && config.sendGroupMessageMMS val isLongMmsMessage = isLongMmsMessage(text) && config.sendLongMessageMMS @@ -1314,11 +1335,12 @@ class ThreadActivity : SimpleActivity() { RadioItem(TYPE_SEND, getString(R.string.send_now)), RadioItem(TYPE_DELETE, getString(R.string.delete)) ) - RadioGroupDialog(activity = this, items = items, titleId = R.string.scheduled_message) { - when (it as Int) { + RadioGroupDialog(activity = this, items = items, titleId = R.string.scheduled_message) { any -> + when (any as Int) { TYPE_DELETE -> cancelScheduledMessageAndRefresh(message.id) TYPE_EDIT -> editScheduledMessage(message) TYPE_SEND -> { + messages.removeAll { message.id == it.id } extractAttachments(message) sendNormalMessage(message.body, message.subscriptionId) cancelScheduledMessageAndRefresh(message.id) diff --git a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt index 6b97672f..28190c1f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt @@ -58,7 +58,13 @@ val Context.messageAttachmentsDB: MessageAttachmentsDao get() = getMessagesDB(). val Context.messagesDB: MessagesDao get() = getMessagesDB().MessagesDao() -fun Context.getMessages(threadId: Long, getImageResolutions: Boolean, dateFrom: Int = -1, includeScheduledMessages: Boolean = true): ArrayList { +fun Context.getMessages( + threadId: Long, + getImageResolutions: Boolean, + dateFrom: Int = -1, + includeScheduledMessages: Boolean = true, + limit: Int = MESSAGES_LIMIT +): ArrayList { val uri = Sms.CONTENT_URI val projection = arrayOf( Sms._ID, @@ -75,7 +81,7 @@ fun Context.getMessages(threadId: Long, getImageResolutions: Boolean, dateFrom: val rangeQuery = if (dateFrom == -1) "" else "AND ${Sms.DATE} < ${dateFrom.toLong() * 1000}" val selection = "${Sms.THREAD_ID} = ? $rangeQuery" val selectionArgs = arrayOf(threadId.toString()) - val sortOrder = "${Sms.DATE} DESC LIMIT $MESSAGES_LIMIT" + val sortOrder = "${Sms.DATE} DESC LIMIT $limit" val blockStatus = HashMap() val blockedNumbers = getBlockedNumbers()