From c3d4834879256a92bad4f3226a33ea767fc46fa3 Mon Sep 17 00:00:00 2001 From: Stuart Breckenridge Date: Sat, 11 Jul 2020 21:02:46 +0800 Subject: [PATCH] Moves to monitoring changes in scene phase --- Multiplatform/Shared/MainApp.swift | 17 ----------------- Multiplatform/Shared/SceneNavigationView.swift | 8 ++++++++ .../iOS/Widget Data/WidgetDataEncoder.swift | 7 +++++++ Technotes/Widget.md | 6 ++---- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Multiplatform/Shared/MainApp.swift b/Multiplatform/Shared/MainApp.swift index d9253545f..d58a25751 100644 --- a/Multiplatform/Shared/MainApp.swift +++ b/Multiplatform/Shared/MainApp.swift @@ -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 } } diff --git a/Multiplatform/Shared/SceneNavigationView.swift b/Multiplatform/Shared/SceneNavigationView.swift index 96cab8c76..6c6294aa3 100644 --- a/Multiplatform/Shared/SceneNavigationView.swift +++ b/Multiplatform/Shared/SceneNavigationView.swift @@ -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) diff --git a/Multiplatform/iOS/Widget Data/WidgetDataEncoder.swift b/Multiplatform/iOS/Widget Data/WidgetDataEncoder.swift index 3c6c8b47b..cfad5da6f 100644 --- a/Multiplatform/iOS/Widget Data/WidgetDataEncoder.swift +++ b/Multiplatform/iOS/Widget Data/WidgetDataEncoder.swift @@ -9,11 +9,15 @@ import Foundation import WidgetKit import os.log +import UIKit struct WidgetDataEncoder { + static let taskIdentifier = "com.ranchero.NetNewsWire.WidgetEncode" + static func encodeWidgetData() { os_log(.info, "Starting widget data encoding") + let task = UIApplication.shared.beginBackgroundTask(withName: taskIdentifier, expirationHandler: nil) do { let articles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! }) var latest = [LatestArticle]() @@ -43,8 +47,11 @@ struct WidgetDataEncoder { WidgetCenter.shared.reloadAllTimelines() os_log(.info, "Finished encoding widget data") + print(UIApplication.shared.backgroundTimeRemaining) + UIApplication.shared.endBackgroundTask(task) } catch { os_log(.error, "%@", error.localizedDescription) + UIApplication.shared.endBackgroundTask(task) } } diff --git a/Technotes/Widget.md b/Technotes/Widget.md index 0b30f5f94..5d99ff6d4 100644 --- a/Technotes/Widget.md +++ b/Technotes/Widget.md @@ -4,8 +4,7 @@ The NetNewsWire iOS widget supports the `systemSmall` and `systemMedium` styles. -The `systemSmall` style displays the current Today and Unread counts; `systemMedium` displays the latest two articles along with current -Today and Unread count. +The `systemSmall` style displays the current Today and Unread counts; `systemMedium` displays the latest two articles. ## Passing Data from the App to the Widget @@ -35,9 +34,8 @@ struct LatestArticle: Codable { ## When is JSON Data Saved? -1. On `unreadCountDidChange` +1. When the app enters the background 2. After a background refresh -3. When the app enters the background After JSON data is saved, Widget timelines are reloaded.