mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
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:
@@ -1820,3 +1820,14 @@ public class GPUStressTest {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func isWidgetActive(_ defaults: UserDefaults?, _ widgets: [String]) -> Bool {
|
||||
for name in widgets {
|
||||
guard let lastUpdate = defaults?.double(forKey: name) else { return false }
|
||||
let timeSinceUpdate = Date().timeIntervalSince1970 - lastUpdate
|
||||
if timeSinceUpdate < 60 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -73,18 +73,14 @@ open class Module {
|
||||
public var settings: Settings_p? = nil
|
||||
public let portal: Portal_p?
|
||||
|
||||
public var name: String {
|
||||
config.name
|
||||
}
|
||||
public var name: String { config.name }
|
||||
public var combinedPosition: Int {
|
||||
get { Store.shared.int(key: "\(self.name)_position", defaultValue: 0) }
|
||||
set { Store.shared.set(key: "\(self.name)_position", value: newValue) }
|
||||
}
|
||||
public var userDefaults: UserDefaults? = UserDefaults(suiteName: "\(Bundle.main.object(forInfoDictionaryKey: "TeamId") as! String).eu.exelban.Stats.widgets")
|
||||
|
||||
public var popupKeyboardShortcut: [UInt16] {
|
||||
return self.popupView?.keyboardShortcut ?? []
|
||||
}
|
||||
public var popupKeyboardShortcut: [UInt16] { self.popupView?.keyboardShortcut ?? [] }
|
||||
|
||||
private var moduleType: ModuleType
|
||||
|
||||
@@ -97,12 +93,8 @@ open class Module {
|
||||
private var readers: [Reader_p] = []
|
||||
|
||||
private var pauseState: Bool {
|
||||
get {
|
||||
Store.shared.bool(key: "pause", defaultValue: false)
|
||||
}
|
||||
set {
|
||||
Store.shared.set(key: "pause", value: newValue)
|
||||
}
|
||||
get { Store.shared.bool(key: "pause", defaultValue: false) }
|
||||
set { Store.shared.set(key: "pause", value: newValue) }
|
||||
}
|
||||
|
||||
public init(moduleType: ModuleType, popup: Popup_p? = nil, settings: Settings_v? = nil, portal: Portal_p? = nil, notifications: NotificationsWrapper? = nil) {
|
||||
|
||||
@@ -234,12 +234,11 @@ public class CPU: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
if #unavailable(macOS 26.0) {
|
||||
guard let blobData = try? JSONEncoder().encode(value) else { return }
|
||||
if isWidgetActive(self.userDefaults, [CPU_entry.kind, "UnitedWidget"]), let blobData = try? JSONEncoder().encode(value) {
|
||||
self.userDefaults?.set(blobData, forKey: "CPU@LoadReader")
|
||||
}
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: CPU_entry.kind)
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: "UnitedWidget")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,8 +39,9 @@ public struct Provider: TimelineProvider {
|
||||
}
|
||||
|
||||
public func getTimeline(in context: Context, completion: @escaping (Timeline<CPU_entry>) -> Void) {
|
||||
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: CPU_entry.kind)
|
||||
var entry = CPU_entry()
|
||||
if let raw = userDefaults?.data(forKey: "CPU@LoadReader"), let load = try? JSONDecoder().decode(CPU_Load.self, from: raw) {
|
||||
if let raw = self.userDefaults?.data(forKey: "CPU@LoadReader"), let load = try? JSONDecoder().decode(CPU_Load.self, from: raw) {
|
||||
entry.value = load
|
||||
}
|
||||
let entries: [CPU_entry] = [entry]
|
||||
|
||||
@@ -330,14 +330,13 @@ public class Disk: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
if #unavailable(macOS 26.0) {
|
||||
guard let blobData = try? JSONEncoder().encode(d) else { return }
|
||||
if isWidgetActive(self.userDefaults, [Disk_entry.kind, "UnitedWidget"]), let blobData = try? JSONEncoder().encode(d) {
|
||||
self.userDefaults?.set(blobData, forKey: "Disk@CapacityReader")
|
||||
}
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: Disk_entry.kind)
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: "UnitedWidget")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func activityCallback(_ value: Disks) {
|
||||
guard self.enabled else { return }
|
||||
|
||||
@@ -39,6 +39,7 @@ public struct Provider: TimelineProvider {
|
||||
}
|
||||
|
||||
public func getTimeline(in context: Context, completion: @escaping (Timeline<Disk_entry>) -> Void) {
|
||||
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: Disk_entry.kind)
|
||||
var entry = Disk_entry()
|
||||
if let raw = userDefaults?.data(forKey: "Disk@CapacityReader"), let load = try? JSONDecoder().decode(drive.self, from: raw) {
|
||||
entry.value = load
|
||||
|
||||
@@ -192,12 +192,11 @@ public class GPU: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
if #unavailable(macOS 26.0) {
|
||||
guard let blobData = try? JSONEncoder().encode(selectedGPU) else { return }
|
||||
if isWidgetActive(self.userDefaults, [GPU_entry.kind, "UnitedWidget"]), let blobData = try? JSONEncoder().encode(selectedGPU) {
|
||||
self.userDefaults?.set(blobData, forKey: "GPU@InfoReader")
|
||||
}
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: GPU_entry.kind)
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: "UnitedWidget")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ public struct Provider: TimelineProvider {
|
||||
}
|
||||
|
||||
public func getTimeline(in context: Context, completion: @escaping (Timeline<GPU_entry>) -> Void) {
|
||||
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: GPU_entry.kind)
|
||||
var entry = GPU_entry()
|
||||
if let raw = userDefaults?.data(forKey: "GPU@InfoReader"), let load = try? JSONDecoder().decode(GPU_Info.self, from: raw) {
|
||||
entry.value = load
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -233,12 +233,11 @@ public class RAM: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 11.0, *) {
|
||||
if #unavailable(macOS 26.0) {
|
||||
guard let blobData = try? JSONEncoder().encode(value) else { return }
|
||||
if isWidgetActive(self.userDefaults, [RAM_entry.kind, "UnitedWidget"]), let blobData = try? JSONEncoder().encode(value) {
|
||||
self.userDefaults?.set(blobData, forKey: "RAM@UsageReader")
|
||||
}
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: RAM_entry.kind)
|
||||
WidgetCenter.shared.reloadTimelines(ofKind: "UnitedWidget")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,6 +55,7 @@ public struct Provider: TimelineProvider {
|
||||
}
|
||||
|
||||
public func getTimeline(in context: Context, completion: @escaping (Timeline<RAM_entry>) -> Void) {
|
||||
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: RAM_entry.kind)
|
||||
var entry = RAM_entry()
|
||||
if let raw = userDefaults?.data(forKey: "RAM@UsageReader"), let load = try? JSONDecoder().decode(RAM_Usage.self, from: raw) {
|
||||
entry.value = load
|
||||
|
||||
@@ -21,7 +21,12 @@ public struct Value {
|
||||
|
||||
public struct United_entry: TimelineEntry {
|
||||
public static let kind = "UnitedWidget"
|
||||
public static var snapshot: United_entry = United_entry()
|
||||
public static var snapshot: United_entry = United_entry(
|
||||
cpu: Value(value: 0.34),
|
||||
gpu: Value(value: 0.45),
|
||||
ram: Value(value: 0.67),
|
||||
disk: Value(value: 0.78)
|
||||
)
|
||||
|
||||
public var date: Date {
|
||||
Calendar.current.date(byAdding: .second, value: 5, to: Date())!
|
||||
@@ -48,6 +53,8 @@ public struct Provider: TimelineProvider {
|
||||
}
|
||||
|
||||
public func getTimeline(in context: Context, completion: @escaping (Timeline<United_entry>) -> Void) {
|
||||
self.userDefaults?.set(Date().timeIntervalSince1970, forKey: United_entry.kind)
|
||||
|
||||
var entry = United_entry()
|
||||
if let raw = userDefaults?.data(forKey: "CPU@LoadReader"), let value = try? JSONDecoder().decode(CPU_Load.self, from: raw) {
|
||||
entry.cpu = Value(value: value.totalUsage)
|
||||
|
||||
Reference in New Issue
Block a user