Feature: store selected notification filter (#1627) (#1663)

* store selected notification filter (#1627)

* store one filter for all accounts
This commit is contained in:
Thai D. V 2023-11-16 15:53:16 +07:00 committed by GitHub
parent 58d6a3b472
commit 4b74532048
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View file

@ -39,6 +39,7 @@ public struct NotificationsListView: View {
} }
} }
} }
.onAppear { viewModel.loadSelectedType() }
.toolbar { .toolbar {
ToolbarItem(placement: .principal) { ToolbarItem(placement: .principal) {
let title = lockedType?.menuTitle() ?? viewModel.selectedType?.menuTitle() ?? "notifications.navigation-title" let title = lockedType?.menuTitle() ?? viewModel.selectedType?.menuTitle() ?? "notifications.navigation-title"

View file

@ -36,16 +36,33 @@ import SwiftUI
var currentAccount: CurrentAccount? var currentAccount: CurrentAccount?
private let filterKey = "notification-filter"
var state: State = .loading var state: State = .loading
var selectedType: Models.Notification.NotificationType? { var selectedType: Models.Notification.NotificationType? {
didSet { didSet {
if oldValue != selectedType { guard oldValue != selectedType,
let id = client?.id
else { return }
UserDefaults.standard.set(selectedType?.rawValue ?? "", forKey: filterKey)
consolidatedNotifications = [] consolidatedNotifications = []
Task { Task {
await fetchNotifications() await fetchNotifications()
} }
} }
} }
func loadSelectedType() {
self.client = client
guard let value = UserDefaults.standard.string(forKey: filterKey)
else {
selectedType = nil
return
}
selectedType = .init(rawValue: value)
} }
var scrollToTopVisible: Bool = false var scrollToTopVisible: Bool = false