@@ -2,65 +2,50 @@ import ComposableArchitecture
22import SwiftUI
33
44private let readMe = """
5- This demonstrates how to best handle alerts and action sheets in the Composable Architecture.
5+ This demonstrates how to best handle alerts and confirmation dialogs in the Composable \
6+ Architecture.
67
78 Because the library demands that all data flow through the application in a single direction, we \
89 cannot leverage SwiftUI's two-way bindings because they can make changes to state without going \
910 through a reducer. This means we can't directly use the standard API to display alerts and sheets.
1011
11- However, the library comes with two types, `AlertState` and `ActionSheetState `, which can be \
12- constructed from reducers and control whether or not an alert or action sheet is displayed. \
13- Further, it automatically handles sending actions when you tap their buttons, which allows you \
14- to properly handle their functionality in the reducer rather than in two-way bindings and action \
15- closures.
12+ However, the library comes with two types, `AlertState` and `ConfirmationDialogState `, which can \
13+ be constructed from reducers and control whether or not an alert or confirmation dialog is \
14+ displayed. Further, it automatically handles sending actions when you tap their buttons, which \
15+ allows you to properly handle their functionality in the reducer rather than in two-way bindings \
16+ and action closures.
1617
1718 The benefit of doing this is that you can get full test coverage on how a user interacts with \
18- alerts and action sheets in your application
19+ alerts and dialogs in your application
1920 """
2021
21- struct AlertAndSheetState : Equatable {
22- var actionSheet : ActionSheetState < AlertAndSheetAction > ?
23- var alert : AlertState < AlertAndSheetAction > ?
22+ struct AlertAndConfirmationDialogState : Equatable {
23+ var alert : AlertState < AlertAndConfirmationDialogAction > ?
24+ var confirmationDialog : ConfirmationDialogState < AlertAndConfirmationDialogAction > ?
2425 var count = 0
2526}
2627
27- enum AlertAndSheetAction : Equatable {
28- case actionSheetButtonTapped
29- case actionSheetDismissed
28+ enum AlertAndConfirmationDialogAction : Equatable {
3029 case alertButtonTapped
3130 case alertDismissed
31+ case confirmationDialogButtonTapped
32+ case confirmationDialogDismissed
3233 case decrementButtonTapped
3334 case incrementButtonTapped
3435}
3536
36- struct AlertAndSheetEnvironment { }
37+ struct AlertAndConfirmationDialogEnvironment { }
3738
38- let alertAndSheetReducer = Reducer<
39- AlertAndSheetState , AlertAndSheetAction , AlertAndSheetEnvironment
39+ let alertAndConfirmationDialogReducer = Reducer<
40+ AlertAndConfirmationDialogState , AlertAndConfirmationDialogAction , AlertAndConfirmationDialogEnvironment
4041> { state, action, _ in
4142
4243 switch action {
43- case . actionSheetButtonTapped:
44- state. actionSheet = . init(
45- title: . init( " Action sheet " ) ,
46- message: . init( " This is an action sheet. " ) ,
47- buttons: [
48- . cancel( ) ,
49- . default( . init( " Increment " ) , action: . send( . incrementButtonTapped) ) ,
50- . default( . init( " Decrement " ) , action: . send( . decrementButtonTapped) ) ,
51- ]
52- )
53- return . none
54-
55- case . actionSheetDismissed:
56- state. actionSheet = nil
57- return . none
58-
5944 case . alertButtonTapped:
6045 state. alert = . init(
6146 title: . init( " Alert! " ) ,
6247 message: . init( " This is an alert " ) ,
63- primaryButton: . cancel( ) ,
48+ primaryButton: . cancel( . init ( " Cancel " ) ) ,
6449 secondaryButton: . default( . init( " Increment " ) , action: . send( . incrementButtonTapped) )
6550 )
6651 return . none
@@ -69,6 +54,22 @@ let alertAndSheetReducer = Reducer<
6954 state. alert = nil
7055 return . none
7156
57+ case . confirmationDialogButtonTapped:
58+ state. confirmationDialog = . init(
59+ title: . init( " Confirmation dialog " ) ,
60+ message: . init( " This is a confirmation dialog. " ) ,
61+ buttons: [
62+ . cancel( . init( " Cancel " ) ) ,
63+ . default( . init( " Increment " ) , action: . send( . incrementButtonTapped) ) ,
64+ . default( . init( " Decrement " ) , action: . send( . decrementButtonTapped) ) ,
65+ ]
66+ )
67+ return . none
68+
69+ case . confirmationDialogDismissed:
70+ state. confirmationDialog = nil
71+ return . none
72+
7273 case . decrementButtonTapped:
7374 state. alert = . init( title: . init( " Decremented! " ) )
7475 state. count -= 1
@@ -81,8 +82,8 @@ let alertAndSheetReducer = Reducer<
8182 }
8283}
8384
84- struct AlertAndSheetView : View {
85- let store : Store < AlertAndSheetState , AlertAndSheetAction >
85+ struct AlertAndConfirmationDialogView : View {
86+ let store : Store < AlertAndConfirmationDialogState , AlertAndConfirmationDialogAction >
8687
8788 var body : some View {
8889 WithViewStore ( self . store) { viewStore in
@@ -96,25 +97,25 @@ struct AlertAndSheetView: View {
9697 dismiss: . alertDismissed
9798 )
9899
99- Button ( " Action sheet " ) { viewStore. send ( . actionSheetButtonTapped ) }
100- . actionSheet (
101- self . store. scope ( state: \. actionSheet ) ,
102- dismiss: . actionSheetDismissed
100+ Button ( " Confirmation Dialog " ) { viewStore. send ( . confirmationDialogButtonTapped ) }
101+ . confirmationDialog (
102+ self . store. scope ( state: \. confirmationDialog ) ,
103+ dismiss: . confirmationDialogDismissed
103104 )
104105 }
105106 }
106107 }
107- . navigationBarTitle ( " Alerts & Action Sheets " )
108+ . navigationBarTitle ( " Alerts & Confirmation Dialogs " )
108109 }
109110}
110111
111- struct AlertAndSheet_Previews : PreviewProvider {
112+ struct AlertAndConfirmationDialog_Previews : PreviewProvider {
112113 static var previews : some View {
113114 NavigationView {
114- AlertAndSheetView (
115+ AlertAndConfirmationDialogView (
115116 store: . init(
116117 initialState: . init( ) ,
117- reducer: alertAndSheetReducer ,
118+ reducer: alertAndConfirmationDialogReducer ,
118119 environment: . init( )
119120 )
120121 )
0 commit comments