@@ -2,7 +2,7 @@ import AppUpdater
2
2
import Cocoa
3
3
import UserNotifications
4
4
5
- class AppDelegate : NSObject , NSApplicationDelegate , StatusBarDelegate {
5
+ class AppDelegate : NSObject , NSApplicationDelegate , StatusBarDelegate , UNUserNotificationCenterDelegate {
6
6
var window : NSWindow !
7
7
var statusBarItem : NSStatusItem !
8
8
let menu = NSMenu ( )
@@ -15,6 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
15
15
16
16
@Atomic var lastTodayTime = 0
17
17
@Atomic var lastTodayText = " "
18
+ @Atomic var lastBrowserWarningTime = 0
18
19
19
20
let updater = AppUpdater ( owner: " wakatime " , repo: " macos-wakatime " )
20
21
@@ -159,7 +160,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
159
160
Task . detached ( priority: . background) {
160
161
self . fetchToday ( )
161
162
}
162
- statusBarItem. popUpMenu ( menu)
163
+ // statusBarItem.popUpMenu(menu)
164
+ statusBarItem. menu = menu
163
165
}
164
166
165
167
func a11yStatusChanged( _ hasPermission: Bool ) {
@@ -175,6 +177,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
175
177
statusBarA11ySeparator. isHidden = hasPermission
176
178
}
177
179
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
+
178
195
private func showSettings( ) {
179
196
NSApp . activate ( ignoringOtherApps: true )
180
197
settingsWindowController. settingsView. setBrowserVisibility ( )
@@ -189,17 +206,32 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
189
206
internal func toastNotification( _ title: String ) {
190
207
let content = UNMutableNotificationContent ( )
191
208
content. title = title
209
+ content. body = " "
192
210
193
211
let uuidString = UUID ( ) . uuidString
194
212
let request = UNNotificationRequest (
195
213
identifier: uuidString,
196
214
content: content, trigger: nil )
197
215
198
216
let notificationCenter = UNUserNotificationCenter . current ( )
217
+ notificationCenter. delegate = self
218
+
199
219
notificationCenter. requestAuthorization ( options: [ . alert, . sound] ) { granted, _ in
200
220
guard granted else { return }
201
221
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
203
235
}
204
236
}
205
237
@@ -248,12 +280,15 @@ class AppDelegate: NSObject, NSApplicationDelegate, StatusBarDelegate {
248
280
try process. execute ( )
249
281
} catch {
250
282
Logging . default. log ( " Failed to run wakatime-cli fetching Today coding activity: \( error) " )
283
+ return
251
284
}
252
285
253
286
let handle = pipe. fileHandleForReading
254
287
let data = handle. readDataToEndOfFile ( )
255
288
let text = ( String ( data: data, encoding: String . Encoding. utf8) ?? " " ) . trimmingCharacters ( in: . whitespacesAndNewlines)
256
289
lastTodayText = text
257
290
setText ( text)
291
+
292
+ checkBrowserDuplicateTracking ( )
258
293
}
259
294
}
0 commit comments