-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generic alerts and action sheets (#201)
* alerts * wip * wip * wip * clean up * wip * wip * wip * wip * format * clean up * clean up * docs * wip * tests * API tweaks * Fix * More API changes * More API changes * More * Fix * Fix docs * Generic alerts optionality (#202) * Use Optional to model generic alerts * Xcode 12 * Refinement * update docs * Fix * Fix * doc fixes * rename * fixes * fixes Co-authored-by: Stephen Celis <stephen@stephencelis.com>
- Loading branch information
1 parent
2ce84cc
commit a905fbf
Showing
68 changed files
with
814 additions
and
339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.swiftpm/xcode/xcshareddata/xcschemes/ComposableArchitecture.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.swiftpm/xcode/xcshareddata/xcschemes/ComposableCoreLocation.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
.swiftpm/xcode/xcshareddata/xcschemes/ComposableCoreMotion.xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s/CaseStudies/CaseStudies.xcodeproj/xcshareddata/xcschemes/CaseStudies (SwiftUI).xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...les/CaseStudies/CaseStudies.xcodeproj/xcshareddata/xcschemes/CaseStudies (UIKit).xcscheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-AlertsAndActionSheets.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import ComposableArchitecture | ||
import SwiftUI | ||
|
||
private let readMe = """ | ||
This demonstrates how to best handle alerts and action sheets in the Composable Architecture. | ||
Because the library demands that all data flow through the application in a single direction, we \ | ||
cannot leverage SwiftUI's two-way bindings because they can make changes to state without going \ | ||
through a reducer. This means we can't directly use the standard API to display alerts and sheets. | ||
However, the library comes with two types, `AlertState` and `ActionSheetState`, which can be \ | ||
constructed from reducers and control whether or not an alert or action sheet is displayed. \ | ||
Further, it automatically handles sending actions when you tap their buttons, which allows you \ | ||
to properly handle their functionality in the reducer rather than in two-way bindings and action \ | ||
closures. | ||
The benefit of doing this is that you can get full test coverage on how a user interacts with \ | ||
with alerts and action sheets in your application | ||
""" | ||
|
||
struct AlertAndSheetState: Equatable { | ||
var actionSheet: ActionSheetState<AlertAndSheetAction>? | ||
var alert: AlertState<AlertAndSheetAction>? | ||
var count = 0 | ||
} | ||
|
||
enum AlertAndSheetAction: Equatable { | ||
case actionSheetButtonTapped | ||
case actionSheetCancelTapped | ||
case alertButtonTapped | ||
case alertCancelTapped | ||
case decrementButtonTapped | ||
case incrementButtonTapped | ||
} | ||
|
||
struct AlertAndSheetEnvironment {} | ||
|
||
let alertAndSheetReducer = Reducer< | ||
AlertAndSheetState, AlertAndSheetAction, AlertAndSheetEnvironment | ||
> { state, action, _ in | ||
|
||
switch action { | ||
case .actionSheetButtonTapped: | ||
state.actionSheet = .init( | ||
title: "Action sheet", | ||
message: "This is an action sheet.", | ||
buttons: [ | ||
.cancel(), | ||
.default("Increment", send: .incrementButtonTapped), | ||
.default("Decrement", send: .decrementButtonTapped), | ||
] | ||
) | ||
return .none | ||
|
||
case .actionSheetCancelTapped: | ||
state.actionSheet = nil | ||
return .none | ||
|
||
case .alertButtonTapped: | ||
state.alert = .init( | ||
title: "Alert!", | ||
message: "This is an alert", | ||
primaryButton: .cancel(), | ||
secondaryButton: .default("Increment", send: .incrementButtonTapped) | ||
) | ||
return .none | ||
|
||
case .alertCancelTapped: | ||
state.alert = nil | ||
return .none | ||
|
||
case .decrementButtonTapped: | ||
state.actionSheet = nil | ||
state.count -= 1 | ||
return .none | ||
|
||
case .incrementButtonTapped: | ||
state.actionSheet = nil | ||
state.alert = nil | ||
state.count += 1 | ||
return .none | ||
} | ||
} | ||
|
||
struct AlertAndSheetView: View { | ||
let store: Store<AlertAndSheetState, AlertAndSheetAction> | ||
|
||
var body: some View { | ||
WithViewStore(self.store) { viewStore in | ||
Form { | ||
Section(header: Text(template: readMe, .caption)) { | ||
Text("Count: \(viewStore.count)") | ||
|
||
Button("Alert") { viewStore.send(.alertButtonTapped) } | ||
.alert( | ||
self.store.scope(state: { $0.alert }), | ||
dismiss: .alertCancelTapped | ||
) | ||
|
||
Button("Action sheet") { viewStore.send(.actionSheetButtonTapped) } | ||
.actionSheet( | ||
self.store.scope(state: { $0.actionSheet }), | ||
dismiss: .actionSheetCancelTapped | ||
) | ||
} | ||
} | ||
} | ||
.navigationBarTitle("Alerts & Action Sheets") | ||
} | ||
} | ||
|
||
struct AlertAndSheet_Previews: PreviewProvider { | ||
static var previews: some View { | ||
NavigationView { | ||
AlertAndSheetView( | ||
store: .init( | ||
initialState: .init(), | ||
reducer: alertAndSheetReducer, | ||
environment: .init() | ||
) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.