Dimillian / MovieSwiftUI

SwiftUI & Combine app using MovieDB API. With a custom Flux (Redux) implementation.
Apache License 2.0
6.46k stars 642 forks source link

UserDetaultWrapper.swift -> public var wrappedValue: T {... never sets value #28

Closed alancook closed 3 years ago

alancook commented 5 years ago

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

Dimillian commented 5 years ago

My breakpoint is shown, so the code is called. Are you sure you touch save on the settings form?

Screenshot 2019-09-10 at 07 45 57
alancook commented 5 years ago

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.

Dimillian commented 5 years ago

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.

alancook commented 5 years ago

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.

mickspecial commented 4 years ago

@Dimillian love how you use UserDefaults, implemented in my app, so much cleaner!

alancook commented 4 years ago

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

Click to Download https://www.icloud.com/attachment/?u=https%3A%2F%2Fcvws.icloud-content.com%2FB%2FAZ07h-8QqyDeBC6wXUh8OMgl3NP2AWguOqVFqU7YZFMbhoeEYqmk7SOb%2F%24%7Bf%7D%3Fo%3DAnIdDQf3ASkGxOxzvdcoT0HrrP83L4vA4xZmhIfk41Lf%26v%3D1%26x%3D3%26a%3DCAogGKoFRoaNKBXhvM4-8E6Gk2J1d8q2r8RKOg2X6w6i1uoSJxCskaDd6y0YrKGbsfUtIgEAKggByAD_J2REw1IEJdzT9loEpO0jmw%26e%3D1577698381%26k%3D%24%7Buk%7D%26fl%3D%26r%3DF5DE7F3F-8493-4B42-B1F8-B0D908EC3447-1%26ckc%3Dcom.apple.largeattachment%26ckz%3D3C8C69AD-ED50-4D1E-8783-E59708BAAE83%26p%3D65%26s%3Ds-15sDj_c99HcC9PU7M7V5jyORk&uk=V1yonQfWVRHTRk_HNo8ucA&f=SwiftUI-AlonaBeachGuideDemo.mp4&sz=284055395SwiftUI-AlonaBeachGuideDemo.mp4 284.1 MB

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.