Add notification indicator

This commit is contained in:
Marcin Czachurski 2023-10-24 15:44:46 +02:00
parent db407195f2
commit 8de6c937ec
2 changed files with 29 additions and 4 deletions

View File

@ -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
}
}

View File

@ -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")