Add mentions filter in notifications

This commit is contained in:
Thomas Ricouard 2022-12-22 07:00:44 +01:00
parent 6144e2ba6a
commit 1b4bef1459
3 changed files with 37 additions and 7 deletions

View file

@ -1,7 +1,7 @@
import Foundation import Foundation
public enum Notifications: Endpoint { public enum Notifications: Endpoint {
case notifications(maxId: String?) case notifications(maxId: String?, onlyMentions: Bool)
public func path() -> String { public func path() -> String {
switch self { switch self {
@ -12,9 +12,15 @@ public enum Notifications: Endpoint {
public func queryItems() -> [URLQueryItem]? { public func queryItems() -> [URLQueryItem]? {
switch self { switch self {
case .notifications(let maxId): case .notifications(let maxId, let onlyMentions):
guard let maxId else { return nil } var params: [URLQueryItem] = []
return [.init(name: "max_id", value: maxId)] if onlyMentions {
params.append(.init(name: "types[]", value: "mention"))
}
if let maxId {
params.append(.init(name: "max_id", value: maxId))
}
return params
} }
} }
} }

View file

@ -14,7 +14,16 @@ public struct NotificationsListView: View {
ScrollView { ScrollView {
LazyVStack { LazyVStack {
if client.isAuth { if client.isAuth {
notificationsView Picker("", selection: $viewModel.tab) {
ForEach(NotificationsViewModel.Tab.allCases, id: \.self) { tab in
Text(tab.rawValue)
}
}
.pickerStyle(.segmented)
Group {
notificationsView
}
.padding(.top, 16)
} else { } else {
Text("Please Sign In to see your notifications") Text("Please Sign In to see your notifications")
.font(.title3) .font(.title3)

View file

@ -14,8 +14,21 @@ class NotificationsViewModel: ObservableObject {
case error(error: Error) case error(error: Error)
} }
public enum Tab: String, CaseIterable {
case all = "All"
case mentions = "Mentions"
}
var client: Client? var client: Client?
@Published var state: State = .loading @Published var state: State = .loading
@Published var tab: Tab = .all {
didSet {
notifications = []
Task {
await fetchNotifications()
}
}
}
private var notifications: [Models.Notification] = [] private var notifications: [Models.Notification] = []
@ -25,7 +38,8 @@ class NotificationsViewModel: ObservableObject {
if notifications.isEmpty { if notifications.isEmpty {
state = .loading state = .loading
} }
notifications = try await client.get(endpoint: Notifications.notifications(maxId: nil)) notifications = try await client.get(endpoint: Notifications.notifications(maxId: nil,
onlyMentions: tab == .mentions))
state = .display(notifications: notifications, nextPageState: .hasNextPage) state = .display(notifications: notifications, nextPageState: .hasNextPage)
} catch { } catch {
state = .error(error: error) state = .error(error: error)
@ -37,7 +51,8 @@ class NotificationsViewModel: ObservableObject {
do { do {
guard let lastId = notifications.last?.id else { return } guard let lastId = notifications.last?.id else { return }
state = .display(notifications: notifications, nextPageState: .loadingNextPage) state = .display(notifications: notifications, nextPageState: .loadingNextPage)
let newNotifications: [Models.Notification] = try await client.get(endpoint: Notifications.notifications(maxId: lastId)) let newNotifications: [Models.Notification] = try await client.get(endpoint: Notifications.notifications(maxId: lastId,
onlyMentions: tab == .mentions))
notifications.append(contentsOf: newNotifications) notifications.append(contentsOf: newNotifications)
state = .display(notifications: notifications, nextPageState: .hasNextPage) state = .display(notifications: notifications, nextPageState: .hasNextPage)
} catch { } catch {