From 5bc262da3374d09211bcd3d0f0055e3a641186c8 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Thu, 29 Dec 2022 09:10:49 +0100 Subject: [PATCH] Clear notifications on list view appear --- Packages/Models/Sources/Models/ServerError.swift | 5 +++++ .../Network/Sources/Network/Endpoint/Notifications.swift | 5 +++++ .../Sources/Notifications/NotificationsListView.swift | 1 + .../Sources/Notifications/NotificationsViewModel.swift | 7 +++++++ 4 files changed, 18 insertions(+) create mode 100644 Packages/Models/Sources/Models/ServerError.swift diff --git a/Packages/Models/Sources/Models/ServerError.swift b/Packages/Models/Sources/Models/ServerError.swift new file mode 100644 index 00000000..a8c13fdb --- /dev/null +++ b/Packages/Models/Sources/Models/ServerError.swift @@ -0,0 +1,5 @@ +import Foundation + +public struct ServerError: Decodable { + public let error: String? +} diff --git a/Packages/Network/Sources/Network/Endpoint/Notifications.swift b/Packages/Network/Sources/Network/Endpoint/Notifications.swift index e5716a48..45f96c03 100644 --- a/Packages/Network/Sources/Network/Endpoint/Notifications.swift +++ b/Packages/Network/Sources/Network/Endpoint/Notifications.swift @@ -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 } } } diff --git a/Packages/Notifications/Sources/Notifications/NotificationsListView.swift b/Packages/Notifications/Sources/Notifications/NotificationsListView.swift index 5abe9d5e..91eae22f 100644 --- a/Packages/Notifications/Sources/Notifications/NotificationsListView.swift +++ b/Packages/Notifications/Sources/Notifications/NotificationsListView.swift @@ -37,6 +37,7 @@ public struct NotificationsListView: View { .task { viewModel.client = client await viewModel.fetchNotifications() + await viewModel.clear() } .refreshable { await viewModel.fetchNotifications() diff --git a/Packages/Notifications/Sources/Notifications/NotificationsViewModel.swift b/Packages/Notifications/Sources/Notifications/NotificationsViewModel.swift index 2dee61bd..a9ce9b21 100644 --- a/Packages/Notifications/Sources/Notifications/NotificationsViewModel.swift +++ b/Packages/Notifications/Sources/Notifications/NotificationsViewModel.swift @@ -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)