Skip to content

Commit

Permalink
Swift 3 latest updates
Browse files Browse the repository at this point in the history
 * I hate that I cannot use argument labels for function typedefs any more
 * There's a bug with the [Any:AnyHashable] as [String:String] at the moment
 * type coercion has pretty much gone away.
 * fileprivate is *more* private
  • Loading branch information
Alan Westbrook committed Aug 21, 2016
1 parent ff832cd commit 0504bb8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 45 deletions.
24 changes: 12 additions & 12 deletions Orangered-Swift/LoginViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ import Cocoa

let kHelpURL = URL(string: "https://www.github.com/voidref/orangered")

typealias LoginAction = (name:String, password:String) -> Void
typealias LoginAction = (_:String, _ password:String) -> Void

class LoginViewController: NSViewController {

private let nameLabel = NSTextField()
private let passwordLabel = NSTextField()
private let nameField = NSTextField()
private let passwordField = NSSecureTextField()
private let loginButton = NSButton()
private let helpButton = NSButton()
private let loginAction:LoginAction
fileprivate let nameLabel = NSTextField()
fileprivate let passwordLabel = NSTextField()
fileprivate let nameField = NSTextField()
fileprivate let passwordField = NSSecureTextField()
fileprivate let loginButton = NSButton()
fileprivate let helpButton = NSButton()
fileprivate let loginAction:LoginAction

init(loginAction action: LoginAction) {
loginAction = action
Expand All @@ -46,7 +46,7 @@ class LoginViewController: NSViewController {
view.translatesAutoresizingMaskIntoConstraints = false
}

private func setup() {
fileprivate func setup() {
view.translatesAutoresizingMaskIntoConstraints = false
for subview in [nameLabel, nameField, passwordLabel, passwordField, loginButton] { add(subview) }

Expand Down Expand Up @@ -113,16 +113,16 @@ class LoginViewController: NSViewController {
])
}

private func add(_ sub:NSView) {
fileprivate func add(_ sub:NSView) {
sub.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(sub)
}

@objc private func loginClicked() {
loginAction(name: nameField.stringValue, password: passwordField.stringValue)
loginAction(nameField.stringValue, passwordField.stringValue)
}

@objc private func helpClicked() {
@objc fileprivate func helpClicked() {
if let urlActual = kHelpURL {
NSWorkspace.shared().open(urlActual)
}
Expand Down
2 changes: 1 addition & 1 deletion Orangered-Swift/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Menu: NSMenu {
fatalError("init(coder:) has not been implemented")
}

private func setup() {
fileprivate func setup() {
autoenablesItems = false
}
}
6 changes: 3 additions & 3 deletions Orangered-Swift/PrefViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PrefViewController: NSViewController {

}

private func setup() {
fileprivate func setup() {
startAtLogin.target = self
startAtLogin.translatesAutoresizingMaskIntoConstraints = false

Expand All @@ -42,8 +42,8 @@ class PrefViewController: NSViewController {
])
}

@objc private func salClicked() {
print(SMLoginItemSetEnabled("com.rockwood.Orangered", true))
@objc fileprivate func salClicked() {
print(SMLoginItemSetEnabled("com.rockwood.Orangered" as CFString, true))
}
}

Expand Down
58 changes: 34 additions & 24 deletions Orangered-Swift/StatusItemController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
case modmail
case update

private static let urlMap = [
fileprivate static let urlMap = [
loggedout: nil,
invalidcredentials: nil,
disconnected: nil,
Expand Down Expand Up @@ -75,7 +75,7 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
}
}

private var state = State.disconnected {
fileprivate var state = State.disconnected {
willSet {
if newValue != state {
// In order to avoid having to set flags for handling values set that were already set, we check `willSet`. This, however, necessitates we reschedule handling until the value is actually set as there doesn't seem to be a way to let it set and then call a method synchronously
Expand All @@ -86,17 +86,17 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
}
}

private let statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)
fileprivate let statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength)

private var statusPoller:Timer?
private let prefs = UserDefaults.standard
private var statusConnection:URLSession?
private let session = URLSession.shared
private var loginWindowController:NSWindowController?
private var prefWindowController:NSWindowController?
private var mailboxItem:NSMenuItem?
private var loginItem:NSMenuItem?
private var mailCount = 0
fileprivate var statusPoller:Timer?
fileprivate let prefs = UserDefaults.standard
fileprivate var statusConnection:URLSession?
fileprivate let session = URLSession.shared
fileprivate var loginWindowController:NSWindowController?
fileprivate var prefWindowController:NSWindowController?
fileprivate var mailboxItem:NSMenuItem?
fileprivate var loginItem:NSMenuItem?
fileprivate var mailCount = 0

override init() {
super.init()
Expand All @@ -108,7 +108,7 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
}
}

private func setup() {
fileprivate func setup() {
NSUserNotificationCenter.default.delegate = self
setupMenu()
}
Expand Down Expand Up @@ -171,13 +171,13 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
loginItem?.title = kAttemptingLoginTitle

let task = session.dataTask(with: request) { (data, response, error) in
self.handleLogin(response: response as? HTTPURLResponse, data:data, error: error)
self.handleLogin(response: response, data:data, error: error)
}

task.resume()
}

private func handleLogin(response:HTTPURLResponse?, data:Data?, error:NSError?) {
fileprivate func handleLogin(response:URLResponse?, data:Data?, error:Error?) {
if let dataActual = data, let
dataString = String(data:dataActual, encoding:String.Encoding.utf8) {
if dataString.contains("wrong password") {
Expand All @@ -197,13 +197,18 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
}
}

guard let headers = response?.allHeaderFields as? [String:String] else {
print("wrong headers ... or so: \(response?.allHeaderFields)")
guard let responseActual = response as? HTTPURLResponse else {
print("Response is not an HTTPURLResponse, somehow: \(response)")
return
}

guard let headers = responseActual.allHeaderFields as NSDictionary? as! [String:String]? else {
print("wrong headers ... or so: \(responseActual.allHeaderFields)")
return
}

guard let url = response?.url else {
print("missing url from response: \(response)")
guard let url = responseActual.url else {
print("missing url from response: \(responseActual)")
return
}

Expand Down Expand Up @@ -242,22 +247,27 @@ class StatusItemController: NSObject, NSUserNotificationCenterDelegate {
window.appearance = NSAppearance(named: NSAppearanceNameVibrantLight)
loginWindowController = NSWindowController(window: window)

NSApp.activateIgnoringOtherApps(true)
NSApp.activate(ignoringOtherApps: true)
loginWindowController?.showWindow(self)
}

private func showPrefWindow() {
let pref = NSWindowController(window: NSPanel(contentViewController: PrefViewController()))

prefWindowController = pref
NSApp.activateIgnoringOtherApps(true)
NSApp.activate(ignoringOtherApps: true)
pref.showWindow(self)
}

private func interpretResponse(json: AnyObject) {
private func interpretResponse(json: Any) {
// Crude, but remarkably immune to data restructuring as long as the key value pairs don't change.

guard let jsonActual = json["data"] as? [String:AnyObject] else {
// WTF Swift 3...
guard let jsonDict = json as AnyObject? else {
return
}

guard let jsonActual = jsonDict["data"] as? [String:AnyObject] else {
print("response json unexpected format: \(json)")
return
}
Expand Down
10 changes: 5 additions & 5 deletions Orangered-Swift/UserDefaults+Orangered.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ extension UserDefaults {
}

// C APIs are *the worst*
static private var keychainItem:SecKeychainItem? = nil
static fileprivate var keychainItem:SecKeychainItem? = nil

private func getPassword() -> String? {
fileprivate func getPassword() -> String? {

guard let uname = username else {
print("no user name set")
return nil
}

var passwordLength:UInt32 = 0
var passwordData:UnsafeMutablePointer<Void>? = nil
var passwordData:UnsafeMutableRawPointer? = nil

let err = SecKeychainFindGenericPassword(nil,
UInt32(kServiceName.characters.count),
Expand All @@ -85,7 +85,7 @@ extension UserDefaults {
return nil
}

private func setPassword(_ pass:String) {
fileprivate func setPassword(_ pass:String) {
guard let uname = username else {
print("No username")
return
Expand All @@ -111,7 +111,7 @@ extension UserDefaults {
}
}

private func updatePassword(_ password:String) {
fileprivate func updatePassword(_ password:String) {
guard let itemActual = UserDefaults.keychainItem else {
print("Must grab a password to init the item before updatint it, bleah")
return
Expand Down

0 comments on commit 0504bb8

Please sign in to comment.