From d427ac94ea4f5242d2a782f9f0be03c9981d8baa Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Sat, 14 Sep 2024 10:32:39 +0200 Subject: [PATCH] Always show monitored app even when not running --- WakaTime/Helpers/MonitoringManager.swift | 41 ++++++++++++++++-------- WakaTime/Views/MonitoredAppsView.swift | 5 ++- WakaTime/WakaTime.swift | 11 ------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/WakaTime/Helpers/MonitoringManager.swift b/WakaTime/Helpers/MonitoringManager.swift index 0f887da..0a427c9 100644 --- a/WakaTime/Helpers/MonitoringManager.swift +++ b/WakaTime/Helpers/MonitoringManager.swift @@ -8,15 +8,7 @@ class MonitoringManager { } static func isAppMonitored(for bundleId: String) -> Bool { - let isMonitoredKey = monitoredKey(bundleId: bundleId) - - if UserDefaults.standard.string(forKey: isMonitoredKey) != nil { - return UserDefaults.standard.bool(forKey: isMonitoredKey) - } else { - UserDefaults.standard.set(false, forKey: isMonitoredKey) - UserDefaults.standard.synchronize() - return false - } + allMonitoredApps.contains(bundleId) } static func isAppMonitored(_ app: NSRunningApplication) -> Bool { @@ -81,10 +73,33 @@ class MonitoringManager { return false } + static var allMonitoredApps: [String] { + if let bundleIds = UserDefaults.standard.stringArray(forKey: monitoringKey) { + return bundleIds + } else { + var bundleIds: [String] = [] + let defaults = UserDefaults.standard.dictionaryRepresentation() + for key in defaults.keys { + if key.starts(with: "is_") && key.contains("_monitored") { + if UserDefaults.standard.bool(forKey: key) { + let bundleId = key.replacingOccurrences(of: "is_", with: "").replacingOccurrences(of: "_monitored", with: "") + bundleIds.append(bundleId) + } + UserDefaults.standard.removeObject(forKey: key) + } + } + UserDefaults.standard.set(bundleIds, forKey: monitoringKey) + UserDefaults.standard.synchronize() + return bundleIds + } + } + static func set(monitoringState: MonitoringState, for bundleId: String) { - UserDefaults.standard.set(monitoringState == .on, forKey: monitoredKey(bundleId: bundleId)) + let allApps = allMonitoredApps + if !allApps.contains(bundleId) { + UserDefaults.standard.set(allApps + [bundleId], forKey: monitoringKey) + } UserDefaults.standard.synchronize() - // NSLog("Monitoring \(monitoringState == .on ? "enabled" : "disabled") for \(AppInfo.getAppName(bundleId: bundleId) ?? "")") } static func enableByDefault(_ bundleId: String) { @@ -97,9 +112,7 @@ class MonitoringManager { } } - static func monitoredKey(bundleId: String) -> String { - "is_\(bundleId)_monitored" - } + static var monitoringKey = "wakatime_monitored_apps" static func entity(for app: NSRunningApplication, _ element: AXUIElement) -> (String?, EntityType)? { if MonitoringManager.isAppBrowser(app) { diff --git a/WakaTime/Views/MonitoredAppsView.swift b/WakaTime/Views/MonitoredAppsView.swift index cde8d2b..360e617 100644 --- a/WakaTime/Views/MonitoredAppsView.swift +++ b/WakaTime/Views/MonitoredAppsView.swift @@ -11,9 +11,9 @@ class MonitoredAppsView: NSView, NSOutlineViewDataSource, NSOutlineViewDelegate private var outlineView: NSOutlineView! private var runningApps: [AppData] = [] - private func refreshRunningApps() -> [AppData] { + private func refreshRunningApps() { var apps = [AppData]() - let bundleIds = sort(MonitoredApp.allBundleIds + getRunningApps()) + let bundleIds = sort(Array(Set(MonitoredApp.allBundleIds + getRunningApps() + MonitoringManager.allMonitoredApps))) var index = 0 for bundleId in bundleIds { if let icon = AppInfo.getIcon(bundleId: bundleId), @@ -30,7 +30,6 @@ class MonitoredAppsView: NSView, NSOutlineViewDataSource, NSOutlineViewDelegate } } runningApps = apps - return apps } private func getRunningApps() -> [String] { diff --git a/WakaTime/WakaTime.swift b/WakaTime/WakaTime.swift index 1e0c91d..8341908 100644 --- a/WakaTime/WakaTime.swift +++ b/WakaTime/WakaTime.swift @@ -33,17 +33,6 @@ class WakaTime: HeartbeatEventHandler { watcher.heartbeatEventHandler = self watcher.statusBarDelegate = delegate - // In local dev builds, print bundle-ids of all running apps to Xcode console - if Dependencies.isLocalDevBuild { - Logging.default.log("********* Start Running Applications *********") - for runningApp in NSWorkspace.shared.runningApplications where runningApp.activationPolicy == .regular { - if let name = runningApp.localizedName, let id = runningApp.bundleIdentifier { - Logging.default.log("\(name): \(id)") - } - } - Logging.default.log("********* End Running Applications *********") - } - if !PropertiesManager.hasLaunchedBefore { for bundleId in MonitoredApp.defaultEnabledApps { MonitoringManager.enableByDefault(bundleId)