Files
swift-composable-architectu…/Examples/CaseStudies/SwiftUICaseStudies/02-Effects-Timers.swift
Stephen Celis 57e804f1cc Macro bonanza (#2553)
* 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 commit f9bdf2f04b.

* disable flakey tests on CI

* wip

* wip

* Revert "Revert "move integration tests to cron""

This reverts commit 66aafa7327.

* 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>
2023-11-13 12:57:35 -08:00

135 lines
3.5 KiB
Swift

import ComposableArchitecture
import SwiftUI
private let readMe = """
This application demonstrates how to work with timers in the Composable Architecture.
It makes use of the `.timer` method on clocks, which is a helper provided by the Swift Clocks \
library included with this library. The helper provides an `AsyncSequence`-friendly API for \
dealing with times in asynchronous code.
"""
// MARK: - Feature domain
@Reducer
struct Timers {
struct State: Equatable {
var isTimerActive = false
var secondsElapsed = 0
}
enum Action {
case onDisappear
case timerTicked
case toggleTimerButtonTapped
}
@Dependency(\.continuousClock) var clock
private enum CancelID { case timer }
var body: some Reducer<State, Action> {
Reduce { state, action in
switch action {
case .onDisappear:
return .cancel(id: CancelID.timer)
case .timerTicked:
state.secondsElapsed += 1
return .none
case .toggleTimerButtonTapped:
state.isTimerActive.toggle()
return .run { [isTimerActive = state.isTimerActive] send in
guard isTimerActive else { return }
for await _ in self.clock.timer(interval: .seconds(1)) {
await send(.timerTicked, animation: .interpolatingSpring(stiffness: 3000, damping: 40))
}
}
.cancellable(id: CancelID.timer, cancelInFlight: true)
}
}
}
}
// MARK: - Feature view
struct TimersView: View {
@State var store = Store(initialState: Timers.State()) {
Timers()
}
var body: some View {
WithViewStore(self.store, observe: { $0 }) { viewStore in
Form {
AboutView(readMe: readMe)
ZStack {
Circle()
.fill(
AngularGradient(
gradient: Gradient(
colors: [
.blue.opacity(0.3),
.blue,
.blue,
.green,
.green,
.yellow,
.yellow,
.red,
.red,
.purple,
.purple,
.purple.opacity(0.3),
]
),
center: .center
)
)
.rotationEffect(.degrees(-90))
GeometryReader { proxy in
Path { path in
path.move(to: CGPoint(x: proxy.size.width / 2, y: proxy.size.height / 2))
path.addLine(to: CGPoint(x: proxy.size.width / 2, y: 0))
}
.stroke(.primary, lineWidth: 3)
.rotationEffect(.degrees(Double(viewStore.secondsElapsed) * 360 / 60))
}
}
.aspectRatio(1, contentMode: .fit)
.frame(maxWidth: 280)
.frame(maxWidth: .infinity)
.padding(.vertical, 16)
Button {
viewStore.send(.toggleTimerButtonTapped)
} label: {
Text(viewStore.isTimerActive ? "Stop" : "Start")
.padding(8)
}
.frame(maxWidth: .infinity)
.tint(viewStore.isTimerActive ? Color.red : .accentColor)
.buttonStyle(.borderedProminent)
}
.navigationTitle("Timers")
.onDisappear {
viewStore.send(.onDisappear)
}
}
}
}
// MARK: - SwiftUI previews
struct TimersView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
TimersView(
store: Store(initialState: Timers.State()) {
Timers()
}
)
}
}
}