From 8f0b35056b9723605122e9a6389fbe5ef898f326 Mon Sep 17 00:00:00 2001 From: Daiki Matsudate Date: Tue, 12 Mar 2024 06:03:26 +0900 Subject: [PATCH] Add `StackActionOf` (#2906) Signed-off-by: Daiki Matsudate --- .../03-NavigationStack.swift | 2 +- .../Legacy/BindingLocalTestCase.swift | 2 +- .../Legacy/NavigationStackTestCase.swift | 2 +- .../iOS 16/NavigationTestCase.swift | 2 +- .../iOS 17/ObservableBindingLocalTest.swift | 2 +- .../iOS 17/ObservableNavigationTestCase.swift | 2 +- Examples/SyncUps/SyncUps/AppFeature.swift | 2 +- .../Documentation.docc/Articles/Navigation.md | 1 + .../Extensions/ReducerForEach.md | 1 + .../Reducer/Reducers/StackReducer.swift | 17 ++++++++- .../Reducers/StackReducerTests.swift | 36 +++++++++---------- .../ScopeLoggerTests.swift | 2 +- 12 files changed, 44 insertions(+), 27 deletions(-) diff --git a/Examples/CaseStudies/SwiftUICaseStudies/03-NavigationStack.swift b/Examples/CaseStudies/SwiftUICaseStudies/03-NavigationStack.swift index af53ffb54d4f..d83b1e1e1489 100644 --- a/Examples/CaseStudies/SwiftUICaseStudies/03-NavigationStack.swift +++ b/Examples/CaseStudies/SwiftUICaseStudies/03-NavigationStack.swift @@ -22,7 +22,7 @@ struct NavigationDemo { enum Action { case goBackToScreen(id: StackElementID) case goToABCButtonTapped - case path(StackAction) + case path(StackActionOf) case popToRoot } diff --git a/Examples/Integration/Integration/Legacy/BindingLocalTestCase.swift b/Examples/Integration/Integration/Legacy/BindingLocalTestCase.swift index ba9180e9bbab..b23deaf8dcd0 100644 --- a/Examples/Integration/Integration/Legacy/BindingLocalTestCase.swift +++ b/Examples/Integration/Integration/Legacy/BindingLocalTestCase.swift @@ -15,7 +15,7 @@ private struct BindingLocalTestCase { case fullScreenCoverButtonTapped case navigationDestination(PresentationAction) case navigationDestinationButtonTapped - case path(StackAction) + case path(StackActionOf) case popover(PresentationAction) case popoverButtonTapped case sheet(PresentationAction) diff --git a/Examples/Integration/Integration/Legacy/NavigationStackTestCase.swift b/Examples/Integration/Integration/Legacy/NavigationStackTestCase.swift index 3f8916e18fda..45c12e7d92cc 100644 --- a/Examples/Integration/Integration/Legacy/NavigationStackTestCase.swift +++ b/Examples/Integration/Integration/Legacy/NavigationStackTestCase.swift @@ -142,7 +142,7 @@ private struct NavigationStackTestCase { var childResponse: Int? } enum Action { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { state, action in diff --git a/Examples/Integration/Integration/iOS 16/NavigationTestCase.swift b/Examples/Integration/Integration/iOS 16/NavigationTestCase.swift index 99257b04048f..576da7ec32ae 100644 --- a/Examples/Integration/Integration/iOS 16/NavigationTestCase.swift +++ b/Examples/Integration/Integration/iOS 16/NavigationTestCase.swift @@ -31,7 +31,7 @@ struct NavigationTestCaseView: View { var path = StackState() } enum Action { - case path(StackAction) + case path(StackActionOf) } var body: some ReducerOf { Reduce { state, action in diff --git a/Examples/Integration/Integration/iOS 17/ObservableBindingLocalTest.swift b/Examples/Integration/Integration/iOS 17/ObservableBindingLocalTest.swift index 024d77a3f4c2..196b9d12f469 100644 --- a/Examples/Integration/Integration/iOS 17/ObservableBindingLocalTest.swift +++ b/Examples/Integration/Integration/iOS 17/ObservableBindingLocalTest.swift @@ -16,7 +16,7 @@ private struct ObservableBindingLocalTestCase { case fullScreenCoverButtonTapped case navigationDestination(PresentationAction) case navigationDestinationButtonTapped - case path(StackAction) + case path(StackActionOf) case popover(PresentationAction) case popoverButtonTapped case sheet(PresentationAction) diff --git a/Examples/Integration/Integration/iOS 17/ObservableNavigationTestCase.swift b/Examples/Integration/Integration/iOS 17/ObservableNavigationTestCase.swift index b6c17e322a9b..6e1847679dd1 100644 --- a/Examples/Integration/Integration/iOS 17/ObservableNavigationTestCase.swift +++ b/Examples/Integration/Integration/iOS 17/ObservableNavigationTestCase.swift @@ -35,7 +35,7 @@ struct ObservableNavigationTestCaseView: View { } enum Action { case path( - StackAction + StackActionOf ) } var body: some ReducerOf { diff --git a/Examples/SyncUps/SyncUps/AppFeature.swift b/Examples/SyncUps/SyncUps/AppFeature.swift index dafca442ac2b..ce8ac1042fe6 100644 --- a/Examples/SyncUps/SyncUps/AppFeature.swift +++ b/Examples/SyncUps/SyncUps/AppFeature.swift @@ -17,7 +17,7 @@ struct AppFeature { } enum Action { - case path(StackAction) + case path(StackActionOf) case syncUpsList(SyncUpsList.Action) } diff --git a/Sources/ComposableArchitecture/Documentation.docc/Articles/Navigation.md b/Sources/ComposableArchitecture/Documentation.docc/Articles/Navigation.md index 61585cb77e01..8d094a20c6ab 100644 --- a/Sources/ComposableArchitecture/Documentation.docc/Articles/Navigation.md +++ b/Sources/ComposableArchitecture/Documentation.docc/Articles/Navigation.md @@ -28,6 +28,7 @@ use these tools. - - ``StackState`` - ``StackAction`` +- ``StackActionOf`` - ``StackElementID`` - ``Reducer/forEach(_:action:destination:fileID:line:)-yz3v`` diff --git a/Sources/ComposableArchitecture/Documentation.docc/Extensions/ReducerForEach.md b/Sources/ComposableArchitecture/Documentation.docc/Extensions/ReducerForEach.md index 5327be897a98..a32952be5867 100644 --- a/Sources/ComposableArchitecture/Documentation.docc/Extensions/ReducerForEach.md +++ b/Sources/ComposableArchitecture/Documentation.docc/Extensions/ReducerForEach.md @@ -10,6 +10,7 @@ - ``StackState`` - ``StackAction`` +- ``StackActionOf`` - ``Reducer/forEach(_:action:destination:fileID:line:)-yz3v`` - ``Reducer/forEach(_:action:)`` - ``DismissEffect`` diff --git a/Sources/ComposableArchitecture/Reducer/Reducers/StackReducer.swift b/Sources/ComposableArchitecture/Reducer/Reducers/StackReducer.swift index f356688409d8..e179f53ebcf8 100644 --- a/Sources/ComposableArchitecture/Reducer/Reducers/StackReducer.swift +++ b/Sources/ComposableArchitecture/Reducer/Reducers/StackReducer.swift @@ -306,7 +306,7 @@ extension Reducer { /// // ... /// } /// enum Action { - /// case path(StackAction) + /// case path(StackActionOf) /// // ... /// } /// var body: some ReducerOf { @@ -412,6 +412,21 @@ extension Reducer { } } +/// A convenience type alias for referring to a stack action of a given reducer's domain. +/// +/// Instead of specifying two generics: +/// +/// ```swift +/// case path(StackAction) +/// ``` +/// +/// You can specify a single generic: +/// +/// ```swift +/// case path(StackActionOf) +/// ``` +public typealias StackActionOf = StackAction + public struct _StackReducer: Reducer { let base: Base let toStackState: WritableKeyPath> diff --git a/Tests/ComposableArchitectureTests/Reducers/StackReducerTests.swift b/Tests/ComposableArchitectureTests/Reducers/StackReducerTests.swift index c34a72efbb5d..389f3d7fabdb 100644 --- a/Tests/ComposableArchitectureTests/Reducers/StackReducerTests.swift +++ b/Tests/ComposableArchitectureTests/Reducers/StackReducerTests.swift @@ -89,7 +89,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) case pushChild } var body: some ReducerOf { @@ -139,7 +139,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) case popChild case pushChild } @@ -203,7 +203,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) case pushChild } var body: some ReducerOf { @@ -252,7 +252,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none }.forEach(\.children, action: /Action.children) { Child() } @@ -307,7 +307,7 @@ var children = StackState() } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none } @@ -369,7 +369,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) case pushChild } var body: some ReducerOf { @@ -452,7 +452,7 @@ var path = StackState() } enum Action: Equatable { - case path(StackAction) + case path(StackActionOf) case pushChild1 case pushChild2 } @@ -507,7 +507,7 @@ var path = StackState() } enum Action { - case path(StackAction) + case path(StackActionOf) case popToRoot case pushChild } @@ -598,7 +598,7 @@ var path = StackState() } enum Action: Equatable { - case path(StackAction) + case path(StackActionOf) case pushChild1 case pushChild2 } @@ -697,7 +697,7 @@ var path = StackState() } enum Action: Equatable { - case path(StackAction) + case path(StackActionOf) case popAll case popFirst } @@ -861,7 +861,7 @@ var path = StackState() } enum Action { - case path(StackAction) + case path(StackActionOf) } var body: some ReducerOf { EmptyReducer() @@ -933,7 +933,7 @@ var children: StackState } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none } @@ -982,7 +982,7 @@ var children: StackState } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none } @@ -1019,7 +1019,7 @@ var children = StackState() } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) case push } var body: some ReducerOf { @@ -1074,7 +1074,7 @@ var children = StackState() } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none } @@ -1120,7 +1120,7 @@ var children = StackState() } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none } @@ -1164,7 +1164,7 @@ var children = StackState() } enum Action: Equatable { - case child(StackAction) + case child(StackActionOf) } var body: some ReducerOf { Reduce { _, _ in .none }.forEach(\.children, action: /Action.child) { Child() } @@ -1265,7 +1265,7 @@ var children = StackState() } enum Action: Equatable { - case children(StackAction) + case children(StackActionOf) case tapAfter case tapBefore } diff --git a/Tests/ComposableArchitectureTests/ScopeLoggerTests.swift b/Tests/ComposableArchitectureTests/ScopeLoggerTests.swift index 4baac97235fe..dfdd5dddc702 100644 --- a/Tests/ComposableArchitectureTests/ScopeLoggerTests.swift +++ b/Tests/ComposableArchitectureTests/ScopeLoggerTests.swift @@ -39,7 +39,7 @@ var path = StackState() } enum Action { - case path(StackAction) + case path(StackActionOf) } var body: some ReducerOf { EmptyReducer()