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
When holding reducers in an IdentifiedArrayOf, how can I obtain the reducer for a specific item when I want to perform detailed settings for it in an inspector?
import ComposableArchitecture
import SwiftUI
@ReducerstructContent{@ObservableStatestructState:Equatable{varitems=IdentifiedArrayOf<Item.State>()varitemSelection:Item.State.ID?varisPresentedInspector:Bool{get{ itemSelection !=nil}set{if !newValue {
itemSelection =nil}}}}enumAction:BindableAction{case binding(BindingAction<State>)case items(IdentifiedActionOf<Item>)case onAppear
}varbody:someReducerOf<Self>{BindingReducer()Reduce{ state, action inswitch action {case.binding:return.none
caselet.items(.element(id: id, action:.openInspectorButtonTapped)):
state.itemSelection = id
return.none
case.items(.element(id: _, action:.closeInspectorButtonTapped)):
state.itemSelection =nilreturn.none
case.items:return.none
case.onAppear:
state.items =.init(uniqueElements:"ABCDE".map({Item.State(title: $0.description)}))return.none
}}.forEach(\.items, action: \.items){Item()}}}structContentView:View{@Bindablevarstore=StoreOf<Content>(initialState:Content.State()){Content()}varbody:someView{VStack{ForEach(store.scope(state: \.items, action: \.items)){ store inItemView(store: store)}}.inspector(isPresented: $store.isPresentedInspector){
// 🛑 This is not a good implementation.
ForEach(store.scope(state: \.items, action: \.items)){ store inif store.id ==self.store.itemSelection {ItemSettingsView(store: store)}}}.onAppear{
store.send(.onAppear)}}}@ReducerstructItem{@ObservableStatestructState:Equatable,Identifiable{varid:UUID=.init()vartitle:Stringvarflag=false}enumAction:BindableAction{case binding(BindingAction<State>)case openInspectorButtonTapped
case closeInspectorButtonTapped
}varbody:someReducerOf<Self>{BindingReducer()}}structItemView:View{varstore:StoreOf<Item>varbody:someView{HStack{Text(store.title)Button{
store.send(.openInspectorButtonTapped)} label:{Image(systemName:"gearshape.fill")}.buttonStyle(.bordered)}}}structItemSettingsView:View{@Bindablevarstore:StoreOf<Item>varbody:someView{VStack{Button{
store.send(.closeInspectorButtonTapped)} label:{Image(systemName:"xmark")}.buttonStyle(.borderless)TextField("Title", text: $store.title).textFieldStyle(.roundedBorder)Spacer()}}}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
When holding reducers in an IdentifiedArrayOf, how can I obtain the reducer for a specific item when I want to perform detailed settings for it in an inspector?
Beta Was this translation helpful? Give feedback.
All reactions