Skip to content

Commit 9f226d9

Browse files
mbrandonwmluisbrown
authored andcommitted
Small updates to animations case study. (#1201)
1 parent a422b09 commit 9f226d9

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

Examples/CaseStudies/SwiftUICaseStudies/01-GettingStarted-Animations.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ extension Effect where Error == Never {
3939

4040
struct AnimationsState: Equatable {
4141
var alert: AlertState<AnimationsAction>?
42-
var circleCenter = CGPoint(x: UIScreen.main.bounds.width / 2, y: UIScreen.main.bounds.height / 2)
42+
var circleCenter = CGPoint(x: 175, y: 300)
4343
var circleColor = Color.black
4444
var isCircleScaled = false
4545
}
@@ -60,6 +60,7 @@ struct AnimationsEnvironment {
6060

6161
let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnvironment> {
6262
state, action, environment in
63+
enum CancelID {}
6364

6465
switch action {
6566
case let .circleScaleToggleChanged(isScaled):
@@ -76,6 +77,7 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
7677
.map { (output: .setColor($0), duration: 1) },
7778
scheduler: environment.mainQueue.animation(.linear)
7879
)
80+
.cancellable(id: CancelID.self)
7981

8082
case .resetButtonTapped:
8183
state.alert = AlertState(
@@ -90,7 +92,7 @@ let animationsReducer = Reducer<AnimationsState, AnimationsAction, AnimationsEnv
9092

9193
case .resetConfirmationButtonTapped:
9294
state = AnimationsState()
93-
return .none
95+
return .cancel(id: CancelID.self)
9496

9597
case let .setColor(color):
9698
state.circleColor = color

Examples/CaseStudies/SwiftUICaseStudiesTests/01-GettingStarted-AnimationsTests.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,44 @@ class AnimationTests: XCTestCase {
5959

6060
self.mainQueue.run()
6161
}
62+
63+
func testReset() {
64+
let mainQueue = DispatchQueue.test
65+
66+
let store = TestStore(
67+
initialState: AnimationsState(),
68+
reducer: animationsReducer,
69+
environment: AnimationsEnvironment(
70+
mainQueue: mainQueue.eraseToAnyScheduler()
71+
)
72+
)
73+
74+
store.send(.rainbowButtonTapped)
75+
76+
store.receive(.setColor(.red)) {
77+
$0.circleColor = .red
78+
}
79+
80+
mainQueue.advance(by: .seconds(1))
81+
store.receive(.setColor(.blue)) {
82+
$0.circleColor = .blue
83+
}
84+
85+
store.send(.resetButtonTapped) {
86+
$0.alert = AlertState(
87+
title: TextState("Reset state?"),
88+
primaryButton: .destructive(
89+
TextState("Reset"),
90+
action: .send(.resetConfirmationButtonTapped, animation: .default)
91+
),
92+
secondaryButton: .cancel(TextState("Cancel"))
93+
)
94+
}
95+
96+
store.send(.resetConfirmationButtonTapped) {
97+
$0 = AnimationsState()
98+
}
99+
100+
mainQueue.run()
101+
}
62102
}

0 commit comments

Comments
 (0)