NavigateAndLoad: Cancel loading on dismiss (#757)

* NavigateAndLoad: Cancel loading on dismiss

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

Co-authored-by: Stephen Celis <stephen.celis@gmail.com>

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

Co-authored-by: Stephen Celis <stephen.celis@gmail.com>

* Add cancellation to UIKitCaseStudies/NavigateAndLoad.swift

* Add cancellation to LoadThenNavigate studies

Co-authored-by: Stephen Celis <stephen.celis@gmail.com>
This commit is contained in:
filblue
2021-09-02 00:25:54 +03:00
committed by GitHub
parent 83e7557d7b
commit dacd3de63f
7 changed files with 46 additions and 3 deletions

View File

@@ -9,6 +9,7 @@ struct LazyNavigationState: Equatable {
}
enum LazyNavigationAction: Equatable {
case onDisappear
case optionalCounter(CounterAction)
case setNavigation(isActive: Bool)
case setNavigationIsActiveDelayCompleted
@@ -30,12 +31,16 @@ let lazyNavigationReducer =
with: Reducer<
LazyNavigationState, LazyNavigationAction, LazyNavigationEnvironment
> { state, action, environment in
struct CancelId: Hashable {}
switch action {
case .onDisappear:
return .cancel(id: CancelId())
case .setNavigation(isActive: true):
state.isActivityIndicatorHidden = false
return Effect(value: .setNavigationIsActiveDelayCompleted)
.delay(for: 1, scheduler: environment.mainQueue)
.eraseToEffect()
.cancellable(id: CancelId())
case .setNavigation(isActive: false):
state.optionalCounter = nil
return .none
@@ -120,6 +125,11 @@ class LazyNavigationViewController: UIViewController {
@objc private func loadOptionalCounterTapped() {
self.viewStore.send(.setNavigation(isActive: true))
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.viewStore.send(.onDisappear)
}
}
struct LazyNavigationViewController_Previews: PreviewProvider {