Top-level tab state restoration

Changelog-Added: Top-level tab state restoration
Closes: #634
This commit is contained in:
Bryan Montz
2023-02-17 06:30:48 -06:00
committed by William Casarin
parent 9f701a7d44
commit c679be9644
4 changed files with 21 additions and 38 deletions

View File

@@ -62,7 +62,7 @@ struct ContentView: View {
@State var status: String = "Not connected" @State var status: String = "Not connected"
@State var active_sheet: Sheets? = nil @State var active_sheet: Sheets? = nil
@State var damus_state: DamusState? = nil @State var damus_state: DamusState? = nil
@State var selected_timeline: Timeline? = .home @SceneStorage("ContentView.selected_timeline") var selected_timeline: Timeline = .home
@State var is_deleted_account: Bool = false @State var is_deleted_account: Bool = false
@State var is_profile_open: Bool = false @State var is_profile_open: Bool = false
@State var event: NostrEvent? = nil @State var event: NostrEvent? = nil
@@ -76,7 +76,7 @@ struct ContentView: View {
@State var confirm_mute: Bool = false @State var confirm_mute: Bool = false
@State var user_muted_confirm: Bool = false @State var user_muted_confirm: Bool = false
@State var confirm_overwrite_mutelist: Bool = false @State var confirm_overwrite_mutelist: Bool = false
@State var filter_state : FilterState = .posts_and_replies @SceneStorage("ContentView.filter_state") var filter_state : FilterState = .posts_and_replies
@State private var isSideBarOpened = false @State private var isSideBarOpened = false
@StateObject var home: HomeModel = HomeModel() @StateObject var home: HomeModel = HomeModel()
@@ -180,9 +180,6 @@ struct ContentView: View {
case .dms: case .dms:
DirectMessagesView(damus_state: damus_state!, model: damus_state!.dms, settings: damus_state!.settings) DirectMessagesView(damus_state: damus_state!, model: damus_state!.dms, settings: damus_state!.settings)
case .none:
EmptyView()
} }
} }
.navigationBarTitle(timeline_name(selected_timeline), displayMode: .inline) .navigationBarTitle(timeline_name(selected_timeline), displayMode: .inline)

View File

