diff --git a/GitHub Notifications/AppDelegate.swift b/GitHub Notifications/AppDelegate.swift index ccb5224..05dcfdf 100644 --- a/GitHub Notifications/AppDelegate.swift +++ b/GitHub Notifications/AppDelegate.swift @@ -66,60 +66,55 @@ class AppDelegate: NSObject, NSApplicationDelegate { } @objc func update() { - let username = Defaults[.username] let password = Defaults[.password] - - let basicAuth = "\(username):\(password)".data(using: .utf8) - - if let base64Encoded = basicAuth?.base64EncodedString(options: Data.Base64EncodingOptions(rawValue: 0)) { - let headers = ["authorization": "Basic \(base64Encoded)"] + + let headers = ["authorization": "token \(password)"] - let request = NSMutableURLRequest( - url: NSURL(string: "https://api.github.com/notifications")! as URL, - cachePolicy: .reloadIgnoringLocalCacheData, - timeoutInterval: 10.0 - ) - - request.httpMethod = "GET" - request.allHTTPHeaderFields = headers + let request = NSMutableURLRequest( + url: NSURL(string: "https://api.github.com/notifications")! as URL, + cachePolicy: .reloadIgnoringLocalCacheData, + timeoutInterval: 10.0 + ) + + request.httpMethod = "GET" + request.allHTTPHeaderFields = headers - let session = URLSession.shared - let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in - if error != nil { - print(error!) - } else { - guard let responseData = data else { - print("Error: did not receive data") - return - } + let session = URLSession.shared + let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in + if error != nil { + print(error!) + } else { + guard let responseData = data else { + print("Error: did not receive data") + return + } - do { - guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: []) - as? [Any] else { - print("error trying to convert data to JSON 1") - return - } - - self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")" + do { + guard let notifications = try JSONSerialization.jsonObject(with: responseData, options: []) + as? [Any] else { + print("error trying to convert data to JSON 1") + return + } + + self.firstMenuItem?.title = "\(notifications.count) notification\(notifications.count > 1 ? "s" : "")" + + let doesNotificationExist = notifications.count >= 1 - let doesNotificationExist = notifications.count >= 1 - - DispatchQueue.main.async { - self.setStatusItemImage( - named: doesNotificationExist - ? "StatusItemImageNotification" - : "StatusItemImage" - ) - } - } catch { - print("error trying to convert data to JSON") - return - } + DispatchQueue.main.async { + self.setStatusItemImage( + named: doesNotificationExist + ? "StatusItemImageNotification" + : "StatusItemImage" + ) + } + } catch { + print("error trying to convert data to JSON") + return } - }) + } + }) - dataTask.resume() - } + dataTask.resume() } func setStatusItemImage(named: String = "StatusItemImage") { diff --git a/GitHub Notifications/Constants.swift b/GitHub Notifications/Constants.swift index 09cfb28..a5cc69e 100644 --- a/GitHub Notifications/Constants.swift +++ b/GitHub Notifications/Constants.swift @@ -10,6 +10,5 @@ import Cocoa import Defaults extension Defaults.Keys { - static let username = Key("username", default: "") static let password = Key("password", default: "") } diff --git a/GitHub Notifications/GeneralPreferenceViewController.swift b/GitHub Notifications/GeneralPreferenceViewController.swift index 7767bbb..f2f61f1 100644 --- a/GitHub Notifications/GeneralPreferenceViewController.swift +++ b/GitHub Notifications/GeneralPreferenceViewController.swift @@ -16,16 +16,13 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane { let preferencePaneTitle = "General" override var nibName: NSNib.Name? { "GeneralPreferenceViewController" } - - @IBOutlet private var usernameTextField: NSTextField! + @IBOutlet private var passwordTextField: NSTextField! @IBOutlet private var saveButton: NSButton! override func viewDidLoad() { super.viewDidLoad() - usernameTextField?.placeholderString = "username" - usernameTextField?.stringValue = Defaults[.username] passwordTextField?.placeholderString = "********" passwordTextField?.stringValue = Defaults[.password] @@ -34,7 +31,6 @@ final class GeneralPreferenceViewController: NSViewController, PreferencePane { @objc func save() { // Save input values in User Defaults - Defaults[.username] = usernameTextField!.stringValue Defaults[.password] = passwordTextField!.stringValue // Close the window when done diff --git a/GitHub Notifications/GeneralPreferenceViewController.xib b/GitHub Notifications/GeneralPreferenceViewController.xib index 5d9a8ae..eadb4cc 100644 --- a/GitHub Notifications/GeneralPreferenceViewController.xib +++ b/GitHub Notifications/GeneralPreferenceViewController.xib @@ -9,54 +9,32 @@ - - - + + - + - - + - - - - - - - - - - - - - - - - - - - - - + @@ -66,7 +44,7 @@ - + @@ -80,7 +58,7 @@