Hello everyone. Please forgive me if something is not clear, I am a very early junior level. I will gladly clarify. Thanks for the help.
I've got this view with the IfLetStore:
WithViewStore(self.store) { viewStore in
ScrollView {
HStack {
LazyVGrid(columns: columns, content: {
ForEachStore(
self.store.scope(
state: \.frameworks,
action: AppAction.framework(index:frameworkAction:)
),
content: SmallView.init(store:)
)
})
}
.sheet(
item: viewStore.binding(
get: \.selectedFramework,
send: .frameworkDetailView(.didCloseFramework)
)) { _ in
IfLetStore(
self.store.scope(
state: \.selectedFramework,
action: AppAction.frameworkDetailView
),
then: FrameworkDetailView.init(store:)
)
}
}
}
Hopefully, as you can see, the presentation of FrameworkDetailView relies on AppState's property selectedFramework. All here is correct and works (unless you have some annotations).
The trick is to dismiss that FrameworkDetailView properly. When dismissing by sliding it down that fires off:
case .frameworkDetailView(.didCloseFramework):
state.selectedFramework = nil
return .none
But the FrameworkDetailView has a CrossButton that also fires off that FrameworkAction that fires off AppAction
struct CrossButton: View {
let store: Store<Framework, FrameworkAction>
var body: some View {
WithViewStore(self.store) { viewStore in
Button {
viewStore.send(.didCloseFramework)
} label: {
Image(systemName: "xmark")
.foregroundColor(Color(.label))
.imageScale(.large)
.frame(width: 44, height: 44, alignment: .center)
}
}
}
}
So when the CrosssButton is tapped, that's what happens:
- I tap the CrossButton that is a part of FrameworkDetailView
Button {
viewStore.send(.didCloseFramework)
} label: {
-
AppAction executes and sets selectedFramework to nil
case .frameworkDetailView(.didCloseFramework):
state.selectedFramework = nil
return .none
-
.sheet's send parameter fires off
.sheet(
item: viewStore.binding(
get: \.selectedFramework,
send: .frameworkDetailView(.didCloseFramework)
)) { _ in
-
AppAction executes and sets selectedFramework to nil. AGAIN
case .frameworkDetailView(.didCloseFramework):
state.selectedFramework = nil
return .none
I am not exactly sure why it happens. I would have never noticed that if testing Composable Architecture wasn't that great and meticulous.
Repository: