Closed alancook closed 3 years ago
My breakpoint is shown, so the code is called. Are you sure you touch save on the settings form?
Yes you are correct, my bad but that lead me to the solution I wanted to have so I have some code to come back to you as I am building a TabView app also and wanted to save the state of the selected index (seams like a easy problem but as it turns out it ended up being a lot of screwing around), anyways if you want to merge these changes to you project you app now saved the last tab item selected, see all changes below in bold, Alan
In AppUserDefaults.swift add the tabViewIndex variable
struct AppUserDefaults { @UserDefault("user_region", defaultValue: Locale.current.regionCode ?? "US") static var region: String
@UserDefault("original_title", defaultValue: false)
static var alwaysOriginalTitle: Bool
@UserDefault("tab_view_index", defaultValue: Tab.movies.rawValue)
static var tabViewIndex: Int
}
In HomeView.swift move the enum out side of the struct TabbarView: View
enum Tab: Int { case movies, discover, fanClub, myLists }
And in struct TabbarView: View change the selectedTab assignment variable
@State var selectedTab = Tab(rawValue: AppUserDefaults.tabViewIndex)!
Then in MoviesHome.swift add . onAppear
var body: some View { NavigationView { Group { if selectedMenu.menu == .genres { GenresList(headerView: AnyView(segmentedView)) } else { MoviesHomeList(menu: $selectedMenu.menu, pageListener: selectedMenu.pageListener, headerView: AnyView(segmentedView)) } } .navigationBarItems(trailing: Button(action: { self.isSettingPresented = true }) { HStack { Image(systemName: "wrench").imageScale(.medium) }.frame(width: 30, height: 30) } ).sheet(isPresented: $isSettingPresented, content: { SettingsForm() }) .onAppear { AppUserDefaults.tabViewIndex = Tab.movies.rawValue } } } }
Then in DiscoverView.swift change . onAppear
func body(props: Props) -> some View {
ZStack(alignment: .center) {
draggableMovies(props: props)
GeometryReader { reader in
self.filterView(props: props)
.position(x: reader.frame(in: .local).midX,
y: reader.frame(in: .local).minY + reader.safeAreaInsets.top + 10)
.frame(height: 50)
.sheet(isPresented: self.$isFilterFormPresented, content: { DiscoverFilterForm().environmentObject(store) })
self.actionsButtons(props: props)
.position(x: reader.frame(in: .local).midX,
y: reader.frame(in: .local).maxY - reader.safeAreaInsets.bottom - self.bottomSafeInsetFix)
}
}
.background(FullscreenMoviePosterImage(imageLoader: ImageLoaderCache.shared.loaderFor(path: props.currentMovie?.poster_path,
size: .original))
.allowsHitTesting(false)
.transition(.opacity)
.animation(.easeInOut))
.onAppear {
self.hapticFeedback.prepare()
self.fetchRandomMovies(props: props, force: false, filter: props.filter)
props.dispatch(MoviesActions.FetchGenres())
AppUserDefaults.tabViewIndex = Tab.discover.rawValue
}
}
Then in FanClubHome.swift change . onAppear
func body(props: Props) -> some View {
NavigationView {
List {
Section {
ForEach(props.peoples, id: \.self) { people in
NavigationLink(destination: PeopleDetail(peopleId: people)) {
PeopleRow(peopleId: people)
}
}.onDelete(perform: { index in
props.dispatch(PeopleActions.RemoveFromFanClub(people: props.peoples[index.first!]))
})
}
Section(header: Text("Popular people to add to your Fan Club")) {
ForEach(props.popular, id: \.self) { people in
NavigationLink(destination: PeopleDetail(peopleId: people)) {
PeopleRow(peopleId: people)
}
}
}
if !props.popular.isEmpty {
Rectangle()
.foregroundColor(.clear)
.onAppear {
self.currentPage += 1
props.dispatch(PeopleActions.FetchPopular(page: self.currentPage))
}
}
}
.navigationBarTitle("Fan Club")
}
.onAppear {
props.dispatch(PeopleActions.FetchPopular(page: self.currentPage))
AppUserDefaults.tabViewIndex = Tab.fanClub.rawValue
}
}
}
Then in MyList.swift add . onAppear
func body(props: Props) -> some View {
NavigationView {
List {
customListsSection(props: props)
Picker(selection: $selectedList, label: Text("")) {
Text("Wishlist").tag(0)
Text("Seenlist").tag(1)
}.pickerStyle(SegmentedPickerStyle())
if selectedList == 0 {
wishlistSection(props: props)
} else if selectedList == 1 {
seenSection(props: props)
}
}
.actionSheet(isPresented: $isSortActionSheetPresented, content: { sortActionSheet })
.navigationBarTitle(Text("My Lists"))
.navigationBarItems(trailing: Button(action: {
self.isSortActionSheetPresented.toggle()
}, label: {
Image(systemName: "line.horizontal.3.decrease.circle")
.resizable()
.frame(width: 25, height: 25)
}))
}
.sheet(isPresented: $isEditingFormPresented) {
CustomListForm(editingListId: nil).environmentObject(self.store)
}
.onAppear {
AppUserDefaults.tabViewIndex = Tab.myLists.rawValue
}
}
Alan Cook Senior Programmer CodeFab alanc@codefab.com www.codefab.com
On Sep 10, 2019, at 1:46 PM, Thomas Ricouard notifications@github.com wrote:
My breakpoint is shown, so the code is called. Are you sure you touch save on the settings form? https://user-images.githubusercontent.com/535509/64587283-165d8800-d39f-11e9-97a4-00822bc7a003.png — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Dimillian/MovieSwiftUI/issues/28?email_source=notifications&email_token=AAGIO2S3IYGMWXUOVRFHUF3QI4YDVA5CNFSM4IVBZ3W2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6J44QQ#issuecomment-529780290, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGIO2WK75GZI362TYA6JK3QI4YDVANCNFSM4IVBZ3WQ.
If it works you could do a proper pull request, It's nice that the app will open back on the last used tab. Note; It could also be backed in a UIState in the redux app state, like the movies and peoples state, it could be saved on app quit, and restored on launch.
Add support for UserState var (mainTabSelected,alwaysOriginalTitle,region) remove AppuserDefaults values which are now saved with the userData in the App documents directory
https://github.com/alancook/MovieSwiftUI https://github.com/alancook/MovieSwiftUI
Alan Cook Senior Programmer CodeFab alanc@codefab.com www.codefab.com
On Sep 10, 2019, at 5:00 PM, Thomas Ricouard notifications@github.com wrote:
If it works you could do a proper pull request, It's nice that the app will open back on the last used tab. Note; It could also be backed in a UIState in the redux app state, like the movies and peoples state, it could be saved on app quit, and restored on launch.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Dimillian/MovieSwiftUI/issues/28?email_source=notifications&email_token=AAGIO2QPXQTFWGCQ3EHYO5TQI5O33A5CNFSM4IVBZ3W2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6KME2I#issuecomment-529842793, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGIO2WSSJ2O3AT5UG7W67TQI5O33ANCNFSM4IVBZ3WQ.
@Dimillian love how you use UserDefaults, implemented in my app, so much cleaner!
Attachment available until Dec 30, 2019 Your welcome, been working on my new guide app you can have quick peak, please do not distribute the video, hope to have it out early next year, Alan
Alan Cook Senior Programmer CodeFab alanc@codefab.com mailto:alanc@codefab.com www.codefab.com
On Sep 11, 2019, at 3:06 PM, Alan Cook <alanc@mac.com mailto:alanc@mac.com> wrote:
Add support for UserState var (mainTabSelected,alwaysOriginalTitle,region) remove AppuserDefaults values which are now saved with the userData in the App documents directory
https://github.com/alancook/MovieSwiftUI https://github.com/alancook/MovieSwiftUI
Alan Cook Senior Programmer CodeFab alanc@codefab.com mailto:alanc@codefab.com www.codefab.com http://www.codefab.com/
On Sep 10, 2019, at 5:00 PM, Thomas Ricouard <notifications@github.com mailto:notifications@github.com> wrote:
If it works you could do a proper pull request, It's nice that the app will open back on the last used tab. Note; It could also be backed in a UIState in the redux app state, like the movies and peoples state, it could be saved on app quit, and restored on launch.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Dimillian/MovieSwiftUI/issues/28?email_source=notifications&email_token=AAGIO2QPXQTFWGCQ3EHYO5TQI5O33A5CNFSM4IVBZ3W2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD6KME2I#issuecomment-529842793, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGIO2WSSJ2O3AT5UG7W67TQI5O33ANCNFSM4IVBZ3WQ.
UserDetaultWrapper.swift -> public var wrappedValue: T {... never sets value
the get { return ...} is called but never the set {...} so the value stored in the UserDefault never changes