@@ -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
@@ -175,6 +176,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
175176 statusBarA11ySeparator. isHidden = hasPermission
176177 }
177178
179+ private func checkBrowserDuplicateTracking( ) {
180+ // Warn about using both Browser extension and Mac app tracking a browser at same time, once per 12 hrs
181+ let time = Int ( NSDate ( ) . timeIntervalSince1970)
182+ if time - lastBrowserWarningTime > Dependencies . twelveHours && MonitoringManager . isMonitoringBrowsing {
183+ Task {
184+ if let browser = await Dependencies . recentBrowserExtension ( ) {
185+ lastBrowserWarningTime = time
186+ delegate. toastNotification ( " Warning: WakaTime \( browser) extension detected. " +
187+ " It’s recommended to only track browsing activity with the \( browser) " +
188+ " extension or Mac Desktop app, but not both. " )
189+ }
190+ }
191+ }
192+ }
193+
178194 private func showSettings( ) {
179195 NSApp . activate ( ignoringOtherApps: true )
180196 settingsWindowController. settingsView. setBrowserVisibility ( )
@@ -189,20 +205,31 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
189205 internal func toastNotification( _ title: String ) {
190206 let content = UNMutableNotificationContent ( )
191207 content. title = title
208+ content. body = " "
192209
193210 let uuidString = UUID ( ) . uuidString
194211 let request = UNNotificationRequest (
195212 identifier: uuidString,
196213 content: content, trigger: nil )
197214
198215 let notificationCenter = UNUserNotificationCenter . current ( )
216+ notificationCenter. delegate = self
217+
199218 notificationCenter. requestAuthorization ( options: [ . alert, . sound] ) { granted, _ in
200219 guard granted else { return }
201220
202- notificationCenter. add ( request)
221+ DispatchQueue . main. async {
222+ notificationCenter. add ( request)
223+ }
203224 }
204225 }
205226
227+ func userNotificationCenter( _ center: UNUserNotificationCenter ,
228+ willPresent notification: UNNotification ,
229+ withCompletionHandler completionHandler: @escaping ( UNNotificationPresentationOptions ) -> Void ) {
230+ completionHandler ( [ . banner, . sound] )
231+ }
232+
206233 private func setText( _ text: String ) {
207234 DispatchQueue . main. async {
208235 Logging . default. log ( " Set status bar text: \( text) " )
@@ -248,12 +275,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
248275 try process. execute ( )
249276 } catch {
250277 Logging . default. log ( " Failed to run wakatime-cli fetching Today coding activity: \( error) " )
278+ return
251279 }
252280
253281 let handle = pipe. fileHandleForReading
254282 let data = handle. readDataToEndOfFile ( )
255283 let text = ( String ( data: data, encoding: String . Encoding. utf8) ?? " " ) . trimmingCharacters ( in: . whitespacesAndNewlines)
256284 lastTodayText = text
257285 setText ( text)
286+
287+ checkBrowserDuplicateTracking ( )
258288 }
259289}
0 commit comments