From dea975cd0a1f53e3be1b33c0b96a278bd135f852 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 22 Aug 2024 15:29:20 -0400 Subject: [PATCH] fix(ac): cache ac unread count and hasUnread in the view (#16152) Fixes #16023 Caches the values of the number of notifications and hasUnseen in the view so that we access status-go only when there is an update or on app start. Also, uses the response value from user actions in the AC to retrieve the hasUnseen value directly instead of re-fetching. Finally, fixes an issue where marking notifs as read/unread wouldn't update the count --- .../main/activity_center/controller.nim | 4 +++ .../main/activity_center/io_interface.nim | 6 ++++ .../modules/main/activity_center/module.nim | 28 +++++++++++-------- src/app/modules/main/activity_center/view.nim | 17 +++++++++-- src/app/modules/main/module.nim | 2 +- .../service/activity_center/service.nim | 19 +++++++++++++ 6 files changed, 61 insertions(+), 15 deletions(-) diff --git a/src/app/modules/main/activity_center/controller.nim b/src/app/modules/main/activity_center/controller.nim index c29f115c05..e93b70ea57 100644 --- a/src/app/modules/main/activity_center/controller.nim +++ b/src/app/modules/main/activity_center/controller.nim @@ -71,6 +71,10 @@ proc init*(self: Controller) = self.delegate.onNotificationsCountMayHaveChanged() self.updateActivityGroupCounters() + self.events.on(activity_center_service.SIGNAL_ACTIVITY_CENTER_UNSEEN_UPDATED) do(e: Args): + var evArgs = ActivityCenterNotificationHasUnseen(e) + self.delegate.onUnseenChanged(evArgs.hasUnseen) + self.events.on(activity_center_service.SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_REMOVED) do(e: Args): var evArgs = ActivityCenterNotificationIdsArgs(e) if (evArgs.notificationIds.len > 0): diff --git a/src/app/modules/main/activity_center/io_interface.nim b/src/app/modules/main/activity_center/io_interface.nim index 5e68251636..a14d0b6609 100644 --- a/src/app/modules/main/activity_center/io_interface.nim +++ b/src/app/modules/main/activity_center/io_interface.nim @@ -24,12 +24,18 @@ method hasMoreToShow*(self: AccessInterface): bool {.base.} = method unreadActivityCenterNotificationsCount*(self: AccessInterface): int {.base.} = raise newException(ValueError, "No implementation available") +method unreadActivityCenterNotificationsCountFromView*(self: AccessInterface): int {.base.} = + raise newException(ValueError, "No implementation available") + method hasUnseenActivityCenterNotifications*(self: AccessInterface): bool {.base.} = raise newException(ValueError, "No implementation available") method onNotificationsCountMayHaveChanged*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") +method onUnseenChanged*(self: AccessInterface, hasUnseen: bool) {.base.} = + raise newException(ValueError, "No implementation available") + method hasUnseenActivityCenterNotificationsChanged*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/activity_center/module.nim b/src/app/modules/main/activity_center/module.nim index 98a66f7f63..2e2002d7ea 100644 --- a/src/app/modules/main/activity_center/module.nim +++ b/src/app/modules/main/activity_center/module.nim @@ -61,24 +61,31 @@ method load*(self: Module) = method isLoaded*(self: Module): bool = return self.moduleLoaded -method viewDidLoad*(self: Module) = - self.moduleLoaded = true - self.delegate.activityCenterDidLoad() - -method hasMoreToShow*(self: Module): bool = - self.controller.hasMoreToShow() - method unreadActivityCenterNotificationsCount*(self: Module): int = self.controller.unreadActivityCenterNotificationsCount() +method unreadActivityCenterNotificationsCountFromView*(self: Module): int = + self.view.unreadCount() + method hasUnseenActivityCenterNotifications*(self: Module): bool = self.controller.hasUnseenActivityCenterNotifications() +method viewDidLoad*(self: Module) = + self.moduleLoaded = true + self.delegate.activityCenterDidLoad() + self.view.setUnreadCount(self.unreadActivityCenterNotificationsCount()) + self.view.setHasUnseen(self.hasUnseenActivityCenterNotifications()) + +method hasMoreToShow*(self: Module): bool = + self.controller.hasMoreToShow() + method onNotificationsCountMayHaveChanged*(self: Module) = - self.view.unreadActivityCenterNotificationsCountChanged() - self.view.hasUnseenActivityCenterNotificationsChanged() + self.view.setUnreadCount(self.unreadActivityCenterNotificationsCount()) self.delegate.onActivityNotificationsUpdated() +method onUnseenChanged*(self: Module, hasUnseen: bool) = + self.view.setHasUnseen(hasUnseen) + proc createMessageItemFromDto(self: Module, message: MessageDto, communityId: string, albumMessages: seq[MessageDto]): MessageItem = let contactDetails = self.controller.getContactDetails(message.`from`) let communityChats = self.controller.getCommunityById(communityId).chats @@ -221,7 +228,6 @@ method markAllActivityCenterNotificationsRead*(self: Module): string = method markAllActivityCenterNotificationsReadDone*(self: Module) = self.view.markAllActivityCenterNotificationsReadDone() - self.view.unreadActivityCenterNotificationsCountChanged() method markActivityCenterNotificationRead*(self: Module, notificationId: string) = self.controller.markActivityCenterNotificationRead(notificationId) @@ -229,7 +235,6 @@ method markActivityCenterNotificationRead*(self: Module, notificationId: string) method markActivityCenterNotificationReadDone*(self: Module, notificationIds: seq[string]) = for notificationId in notificationIds: self.view.markActivityCenterNotificationReadDone(notificationId) - self.view.unreadActivityCenterNotificationsCountChanged() method markAsSeenActivityCenterNotifications*(self: Module) = self.controller.markAsSeenActivityCenterNotifications() @@ -259,7 +264,6 @@ method dismissActivityCenterNotificationDone*(self: Module, notificationId: stri method markActivityCenterNotificationUnreadDone*(self: Module, notificationIds: seq[string]) = for notificationId in notificationIds: self.view.markActivityCenterNotificationUnreadDone(notificationId) - self.view.unreadActivityCenterNotificationsCountChanged() method removeActivityCenterNotifications*(self: Module, notificationIds: seq[string]) = self.view.removeActivityCenterNotifications(notificationIds) diff --git a/src/app/modules/main/activity_center/view.nim b/src/app/modules/main/activity_center/view.nim index cb1f6259ea..2fbbd93289 100644 --- a/src/app/modules/main/activity_center/view.nim +++ b/src/app/modules/main/activity_center/view.nim @@ -12,6 +12,7 @@ QtObject: modelVariant: QVariant groupCounters: Table[ActivityCenterGroup, int] unreadCount: int + hasUnseen: bool proc delete*(self: View) = self.QObject.delete @@ -24,6 +25,7 @@ QtObject: result.modelVariant = newQVariant(result.model) result.groupCounters = initTable[ActivityCenterGroup, int]() result.unreadCount = 0 + result.hasUnseen = false proc load*(self: View) = self.delegate.viewDidLoad() @@ -52,22 +54,33 @@ QtObject: proc unreadActivityCenterNotificationsCountChanged*(self: View) {.signal.} proc unreadActivityCenterNotificationsCount*(self: View): int {.slot.} = - self.unreadCount = self.delegate.unreadActivityCenterNotificationsCount() return self.unreadCount QtProperty[int] unreadActivityCenterNotificationsCount: read = unreadActivityCenterNotificationsCount notify = unreadActivityCenterNotificationsCountChanged + proc setUnreadCount*(self: View, count: int) = + if self.unreadCount == count: + return + self.unreadCount = count + self.unreadActivityCenterNotificationsCountChanged() + proc hasUnseenActivityCenterNotificationsChanged*(self: View) {.signal.} proc hasUnseenActivityCenterNotifications*(self: View): bool {.slot.} = - self.delegate.hasUnseenActivityCenterNotifications() + return self.hasUnseen QtProperty[bool] hasUnseenActivityCenterNotifications: read = hasUnseenActivityCenterNotifications notify = hasUnseenActivityCenterNotificationsChanged + proc setHasUnseen*(self: View, hasUnseen: bool) = + if self.hasUnseen == hasUnseen: + return + self.hasUnseen = hasUnseen + self.hasUnseenActivityCenterNotificationsChanged() + proc fetchActivityCenterNotifications(self: View) {.slot.} = self.delegate.fetchActivityCenterNotifications() diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index f502c56763..d76bfea647 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -972,7 +972,7 @@ method onChatLeft*[T](self: Module[T], chatId: string) = proc checkIfWeHaveNotifications[T](self: Module[T]) = let sectionWithUnread = self.view.model().isThereASectionWithUnreadMessages() - let activtyCenterNotifications = self.activityCenterModule.unreadActivityCenterNotificationsCount() > 0 + let activtyCenterNotifications = self.activityCenterModule.unreadActivityCenterNotificationsCountFromView() > 0 self.view.setNotificationAvailable(sectionWithUnread or activtyCenterNotifications) method onActivityNotificationsUpdated[T](self: Module[T]) = diff --git a/src/app_service/service/activity_center/service.nim b/src/app_service/service/activity_center/service.nim index ca1d048e67..49d4b03c0a 100644 --- a/src/app_service/service/activity_center/service.nim +++ b/src/app_service/service/activity_center/service.nim @@ -32,9 +32,13 @@ type ActivityCenterNotificationIdArgs* = ref object of Args notificationId*: string + ActivityCenterNotificationHasUnseen* = ref object of Args + hasUnseen*: bool + # Signals which may be emitted by this service: const SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_LOADED* = "activityCenterNotificationsLoaded" const SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED* = "activityCenterNotificationsCountMayChanged" +const SIGNAL_ACTIVITY_CENTER_UNSEEN_UPDATED* = "activityCenterNotificationsHasUnseenUpdated" const SIGNAL_ACTIVITY_CENTER_MARK_NOTIFICATIONS_AS_READ* = "activityCenterMarkNotificationsAsRead" const SIGNAL_ACTIVITY_CENTER_MARK_NOTIFICATIONS_AS_UNREAD* = "activityCenterMarkNotificationsAsUnread" const SIGNAL_ACTIVITY_CENTER_MARK_ALL_NOTIFICATIONS_AS_READ* = "activityCenterMarkAllNotificationsAsRead" @@ -137,6 +141,12 @@ QtObject: proc hasMoreToShow*(self: Service): bool = return self.cursor != "" + proc parseActivityCenterState*(self: Service, response: RpcResponse) = + var activityCenterState: JsonNode = newJObject() + if response.result.getProp("activityCenterState", activityCenterState): + let hasSeen = activityCenterState["hasSeen"].getBool + self.events.emit(SIGNAL_ACTIVITY_CENTER_UNSEEN_UPDATED, ActivityCenterNotificationHasUnseen(hasUnseen: not hasSeen)) + proc asyncActivityNotificationLoad*(self: Service) = let arg = AsyncActivityNotificationLoadTaskArg( tptr: asyncActivityNotificationLoadTask, @@ -234,7 +244,9 @@ QtObject: messagesWithMentionsCount: seenAndUnseenMessages.countWithMentions) self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_MARK_NOTIFICATIONS_AS_READ, ActivityCenterNotificationIdsArgs(notificationIds: notificationIds)) + self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: error "Error marking as read", msg = e.msg @@ -245,7 +257,9 @@ QtObject: if response.error != nil: raise newException(RpcException, response.error.message) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_MARK_NOTIFICATIONS_AS_UNREAD, ActivityCenterNotificationIdsArgs(notificationIds: notificationIds)) + self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: error "Error marking as unread", msg = e.msg @@ -263,7 +277,9 @@ QtObject: let data = MessagesMarkedAsReadArgs(chatId: seenAndUnseenMessages.chatId, allMessagesMarked: true) self.events.emit(SIGNAL_MESSAGES_MARKED_AS_READ, data) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_MARK_ALL_NOTIFICATIONS_AS_READ, Args()) + self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: error "Error marking all as read", msg = e.msg @@ -296,6 +312,7 @@ QtObject: if response.error != nil: raise newException(RpcException, response.error.message) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_REMOVED, ActivityCenterNotificationIdsArgs(notificationIds: notificationIds)) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: @@ -322,6 +339,7 @@ QtObject: self.chatService.updateOrAddChat(chat) self.events.emit(SIGNAL_CHAT_UPDATE, ChatUpdateArgs(chats: @[chat])) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_ACCEPTED, ActivityCenterNotificationIdArgs(notificationId: notificationId)) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: @@ -334,6 +352,7 @@ QtObject: if response.error != nil: raise newException(RpcException, response.error.message) + self.parseActivityCenterState(response) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_DISMISSED, ActivityCenterNotificationIdArgs(notificationId: notificationId)) self.events.emit(SIGNAL_ACTIVITY_CENTER_NOTIFICATIONS_COUNT_MAY_HAVE_CHANGED, Args()) except Exception as e: