Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v5.23.5 #316

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading