Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ac): cache ac unread count and hasUnread in the view #16152

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app/modules/main/activity_center/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions src/app/modules/main/activity_center/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
26 changes: 15 additions & 11 deletions src/app/modules/main/activity_center/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,31 @@ method load*(self: Module) =
method isLoaded*(self: Module): bool =
return self.moduleLoaded

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 unreadActivityCenterNotificationsCount*(self: Module): int =
self.controller.unreadActivityCenterNotificationsCount()

method hasUnseenActivityCenterNotifications*(self: Module): bool =
self.controller.hasUnseenActivityCenterNotifications()

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
Expand Down Expand Up @@ -221,15 +228,13 @@ 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)

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()
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 15 additions & 2 deletions src/app/modules/main/activity_center/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ QtObject:
modelVariant: QVariant
groupCounters: Table[ActivityCenterGroup, int]
unreadCount: int
hasUnseen: bool

proc delete*(self: View) =
self.QObject.delete
Expand All @@ -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()
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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]) =
Expand Down
19 changes: 19 additions & 0 deletions src/app_service/service/activity_center/service.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down