The incoming vm data sets the Picker as expected, but .onchange is NOT executing. Is this another Apple bug or am I doing this wrong?
import Foundation
import SwiftUI
struct ExamplePicker: View {
@EnvironmentObject var vm: ViewModel
let availableSymbolRates = [
/* band 0 */ ["-","AUTO","1500"],
/* band 1 */ ["-","AUTO","25","33","66","125"],
/* band 2 */ ["-","AUTO","250","333","500"],
/* band 3 */ ["-","AUTO","1000","1500"]
]
var body: some View {
HStack {
Text("Symbol Rate")
Spacer()
Picker("", selection: $vm.response.RxSymbolRate) {
ForEach(availableSymbolRates[vm.response.RxBand], id: \.self) {
Text($0)
}
}
.onChange(of: vm.response.RxSymbolRate, perform: { value in
print("Rx Symbol Rate changed to: \(value)")
vm.request(action: .RXSR, param: value)
})
}
}
}
The double line spacing is a forum bug.
This is the most ridiculous solution I've ever come up with, but it works.
Spacer().onchange - really?
Please could someone from Apple, or anyone, show me a more elegant way.
HStack {
Text("Symbol Rate")
Spacer().onChange(of: vm.response.RxSymbolRate, perform: { value in
symbolRateSTATE = vm.response.RxSymbolRate
})
Picker("", selection: $symbolRateSTATE) {
ForEach(vm.response.RxSymbolRateArray, id: \.self) {
Text($0)
}
}
.onChange(of: symbolRateSTATE, perform: { newState in
vm.request(action: .RXSR, param: newState)
})
.onChange(of: vm.response.RxSymbolRate, perform: { newState in
symbolRateSTATE = newState
})
}