Merge pull request #604 from Naveen3Singh/rename_conv_fix

Avoid resetting conversation name when new messages are received
This commit is contained in:
Tibor Kaputa 2023-03-03 12:33:15 +01:00 committed by GitHub
commit c94caef4bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 13 deletions

View File

@ -34,7 +34,12 @@ class ConversationDetailsActivity : SimpleActivity() {
threadId = intent.getLongExtra(THREAD_ID, 0L)
ensureBackgroundThread {
conversation = conversationsDB.getConversationWithThreadId(threadId)
participants = getThreadParticipants(threadId, null)
participants = if (conversation != null && conversation!!.isScheduled) {
val message = messagesDB.getThreadMessages(conversation!!.threadId).firstOrNull()
message?.participants ?: arrayListOf()
} else {
getThreadParticipants(threadId, null)
}
runOnUiThread {
setupTextViews()
setupParticipants()

View File

@ -298,6 +298,7 @@ class MainActivity : SimpleActivity() {
.forEach { message ->
messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId))
}
insertOrUpdateConversation(newConversation, cachedConversation)
}
}
@ -307,10 +308,8 @@ class MainActivity : SimpleActivity() {
}
if (conv != null) {
val lastModified = maxOf(cachedConv.date, conv.date)
val usesCustomTitle = cachedConv.usesCustomTitle
val title = if (usesCustomTitle) cachedConv.title else conv.title
val conversation = conv.copy(date = lastModified, title = title, usesCustomTitle = usesCustomTitle)
conversationsDB.insertOrUpdate(conversation)
val conversation = conv.copy(date = lastModified)
insertOrUpdateConversation(conversation)
}
}

View File

@ -494,7 +494,7 @@ class ThreadActivity : SimpleActivity() {
if (messages.isNotEmpty() && messages.all { it.isScheduled }) {
val scheduledMessage = messages.last()
val fakeThreadId = generateRandomId()
createTemporaryThread(scheduledMessage, fakeThreadId)
createTemporaryThread(scheduledMessage, fakeThreadId, conversation)
updateScheduledMessagesThreadId(messages, fakeThreadId)
threadId = fakeThreadId
}
@ -1198,7 +1198,7 @@ class ThreadActivity : SimpleActivity() {
if (messages.isEmpty()) {
// create a temporary thread until a real message is sent
threadId = message.threadId
createTemporaryThread(message, message.threadId)
createTemporaryThread(message, message.threadId, conversation)
}
val conversation = conversationsDB.getConversationWithThreadId(threadId)
if (conversation != null) {

View File

@ -824,7 +824,7 @@ fun Context.updateLastConversationMessage(threadId: Long) {
try {
contentResolver.delete(uri, selection, selectionArgs)
val newConversation = getConversations(threadId)[0]
conversationsDB.insertOrUpdate(newConversation)
insertOrUpdateConversation(newConversation)
} catch (e: Exception) {
}
}
@ -878,6 +878,24 @@ fun Context.subscriptionManagerCompat(): SubscriptionManager {
return getSystemService(SubscriptionManager::class.java)
}
fun Context.insertOrUpdateConversation(
conversation: Conversation,
cachedConv: Conversation? = conversationsDB.getConversationWithThreadId(conversation.threadId)
) {
val updatedConv = if (cachedConv != null) {
val usesCustomTitle = cachedConv.usesCustomTitle
val title = if (usesCustomTitle) {
cachedConv.title
} else {
conversation.title
}
conversation.copy(title = title, usesCustomTitle = usesCustomTitle)
} else {
conversation
}
conversationsDB.insertOrUpdate(updatedConv)
}
fun Context.renameConversation(conversation: Conversation, newTitle: String): Conversation {
val updatedConv = conversation.copy(title = newTitle, usesCustomTitle = true)
try {
@ -888,21 +906,27 @@ fun Context.renameConversation(conversation: Conversation, newTitle: String): Co
return updatedConv
}
fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId()) {
fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId(), cachedConv: Conversation?) {
val simpleContactHelper = SimpleContactsHelper(this)
val addresses = message.participants.getAddresses()
val photoUri = if (addresses.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(addresses.first()) else ""
val title = if (cachedConv != null && cachedConv.usesCustomTitle) {
cachedConv.title
} else {
message.participants.getThreadTitle()
}
val conversation = Conversation(
threadId = threadId,
snippet = message.body,
date = message.date,
read = true,
title = message.participants.getThreadTitle(),
title = title,
photoUri = photoUri,
isGroupConversation = addresses.size > 1,
phoneNumber = addresses.first(),
isScheduled = true
isScheduled = true,
usesCustomTitle = cachedConv?.usesCustomTitle == true
)
try {
conversationsDB.insertOrUpdate(conversation)

View File

@ -41,7 +41,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap)
val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post
ensureBackgroundThread {
context.conversationsDB.insertOrUpdate(conversation)
context.insertOrUpdateConversation(conversation)
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
refreshMessages()
}

View File

@ -67,7 +67,7 @@ class SmsReceiver : BroadcastReceiver() {
val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
try {
context.conversationsDB.insertOrUpdate(conversation)
context.insertOrUpdateConversation(conversation)
} catch (ignored: Exception) {
}