feat: added a check if the macOS widget is active before updating the read value in the user defaults. It will prevent unnecessary writes when the widget is not enabled (#2733)

This commit is contained in:
Serhiy Mytrovtsiy
2025-12-02 21:00:52 +01:00
parent a96232b0c1
commit 3cad4f49dd
13 changed files with 43 additions and 33 deletions

View File

@@ -326,11 +326,10 @@ public class Network: Module {
}
if #available(macOS 11.0, *) {
if #unavailable(macOS 26.0) {
guard let blobData = try? JSONEncoder().encode(raw) else { return }
if isWidgetActive(self.userDefaults, [Network_entry.kind]), let blobData = try? JSONEncoder().encode(raw) {
self.userDefaults?.set(blobData, forKey: "Network@UsageReader")
WidgetCenter.shared.reloadTimelines(ofKind: Network_entry.kind)
}
WidgetCenter.shared.reloadTimelines(ofKind: Network_entry.kind)
}
}

View File

@@ -44,6 +44,7 @@ public struct Provider: TimelineProvider {
}
public func getTimeline(in context: Context, completion: @escaping (Timeline<Network_entry>) -> Void) {
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: Network_entry.kind)
var entry = Network_entry()
if let raw = userDefaults?.data(forKey: "Network@UsageReader"), let load = try? JSONDecoder().decode(Network_Usage.self, from: raw) {
entry.value = load