Skip to content

Commit

Permalink
Updates parameter names and adds docs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpatrickmorgan committed Apr 30, 2024
1 parent 4e589a4 commit 7772e25
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 12 deletions.
23 changes: 18 additions & 5 deletions Sources/TCACoordinators/Reducers/ForEachIdentifiedRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ struct ForEachIdentifiedRoute<
}

public extension Reducer {
/// Allows a screen reducer to be incorporated into a coordinator reducer, such that each screen in
/// the coordinator's routes IdentifiedArray will have its actions and state propagated. When screens are
/// dismissed, the routes will be updated. If a cancellation identifier is passed, in-flight effects
/// will be cancelled when the screen from which they originated is dismissed.
/// - Parameters:
/// - routes: A writable keypath for the routes `IdentifiedArray`.
/// - action: A casepath for the router action from this reducer's Action type.
/// - cancellationId: An identifier to use for cancelling in-flight effects when a view is dismissed. It
/// will be combined with the screen's identifier. If `nil`, there will be no automatic cancellation.
/// - screenReducer: The reducer that operates on all of the individual screens.
/// - Returns: A new reducer combining the coordinator-level and screen-level reducers.
func forEachRoute<ScreenReducer: Reducer, ScreenState, ScreenAction>(
_ routes: WritableKeyPath<State, IdentifiedArrayOf<Route<ScreenState>>>,
action: CaseKeyPath<Action, IdentifiedRouterAction<ScreenState, ScreenAction>>,
cancellationId: (some Hashable)?,
toLocalState: WritableKeyPath<Self.State, IdentifiedArrayOf<Route<ScreenState>>>,
toLocalAction: CaseKeyPath<Self.Action, IdentifiedRouterAction<ScreenState, ScreenAction>>,
@ReducerBuilder<ScreenState, ScreenAction> screenReducer: () -> ScreenReducer
) -> some ReducerOf<Self>
where Action: CasePathable,
Expand All @@ -52,8 +63,8 @@ public extension Reducer {
coordinatorReducer: self,
screenReducer: screenReducer(),
cancellationId: cancellationId,
toLocalState: toLocalState,
toLocalAction: toLocalAction
toLocalState: routes,
toLocalAction: action
)
}

Expand All @@ -62,14 +73,16 @@ public extension Reducer {
/// dismissed, the routes will be updated. If a cancellation identifier is passed, in-flight effects
/// will be cancelled when the screen from which they originated is dismissed.
/// - Parameters:
/// - routes: A writable keypath for the routes `IdentifiedArray`.
/// - action: A casepath for the router action from this reducer's Action type.
/// - cancellationIdType: A type to use for cancelling in-flight effects when a view is dismissed. It
/// will be combined with the screen's identifier. Defaults to the type of the parent reducer.
/// - screenReducer: The reducer that operates on all of the individual screens.
/// - Returns: A new reducer combining the coordinator-level and screen-level reducers.
func forEachRoute<ScreenReducer: Reducer, ScreenState, ScreenAction>(
cancellationIdType: Any.Type = Self.self,
_ routes: WritableKeyPath<State, IdentifiedArrayOf<Route<ScreenState>>>,
action: CaseKeyPath<Action, IdentifiedRouterAction<ScreenState, ScreenAction>>,
cancellationIdType: Any.Type = Self.self,
@ReducerBuilder<ScreenState, ScreenAction> screenReducer: () -> ScreenReducer
) -> some ReducerOf<Self>
where ScreenReducer.State: Identifiable,
Expand Down
27 changes: 20 additions & 7 deletions Sources/TCACoordinators/Reducers/ForEachIndexedRoute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,21 @@ struct ForEachIndexedRoute<
}

