Hello everyone,
I am trying to understand Composable Architecture. For that, I am working on a little application that shows Apple's Frameworks in LazyVGrid, on tap gesture it shows the full description of one specific Framework.
So far I have managed to present modally a framework. Right now I am facing another issue.
I will try to put as little code as possible, but so it can still make sense
The structure of my app is:
AppState AppAction AppEnvironment
struct AppState: Equatable {
var frameworks: [Framework] = MockData.frameworks
var selectedFramework: Framework?
}
Then FrameworkAction, FrameworkEnvironment for each Framework
Here is my app Reducer
let appReducer = Reducer<AppState, AppAction, AppEnvironment>.combine(
frameworkReducer.forEach(
state: \AppState.frameworks,
action: /AppAction.framework(index:frameworkAction:),
environment: { _ in FrameworkEnvironment() }
),
Reducer { state, action, environment in
switch action {
case .framework(index: let index, frameworkAction: FrameworkAction.didTapFramework):
state.selectedFramework = state.frameworks[index]
return .none
case .framework(index: let index, frameworkAction: let frameworkAction):
return .none
case .selectedFramework(let framework):
state.selectedFramework = framework
return .none
}
}
)
And here is the Content:
struct ContentView: View {
let store: Store<AppState, AppAction>
let columns = [GridItem(.flexible()), GridItem(.flexible()), GridItem(.flexible())]
var body: some View {
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: AppAction.selectedFramework(viewStore.selectedFramework)
), content: { FrameworkDetailView(store: <#Store<Framework, FrameworkAction>#>, framework: $0) })
}
}
}
}
In order to perform some FrameworkAction, in FrameworkDetailView, like dismissing that View or firing off the button that takes you to Apple Website through Safari, I need to pass Store<Framework, FrameworkAction> to FrameworkDetailView but I have no idea how. Not sure how to scope it.
Or maybe I should take a completely different approach to it?
Any help will be very appreciated!
Here is my repository if anyone has time to have a look: