Prefer last modified time over last message time

This commit is contained in:
Naveen 2022-10-08 20:50:49 +05:30
parent 6f07d6971a
commit 203f10618f
4 changed files with 28 additions and 15 deletions

View File

@ -36,7 +36,6 @@ import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode import org.greenrobot.eventbus.ThreadMode
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
import java.util.*
class MainActivity : SimpleActivity() { class MainActivity : SimpleActivity() {
private val MAKE_DEFAULT_APP_REQUEST = 1 private val MAKE_DEFAULT_APP_REQUEST = 1
@ -220,14 +219,6 @@ class MainActivity : SimpleActivity() {
val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor) val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
val conversations = getConversations(privateContacts = privateContacts) val conversations = getConversations(privateContacts = privateContacts)
val scheduledConversations = cachedConversations.filter { it.isScheduled }
val allConversations = conversations.toArrayList().apply {
addAll(scheduledConversations)
}
runOnUiThread {
setupConversations(allConversations)
}
conversations.forEach { clonedConversation -> conversations.forEach { clonedConversation ->
val threadIds = cachedConversations.map { it.threadId } val threadIds = cachedConversations.map { it.threadId }
if (!threadIds.contains(clonedConversation.threadId)) { if (!threadIds.contains(clonedConversation.threadId)) {
@ -256,13 +247,19 @@ class MainActivity : SimpleActivity() {
} }
} }
cachedConversations.forEach { cachedConversation -> cachedConversations.forEach { cachedConv ->
val conv = conversations.firstOrNull { it.threadId == cachedConversation.threadId && it.toString() != cachedConversation.toString() } val conv = conversations.find { it.threadId == cachedConv.threadId && !cachedConv.areContentsTheSame(it) }
if (conv != null) { if (conv != null) {
conversationsDB.insertOrUpdate(conv) val conversation = conv.copy(date = maxOf(cachedConv.date, conv.date))
conversationsDB.insertOrUpdate(conversation)
} }
} }
val allConversations = conversationsDB.getAll() as ArrayList<Conversation>
runOnUiThread {
setupConversations(allConversations)
}
if (config.appRunCount == 1) { if (config.appRunCount == 1) {
conversations.map { it.threadId }.forEach { threadId -> conversations.map { it.threadId }.forEach { threadId ->
val messages = getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false) val messages = getMessages(threadId, getImageResolutions = false, includeScheduledMessages = false)
@ -336,7 +333,7 @@ class MainActivity : SimpleActivity() {
val manager = getSystemService(ShortcutManager::class.java) val manager = getSystemService(ShortcutManager::class.java)
try { try {
manager.dynamicShortcuts = Arrays.asList(newConversation) manager.dynamicShortcuts = listOf(newConversation)
config.lastHandledShortcutColor = appIconColor config.lastHandledShortcutColor = appIconColor
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }

View File

@ -977,6 +977,11 @@ class ThreadActivity : SimpleActivity() {
createTemporaryThread(message, message.threadId) createTemporaryThread(message, message.threadId)
} }
messagesDB.insertOrUpdate(message) 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) scheduleMessage(message)
} }
clearCurrentMessage() clearCurrentMessage()
@ -1257,6 +1262,7 @@ class ThreadActivity : SimpleActivity() {
private fun cancelScheduledMessageAndRefresh(messageId: Long) { private fun cancelScheduledMessageAndRefresh(messageId: Long) {
ensureBackgroundThread { ensureBackgroundThread {
deleteScheduledMessage(messageId) deleteScheduledMessage(messageId)
cancelScheduleSendPendingIntent(messageId)
refreshMessages() refreshMessages()
} }
} }

View File

@ -609,7 +609,6 @@ fun Context.deleteMessage(id: Long, isMMS: Boolean) {
fun Context.deleteScheduledMessage(messageId: Long) { fun Context.deleteScheduledMessage(messageId: Long) {
try { try {
messagesDB.delete(messageId) messagesDB.delete(messageId)
cancelScheduleSendPendingIntent(messageId)
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
} }

View File

@ -16,4 +16,15 @@ data class Conversation(
@ColumnInfo(name = "is_group_conversation") var isGroupConversation: Boolean, @ColumnInfo(name = "is_group_conversation") var isGroupConversation: Boolean,
@ColumnInfo(name = "phone_number") var phoneNumber: String, @ColumnInfo(name = "phone_number") var phoneNumber: String,
@ColumnInfo(name = "is_scheduled") var isScheduled: Boolean = false @ColumnInfo(name = "is_scheduled") var isScheduled: Boolean = false
) ) {
fun areContentsTheSame(other: Conversation): Boolean {
return snippet == other.snippet &&
date == other.date &&
read == other.read &&
title == other.title &&
photoUri == other.photoUri &&
isGroupConversation == other.isGroupConversation &&
phoneNumber == other.phoneNumber
}
}