public extension Reducer {
/// Allows a screen reducer to be incorporated into a coordinator reducer, such that each screen in
/// the coordinator's routes array will have its actions and state propagated. When screens are
/// dismissed, the routes will be updated. If a cancellation identifier is passed, in-flight effects
/// will be cancelled when the screen from which they originated is dismissed.
/// - Parameters:
/// - routes: A writable keypath for the routes array.
/// - action: A casepath for the router action from this reducer's Action type.
/// - cancellationId: An identifier to use for cancelling in-flight effects when a view is dismissed. It
/// will be combined with the screen's identifier. If `nil`, there will be no automatic cancellation.
/// - screenReducer: The reducer that operates on all of the individual screens.
/// - Returns: A new reducer combining the coordinator-level and screen-level reducers.
func forEachRoute<ScreenReducer: Reducer, ScreenState, ScreenAction>(
coordinatorIdForCancellation: (some Hashable)?,
toLocalState: WritableKeyPath<Self.State, [Route<ScreenState>]>,
toLocalAction: CaseKeyPath<Self.Action, IndexedRouterAction<ScreenState, ScreenAction>>,
_ routes: WritableKeyPath<State, [Route<ScreenState>]>,
action: CaseKeyPath<Action, IndexedRouterAction<ScreenState, ScreenAction>>,
cancellationId: (some Hashable)?,
@ReducerBuilder<ScreenState, ScreenAction> screenReducer: () -> ScreenReducer
) -> some ReducerOf<Self>
where Action: CasePathable,
Expand All @@ -49,17 +60,19 @@ public extension Reducer {
ForEachIndexedRoute(
coordinatorReducer: self,
screenReducer: screenReducer(),
cancellationId: coordinatorIdForCancellation,
toLocalState: toLocalState,
toLocalAction: toLocalAction
cancellationId: cancellationId,
toLocalState: routes,
toLocalAction: action
)
}

/// Allows a screen reducer to be incorporated into a coordinator reducer, such that each screen in
/// the coordinator's routes Array will have its actions and state propagated. When screens are
/// dismissed, the routes will be updated. If a cancellation identifier is passed, in-flight effects
/// dismissed, the routes will be updated. In-flight effects
/// will be cancelled when the screen from which they originated is dismissed.
/// - Parameters:
/// - routes: A writable keypath for the routes array.
/// - action: A casepath for the router action from this reducer's Action type.
/// - cancellationIdType: A type to use for cancelling in-flight effects when a view is dismissed. It
/// will be combined with the screen's identifier. Defaults to the type of the parent reducer.
/// - screenReducer: The reducer that operates on all of the individual screens.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ComposableArchitecture

/// A ``RouterAction`` that identifies Identifiable screens by their identity.
public typealias IdentifiedRouterAction<Screen, ScreenAction> = RouterAction<Screen.ID, Screen, ScreenAction> where Screen: Identifiable

/// A ``RouterAction`` that identifies Identifiable screens by their identity.
public typealias IdentifiedRouterActionOf<R: Reducer> = RouterAction<R.State.ID, R.State, R.Action> where R.State: Identifiable
2 changes: 2 additions & 0 deletions Sources/TCACoordinators/TCARouter/IndexedRouterAction.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import ComposableArchitecture

/// A ``RouterAction`` that identifies screens by their index in the routes array.
public typealias IndexedRouterAction<Screen, ScreenAction> = RouterAction<Int, Screen, ScreenAction>

/// A ``RouterAction`` that identifies screens by their index in the routes array.
public typealias IndexedRouterActionOf<R: Reducer> = RouterAction<Int, R.State, R.Action>
2 changes: 2 additions & 0 deletions Sources/TCACoordinators/TCARouter/RouterAction.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import ComposableArchitecture
import FlowStacks

/// A special action type used in coordinators, which ensures screen-level actions are dispatched to the correct screen reducer,
/// and allows routes to be updated when a user navigates back.
public enum RouterAction<ID: Hashable, Screen, ScreenAction>: CasePathable {
case updateRoutes(_ routes: [Route<Screen>])
case routeAction(id: ID, action: ScreenAction)
Expand Down

0 comments on commit 7772e25

Please sign in to comment.