Skip to content

Commit dacd3de

Browse files
NavigateAndLoad: Cancel loading on dismiss (#757)
* NavigateAndLoad: Cancel loading on dismiss * Update Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-NavigateAndLoad.swift Co-authored-by: Stephen Celis <[email protected]> * Update Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Sheet-PresentAndLoad.swift Co-authored-by: Stephen Celis <[email protected]> * Add cancellation to UIKitCaseStudies/NavigateAndLoad.swift * Add cancellation to LoadThenNavigate studies Co-authored-by: Stephen Celis <[email protected]>
1 parent 83e7557 commit dacd3de

File tree

7 files changed

+46
-3
lines changed

7 files changed

+46
-3
lines changed

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Lists-LoadThenNavigate.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ struct LoadThenNavigateListState: Equatable {
2626

2727
enum LoadThenNavigateListAction: Equatable {
2828
case counter(CounterAction)
29+
case onDisappear
2930
case setNavigation(selection: UUID?)
3031
case setNavigationSelectionDelayCompleted(UUID)
3132
}
@@ -57,6 +58,9 @@ let loadThenNavigateListReducer =
5758
case .counter:
5859
return .none
5960

61+
case .onDisappear:
62+
return .cancel(id: CancelId())
63+
6064
case let .setNavigation(selection: .some(navigatedId)):
6165
for row in state.rows {
6266
state.rows[id: row.id]?.isActivityIndicatorVisible = row.id == navigatedId
@@ -119,6 +123,7 @@ struct LoadThenNavigateListView: View {
119123
}
120124
}
121125
.navigationBarTitle("Load then navigate")
126+
.onDisappear { viewStore.send(.onDisappear) }
122127
}
123128
}
124129
}

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-LoadThenNavigate.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct LoadThenNavigateState: Equatable {
1717
}
1818

