Can I ignore Perception warning if the change is detected well in this case ? #2927
-
I'm beginner in Swift and TCA. Question)
I wrapped my view code with (I want to use ForEach to to remove duplicate code) // ProfileFilterFeature
@ObservableState
struct State: Equatable {
var alcoholFrequency: [AlcoholFrequencyType?: Bool] = [
nil: true,
.NONE: false,
.ONCE_PER_MONTH: false,
.A_FEW_TIMES_PER_MONTH: false,
.ONCE_PER_WEEK: false,
.A_FEW_TIMES_PER_WEEK: false
]
} // View
struct ProfileFilterFormView: View {
@Perception.Bindable
var store: StoreOf<ProfileFilterFeature>
var alcoholFrequencies: [AlcoholFrequencyType?] = [
nil,
.NONE,
.ONCE_PER_MONTH,
.A_FEW_TIMES_PER_MONTH,
.ONCE_PER_WEEK,
.A_FEW_TIMES_PER_WEEK
]
var body: some View {
VStack(alignment: .leading, spacing: 20) {
ForEach(alcoholFrequencies, id: \.self) { alcoholFrequency in
WithPerceptionTracking {
let binding = Binding<Bool>(
get: {
store.alcoholFrequency[alcoholFrequency] ?? false
},
set: { _ in
store.send(.setAlcoholFrequency(alcoholFrequency: alcoholFrequency))
})
Toggle(isOn: binding, label: {
Text("\(alcoholFrequency?.rawValue ?? "No Concern")")
})
.toggleStyle(IOSCheckbox())
}
}
}
}
} (This code has been partially omitted.) Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@teamboot88 This is unfortunately to be expected since you're accessing things in a custom binding using extension StoreOf<ProfileFilterFeature> {
subscript(alcoholFrequency alcoholFrequency: AlcoholFrequencyType?) -> Bool {
get { self.alcoholFrequency[alcoholFrequency] ?? false }
set { send(.setAlcoholFrequency(alcoholFrequency: alcoholFrequency)) }
}
} And then in the view, instead of a custom binding: Toggle(isOn: $store[alcoholFrequency: alcoholFrequency]) {
Text("\(alcoholFrequency?.rawValue ?? "No Concern")")
} |
Beta Was this translation helpful? Give feedback.
@teamboot88 This is unfortunately to be expected since you're accessing things in a custom binding using
Binding.init(get:set:)
instead of deriving a binding via dynamic member lookup. You can instead leverage dynamic member lookup on bindings of stores by defining computed properties or subscripts. I think you could do the above as a subscript that takes an alcohol frequency:And then in the view, instead of a custom binding:
To…