Skip to content

Commit 9dfcff8

Browse files
authored
fix: update notification read status on refresh (#706)
1 parent 336f0f6 commit 9dfcff8

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

Splajompy.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Splajompy/Notifications/NotificationRepository.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ struct Notification: Identifiable, Decodable, Equatable {
2121
var post: Post?
2222
var comment: Comment?
2323

24-
var id: Int { notificationId }
24+
// TODO: this is kind of a hack. in an ideal world we'd have an observable object for each notification
25+
var id: String { "\(notificationId) \(viewed)" }
2526

2627
var richContent: AttributedString {
2728
let markdown = generateAttributedStringUsingFacets(

Splajompy/Notifications/NotificationRow.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct NotificationRow: View {
99
notification.hasNotificationActors == true
1010
{
1111
return .notificationActorsList(
12-
notificationId: notification.id,
12+
notificationId: notification.notificationId,
1313
postId: postId
1414
)
1515
} else if let postId = notification.postId {

Splajompy/Notifications/NotificationsView.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct NotificationsView: View {
2323
notifications: notifications
2424
)
2525
.padding(.horizontal)
26-
.task {
26+
.task(id: viewModel.lastRefreshTime) {
2727
if viewModel.lastRefreshTime.addingTimeInterval(5) > Date() {
2828
do {
2929
try await Task.sleep(for: .seconds(2))
@@ -82,6 +82,15 @@ struct NotificationsView: View {
8282
}
8383
}
8484
#else
85+
.toolbar {
86+
ToolbarItem {
87+
Button("Refresh", systemImage: "arrow.clockwise") {
88+
Task {
89+
await viewModel.refreshNotifications()
90+
}
91+
}
92+
}
93+
}
8594
.navigationTitle("Notifications")
8695
#endif
8796
}
@@ -110,7 +119,7 @@ struct NotificationsView: View {
110119
private func notificationsList(
111120
notifications: [Notification]
112121
) -> some View {
113-
ForEach(notifications, id: \.notificationId) { notification in
122+
ForEach(notifications) { notification in
114123
VStack {
115124
NotificationRow(notification: notification)
116125
#if os(macOS)

Splajompy/Notifications/NotificationsViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ extension NotificationsView {
7070

7171
lastReadNotificationTime = nil
7272
lastUnreadNotificationTime = nil
73-
lastRefreshTime = Date()
7473

7574
async let unreadResult = service.getUnreadNotificationsWithTimeOffset(
7675
beforeTime: nil,
@@ -93,6 +92,7 @@ extension NotificationsView {
9392
lastReadNotificationTime = readNotifications.last?.createdAt
9493
hasMoreUnreadToLoad = unreadNotifications.count == limit
9594
hasMoreToLoad = readNotifications.count == limit
95+
lastRefreshTime = Date()
9696
case (.failure(let error), _), (_, .failure(let error)):
9797
state = .failed(error)
9898
}
@@ -153,7 +153,7 @@ extension NotificationsView {
153153
func markNotificationAsRead(notificationId: Int) async {
154154
guard case .loaded(var notifications) = state else { return }
155155

156-
if let index = notifications.firstIndex(where: { $0.id == notificationId }
156+
if let index = notifications.firstIndex(where: { $0.notificationId == notificationId }
157157
) {
158158
notifications[index].viewed = true
159159
}

0 commit comments

Comments
 (0)