How to change the selection of the second picker after changing the first picker?

75 Views Asked by At

Here I'm trying to create the first little app that helps to convert a value from one unit of measurement to another. The first conversion picker helps me to select data for the second picker(from which measure I'm going to converse) and the third picker(to which measure I'm going converse). But when I'm changing the first picker - it doesn't change the @State value of the second and third pickers. I tried different approaches but the result is the same. Could you please help me?

struct ContentView: View {
    @State private var userInput = 0.0
    
    @State var selectionOfTypeOfConversion: Conversions = .temperature
    @State var selectionFromConversion: String = TemperatureConversions.celsius.rawValue
    @State var selectionToConversion: String = TemperatureConversions.celsius.rawValue
    @FocusState private var inputIsFocused: Bool
    
    var output: Double {
        //doing all counting
    }
    
    var body: some View {
        VStack {
            NavigationView {
                Form {
                    Section {
                        Picker("Type of conversion", selection: $selectionOfTypeOfConversion) {
                            ForEach(Conversions.allCases, id: \.self) {
                                Text($0.rawValue)

                            }
                        }.onSubmit {
                            checkConvention()
                        }
                        TextField("", value: $userInput, format: .number)
                            .keyboardType(.decimalPad)
                            .focused($inputIsFocused)
                        Picker("From conversion", selection: $selectionFromConversion) {
                            switch selectionOfTypeOfConversion {
                            case Conversions.temperature:
                                ForEach(TemperatureConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            case Conversions.length:
                                ForEach(LengthConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            case Conversions.volume:
                                ForEach(VolumeConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            }
                        }
                    } header: {
                        Text("From")
                    }
                    
                    Section {
                        Picker("To conversion", selection: $selectionToConversion) {
                            switch selectionOfTypeOfConversion {
                            case Conversions.temperature:
                                ForEach(TemperatureConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            case Conversions.length:
                                ForEach(LengthConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            case Conversions.volume:
                                ForEach(VolumeConversions.allCases, id: \.self) {
                                    Text($0.rawValue).tag($0.rawValue)
                                }
                            }
                        }
                        Text(output, format: .number)
                    } header: {
                        Text("To")
                    }
                }
                .toolbar {
                    ToolbarItemGroup(placement: .keyboard) {
                        Spacer()
                        Button("Done") {
                            inputIsFocused = false
                        }
                    }
                }
            }
        }
    }
    
    private func checkConvention() {
        if selectionOfTypeOfConversion == Conversions.temperature {
            selectionFromConversion = TemperatureConversions.celsius.rawValue
            selectionToConversion = TemperatureConversions.celsius.rawValue
        } else if selectionOfTypeOfConversion == Conversions.length{
            selectionFromConversion =  LengthConversions.meters.rawValue
            selectionToConversion = LengthConversions.meters.rawValue
        } else if selectionOfTypeOfConversion == Conversions.volume{
            selectionFromConversion =  VolumeConversions.milliliters.rawValue
            selectionToConversion = VolumeConversions.milliliters.rawValue
        }
    }
    
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

enum Conversions: String, CaseIterable {
    case temperature = "temperature"
    case length = "length"
    case volume = "volume"
}

enum TemperatureConversions: String, CaseIterable {
    var id: Self { self }
    
    case celsius = "celsius"
    case fahrenheit = "fahrenheit"
    case kelvin = "kelvin"
}

enum LengthConversions: String, CaseIterable {
    var id: Self { self }
    
    case meters = "meters"
    case feet = "feet"
    case miles = "miles"
}

enum VolumeConversions: String, CaseIterable {
    var id: Self { self }
    
    case milliliters = "ml"
    case pints = "pints"
    case gallons = "gallons"
}


Expecting: change the formula which counts input into output.

0

There are 0 best solutions below