mirror of
				https://github.com/SimpleMobileTools/Simple-SMS-Messenger.git
				synced 2025-06-05 21:49:22 +02:00 
			
		
		
		
	Merge pull request #604 from Naveen3Singh/rename_conv_fix
Avoid resetting conversation name when new messages are received
This commit is contained in:
		| @@ -34,7 +34,12 @@ class ConversationDetailsActivity : SimpleActivity() { | |||||||
|         threadId = intent.getLongExtra(THREAD_ID, 0L) |         threadId = intent.getLongExtra(THREAD_ID, 0L) | ||||||
|         ensureBackgroundThread { |         ensureBackgroundThread { | ||||||
|             conversation = conversationsDB.getConversationWithThreadId(threadId) |             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 { |             runOnUiThread { | ||||||
|                 setupTextViews() |                 setupTextViews() | ||||||
|                 setupParticipants() |                 setupParticipants() | ||||||
|   | |||||||
| @@ -298,6 +298,7 @@ class MainActivity : SimpleActivity() { | |||||||
|                         .forEach { message -> |                         .forEach { message -> | ||||||
|                             messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId)) |                             messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId)) | ||||||
|                         } |                         } | ||||||
|  |                     insertOrUpdateConversation(newConversation, cachedConversation) | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
| @@ -307,10 +308,8 @@ class MainActivity : SimpleActivity() { | |||||||
|                 } |                 } | ||||||
|                 if (conv != null) { |                 if (conv != null) { | ||||||
|                     val lastModified = maxOf(cachedConv.date, conv.date) |                     val lastModified = maxOf(cachedConv.date, conv.date) | ||||||
|                     val usesCustomTitle = cachedConv.usesCustomTitle |                     val conversation = conv.copy(date = lastModified) | ||||||
|                     val title = if (usesCustomTitle) cachedConv.title else conv.title |                     insertOrUpdateConversation(conversation) | ||||||
|                     val conversation = conv.copy(date = lastModified, title = title, usesCustomTitle = usesCustomTitle) |  | ||||||
|                     conversationsDB.insertOrUpdate(conversation) |  | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -494,7 +494,7 @@ class ThreadActivity : SimpleActivity() { | |||||||
|         if (messages.isNotEmpty() && messages.all { it.isScheduled }) { |         if (messages.isNotEmpty() && messages.all { it.isScheduled }) { | ||||||
|             val scheduledMessage = messages.last() |             val scheduledMessage = messages.last() | ||||||
|             val fakeThreadId = generateRandomId() |             val fakeThreadId = generateRandomId() | ||||||
|             createTemporaryThread(scheduledMessage, fakeThreadId) |             createTemporaryThread(scheduledMessage, fakeThreadId, conversation) | ||||||
|             updateScheduledMessagesThreadId(messages, fakeThreadId) |             updateScheduledMessagesThreadId(messages, fakeThreadId) | ||||||
|             threadId = fakeThreadId |             threadId = fakeThreadId | ||||||
|         } |         } | ||||||
| @@ -1198,7 +1198,7 @@ class ThreadActivity : SimpleActivity() { | |||||||
|                 if (messages.isEmpty()) { |                 if (messages.isEmpty()) { | ||||||
|                     // create a temporary thread until a real message is sent |                     // create a temporary thread until a real message is sent | ||||||
|                     threadId = message.threadId |                     threadId = message.threadId | ||||||
|                     createTemporaryThread(message, message.threadId) |                     createTemporaryThread(message, message.threadId, conversation) | ||||||
|                 } |                 } | ||||||
|                 val conversation = conversationsDB.getConversationWithThreadId(threadId) |                 val conversation = conversationsDB.getConversationWithThreadId(threadId) | ||||||
|                 if (conversation != null) { |                 if (conversation != null) { | ||||||
|   | |||||||
| @@ -824,7 +824,7 @@ fun Context.updateLastConversationMessage(threadId: Long) { | |||||||
|     try { |     try { | ||||||
|         contentResolver.delete(uri, selection, selectionArgs) |         contentResolver.delete(uri, selection, selectionArgs) | ||||||
|         val newConversation = getConversations(threadId)[0] |         val newConversation = getConversations(threadId)[0] | ||||||
|         conversationsDB.insertOrUpdate(newConversation) |         insertOrUpdateConversation(newConversation) | ||||||
|     } catch (e: Exception) { |     } catch (e: Exception) { | ||||||
|     } |     } | ||||||
| } | } | ||||||
| @@ -878,6 +878,24 @@ fun Context.subscriptionManagerCompat(): SubscriptionManager { | |||||||
|     return getSystemService(SubscriptionManager::class.java) |     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 { | fun Context.renameConversation(conversation: Conversation, newTitle: String): Conversation { | ||||||
|     val updatedConv = conversation.copy(title = newTitle, usesCustomTitle = true) |     val updatedConv = conversation.copy(title = newTitle, usesCustomTitle = true) | ||||||
|     try { |     try { | ||||||
| @@ -888,21 +906,27 @@ fun Context.renameConversation(conversation: Conversation, newTitle: String): Co | |||||||
|     return updatedConv |     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 simpleContactHelper = SimpleContactsHelper(this) | ||||||
|     val addresses = message.participants.getAddresses() |     val addresses = message.participants.getAddresses() | ||||||
|     val photoUri = if (addresses.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(addresses.first()) else "" |     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( |     val conversation = Conversation( | ||||||
|         threadId = threadId, |         threadId = threadId, | ||||||
|         snippet = message.body, |         snippet = message.body, | ||||||
|         date = message.date, |         date = message.date, | ||||||
|         read = true, |         read = true, | ||||||
|         title = message.participants.getThreadTitle(), |         title = title, | ||||||
|         photoUri = photoUri, |         photoUri = photoUri, | ||||||
|         isGroupConversation = addresses.size > 1, |         isGroupConversation = addresses.size > 1, | ||||||
|         phoneNumber = addresses.first(), |         phoneNumber = addresses.first(), | ||||||
|         isScheduled = true |         isScheduled = true, | ||||||
|  |         usesCustomTitle = cachedConv?.usesCustomTitle == true | ||||||
|     ) |     ) | ||||||
|     try { |     try { | ||||||
|         conversationsDB.insertOrUpdate(conversation) |         conversationsDB.insertOrUpdate(conversation) | ||||||
|   | |||||||
| @@ -41,7 +41,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() { | |||||||
|                 context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap) |                 context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap) | ||||||
|                 val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post |                 val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post | ||||||
|                 ensureBackgroundThread { |                 ensureBackgroundThread { | ||||||
|                     context.conversationsDB.insertOrUpdate(conversation) |                     context.insertOrUpdateConversation(conversation) | ||||||
|                     context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations()) |                     context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations()) | ||||||
|                     refreshMessages() |                     refreshMessages() | ||||||
|                 } |                 } | ||||||
|   | |||||||
| @@ -67,7 +67,7 @@ class SmsReceiver : BroadcastReceiver() { | |||||||
|  |  | ||||||
|                     val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread |                     val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread | ||||||
|                     try { |                     try { | ||||||
|                         context.conversationsDB.insertOrUpdate(conversation) |                         context.insertOrUpdateConversation(conversation) | ||||||
|                     } catch (ignored: Exception) { |                     } catch (ignored: Exception) { | ||||||
|                     } |                     } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user