Clear notifications on list view appear

This commit is contained in:
Thomas Ricouard 2022-12-29 09:10:49 +01:00
parent 346345e0c9
commit 5bc262da33
4 changed files with 18 additions and 0 deletions

View file

@ -0,0 +1,5 @@
import Foundation
public struct ServerError: Decodable {
public let error: String?
}

View file

@ -4,11 +4,14 @@ public enum Notifications: Endpoint {
case notifications(sinceId: String?,
maxId: String?,
types: [String]?)
case clear
public func path() -> String {
switch self {
case .notifications:
return "notifications"
case .clear:
return "notifications/clear"
}
}
@ -22,6 +25,8 @@ public enum Notifications: Endpoint {
}
}
return params
default:
return nil
}
}
}

View file

@ -37,6 +37,7 @@ public struct NotificationsListView: View {
.task {
viewModel.client = client
await viewModel.fetchNotifications()
await viewModel.clear()
}
.refreshable {
await viewModel.fetchNotifications()

View file

@ -72,6 +72,13 @@ class NotificationsViewModel: ObservableObject {
}
}
func clear() async {
guard let client else { return }
do {
let _: ServerError = try await client.post(endpoint: Notifications.clear)
} catch { }
}
func handleEvent(event: any StreamEvent) {
if let event = event as? StreamEventNotification {
notifications.insert(event.notification, at: 0)