@@ -17,7 +17,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
1717 var nc : NSUserNotificationCenter !
1818 var repos = [ Repo] ( )
1919
20- let username = " ad "
20+ var username = " "
21+ var accessToken = " "
2122 let interval = 120
2223
2324 let clientId = " 88a135874dd3d8db2cc5 "
@@ -26,16 +27,13 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
2627 private var timer : Timer !
2728
2829 let app : NSApplication
29- let controller : NSWindowController
30+ var controller : NSWindowController !
3031
3132 init ( app: NSApplication ) {
3233 self . app = app
33- self . controller = NiblessWindowController ( )
3434 }
3535
3636 func applicationDidFinishLaunching( _ aNotification: Notification ) {
37-
38-
3937 let item = NSStatusBar . system. statusItem ( withLength: NSStatusItem . variableLength)
4038 self . statusItem = item
4139 self . statusItem. highlightMode = true
@@ -46,35 +44,27 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
4644 icon. isTemplate = true
4745
4846 self . statusItem. image = icon
49-
50- createMenu ( )
51-
47+
5248 self . nc = NSUserNotificationCenter . default
5349 nc. delegate = self
5450
5551 nc. removeAllDeliveredNotifications ( )
5652
57- controller. showWindow ( nil )
58- app. activate ( ignoringOtherApps: true )
59-
60- // let webview = WebView()
61-
62- // timer = Timer.scheduledTimer(timeInterval: TimeInterval(self.interval), target: self, selector: #selector(updateData), userInfo: nil, repeats: true)
63- //
64- // updateData()
65-
66- // newWindow = NSWindow(contentRect: NSMakeRect(10, 10, 300, 300), styleMask: .resizable, backing: .buffered, defer: false)
67-
68- // controller = SignInViewController()
69- // controller = NiblessWindowController()
70- // let content = controller.window?.contentView! as NSView
71- // let view = controller!.view
72- // controller?.showWindow(nil)
73- // NSApplication.shared.activate(ignoringOtherApps: true)
74- // }
75- // content.addSubview(webview)
76-
77- // newWindow!.makeKeyAndOrderFront(nil)
53+ if let accessToken = self . defaults. string ( forKey: " GHL.access_token " ) {
54+ self . accessToken = accessToken
55+
56+ print ( " access_token read " , accessToken)
57+ if let username = self . defaults. string ( forKey: " GHL.username " ) {
58+ self . username = username
59+ createTimer ( )
60+ updateData ( )
61+ createMenu ( )
62+ }
63+ } else {
64+ controller = NiblessWindowController ( )
65+ controller. showWindow ( nil )
66+ app. activate ( ignoringOtherApps: true )
67+ }
7868 }
7969
8070 func applicationWillTerminate( _ aNotification: Notification ) {
@@ -87,6 +77,10 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
8777 NSWorkspace . shared. open ( url)
8878 }
8979 }
80+
81+ public func createTimer( ) {
82+ timer = Timer . scheduledTimer ( timeInterval: TimeInterval ( self . interval) , target: self , selector: #selector( updateData) , userInfo: nil , repeats: true )
83+ }
9084
9185 func createMenu( ) {
9286 let menu = NSMenu ( )
@@ -96,7 +90,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
9690 statusItem. menu = menu
9791 }
9892
99- @objc func updateData( ) {
93+ @objc public func updateData( ) {
10094
10195 self . getRepos ( for: self . username) { ( result) in
10296 switch result {
@@ -224,6 +218,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
224218 var request = URLRequest ( url: url)
225219
226220 request. httpMethod = " GET "
221+
222+ request. addValue ( " application/json " , forHTTPHeaderField: " Accept " )
223+ request. addValue ( " token \( self . accessToken) " , forHTTPHeaderField: " Authorization " )
227224
228225 let config = URLSessionConfiguration . default
229226 let session = URLSession ( configuration: config)
@@ -279,6 +276,9 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
279276 } else {
280277 var request = URLRequest ( url: url)
281278 request. httpMethod = " GET "
279+
280+ request. addValue ( " application/json " , forHTTPHeaderField: " Accept " )
281+ request. addValue ( " token \( self . accessToken) " , forHTTPHeaderField: " Authorization " )
282282
283283 let config = URLSessionConfiguration . default
284284 let session = URLSession ( configuration: config)
@@ -317,13 +317,16 @@ final class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCent
317317 func checkStatus( url: URL , completion: ( ( _ isModified: Bool ) -> ( ) ) ? ) {
318318 let request = NSMutableURLRequest ( url: url)
319319 request. httpMethod = " HEAD "
320+
321+ request. addValue ( " application/json " , forHTTPHeaderField: " Accept " )
322+ request. addValue ( " token \( accessToken) " , forHTTPHeaderField: " Authorization " )
323+
320324 let config = URLSessionConfiguration . default
321325 let session = URLSession ( configuration: config)
322326
323327 var isModified = true
324328
325329 let task = session. dataTask ( with: request as URLRequest , completionHandler: { [ weak self] data, response, error -> Void in
326-
327330 if let httpResp: HTTPURLResponse = response as? HTTPURLResponse {
328331 let status = httpResp. allHeaderFields [ " Status " ] as? String
329332 let xRateLimitRemaining = httpResp. allHeaderFields [ " X-RateLimit-Remaining " ] as? String
0 commit comments