I have a parent feature that contains a non-optional route
property:
struct LocationAuthorization: ReducerProtocol {
struct State: Equatable {
enum Route: Equatable {
case locationAuthorizationDenied(LocationAuthorizationDenied.State)
case locationAuthorizationPrompt(LocationAuthorizationPrompt.State)
case locationAuthorizationRestricted(LocationAuthorizationRestricted.State)
}
var route: Route
//... other properties for parent feature
}
enum Action: Equatable {
enum Route: Equatable {
case locationAuthorizationDenied(LocationAuthorizationDenied.Action)
case locationAuthorizationPrompt(LocationAuthorizationPrompt.Action)
case locationAuthorizationRestricted(LocationAuthorizationRestricted.Action)
}
//... other actions to be handled by parent feature
case onAppear
case route(Route)
}
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .onAppear:
//... logic to determine which route to show. default to view for explaining why location authorization is needed and CTA to initiate location authorization prompt.
state.route = .locationAuthorizationPrompt(.init())
return .none
case .route:
return .none
}
}
}
}
Each of the routes maintain some of their own state, such as views to explain that location authorization is denied, restricted, or needed. Some of them can send the user to the device Settings app, while others contain other relevant location-based logic, which is why they are their own cases in a non-optional enum
.
When running the app, the view for prompting the user for location authorization is displayed, as expected. However, the user is not prompted when the CTA is tapped because the LocationAuthorizationPrompt
reducer is not initialized in the parent reducer body.
When using an optional route
, the ifLet
operator can be used on the reducer, but I want to use a non-optional property because there should never be a scenario in which one of the routes is not shown.
Can you help me understand what I need to add to the parent reducer so that the reducers for the routes are able to run?
var body: some ReducerProtocol<State, Action> {
Reduce { state, action in
switch action {
case .onAppear:
//... logic to determine which route to show. default to view for explaining why location authorization is needed and CTA to initiate location authorization prompt.
state.route = .locationAuthorizationPrompt(.init())
return .none
case .route:
return .none
}
}
// What goes here?
}