From 8de6c937ec5ed5e3e2648019566a1e5b82d76006 Mon Sep 17 00:00:00 2001 From: Marcin Czachurski Date: Tue, 24 Oct 2023 15:44:46 +0200 Subject: [PATCH] Add notification indicator --- .../NotificationsView/NotificationsView.swift | 21 ++++++++++++++++++- .../Subviews/NotificationRowView.swift | 12 ++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Vernissage/Views/NotificationsView/NotificationsView.swift b/Vernissage/Views/NotificationsView/NotificationsView.swift index 035dae7..c3cd247 100644 --- a/Vernissage/Views/NotificationsView/NotificationsView.swift +++ b/Vernissage/Views/NotificationsView/NotificationsView.swift @@ -24,6 +24,7 @@ struct NotificationsView: View { @State private var minId: String? @State private var maxId: String? + @State private var lastSeenNotificationId: String? private let defaultPageSize = 40 @@ -59,7 +60,7 @@ struct NotificationsView: View { private func list() -> some View { List { ForEach(notifications, id: \.id) { notification in - NotificationRowView(notification: notification) + NotificationRowView(notification: notification, isNewNotification: self.isNewNotification(notification: notification)) } if allItemsLoaded == false { @@ -84,6 +85,11 @@ struct NotificationsView: View { func loadNotifications() async { do { + if let accountId = applicationState.account?.id { + let accountData = AccountDataHandler.shared.getAccountData(accountId: accountId, modelContext: modelContext) + self.lastSeenNotificationId = accountData?.lastSeenNotificationId + } + if let linkable = try await self.client.notifications?.notifications(maxId: maxId, minId: minId, limit: 5) { self.minId = linkable.link?.minId self.maxId = linkable.link?.maxId @@ -131,6 +137,11 @@ struct NotificationsView: View { private func refreshNotifications() async { do { + if let accountId = applicationState.account?.id { + let accountData = AccountDataHandler.shared.getAccountData(accountId: accountId, modelContext: modelContext) + self.lastSeenNotificationId = accountData?.lastSeenNotificationId + } + if let linkable = try await self.client.notifications?.notifications(minId: self.minId, limit: self.defaultPageSize) { if let first = linkable.data.first, self.notifications.contains(where: { notification in notification.id == first.id }) { // We have all notifications, we don't have to do anything. @@ -150,4 +161,12 @@ struct NotificationsView: View { ErrorService.shared.handle(error, message: "notifications.error.loadingNotificationsFailed", showToastr: !Task.isCancelled) } } + + private func isNewNotification(notification: PixelfedKit.Notification) -> Bool { + guard let lastSeenNotificationId = self.lastSeenNotificationId else { + return false + } + + return notification.id > lastSeenNotificationId + } } diff --git a/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift b/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift index 9b0738f..751335b 100644 --- a/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift +++ b/Vernissage/Views/NotificationsView/Subviews/NotificationRowView.swift @@ -20,10 +20,12 @@ struct NotificationRowView: View { @State private var image: SwiftUI.Image? private var attachment: MediaAttachment? - private var notification: PixelfedKit.Notification + private let isNewNotification: Bool + private let notification: PixelfedKit.Notification - public init(notification: PixelfedKit.Notification) { + public init(notification: PixelfedKit.Notification, isNewNotification: Bool) { self.notification = notification + self.isNewNotification = isNewNotification self.attachment = notification.status?.getAllImageMediaAttachments().first if let attachment, let previewUrl = attachment.previewUrl, let imageFromCache = CacheImageService.shared.get(for: previewUrl) { @@ -48,7 +50,7 @@ struct NotificationRowView: View { .frame(width: 56, height: 56) VStack(alignment: .leading, spacing: 0) { - HStack(alignment: .top) { + HStack(alignment: .center) { Text(self.notification.account.displayNameWithoutEmojis) .foregroundColor(.mainTextColor) .font(.footnote) @@ -61,6 +63,10 @@ struct NotificationRowView: View { .foregroundColor(.customGrayColor) .font(.footnote) } + + Circle() + .foregroundStyle(self.isNewNotification ? self.applicationState.tintColor.color() : Color.clear) + .frame(width: 8.0, height: 8.0) } Text(self.getTitle(), comment: "Notification type")