You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add the ability to state the current value for a specific view, and not on the view in which the StateObject/ObservedObject for the ViewModel was created
here is an example:
ViewModel+Binding.swift
classSubscriptionBag:ObservableObject{}extensionObservableObjectwhere Self:ViewModel{func binding<T, R>(
_ flowKey:KeyPath<Self,CMutableStateFlow<T>>,
equals:@escaping(T?,T?)->Bool,
getMapper:@escaping(T)->R,
setMapper:@escaping(R)->T,
to bag:SubscriptionBag)->Binding<R>{letstateFlow:CMutableStateFlow<T>=self[keyPath: flowKey]varlastValue:T?= stateFlow.value
vardisposable:DisposableHandle?=nil
disposable = stateFlow.subscribe(onCollect:{ value in
if !equals(lastValue, value){
lastValue = value
bag.objectWillChange.send()
disposable?.dispose()}})returnBinding(
get:{getMapper(stateFlow.value!)},
set:{
stateFlow.value =setMapper($0)})}}
structSomeScreenContent:View{@EnvironmentObjectvarviewModel:SomeViewModel@StateObjectprivatevarsubscriptionBag=SubscriptionBag()varbody:someView{TextField(placeholderText, text: viewModel.binding(\.text, to: subscriptionBag))}}
The text was updated successfully, but these errors were encountered:
add the ability to state the current value for a specific view, and not on the view in which the StateObject/ObservedObject for the ViewModel was created
here is an example:
ViewModel+Binding.swift
SomeScreen.swift
SomeScreenContent.swift
The text was updated successfully, but these errors were encountered: