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
No known key found for this signature in database
GPG Key ID: 79BD673276AE83CE
4 changed files with 17 additions and 21 deletions

View File

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

View File

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

View File

@ -9,11 +9,15 @@
import Foundation import Foundation
import WidgetKit import WidgetKit
import os.log import os.log
import UIKit
struct WidgetDataEncoder { struct WidgetDataEncoder {
static let taskIdentifier = "com.ranchero.NetNewsWire.WidgetEncode"
static func encodeWidgetData() { static func encodeWidgetData() {
os_log(.info, "Starting widget data encoding") os_log(.info, "Starting widget data encoding")
let task = UIApplication.shared.beginBackgroundTask(withName: taskIdentifier, expirationHandler: nil)
do { do {
let articles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! }) let articles = try SmartFeedsController.shared.unreadFeed.fetchArticles().sorted(by: { $0.datePublished! > $1.datePublished! })
var latest = [LatestArticle]() var latest = [LatestArticle]()
@ -43,8 +47,11 @@ struct WidgetDataEncoder {
WidgetCenter.shared.reloadAllTimelines() WidgetCenter.shared.reloadAllTimelines()
os_log(.info, "Finished encoding widget data") os_log(.info, "Finished encoding widget data")
print(UIApplication.shared.backgroundTimeRemaining)
UIApplication.shared.endBackgroundTask(task)
} catch { } catch {
os_log(.error, "%@", error.localizedDescription) os_log(.error, "%@", error.localizedDescription)
UIApplication.shared.endBackgroundTask(task)
} }
} }

View File

@ -4,8 +4,7 @@
The NetNewsWire iOS widget supports the `systemSmall` and `systemMedium` styles. 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 The `systemSmall` style displays the current Today and Unread counts; `systemMedium` displays the latest two articles.
Today and Unread count.
## Passing Data from the App to the Widget ## Passing Data from the App to the Widget
@ -35,9 +34,8 @@ struct LatestArticle: Codable {
## When is JSON Data Saved? ## When is JSON Data Saved?
1. On `unreadCountDidChange` 1. When the app enters the background
2. After a background refresh 2. After a background refresh
3. When the app enters the background
After JSON data is saved, Widget timelines are reloaded. After JSON data is saved, Widget timelines are reloaded.