Skip to content

Commit 30c7995

Browse files
committed
* auth ready
1 parent 77cc24a commit 30c7995

2 files changed

Lines changed: 72 additions & 32 deletions

File tree

GitHubListener/AppDelegate.swift

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -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

GitHubListener/NiblessWindowController.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class NiblessWindowController: NSWindowController, WKNavigationDelegate {
7878
do {
7979
if let content = try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject] {
8080
if let accessToken = content["access_token"] as? String {
81-
print("access_token", accessToken)
81+
self.getUser(accessToken: accessToken)
8282
self.window?.close()
8383
}
8484
}
@@ -92,4 +92,41 @@ class NiblessWindowController: NSWindowController, WKNavigationDelegate {
9292
}
9393
decisionHandler(WKNavigationActionPolicy.allow)
9494
}
95+
96+
func getUser(accessToken: String) {
97+
let urlString = "https://api.github.com/user"
98+
if let url = NSURL(string: urlString) {
99+
let req = NSMutableURLRequest(url: url as URL)
100+
req.addValue("application/json", forHTTPHeaderField: "Accept")
101+
req.addValue("token \(accessToken)", forHTTPHeaderField: "Authorization")
102+
let task = URLSession.shared.dataTask(with: req as URLRequest) { data, response, error in
103+
if let data = data {
104+
if let content = String(data: data, encoding: String.Encoding.utf8) {
105+
do {
106+
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String:Any] {
107+
if let login = jsonResult["login"] {
108+
UserDefaults.standard.set(login, forKey: "GHL.username")
109+
UserDefaults.standard.set(accessToken, forKey: "GHL.access_token")
110+
UserDefaults.standard.synchronize()
111+
112+
DispatchQueue.main.async() {
113+
let appDelegate = NSApplication.shared.delegate as! AppDelegate
114+
appDelegate.accessToken = accessToken
115+
appDelegate.createTimer()
116+
appDelegate.updateData()
117+
appDelegate.createMenu()
118+
119+
// print("access_token received", accessToken, "for user", login)
120+
}
121+
}
122+
}
123+
} catch {
124+
print(error.localizedDescription)
125+
}
126+
}
127+
}
128+
}
129+
task.resume()
130+
}
131+
}
95132
}

0 commit comments

Comments
 (0)