Moves to monitoring changes in scene phase

This commit is contained in:
Stuart Breckenridge
2020-07-11 21:02:46 +08:00
parent 9991b3760e
commit c3d4834879
4 changed files with 17 additions and 21 deletions

View File

@@ -16,7 +16,6 @@ struct MainApp: App {
#endif
#if os(iOS)
@UIApplicationDelegateAdaptor(AppDelegate.self) private var delegate
@Environment(\.scenePhase) private var scenePhase
#endif
@StateObject private var defaults = AppDefaults.shared
@@ -87,10 +86,6 @@ struct MainApp: App {
SceneNavigationView()
.environmentObject(defaults)
.modifier(PreferredColorSchemeModifier(preferredColorScheme: defaults.userInterfaceColorPalette))
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didEnterBackgroundNotification)) { _ in
print("didEnterBackgroundNotification")
WidgetDataEncoder.encodeWidgetData()
}
}
.commands {
CommandGroup(after: .newItem, addition: {
@@ -136,18 +131,6 @@ struct MainApp: App {
.keyboardShortcut(.rightArrow, modifiers: [.command])
})
}
.onChange(of: scenePhase, perform: { newPhase in
switch newPhase {
case .active:
print("active")
case .inactive:
print("inactive")
case .background:
print("background")
@unknown default:
print("unknown")
}
})
#endif
}
}

View File

@@ -16,6 +16,7 @@ struct SceneNavigationView: View {
#if os(iOS)
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@Environment(\.scenePhase) private var scenePhase
#endif
var body: some View {
@@ -62,6 +63,13 @@ struct SceneNavigationView: View {
.onChange(of: sheetToShow) { value in
value != .none ? (showSheet = true) : (showSheet = false)
}
.onChange(of: scenePhase) { newPhase in
if newPhase == .background {
#if os(iOS)
WidgetDataEncoder.encodeWidgetData()
#endif
}
}
.toolbar {
#if os(macOS)