Skip to content

Commit c94caef

Browse files
authored
Merge pull request #604 from Naveen3Singh/rename_conv_fix
Avoid resetting conversation name when new messages are received
2 parents 1cb551c + eeb8783 commit c94caef

File tree

6 files changed

+41
-13
lines changed

6 files changed

+41
-13
lines changed

app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ConversationDetailsActivity.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ class ConversationDetailsActivity : SimpleActivity() {
3434
threadId = intent.getLongExtra(THREAD_ID, 0L)
3535
ensureBackgroundThread {
3636
conversation = conversationsDB.getConversationWithThreadId(threadId)
37-
participants = getThreadParticipants(threadId, null)
37+
participants = if (conversation != null && conversation!!.isScheduled) {
38+
val message = messagesDB.getThreadMessages(conversation!!.threadId).firstOrNull()
39+
message?.participants ?: arrayListOf()
40+
} else {
41+
getThreadParticipants(threadId, null)
42+
}
3843
runOnUiThread {
3944
setupTextViews()
4045
setupParticipants()

app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/MainActivity.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ class MainActivity : SimpleActivity() {
298298
.forEach { message ->
299299
messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId))
300300
}
301+
insertOrUpdateConversation(newConversation, cachedConversation)
301302
}
302303
}
303304

@@ -307,10 +308,8 @@ class MainActivity : SimpleActivity() {
307308
}
308309
if (conv != null) {
309310
val lastModified = maxOf(cachedConv.date, conv.date)
310-
val usesCustomTitle = cachedConv.usesCustomTitle
311-
val title = if (usesCustomTitle) cachedConv.title else conv.title
312-
val conversation = conv.copy(date = lastModified, title = title, usesCustomTitle = usesCustomTitle)
313-
conversationsDB.insertOrUpdate(conversation)
311+
val conversation = conv.copy(date = lastModified)
312+
insertOrUpdateConversation(conversation)
314313
}
315314
}
316315

app/src/main/kotlin/com/simplemobiletools/smsmessenger/activities/ThreadActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ class ThreadActivity : SimpleActivity() {
494494
if (messages.isNotEmpty() && messages.all { it.isScheduled }) {
495495
val scheduledMessage = messages.last()
496496
val fakeThreadId = generateRandomId()
497-
createTemporaryThread(scheduledMessage, fakeThreadId)
497+
createTemporaryThread(scheduledMessage, fakeThreadId, conversation)
498498
updateScheduledMessagesThreadId(messages, fakeThreadId)
499499
threadId = fakeThreadId
500500
}
@@ -1198,7 +1198,7 @@ class ThreadActivity : SimpleActivity() {
11981198
if (messages.isEmpty()) {
11991199
// create a temporary thread until a real message is sent
12001200
threadId = message.threadId
1201-
createTemporaryThread(message, message.threadId)
1201+
createTemporaryThread(message, message.threadId, conversation)
12021202
}
12031203
val conversation = conversationsDB.getConversationWithThreadId(threadId)
12041204
if (conversation != null) {

app/src/main/kotlin/com/simplemobiletools/smsmessenger/extensions/Context.kt

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,7 @@ fun Context.updateLastConversationMessage(threadId: Long) {
824824
try {
825825
contentResolver.delete(uri, selection, selectionArgs)
826826
val newConversation = getConversations(threadId)[0]
827-
conversationsDB.insertOrUpdate(newConversation)
827+
insertOrUpdateConversation(newConversation)
828828
} catch (e: Exception) {
829829
}
830830
}
@@ -878,6 +878,24 @@ fun Context.subscriptionManagerCompat(): SubscriptionManager {
878878
return getSystemService(SubscriptionManager::class.java)
879879
}
880880

881+
fun Context.insertOrUpdateConversation(
882+
conversation: Conversation,
883+
cachedConv: Conversation? = conversationsDB.getConversationWithThreadId(conversation.threadId)
884+
) {
885+
val updatedConv = if (cachedConv != null) {
886+
val usesCustomTitle = cachedConv.usesCustomTitle
887+
val title = if (usesCustomTitle) {
888+
cachedConv.title
889+
} else {
890+
conversation.title
891+
}
892+
conversation.copy(title = title, usesCustomTitle = usesCustomTitle)
893+
} else {
894+
conversation
895+
}
896+
conversationsDB.insertOrUpdate(updatedConv)
897+
}
898+
881899
fun Context.renameConversation(conversation: Conversation, newTitle: String): Conversation {
882900
val updatedConv = conversation.copy(title = newTitle, usesCustomTitle = true)
883901
try {
@@ -888,21 +906,27 @@ fun Context.renameConversation(conversation: Conversation, newTitle: String): Co
888906
return updatedConv
889907
}
890908

891-
fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId()) {
909+
fun Context.createTemporaryThread(message: Message, threadId: Long = generateRandomId(), cachedConv: Conversation?) {
892910
val simpleContactHelper = SimpleContactsHelper(this)
893911
val addresses = message.participants.getAddresses()
894912
val photoUri = if (addresses.size == 1) simpleContactHelper.getPhotoUriFromPhoneNumber(addresses.first()) else ""
913+
val title = if (cachedConv != null && cachedConv.usesCustomTitle) {
914+
cachedConv.title
915+
} else {
916+
message.participants.getThreadTitle()
917+
}
895918

896919
val conversation = Conversation(
897920
threadId = threadId,
898921
snippet = message.body,
899922
date = message.date,
900923
read = true,
901-
title = message.participants.getThreadTitle(),
924+
title = title,
902925
photoUri = photoUri,
903926
isGroupConversation = addresses.size > 1,
904927
phoneNumber = addresses.first(),
905-
isScheduled = true
928+
isScheduled = true,
929+
usesCustomTitle = cachedConv?.usesCustomTitle == true
906930
)
907931
try {
908932
conversationsDB.insertOrUpdate(conversation)

app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/MmsReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class MmsReceiver : com.klinker.android.send_message.MmsReceivedReceiver() {
4141
context.showReceivedMessageNotification(address, mms.body, mms.threadId, glideBitmap)
4242
val conversation = context.getConversations(mms.threadId).firstOrNull() ?: return@post
4343
ensureBackgroundThread {
44-
context.conversationsDB.insertOrUpdate(conversation)
44+
context.insertOrUpdateConversation(conversation)
4545
context.updateUnreadCountBadge(context.conversationsDB.getUnreadConversations())
4646
refreshMessages()
4747
}

app/src/main/kotlin/com/simplemobiletools/smsmessenger/receivers/SmsReceiver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SmsReceiver : BroadcastReceiver() {
6767

6868
val conversation = context.getConversations(threadId).firstOrNull() ?: return@ensureBackgroundThread
6969
try {
70-
context.conversationsDB.insertOrUpdate(conversation)
70+
context.insertOrUpdateConversation(conversation)
7171
} catch (ignored: Exception) {
7272
}
7373

0 commit comments

Comments
 (0)