<commit_before>//
//  ViewController.swift
//  ChatClient
//
//  Created by <NAME> on 9/15/14.
//  Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//

import UIKit
import DeferredTCPSocket
import Deferred
import Result

extension NSUserDefaults {
    var host: String? {
        get {
            return self.stringForKey("host")
        }
        set {
            self.setObject(newValue, forKey: "host")
        }
    }

    var username: String? {
        get {
            return self.stringForKey("username")
        }
        set {
            self.setObject(newValue, forKey: "username")
        }
    }
}

class ConnectViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var serverTextField: UITextField!
    @IBOutlet weak var chatNameTextField: UITextField!
    @IBOutlet weak var connectButton: UIButton!

    let userDefaults = NSUserDefaults.standardUserDefaults()

    var connecting: Bool = false {
        didSet {
            serverTextField.enabled = !connecting
            chatNameTextField.enabled = !connecting
            connectButton.enabled = !connecting
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        serverTextField.text = userDefaults.host ?? ""
        chatNameTextField.text = userDefaults.username ?? ""
    }

    @IBAction func connectButtonPressed(sender: AnyObject) {
        connectToChatServer()
    }

    func connectToChatServer() {
        let host = serverTextField.text
        let name = chatNameTextField.text
        connecting = true

        let deferredSocket = TCPCommSocket.connectToHost(serverTextField.text, serviceOrPort: "13579")

        let handshakeResult: Deferred<WriteResult> = deferredSocket.map { socketResult in
            socketResult.bind { socket -> WriteResult in
                socket.writeString("c:\(name)\n", withEncoding: NSUTF8StringEncoding).value
            }
        }

        handshakeResult.uponQueue(dispatch_get_main_queue()) { result in
            self.connecting = false

            switch result {
            case .Success:
                self.presentChatViewControllerWithSocket(deferredSocket.value.successValue!, name: name)
                self.userDefaults.host = host
                self.userDefaults.username = name

            case let .Failure(error):
                NSLog("Connection failed: \(error)")<commit_msg>Popup alert with error details when trying to connect<commit_after>//
//  ViewController.swift
//  ChatClient
//
//  Created by <NAME> on 9/15/14.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

import UIKit
import DeferredTCPSocket
import Deferred
import Result

extension NSUserDefaults {
    var host: String? {
        get {
            return self.stringForKey("host")
        }
        set {
            self.setObject(newValue, forKey: "host")
        }
    }

    var username: String? {
        get {
            return self.stringForKey("username")
        }
        set {
            self.setObject(newValue, forKey: "username")
        }
    }
}

class ConnectViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var serverTextField: UITextField!
    @IBOutlet weak var chatNameTextField: UITextField!
    @IBOutlet weak var connectButton: UIButton!

    let userDefaults = NSUserDefaults.standardUserDefaults()

    var connecting: Bool = false {
        didSet {
            serverTextField.enabled = !connecting
            chatNameTextField.enabled = !connecting
            connectButton.enabled = !connecting
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        serverTextField.text = userDefaults.host ?? ""
        chatNameTextField.text = userDefaults.username ?? ""
    }

    @IBAction func connectButtonPressed(sender: AnyObject) {
        connectToChatServer()
    }

    func connectToChatServer() {
        let host = serverTextField.text
        let name = chatNameTextField.text
        connecting = true

        let deferredSocket = TCPCommSocket.connectToHost(serverTextField.text, serviceOrPort: "13579")

        let handshakeResult: Deferred<WriteResult> = deferredSocket.map { socketResult in
            socketResult.bind { socket -> WriteResult in
                socket.writeString("c:\(name)\n", withEncoding: NSUTF8StringEncoding).value
            }
        }

        handshakeResult.uponQueue(dispatch_get_main_queue()) { result in
            self.connecting = false

            switch result {
            case .Success:
                self.presentChatViewControllerWithSocket(deferredSocket.value.successValue!, name: name)
                self.userDefaults.host = host
                self.userDefaults.username = name

            case let .Failure(error):
                var message = "Unknown error occurred"
                if let libcError = error as? LibCError {
                    if let errnoString = String(UTF8String: strerror(libcError.errno)) {
                        message = "Error: \(libcError.functionName) failed with \(errnoString)"
                    }
                }
                let alert = UIAlertController(title: "Error", message: message, preferredStyle: .Alert)
                alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { [weak alert] _ in
                    alert?.dismissViewControllerAnimated(true, completion: nil)
                    return
                }))
                self.presentViewController(alert, animated: true, completion: nil)
{'fraction_non_alphanumeric': 0.08626601696444686, 'fraction_numerical': 0.005594658003970403, 'mean_word_length': 2.1722953634802518, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820944', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1391, 'n_words': 335}