mirror of
https://github.com/pointfreeco/swift-composable-architecture.git
synced 2025-12-20 09:11:33 +01:00
* wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Silence test warnings * wip * wip * wip * update a bunch of docs * wip * wip * fix * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Kill integration tests for now * wip * wip * wip * wip * updating docs for @Reducer macro * replaced more Reducer protocols with @Reducer * Fixed some broken docc references * wip * Some @Reducer docs * more docs * convert some old styles to new style * wip * wip * wip * wip * wip * wip * wip * bump * update tutorials to use body * update tutorials to use DML on destination state enum * Add diagnostic * wip * updated a few more tests * wip * wip * Add another gotcha * wip * wip * wip * fixes * wip * wip * wip * wip * wip * fix * wip * remove for now * wip * wip * updated some docs * migration guides * more migration guide * fix ci * fix * soft deprecate all apis using AnyCasePath * wip * Fix * fix tests * swift-format 509 compatibility * wip * wip * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> * wip * wip * update optional state case study * remove initializer * Don't use @State for BasicsView integration demo * fix tests * remove reduce diagnostics for now * diagnose error not warning * Update Sources/ComposableArchitecture/Macros.swift Co-authored-by: Jesse Tipton <jesse@jessetipton.com> * wip * move integration tests to cron * Revert "move integration tests to cron" This reverts commitf9bdf2f04b. * disable flakey tests on CI * wip * wip * Revert "Revert "move integration tests to cron"" This reverts commit66aafa7327. * fix * wip * fix --------- Co-authored-by: Brandon Williams <mbrandonw@hey.com> Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com> Co-authored-by: Brandon Williams <135203+mbrandonw@users.noreply.github.com> Co-authored-by: Jesse Tipton <jesse@jessetipton.com>
137 lines
3.7 KiB
Swift
137 lines
3.7 KiB
Swift
import ComposableArchitecture
|
|
import SwiftUI
|
|
|
|
private let readMe = """
|
|
This screen demonstrates how one can cancel in-flight effects in the Composable Architecture.
|
|
|
|
Use the stepper to count to a number, and then tap the "Number fact" button to fetch \
|
|
a random fact about that number using an API.
|
|
|
|
While the API request is in-flight, you can tap "Cancel" to cancel the effect and prevent \
|
|
it from feeding data back into the application. Interacting with the stepper while a \
|
|
request is in-flight will also cancel it.
|
|
"""
|
|
|
|
// MARK: - Feature domain
|
|
|
|
@Reducer
|
|
struct EffectsCancellation {
|
|
struct State: Equatable {
|
|
var count = 0
|
|
var currentFact: String?
|
|
var isFactRequestInFlight = false
|
|
}
|
|
|
|
enum Action {
|
|
case cancelButtonTapped
|
|
case stepperChanged(Int)
|
|
case factButtonTapped
|
|
case factResponse(Result<String, Error>)
|
|
}
|
|
|
|
@Dependency(\.factClient) var factClient
|
|
private enum CancelID { case factRequest }
|
|
|
|
var body: some Reducer<State, Action> {
|
|
Reduce { state, action in
|
|
switch action {
|
|
case .cancelButtonTapped:
|
|
state.isFactRequestInFlight = false
|
|
return .cancel(id: CancelID.factRequest)
|
|
|
|
case let .stepperChanged(value):
|
|
state.count = value
|
|
state.currentFact = nil
|
|
state.isFactRequestInFlight = false
|
|
return .cancel(id: CancelID.factRequest)
|
|
|
|
case .factButtonTapped:
|
|
state.currentFact = nil
|
|
state.isFactRequestInFlight = true
|
|
|
|
return .run { [count = state.count] send in
|
|
await send(.factResponse(Result { try await self.factClient.fetch(count) }))
|
|
}
|
|
.cancellable(id: CancelID.factRequest)
|
|
|
|
case let .factResponse(.success(response)):
|
|
state.isFactRequestInFlight = false
|
|
state.currentFact = response
|
|
return .none
|
|
|
|
case .factResponse(.failure):
|
|
state.isFactRequestInFlight = false
|
|
return .none
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Feature view
|
|
|
|
struct EffectsCancellationView: View {
|
|
@State var store = Store(initialState: EffectsCancellation.State()) {
|
|
EffectsCancellation()
|
|
}
|
|
@Environment(\.openURL) var openURL
|
|
|
|
var body: some View {
|
|
WithViewStore(self.store, observe: { $0 }) { viewStore in
|
|
Form {
|
|
Section {
|
|
AboutView(readMe: readMe)
|
|
}
|
|
|
|
Section {
|
|
Stepper(
|
|
"\(viewStore.count)",
|
|
value: viewStore.binding(get: \.count, send: { .stepperChanged($0) })
|
|
)
|
|
|
|
if viewStore.isFactRequestInFlight {
|
|
HStack {
|
|
Button("Cancel") { viewStore.send(.cancelButtonTapped) }
|
|
Spacer()
|
|
ProgressView()
|
|
// NB: There seems to be a bug in SwiftUI where the progress view does not show
|
|
// a second time unless it is given a new identity.
|
|
.id(UUID())
|
|
}
|
|
} else {
|
|
Button("Number fact") { viewStore.send(.factButtonTapped) }
|
|
.disabled(viewStore.isFactRequestInFlight)
|
|
}
|
|
|
|
viewStore.currentFact.map {
|
|
Text($0).padding(.vertical, 8)
|
|
}
|
|
}
|
|
|
|
Section {
|
|
Button("Number facts provided by numbersapi.com") {
|
|
self.openURL(URL(string: "http://numbersapi.com")!)
|
|
}
|
|
.foregroundStyle(.secondary)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
.buttonStyle(.borderless)
|
|
}
|
|
.navigationTitle("Effect cancellation")
|
|
}
|
|
}
|
|
|
|
// MARK: - SwiftUI previews
|
|
|
|
struct EffectsCancellation_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
NavigationView {
|
|
EffectsCancellationView(
|
|
store: Store(initialState: EffectsCancellation.State()) {
|
|
EffectsCancellation()
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|