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
#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)

View File

@ -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)
}
}

View File

@ -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.