TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Picker options does not update when data source has changed

Forums > SwiftUI

Hi guys,

How did u solve that picker options does not update when data source has changed? I have found some trick with putting .id() on picker and update picker but seems that trick does not work anymore.

What I want to achieve, when toggle on langauge is true, this langauge should appear as option in segmented picker. Hovewer if isOn has changed, nothing appear on segemnted picker until I reopen settings view.

 struct Settings: View {

    let settingsCategories:[String] = ["Content Langauge", "Primary Language"]

    @ObservedObject var contentLanguages = ListModel()
    @ObservedObject var primaryLanguage = PrimaryLangauge()

    init() {
        UITableViewCell.appearance().backgroundColor = UIColor(named: "dhollandia-orange-dark")
        UITableView.appearance().backgroundColor = UIColor(named: "dhollandia-orange")
        UITableView.appearance().separatorColor = UIColor.white
        UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor:UIColor.white]
        UISegmentedControl.appearance().selectedSegmentTintColor = .white
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
        UISegmentedControl.appearance().setTitleTextAttributes([.foregroundColor: UIColor.white], for: .normal)
    }

    var body: some View {
        VStack(alignment: .leading) {

            NavigationView {
                Form {
                    List {
                        ForEach(0..<settingsCategories.count) {index in
                            Section(header:

                                Text(LocalizedStringKey(self.settingsCategories[index]))
                                    .padding([.top, .bottom])
                                    .foregroundColor(.white)) {

                                if index == 0 {

                                    ForEach(0..<self.contentLanguages.languages.count) { index in
                                        Toggle(isOn: self.$contentLanguages.languages[index].isOn) {
                                            Text(LocalizedStringKey(self.contentLanguages.languages[index].country))
                                                .foregroundColor(.white)
                                        }
                                    }

                                }
                                else {

                                    Picker(selection: self.$primaryLanguage.primaryLanguage.index, label:
                                        Text("Select Langauge")
                                            .foregroundColor(.white)
                                    ) {
                                        ForEach(0..<self.contentLanguages.languages.count, id: \.self) { index in
                                            Group {
                                                if self.contentLanguages.languages[index].isOn {
                                                    Text(LocalizedStringKey(self.contentLanguages.languages[index].country))
                                                    .foregroundColor(.white)
                                                    .tag(index)
                                                }
                                            }
                                        }
                                    }
                                    .pickerStyle(SegmentedPickerStyle())
                                }
                            }
                        }
                    }
                    .navigationBarTitle("Settings")
                    .navigationBarHidden(false)
                }
            }
        }
        .background(Color("dhollandia-orange"))
    }
}

class PrimaryLangauge: ObservableObject {
    @Published var primaryLanguage : Language = Language(index: UserDefaults.standard.integer(forKey: "primaryLanguage"))
}

struct Language {
    var index : Int = 2 {
        didSet {
            UserDefaults.standard.set(index, forKey: "primaryLanguage")
        }
    }
}

class ListModel: ObservableObject {
    @Published var languages: [ContentLangauge] = [
        ContentLangauge(country: "Czech", isOn: UserDefaults.standard.bool(forKey: "Czech")),
        ContentLangauge(country: "Dutch", isOn: UserDefaults.standard.bool(forKey: "Dutch")),
        ContentLangauge(country: "English", isOn: UserDefaults.standard.bool(forKey: "English")),
        ContentLangauge(country: "French", isOn: UserDefaults.standard.bool(forKey: "French")),
        ContentLangauge(country: "German", isOn: UserDefaults.standard.bool(forKey: "German")),
        ContentLangauge(country: "Italian", isOn: UserDefaults.standard.bool(forKey: "Italian")),
        ContentLangauge(country: "Slovak", isOn: UserDefaults.standard.bool(forKey: "Slovak")),
        ContentLangauge(country: "Spanish", isOn: UserDefaults.standard.bool(forKey: "Spanish"))
    ]
}

struct ContentLangauge : Identifiable, Hashable {
    var id = UUID()
    var country : String
    var isOn : Bool = false {
        didSet {
            //print("\(country) is \(isOn ? "enabled" : "disabled")")
            UserDefaults.standard.set(isOn, forKey: country)
        }
    }
}

struct Settings_Previews: PreviewProvider {
    static var previews: some View {
        Settings()
    }
}

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.