From 208bb6b1579c5ce53cf2df659ccb040ab1a0844a Mon Sep 17 00:00:00 2001 From: johnpatrickmorgan Date: Sat, 24 Aug 2024 22:42:57 +0100 Subject: [PATCH] Avoids updating path.routes while app is in background 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. --- Sources/FlowStacks/ScreenModifier.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/FlowStacks/ScreenModifier.swift b/Sources/FlowStacks/ScreenModifier.swift index 406f8ae..ca4d0af 100644 --- a/Sources/FlowStacks/ScreenModifier.swift +++ b/Sources/FlowStacks/ScreenModifier.swift @@ -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: ViewModifier { + @Environment(\.scenePhase) var scenePhase var path: RoutesHolder var destinationBuilder: DestinationBuilderHolder var navigator: FlowNavigator @@ -26,6 +27,7 @@ struct ScreenModifier: ViewModifier { } } .onChange(of: typedPath) { typedPath in + guard scenePhase == .active else { return } path.routes = typedPath.map { $0.erased() } } .onChange(of: path.routes) { routes in @@ -39,5 +41,9 @@ struct ScreenModifier: 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() } + } } }