mirror of
https://github.com/pointfreeco/swift-composable-architecture.git
synced 2025-12-20 09:11:33 +01:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user