commit 3c2f281c6d2a4633d85be8ef40c6b10c14626884
parent 4ba63b0dbd28928c69898f698f323d5fc757b216
Author: William Casarin <jb55@jb55.com>
Date: Sun, 5 Mar 2023 20:36:53 -0500
Remember last notification tab
Suggested-By: Jack Dorsey
Diffstat:
1 file changed, 36 insertions(+), 3 deletions(-)
diff --git a/damus/Views/Notifications/NotificationsView.swift b/damus/Views/Notifications/NotificationsView.swift
@@ -7,7 +7,7 @@
import SwiftUI
-enum NotificationFilterState {
+enum NotificationFilterState: String {
case all
case zaps
case replies
@@ -27,10 +27,16 @@ enum NotificationFilterState {
struct NotificationsView: View {
let state: DamusState
@ObservedObject var notifications: NotificationsModel
- @State var filter_state: NotificationFilterState = .all
+ @State var filter_state: NotificationFilterState
@Environment(\.colorScheme) var colorScheme
+ init(state: DamusState, notifications: NotificationsModel) {
+ self.state = state
+ self._notifications = ObservedObject(initialValue: notifications)
+ self._filter_state = State(initialValue: load_notification_filter_state(pubkey: state.pubkey))
+ }
+
var body: some View {
TabView(selection: $filter_state) {
NotificationTab(NotificationFilterState.all)
@@ -45,6 +51,9 @@ struct NotificationsView: View {
.tag(NotificationFilterState.replies)
.id(NotificationFilterState.replies)
}
+ .onChange(of: filter_state) { val in
+ save_notification_filter_state(pubkey: state.pubkey, state: val)
+ }
.safeAreaInset(edge: .top, spacing: 0) {
VStack(spacing: 0) {
CustomPicker(selection: $filter_state, content: {
@@ -99,6 +108,30 @@ struct NotificationsView: View {
struct NotificationsView_Previews: PreviewProvider {
static var previews: some View {
- NotificationsView(state: test_damus_state(), notifications: NotificationsModel(), filter_state: .all )
+ NotificationsView(state: test_damus_state(), notifications: NotificationsModel())
}
}
+
+func notification_filter_state_key(pubkey: String) -> String {
+ return pk_setting_key(pubkey, key: "notification_filter_state")
+}
+
+func load_notification_filter_state(pubkey: String) -> NotificationFilterState {
+ let key = notification_filter_state_key(pubkey: pubkey)
+
+ guard let state_str = UserDefaults.standard.string(forKey: key) else {
+ return .all
+ }
+
+ guard let state = NotificationFilterState(rawValue: state_str) else {
+ return .all
+ }
+
+ return state
+}
+
+
+func save_notification_filter_state(pubkey: String, state: NotificationFilterState) {
+ let key = notification_filter_state_key(pubkey: pubkey)
+ UserDefaults.standard.set(state.rawValue, forKey: key)
+}