@@ -152,9 +152,6 @@ class UserSettingsStore: ObservableObject {
@StringSetting(key: "friend_filter", default_value: .all) @StringSetting(key: "friend_filter", default_value: .all)
var friend_filter: FriendFilter var friend_filter: FriendFilter
@StringSetting(key: "notification_state", default_value: .all)
var notification_state: NotificationFilterState
@StringSetting(key: "translation_service", default_value: .none) @StringSetting(key: "translation_service", default_value: .none)
var translation_service: TranslationService var translation_service: TranslationService

View File

@@ -37,7 +37,7 @@ func show_indicator(timeline: Timeline, current: NewEventsBits, indicator_settin
struct TabButton: View { struct TabButton: View {
let timeline: Timeline let timeline: Timeline
let img: String let img: String
@Binding var selected: Timeline? @Binding var selected: Timeline
@Binding var new_events: NewEventsBits @Binding var new_events: NewEventsBits
let settings: UserSettingsStore let settings: UserSettingsStore
@@ -75,7 +75,7 @@ struct TabButton: View {
struct TabBar: View { struct TabBar: View {
@Binding var new_events: NewEventsBits @Binding var new_events: NewEventsBits
@Binding var selected: Timeline? @Binding var selected: Timeline
let settings: UserSettingsStore let settings: UserSettingsStore
let action: (Timeline) -> () let action: (Timeline) -> ()

View File

@@ -74,25 +74,13 @@ class NotificationFilter: ObservableObject, Equatable {
} }
} }
enum NotificationFilterState: String, StringCodable { enum NotificationFilterState: String {
case all case all
case zaps case zaps
case replies case replies
init?(from string: String) {
guard let val = NotificationFilterState(rawValue: string) else {
return nil
}
self = val
}
func to_string() -> String {
self.rawValue
}
func is_other( item: NotificationItem) -> Bool { func is_other( item: NotificationItem) -> Bool {
return item.is_zap == nil && item.is_reply == nil item.is_zap == nil && item.is_reply == nil
} }
func filter(_ item: NotificationItem) -> Bool { func filter(_ item: NotificationItem) -> Bool {
@@ -110,7 +98,8 @@ enum NotificationFilterState: String, StringCodable {
struct NotificationsView: View { struct NotificationsView: View {
let state: DamusState let state: DamusState
@ObservedObject var notifications: NotificationsModel @ObservedObject var notifications: NotificationsModel
@StateObject var filter_state: NotificationFilter = NotificationFilter() @StateObject var filter = NotificationFilter()
@SceneStorage("NotificationsView.filter_state") var filter_state: NotificationFilterState = .all
@Environment(\.colorScheme) var colorScheme @Environment(\.colorScheme) var colorScheme
@@ -123,14 +112,14 @@ struct NotificationsView: View {
} }
var body: some View { var body: some View {
TabView(selection: $filter_state.state) { TabView(selection: $filter_state) {
// This is needed or else there is a bug when switching from the 3rd or 2nd tab to first. no idea why. // This is needed or else there is a bug when switching from the 3rd or 2nd tab to first. no idea why.
mystery mystery
NotificationTab( NotificationTab(
NotificationFilter( NotificationFilter(
state: .all, state: .all,
fine_filter: filter_state.fine_filter fine_filter: filter.fine_filter
) )
) )
.tag(NotificationFilterState.all) .tag(NotificationFilterState.all)
@@ -138,7 +127,7 @@ struct NotificationsView: View {
NotificationTab( NotificationTab(
NotificationFilter( NotificationFilter(
state: .zaps, state: .zaps,
fine_filter: filter_state.fine_filter fine_filter: filter.fine_filter
) )
) )
.tag(NotificationFilterState.zaps) .tag(NotificationFilterState.zaps)
@@ -146,31 +135,31 @@ struct NotificationsView: View {
NotificationTab( NotificationTab(
NotificationFilter( NotificationFilter(
state: .replies, state: .replies,
fine_filter: filter_state.fine_filter fine_filter: filter.fine_filter
) )
) )
.tag(NotificationFilterState.replies) .tag(NotificationFilterState.replies)
} }
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {
if would_filter_non_friends_from_notifications(contacts: state.contacts, state: self.filter_state.state, items: self.notifications.notifications) { if would_filter_non_friends_from_notifications(contacts: state.contacts, state: filter_state, items: self.notifications.notifications) {
FriendsButton(filter: $filter_state.fine_filter) FriendsButton(filter: $filter.fine_filter)
} }
} }
} }
.onChange(of: filter_state.fine_filter) { val in .onChange(of: filter.fine_filter) { val in
state.settings.friend_filter = val state.settings.friend_filter = val
} }
.onChange(of: filter_state.state) { val in .onChange(of: filter_state) { val in
state.settings.notification_state = val filter.state = val
} }
.onAppear { .onAppear {
self.filter_state.fine_filter = state.settings.friend_filter self.filter.fine_filter = state.settings.friend_filter
self.filter_state.state = state.settings.notification_state filter.state = filter_state
} }
.safeAreaInset(edge: .top, spacing: 0) { .safeAreaInset(edge: .top, spacing: 0) {
VStack(spacing: 0) { VStack(spacing: 0) {
CustomPicker(selection: $filter_state.state, content: { CustomPicker(selection: $filter_state, content: {
Text("All", comment: "Label for filter for all notifications.") Text("All", comment: "Label for filter for all notifications.")
.tag(NotificationFilterState.all) .tag(NotificationFilterState.all)
@@ -221,7 +210,7 @@ struct NotificationsView: View {
struct NotificationsView_Previews: PreviewProvider { struct NotificationsView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
NotificationsView(state: test_damus_state(), notifications: NotificationsModel(), filter_state: NotificationFilter()) NotificationsView(state: test_damus_state(), notifications: NotificationsModel(), filter: NotificationFilter())
} }
} }