From c1ee04eed17ac128b8788c44bb94a71687d88256 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Wed, 7 Dec 2022 18:49:17 +0300 Subject: [PATCH] android: Cancel notification after message deletion (#1512) * android: Cancel notification after message deletion * Improve * Temporary chat item * Better * Changes * cInfo, cItem Co-authored-by: JRoberts <8711996+jr-simplex@users.noreply.github.com> --- .../java/chat/simplex/app/model/ChatModel.kt | 24 ++++++++++++++++++- .../java/chat/simplex/app/model/NtfManager.kt | 2 ++ .../java/chat/simplex/app/model/SimpleXAPI.kt | 17 ++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt index aa8bc0f63..b9c5d9819 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/ChatModel.kt @@ -222,7 +222,10 @@ class ChatModel(val controller: ChatController) { chat = chats[i] val pItem = chat.chatItems.lastOrNull() if (pItem?.id == cItem.id) { - chats[i] = chat.copy(chatItems = arrayListOf(cItem)) + chats[i] = chat.copy(chatItems = arrayListOf(ChatItem.defaultDeleted)) + } + if (cItem.isRcvNew) { + decreaseCounterInChat(cInfo.id) } } // remove from current chat @@ -1201,6 +1204,25 @@ data class ChatItem ( file = null ) } + + private const val TEMP_DELETED_CHAT_ITEM_ID = -1L + val defaultDeleted: ChatItem + get() = ChatItem( + chatDir = CIDirection.DirectRcv(), + meta = CIMeta( + itemId = TEMP_DELETED_CHAT_ITEM_ID, + itemTs = Clock.System.now(), + itemText = generalGetString(R.string.deleted_description), + itemStatus = CIStatus.RcvRead(), + createdAt = Clock.System.now(), + itemDeleted = false, + itemEdited = false, + editable = false + ), + content = CIContent.RcvDeleted(deleteMode = CIDeleteMode.cidmBroadcast), + quotedItem = null, + file = null + ) } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt index 1dc71ef29..91d3948a9 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/NtfManager.kt @@ -226,6 +226,8 @@ class NtfManager(val context: Context, private val appPreferences: AppPreference manager.cancel(CallNotificationId) } + fun hasNotificationsForChat(chatId: String): Boolean = manager.activeNotifications.any { it.id == chatId.hashCode() } + private fun hideSecrets(cItem: ChatItem) : String { val md = cItem.formattedText return if (md != null) { diff --git a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt index e1274c000..4be1db550 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/model/SimpleXAPI.kt @@ -1070,11 +1070,22 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a is CR.ChatItemUpdated -> chatItemSimpleUpdate(r.chatItem) is CR.ChatItemDeleted -> { - AudioPlayer.stop(r.deletedChatItem.chatItem) + val cInfo = r.deletedChatItem.chatInfo + val cItem = r.deletedChatItem.chatItem + AudioPlayer.stop(cItem) + val isLastChatItem = chatModel.getChat(cInfo.id)?.chatItems?.lastOrNull()?.id == cItem.id + if (isLastChatItem && ntfManager.hasNotificationsForChat(cInfo.id)) { + ntfManager.cancelNotificationsForChat(cInfo.id) + ntfManager.notifyMessageReceived( + cInfo.id, + cInfo.displayName, + generalGetString(if (r.toChatItem != null) R.string.marked_deleted_description else R.string.deleted_description) + ) + } if (r.toChatItem == null) { - chatModel.removeChatItem(r.deletedChatItem.chatInfo, r.deletedChatItem.chatItem) + chatModel.removeChatItem(cInfo, cItem) } else { - chatModel.upsertChatItem(r.toChatItem.chatInfo, r.toChatItem.chatItem) + chatModel.upsertChatItem(cInfo, r.toChatItem.chatItem) } } is CR.ReceivedGroupInvitation -> {