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
public enum Notifications: Endpoint {
case notifications(maxId: String?)
case notifications(maxId: String?, onlyMentions: Bool)
public func path() -> String {
switch self {
@ -12,9 +12,15 @@ public enum Notifications: Endpoint {
public func queryItems() -> [URLQueryItem]? {
switch self {
case .notifications(let maxId):
guard let maxId else { return nil }
return [.init(name: "max_id", value: maxId)]
case .notifications(let maxId, let onlyMentions):
var params: [URLQueryItem] = []
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 {
LazyVStack {
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 {
Text("Please Sign In to see your notifications")
.font(.title3)

View file

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