From 7772e2511c7679ab72705e951a2171daf5a295f2 Mon Sep 17 00:00:00 2001 From: johnpatrickmorgan Date: Tue, 30 Apr 2024 17:07:54 +0100 Subject: [PATCH] Updates parameter names and adds docs --- .../Reducers/ForEachIdentifiedRoute.swift | 23 ++++++++++++---- .../Reducers/ForEachIndexedRoute.swift | 27 ++++++++++++++----- .../TCARouter/IdentifiedRouterAction.swift | 2 ++ .../TCARouter/IndexedRouterAction.swift | 2 ++ .../TCARouter/RouterAction.swift | 2 ++ 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/Sources/TCACoordinators/Reducers/ForEachIdentifiedRoute.swift b/Sources/TCACoordinators/Reducers/ForEachIdentifiedRoute.swift index 75da44d..7c1270e 100644 --- a/Sources/TCACoordinators/Reducers/ForEachIdentifiedRoute.swift +++ b/Sources/TCACoordinators/Reducers/ForEachIdentifiedRoute.swift @@ -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( + _ routes: WritableKeyPath>>, + action: CaseKeyPath>, cancellationId: (some Hashable)?, - toLocalState: WritableKeyPath>>, - toLocalAction: CaseKeyPath>, @ReducerBuilder screenReducer: () -> ScreenReducer ) -> some ReducerOf where Action: CasePathable, @@ -52,8 +63,8 @@ public extension Reducer { coordinatorReducer: self, screenReducer: screenReducer(), cancellationId: cancellationId, - toLocalState: toLocalState, - toLocalAction: toLocalAction + toLocalState: routes, + toLocalAction: action ) } @@ -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( - cancellationIdType: Any.Type = Self.self, _ routes: WritableKeyPath>>, action: CaseKeyPath>, + cancellationIdType: Any.Type = Self.self, @ReducerBuilder screenReducer: () -> ScreenReducer ) -> some ReducerOf where ScreenReducer.State: Identifiable, diff --git a/Sources/TCACoordinators/Reducers/ForEachIndexedRoute.swift b/Sources/TCACoordinators/Reducers/ForEachIndexedRoute.swift index 5d91ae2..d7ce79e 100644 --- a/Sources/TCACoordinators/Reducers/ForEachIndexedRoute.swift +++ b/Sources/TCACoordinators/Reducers/ForEachIndexedRoute.swift @@ -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( - coordinatorIdForCancellation: (some Hashable)?, - toLocalState: WritableKeyPath]>, - toLocalAction: CaseKeyPath>, + _ routes: WritableKeyPath]>, + action: CaseKeyPath>, + cancellationId: (some Hashable)?, @ReducerBuilder screenReducer: () -> ScreenReducer ) -> some ReducerOf where Action: CasePathable, @@ -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. diff --git a/Sources/TCACoordinators/TCARouter/IdentifiedRouterAction.swift b/Sources/TCACoordinators/TCARouter/IdentifiedRouterAction.swift index 3b634c4..76cabf6 100644 --- a/Sources/TCACoordinators/TCARouter/IdentifiedRouterAction.swift +++ b/Sources/TCACoordinators/TCARouter/IdentifiedRouterAction.swift @@ -1,5 +1,7 @@ import ComposableArchitecture +/// A ``RouterAction`` that identifies Identifiable screens by their identity. public typealias IdentifiedRouterAction = RouterAction where Screen: Identifiable +/// A ``RouterAction`` that identifies Identifiable screens by their identity. public typealias IdentifiedRouterActionOf = RouterAction where R.State: Identifiable diff --git a/Sources/TCACoordinators/TCARouter/IndexedRouterAction.swift b/Sources/TCACoordinators/TCARouter/IndexedRouterAction.swift index 8ecdf61..88c007d 100644 --- a/Sources/TCACoordinators/TCARouter/IndexedRouterAction.swift +++ b/Sources/TCACoordinators/TCARouter/IndexedRouterAction.swift @@ -1,5 +1,7 @@ import ComposableArchitecture +/// A ``RouterAction`` that identifies screens by their index in the routes array. public typealias IndexedRouterAction = RouterAction +/// A ``RouterAction`` that identifies screens by their index in the routes array. public typealias IndexedRouterActionOf = RouterAction diff --git a/Sources/TCACoordinators/TCARouter/RouterAction.swift b/Sources/TCACoordinators/TCARouter/RouterAction.swift index cc4614f..19bde98 100644 --- a/Sources/TCACoordinators/TCARouter/RouterAction.swift +++ b/Sources/TCACoordinators/TCARouter/RouterAction.swift @@ -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: CasePathable { case updateRoutes(_ routes: [Route]) case routeAction(id: ID, action: ScreenAction)