Skip to content
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: 38 additions & 3 deletions WakaTime/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import AppUpdater
import Cocoa
import UserNotifications

class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate, UNUserNotificationCenterDelegate {
var window: NSWindow!
var statusBarItem: NSStatusItem!
let menu = NSMenu()
Expand All @@ -15,6 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {

@Atomic var lastTodayTime = 0
@Atomic var lastTodayText = ""
@Atomic var lastBrowserWarningTime = 0

let updater = AppUpdater(owner: "wakatime", repo: "macos-wakatime")

Expand Down Expand Up @@ -159,7 +160,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
Task.detached(priority: .background) {
self.fetchToday()
}
statusBarItem.popUpMenu(menu)
// statusBarItem.popUpMenu(menu)
statusBarItem.menu = menu
}

func a11yStatusChanged(_ hasPermission: Bool) {
Expand All @@ -175,6 +177,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
statusBarA11ySeparator.isHidden = hasPermission
}

private func checkBrowserDuplicateTracking() {
// Warn about using both Browser extension and Mac app tracking a browser at same time, once per 12 hrs
let time = Int(NSDate().timeIntervalSince1970)
if time - lastBrowserWarningTime > Dependencies.twelveHours && MonitoringManager.isMonitoringBrowsing {
Task {
if let browser = await Dependencies.recentBrowserExtension() {
lastBrowserWarningTime = time
delegate.toastNotification("Warning: WakaTime \(browser) extension detected. " +
"It’s recommended to only track browsing activity with the \(browser) " +
"extension or Mac Desktop app, but not both.")
}
}
}
}

private func showSettings() {
NSApp.activate(ignoringOtherApps: true)
settingsWindowController.settingsView.setBrowserVisibility()
Expand All @@ -189,17 +206,32 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
internal func toastNotification(_ title: String) {
let content = UNMutableNotificationContent()
content.title = title
content.body = " "

let uuidString = UUID().uuidString
let request = UNNotificationRequest(
identifier: uuidString,
content: content, trigger: nil)

let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self

notificationCenter.requestAuthorization(options: [.alert, .sound]) { granted, _ in
guard granted else { return }

notificationCenter.add(request)
DispatchQueue.main.async {
notificationCenter.add(request)
}
}
}

func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if #available(macOS 11.0, *) {
completionHandler([.banner, .sound])
} else {
completionHandler([.alert, .sound]) // Fallback for older macOS versions
}
}

Expand Down Expand Up @@ -248,12 +280,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
try process.execute()
} catch {
Logging.default.log("Failed to run wakatime-cli fetching Today coding activity: \(error)")
return
}

let handle = pipe.fileHandleForReading
let data = handle.readDataToEndOfFile()
let text = (String(data: data, encoding: String.Encoding.utf8) ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
lastTodayText = text
setText(text)

checkBrowserDuplicateTracking()
}
}
4 changes: 3 additions & 1 deletion WakaTime/Helpers/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Foundation
// swiftlint:disable force_unwrapping
// swiftlint:disable force_try
class Dependencies {
public static var twelveHours = 43200

public static func installDependencies() {
Task {
if !(await isCLILatest()) {
Expand Down Expand Up @@ -57,7 +59,7 @@ class Dependencies {
isoDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
isoDateFormatter.formatOptions = [.withInternetDateTime]
if let lastSeen = isoDateFormatter.date(from: lastSeenAt) {
if now.timeIntervalSince(lastSeen) > 600 {
if Int(now.timeIntervalSince(lastSeen)) > twelveHours {
break
}
}
Expand Down
6 changes: 4 additions & 2 deletions WakaTime/WakaTime.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.app-sandbox</key>
<false/>
<key>com.apple.security.notifications</key>
<true/>
</dict>
</plist>
10 changes: 0 additions & 10 deletions WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,6 @@ class WakaTime: HeartbeatEventHandler {
}
PropertiesManager.hasLaunchedBefore = true
}

if MonitoringManager.isMonitoringBrowsing {
Task {
if let browser = await Dependencies.recentBrowserExtension() {
delegate.toastNotification("Warning: WakaTime \(browser) extension detected. " +
"It’s recommended to only track browsing activity with the \(browser) " +
"extension or Mac Desktop app, but not both.")
}
}
}
}

private func configureFirebase() {
Expand Down
2 changes: 1 addition & 1 deletion project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ packages:
branch: master
Firebase:
url: https://github.com/firebase/firebase-ios-sdk
from: 10.0.0
from: 11.11.0

targets:
WakaTime:
Expand Down
Loading