Skip to content

Commit

Permalink
Merge pull request #315 from wakatime/bugfix/always-list-enabled-apps
Browse files Browse the repository at this point in the history
Always show monitored app even when not running
  • Loading branch information
alanhamlett authored Sep 14, 2024
2 parents 5b4e03b + d427ac9 commit 42e72c3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
41 changes: 27 additions & 14 deletions WakaTime/Helpers/MonitoringManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions WakaTime/Views/MonitoredAppsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -30,7 +30,6 @@ class MonitoredAppsView: NSView, NSOutlineViewDataSource, NSOutlineViewDelegate
}
}
runningApps = apps
return apps
}

private func getRunningApps() -> [String] {
Expand Down
11 changes: 0 additions & 11 deletions WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 42e72c3

Please sign in to comment.