From 22b7aa90b2fb996258670c24ddaee028630b5782 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Thu, 26 Jan 2023 23:00:54 +0000 Subject: [PATCH] android: multiuser-notifications (#1846) --- .../java/chat/simplex/app/model/SimpleXAPI.kt | 53 +++++++------------ .../chat/simplex/app/views/chat/ChatView.kt | 5 +- 2 files changed, 24 insertions(+), 34 deletions(-) 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 603ae95cbe..d00fcf4f75 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 @@ -1160,6 +1160,8 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a chatModel.updateContact(r.contact) chatModel.dismissConnReqView(r.contact.activeConn.id) chatModel.removeChat(r.contact.activeConn.id) + } + if (r.contact.directOrUsed) { ntfManager.notifyContactConnected(r.user, r.contact) } chatModel.setContactNetworkStatus(r.contact, NetworkStatus.Connected()) @@ -1215,22 +1217,20 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } is CR.NewChatItem -> { - if (!active(r.user)) { - if (r.chatItem.chatItem.isRcvNew && r.chatItem.chatInfo.ntfsEnabled) { - chatModel.increaseUnreadCounter(r.user) - } - return - } val cInfo = r.chatItem.chatInfo val cItem = r.chatItem.chatItem - chatModel.addChatItem(cInfo, cItem) + if (active(r.user)) { + chatModel.addChatItem(cInfo, cItem) + } else if (cItem.isRcvNew && cInfo.ntfsEnabled) { + chatModel.increaseUnreadCounter(r.user) + } val file = cItem.file val mc = cItem.content.msgContent if (file != null && file.fileSize <= MAX_IMAGE_SIZE_AUTO_RCV) { val acceptImages = appPrefs.privacyAcceptImages.get() if ((mc is MsgContent.MCImage && acceptImages) || (mc is MsgContent.MCVoice && ((file.fileSize > MAX_VOICE_SIZE_FOR_SENDING && acceptImages) || cInfo is ChatInfo.Group))) { - withApi { receiveFile(file.fileId) } // TODO check inlineFileMode != IFMSent + withApi { receiveFile(r.user, file.fileId) } // TODO check inlineFileMode != IFMSent } } if (cItem.showNotification && (!SimplexApp.context.isAppOnForeground || chatModel.chatId.value != cInfo.id)) { @@ -1238,22 +1238,17 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } is CR.ChatItemStatusUpdated -> { - if (!active(r.user)) return - val cInfo = r.chatItem.chatInfo val cItem = r.chatItem.chatItem - var res = false - if (!cItem.isDeletedContent) { - res = chatModel.upsertChatItem(cInfo, cItem) + if (active(r.user) && !cItem.isDeletedContent && chatModel.upsertChatItem(cInfo, cItem)) { + ntfManager.notifyMessageReceived(r.user, cInfo, cItem) } - if (res) { + if (!active(r.user) && !cItem.isDeletedContent) { ntfManager.notifyMessageReceived(r.user, cInfo, cItem) } } is CR.ChatItemUpdated -> - if (active(r.user)) { - chatItemSimpleUpdate(r.chatItem) - } + chatItemSimpleUpdate(r.user, r.chatItem) is CR.ChatItemDeleted -> { if (!active(r.user)) { if (r.toChatItem == null && r.deletedChatItem.chatItem.isRcvNew && r.deletedChatItem.chatInfo.ntfsEnabled) { @@ -1341,21 +1336,13 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a chatModel.updateGroup(r.toGroup) } is CR.RcvFileStart -> - if (active(r.user)) { - chatItemSimpleUpdate(r.chatItem) - } + chatItemSimpleUpdate(r.user, r.chatItem) is CR.RcvFileComplete -> - if (active(r.user)) { - chatItemSimpleUpdate(r.chatItem) - } + chatItemSimpleUpdate(r.user, r.chatItem) is CR.SndFileStart -> - if (active(r.user)) { - chatItemSimpleUpdate(r.chatItem) - } + chatItemSimpleUpdate(r.user, r.chatItem) is CR.SndFileComplete -> { - if (!active(r.user)) return - - chatItemSimpleUpdate(r.chatItem) + chatItemSimpleUpdate(r.user, r.chatItem) val cItem = r.chatItem.chatItem val mc = cItem.content.msgContent val fileName = cItem.file?.fileName @@ -1427,11 +1414,11 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } - suspend fun receiveFile(fileId: Long) { + suspend fun receiveFile(user: User, fileId: Long) { val inline = appPrefs.privacyTransferImagesInline.get() val chatItem = apiReceiveFile(fileId, inline) if (chatItem != null) { - chatItemSimpleUpdate(chatItem) + chatItemSimpleUpdate(user, chatItem) } } @@ -1442,10 +1429,10 @@ open class ChatController(var ctrl: ChatCtrl?, val ntfManager: NtfManager, val a } } - private suspend fun chatItemSimpleUpdate(aChatItem: AChatItem) { + private suspend fun chatItemSimpleUpdate(user: User, aChatItem: AChatItem) { val cInfo = aChatItem.chatInfo val cItem = aChatItem.chatItem - if (chatModel.upsertChatItem(cInfo, cItem)) { + if (!active(user) || chatModel.upsertChatItem(cInfo, cItem)) { ntfManager.notifyMessageReceived(chatModel.currentUser.value!!, cInfo, cItem) } } diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt index 22a5a14e55..34edf87a75 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt @@ -204,7 +204,10 @@ fun ChatView(chatId: String, chatModel: ChatModel, onComposed: () -> Unit) { } }, receiveFile = { fileId -> - withApi { chatModel.controller.receiveFile(fileId) } + val user = chatModel.currentUser.value + if (user != null) { + withApi { chatModel.controller.receiveFile(user, fileId) } + } }, joinGroup = { groupId -> withApi { chatModel.controller.apiJoinGroup(groupId) }