Update the Ui ASAP when sending a message

This commit is contained in:
Naveen 2022-11-18 01:05:19 +05:30
parent ca1ad0043b
commit da6354c9a1
2 changed files with 52 additions and 24 deletions

View File

@ -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<View>) {
@ -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)

View File

@ -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<Message> {
fun Context.getMessages(
threadId: Long,
getImageResolutions: Boolean,
dateFrom: Int = -1,
includeScheduledMessages: Boolean = true,
limit: Int = MESSAGES_LIMIT
): ArrayList<Message> {
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<String, Boolean>()
val blockedNumbers = getBlockedNumbers()