Skip to content

Commit

Permalink
Avoids updating path.routes while app is in background
Browse files Browse the repository at this point in the history
SwiftUI will ignore the changes until the app becomes active, at which point it will
try to make all changes in one go, which might fail if the changes aren't supported
in one update.
  • Loading branch information
johnpatrickmorgan committed Aug 24, 2024
1 parent ae82976 commit 208bb6b
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Sources/FlowStacks/ScreenModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import SwiftUI
/// nested FlowStack using its parent's navigation view would not have the child's environment objects propagated to
/// pushed screens.
struct ScreenModifier<Data: Hashable>: ViewModifier {
@Environment(\.scenePhase) var scenePhase
var path: RoutesHolder
var destinationBuilder: DestinationBuilderHolder
var navigator: FlowNavigator<Data>
Expand All @@ -26,6 +27,7 @@ struct ScreenModifier<Data: Hashable>: ViewModifier {
}
}
.onChange(of: typedPath) { typedPath in
guard scenePhase == .active else { return }
path.routes = typedPath.map { $0.erased() }
}
.onChange(of: path.routes) { routes in
Expand All @@ -39,5 +41,9 @@ struct ScreenModifier<Data: Hashable>: ViewModifier {
fatalError("Cannot add \(type(of: route.screen.base)) to stack of \(Data.self)")
}
}
.onChange(of: scenePhase) { phase in
guard phase == .active else { return }
path.routes = typedPath.map { $0.erased() }
}
}
}

0 comments on commit 208bb6b

Please sign in to comment.