Handle conversations with scheduled messages only

This commit is contained in:
Naveen 2022-09-28 02:05:06 +05:30
parent f837790948
commit ee8130c767
11 changed files with 171 additions and 36 deletions

View file

@ -208,29 +208,51 @@ class MainActivity : SimpleActivity() {
setupConversations(conversations)
getNewConversations(conversations)
}
conversations.forEach {
clearExpiredScheduledMessages(it.threadId)
}
}
}
private fun getNewConversations(cachedConversations: ArrayList<Conversation>) {
val privateCursor = getMyContactsCursor(false, true)
val privateCursor = getMyContactsCursor(favoritesOnly = false, withPhoneNumbersOnly = true)
ensureBackgroundThread {
val privateContacts = MyContactsContentProvider.getSimpleContacts(this, privateCursor)
val conversations = getConversations(privateContacts = privateContacts)
val scheduledConversations = cachedConversations.filter { it.isScheduled }
val allConversations = conversations.toArrayList().apply {
addAll(scheduledConversations)
}
runOnUiThread {
setupConversations(conversations)
setupConversations(allConversations)
}
conversations.forEach { clonedConversation ->
if (!cachedConversations.map { it.threadId }.contains(clonedConversation.threadId)) {
val threadIds = cachedConversations.map { it.threadId }
if (!threadIds.contains(clonedConversation.threadId)) {
conversationsDB.insertOrUpdate(clonedConversation)
cachedConversations.add(clonedConversation)
}
}
cachedConversations.forEach { cachedConversation ->
if (!conversations.map { it.threadId }.contains(cachedConversation.threadId)) {
conversationsDB.deleteThreadId(cachedConversation.threadId)
val threadId = cachedConversation.threadId
val isTemporaryThread = cachedConversation.isScheduled
val isConversationDeleted = !conversations.map { it.threadId }.contains(threadId)
if (isConversationDeleted && !isTemporaryThread) {
conversationsDB.deleteThreadId(threadId)
}
val newConversation = conversations.find { it.phoneNumber == cachedConversation.phoneNumber }
if (isTemporaryThread && newConversation != null) {
// delete the original temporary thread and move any scheduled messages to the new thread
conversationsDB.deleteThreadId(threadId)
messagesDB.getScheduledThreadMessages(threadId)
.forEach { message ->
messagesDB.insertOrUpdate(message.copy(threadId = newConversation.threadId))
}
}
}
@ -273,8 +295,9 @@ class MainActivity : SimpleActivity() {
hideKeyboard()
ConversationsAdapter(this, sortedConversations, conversations_list) {
Intent(this, ThreadActivity::class.java).apply {
putExtra(THREAD_ID, (it as Conversation).threadId)
putExtra(THREAD_TITLE, it.title)
val conversation = it as Conversation
putExtra(THREAD_ID, conversation.threadId)
putExtra(THREAD_TITLE, conversation.title)
startActivity(this)
}
}.apply {