@@ -2,7 +2,7 @@ import AppUpdater
22import Cocoa
33import UserNotifications
44
5- class AppDelegate : NSObject , NSApplicationDelegate , StatusBarDelegate {
5+ class AppDelegate : NSObject , NSApplicationDelegate , StatusBarDelegate , UNUserNotificationCenterDelegate {
66 var window : NSWindow !
77 var statusBarItem : NSStatusItem !
88 let menu = NSMenu ( )
@@ -15,6 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
1515
1616 @Atomic var lastTodayTime = 0
1717 @Atomic var lastTodayText = " "
18+ @Atomic var lastBrowserWarningTime = 0
1819
1920 let updater = AppUpdater ( owner: " wakatime " , repo: " macos-wakatime " )
2021
@@ -159,7 +160,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
159160 Task . detached ( priority: . background) {
160161 self . fetchToday ( )
161162 }
162- statusBarItem. popUpMenu ( menu)
163+ // statusBarItem.popUpMenu(menu)
164+ statusBarItem. menu = menu
163165 }
164166
165167 func a11yStatusChanged( _ hasPermission: Bool ) {
@@ -175,6 +177,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
175177 statusBarA11ySeparator. isHidden = hasPermission
176178 }
177179
180+ private func checkBrowserDuplicateTracking( ) {
181+ // Warn about using both Browser extension and Mac app tracking a browser at same time, once per 12 hrs
182+ let time = Int ( NSDate ( ) . timeIntervalSince1970)
183+ if time - lastBrowserWarningTime > Dependencies . twelveHours && MonitoringManager . isMonitoringBrowsing {
184+ Task {
185+ if let browser = await Dependencies . recentBrowserExtension ( ) {
186+ lastBrowserWarningTime = time
187+ delegate. toastNotification ( " Warning: WakaTime \( browser) extension detected. " +
188+ " It’s recommended to only track browsing activity with the \( browser) " +
189+ " extension or Mac Desktop app, but not both. " )
190+ }
191+ }
192+ }
193+ }
194+
178195 private func showSettings( ) {
179196 NSApp . activate ( ignoringOtherApps: true )
180197 settingsWindowController. settingsView. setBrowserVisibility ( )
@@ -189,17 +206,32 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
189206 internal func toastNotification( _ title: String ) {
190207 let content = UNMutableNotificationContent ( )
191208 content. title = title
209+ content. body = " "
192210
193211 let uuidString = UUID ( ) . uuidString
194212 let request = UNNotificationRequest (
195213 identifier: uuidString,
196214 content: content, trigger: nil )
197215
198216 let notificationCenter = UNUserNotificationCenter . current ( )
217+ notificationCenter. delegate = self
218+
199219 notificationCenter. requestAuthorization ( options: [ . alert, . sound] ) { granted, _ in
200220 guard granted else { return }
201221
202- notificationCenter. add ( request)
222+ DispatchQueue . main. async {
223+ notificationCenter. add ( request)
224+ }
225+ }
226+ }
227+
228+ func userNotificationCenter( _ center: UNUserNotificationCenter ,
229+ willPresent notification: UNNotification ,
230+ withCompletionHandler completionHandler: @escaping ( UNNotificationPresentationOptions ) -> Void ) {
231+ if #available( macOS 11 . 0 , * ) {
232+ completionHandler ( [ . banner, . sound] )
233+ } else {
234+ completionHandler ( [ . alert, . sound] ) // Fallback for older macOS versions
203235 }
204236 }
205237
@@ -248,12 +280,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
248280 try process. execute ( )
249281 } catch {
250282 Logging . default. log ( " Failed to run wakatime-cli fetching Today coding activity: \( error) " )
283+ return
251284 }
252285
253286 let handle = pipe. fileHandleForReading
254287 let data = handle. readDataToEndOfFile ( )
255288 let text = ( String ( data: data, encoding: String . Encoding. utf8) ?? " " ) . trimmingCharacters ( in: . whitespacesAndNewlines)
256289 lastTodayText = text
257290 setText ( text)
291+
292+ checkBrowserDuplicateTracking ( )
258293 }
259294}
0 commit comments