swiftui-picker

Picker .onChange doesn't change when choosing default


I have pretty much same question as here: SwiftUI Picker onChange or equivalent?, but .onChange won't solve it exactly I need.

I have wrote a question in that link, but I was told to ask a new question (as I wasn't able to find answer anywhere :(

I have this enum:

enum FruitTea: String, CustomStringConvertible, CaseIterable, Codable {
    case peach = "Peach"
    case passionFruit = "Passion Fruit"
    case mango = "Mango"
    case greenApple = "Green Apple"
    var description: String { rawValue }
            
}
    
enum TypeOfTea: String, CustomStringConvertible, CaseIterable, Codable {
    case specialTeaPresso = "Special Tea Presso"
    case teaLatte = "Tea Latte"
    case mousseSeries = "Mousse Series"
    case milkTea = "Milk Tea"
    case fruitTea = "Fruit Tea"
    var description: String { rawValue }
}

And this picker:

@State private var type: TypeOfTea = .specialTeaPresso
@State private var fruit: FruitTea = .passionFruit
Picker("Fruit Tea", selection: $fruit) {
    ForEach(FruitTea.allCases, id: \.self) {
        Text($0.rawValue).tag($0)
    }
}
.onChange(of: fruit, perform: { _ in
    type = .fruitTea
})

When I choose another kind of fruit tea that is already chosen, it works. But If I choose what is default value, my TypeOfTea won't change. I know why - because there was no change. But for my app clicking on Picker means choosing type. Can you please help? Thanks.

In case someone is interested in my all app:

My app on github


Solution

  • I have found a solution based on: here

    I have reduced those 5 pickers to 2. First to choose type of tea and second will show correct view using function that returns some View. Pickers had to be enclosed in AnyView().