1919
enum LoadThenNavigateAction: Equatable {
20+
case onDisappear
2021
case optionalCounter(CounterAction)
2122
case setNavigation(isActive: Bool)
2223
case setNavigationIsActiveDelayCompleted
@@ -38,12 +39,20 @@ let loadThenNavigateReducer =
3839
with: Reducer<
3940
LoadThenNavigateState, LoadThenNavigateAction, LoadThenNavigateEnvironment
4041
> { state, action, environment in
42+
43+
struct CancelId: Hashable {}
44+
4145
switch action {
46+
47+
case .onDisappear:
48+
return .cancel(id: CancelId())
49+
4250
case .setNavigation(isActive: true):
4351
state.isActivityIndicatorVisible = true
4452
return Effect(value: .setNavigationIsActiveDelayCompleted)
4553
.delay(for: 1, scheduler: environment.mainQueue)
4654
.eraseToEffect()
55+
.cancellable(id: CancelId())
4756

4857
case .setNavigation(isActive: false):
4958
state.optionalCounter = nil
@@ -90,6 +99,7 @@ struct LoadThenNavigateView: View {
9099
}
91100
}
92101
}
102+
.onDisappear { viewStore.send(.onDisappear) }
93103
}
94104
.navigationBarTitle("Load then navigate")
95105
}

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-NavigateAndLoad.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,19 @@ let navigateAndLoadReducer =
3737
with: Reducer<
3838
NavigateAndLoadState, NavigateAndLoadAction, NavigateAndLoadEnvironment
3939
> { state, action, environment in
40+
struct CancelId: Hashable {}
4041
switch action {
4142
case .setNavigation(isActive: true):
4243
state.isNavigationActive = true
4344
return Effect(value: .setNavigationIsActiveDelayCompleted)
4445
.delay(for: 1, scheduler: environment.mainQueue)
4546
.eraseToEffect()
47+
.cancellable(id: CancelId())
4648

4749
case .setNavigation(isActive: false):
4850
state.isNavigationActive = false
4951
state.optionalCounter = nil
50-
return .none
52+
return .cancel(id: CancelId())
5153

5254
case .setNavigationIsActiveDelayCompleted:
5355
state.optionalCounter = CounterState()

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Sheet-LoadThenPresent.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct LoadThenPresentState: Equatable {
1717
}
1818

1919
enum LoadThenPresentAction {
20+
case onDisappear
2021
case optionalCounter(CounterAction)
2122
case setSheet(isPresented: Bool)
2223
case setSheetIsPresentedDelayCompleted
@@ -38,12 +39,20 @@ let loadThenPresentReducer =
3839
with: Reducer<
3940
LoadThenPresentState, LoadThenPresentAction, LoadThenPresentEnvironment
4041
> { state, action, environment in
42+
43+
struct CancelId: Hashable {}
44+
4145
switch action {
46+
47+
case .onDisappear:
48+
return .cancel(id: CancelId())
49+
4250
case .setSheet(isPresented: true):
4351
state.isActivityIndicatorVisible = true
4452
return Effect(value: .setSheetIsPresentedDelayCompleted)
4553
.delay(for: 1, scheduler: environment.mainQueue)
4654
.eraseToEffect()
55+
.cancellable(id: CancelId())
4756

4857
case .setSheet(isPresented: false):
4958
state.optionalCounter = nil
@@ -93,6 +102,7 @@ struct LoadThenPresentView: View {
93102
)
94103
}
95104
.navigationBarTitle("Load and present")
105+
.onDisappear { viewStore.send(.onDisappear) }
96106
}
97107
}
98108
}

Examples/CaseStudies/SwiftUICaseStudies/03-Navigation-Sheet-PresentAndLoad.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,19 @@ let presentAndLoadReducer =
3535
with: Reducer<
3636
PresentAndLoadState, PresentAndLoadAction, PresentAndLoadEnvironment
3737
> { state, action, environment in
38+
struct CancelId: Hashable {}
3839
switch action {
3940
case .setSheet(isPresented: true):
4041
state.isSheetPresented = true
4142
return Effect(value: .setSheetIsPresentedDelayCompleted)
4243
.delay(for: 1, scheduler: environment.mainQueue)
4344
.eraseToEffect()
45+
.cancellable(id: CancelId())
4446

4547
case .setSheet(isPresented: false):
4648
state.isSheetPresented = false
4749
state.optionalCounter = nil
48-
return .none
50+
return .cancel(id: CancelId())
4951

5052
case .setSheetIsPresentedDelayCompleted:
5153
state.optionalCounter = CounterState()

Examples/CaseStudies/UIKitCaseStudies/LoadThenNavigate.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ struct LazyNavigationState: Equatable {
99
}
1010

1111
enum LazyNavigationAction: Equatable {
12+
case onDisappear
1213
case optionalCounter(CounterAction)
1314
case setNavigation(isActive: Bool)
1415
case setNavigationIsActiveDelayCompleted
@@ -30,12 +31,16 @@ let lazyNavigationReducer =
3031
with: Reducer<
3132
LazyNavigationState, LazyNavigationAction, LazyNavigationEnvironment
3233
> { state, action, environment in
34+
struct CancelId: Hashable {}
3335
switch action {
36+
case .onDisappear:
37+
return .cancel(id: CancelId())
3438
case .setNavigation(isActive: true):
3539
state.isActivityIndicatorHidden = false
3640
return Effect(value: .setNavigationIsActiveDelayCompleted)
3741
.delay(for: 1, scheduler: environment.mainQueue)
3842
.eraseToEffect()
43+
.cancellable(id: CancelId())
3944
case .setNavigation(isActive: false):
4045
state.optionalCounter = nil
4146
return .none
@@ -120,6 +125,11 @@ class LazyNavigationViewController: UIViewController {
120125
@objc private func loadOptionalCounterTapped() {
121126
self.viewStore.send(.setNavigation(isActive: true))
122127
}
128+
129+
override func viewDidDisappear(_ animated: Bool) {
130+
super.viewDidDisappear(animated)
131+
self.viewStore.send(.onDisappear)
132+
}
123133
}
124134

125135
struct LazyNavigationViewController_Previews: PreviewProvider {

Examples/CaseStudies/UIKitCaseStudies/NavigateAndLoad.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ let eagerNavigationReducer =
3030
with: Reducer<
3131
EagerNavigationState, EagerNavigationAction, EagerNavigationEnvironment
3232
> { state, action, environment in
33+
34+
struct CancelId: Hashable {}
35+
3336
switch action {
3437
case .setNavigation(isActive: true):
3538
state.isNavigationActive = true
3639
return Effect(value: .setNavigationIsActiveDelayCompleted)
3740
.delay(for: 1, scheduler: environment.mainQueue)
3841
.eraseToEffect()
42+
.cancellable(id: CancelId())
3943
case .setNavigation(isActive: false):
4044
state.isNavigationActive = false
4145
state.optionalCounter = nil
42-
return .none
46+
return .cancel(id: CancelId())
4347
case .setNavigationIsActiveDelayCompleted:
4448
state.optionalCounter = CounterState()
4549
return .none

0 commit comments

Comments
 (0)