<commit_before>//
//  LoginRegisterVC.swift
//  TapglueSample
//
//  Created by <NAME> on 25.10.15.
//  Copyright © 2015 Tapglue. All rights reserved.
//

import UIKit
import Tapglue

class LoginRegisterVC: UIViewController {
    
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var lastNameTextField: UITextField!
    @IBOutlet weak var aboutTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    var currentAvatar: String?
    
    let appDel = UIApplication.sharedApplication().delegate! as! AppDelegate
    
    // Free http://uifaces.com/authorized profile pictures
    let userProfileImageURLs =  ["https://s3.amazonaws.com/uifaces/faces/twitter/rem/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/nedknowles/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/jadlimcaco/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/zack415/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/rssems/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/kfriedson/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/nuraika/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/raquelromanp/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/geeftvorm/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/hellgy/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/allisongrayce/128.jpg"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillDisappear(animated: Bool) {
        // Show navigationBar again when view disappears
        self.navigationController?.navigationBarHidden = false
    }
    
    @IBAction func signUpButtonPressed(sender: AnyObject) {
        // If all textFields have more than 2 charcaters, begin Tapglue login
        if userNameTextField.text?.characters.count > 2 && firstNameTextField.text?.characters.count > 2 && lastNameTextField.text?.characters.count > 2 && aboutTextField.text?.characters.count > 2 && emailTextField.text?.characters.count > 2 && passwordTextField.text?.characters.count > 2 {
            
            let usr = User()
            
            usr.username = userNameTextField.text!
            usr.firstName = firstNameTextField.text!
            usr.lastName = lastNameTextField.text!
            usr.email = emailTextField.text!
            usr.password = <PASSWORD>
            usr.about = aboutTextField.text!
            
            // New SDK
            let randomIndex = Int(arc4random_uniform(UInt32(userProfileImageURLs.count)))
            let imageURL = userProfileImageURLs[randomIndex]
            let userImage = Image(url: imageURL)
            usr.images = ["profile": userImage]
            
            // Create New User
            appDel.rxTapglue.createUser(usr).subscribe { (event) in
                switch event {
                case .Next(let newUser):
                    print(newUser.email)
                    // Login new User
                    self.appDel.rxTapglue.loginUser(usr.username!, password: usr.password!).subscribe({ (event) in
                        switch event {
                        case .Next(let newUser):
                            print(newUser.email)
                            print("Go to next screen")
                        case .Error(let error):
                            self.appDel.printOutErrorMessageAndCode(error as? TapglueError)
                        case .Completed:
                            print("Completed Login")
                            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                                self.navigationController?.popToRootViewControllerAnimated(false)
                            })
                        }
                    }).addDisposableTo(self.appDel.disposeBag)
                case .Error(let error):
                    self.appDel.printOutErrorMessageAndCode(error as? TapglueError)
                case .Completed:
                    break
                }
            }.addDisposableTo(self.appDel.disposeBag)
            
        } else {
            print("Not enough characters")
        }
    }
}

extension LoginRegisterVC: UITextFieldDelegate {
    // Mark: TextField<commit_msg>Refactored<commit_after>//
//  LoginRegisterVC.swift
//  TapglueSample
//
//  Created by <NAME> on 25.10.15.
//  Copyright © 2015 Tapglue. All rights reserved.
//

import UIKit
import Tapglue

class LoginRegisterVC: UIViewController {
    
    @IBOutlet weak var userNameTextField: UITextField!
    @IBOutlet weak var firstNameTextField: UITextField!
    @IBOutlet weak var lastNameTextField: UITextField!
    @IBOutlet weak var aboutTextField: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!
    
    var currentAvatar: String?
    
    let appDel = UIApplication.sharedApplication().delegate! as! AppDelegate
    
    // Free http://uifaces.com/authorized profile pictures
    let userProfileImageURLs =  ["https://s3.amazonaws.com/uifaces/faces/twitter/rem/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/nedknowles/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/jadlimcaco/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/zack415/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/rssems/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/kfriedson/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/nuraika/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/raquelromanp/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/geeftvorm/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/hellgy/128.jpg",
                                "https://s3.amazonaws.com/uifaces/faces/twitter/allisongrayce/128.jpg"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillDisappear(animated: Bool) {
        // Show navigationBar again when view disappears
        self.navigationController?.navigationBarHidden = false
    }
    
    @IBAction func signUpButtonPressed(sender: AnyObject) {
        // If all textFields have more than 2 charcaters, begin Tapglue login
        if userNameTextField.text?.characters.count > 2 && firstNameTextField.text?.characters.count > 2 && lastNameTextField.text?.characters.count > 2 && aboutTextField.text?.characters.count > 2 && emailTextField.text?.characters.count > 2 && passwordTextField.text?.characters.count > 2 {
            
            let usr = User()
            
            usr.username = userNameTextField.text!
            usr.firstName = firstNameTextField.text!
            usr.lastName = lastNameTextField.text!
            usr.email = emailTextField.text!
            usr.password = <PASSWORD>
            usr.about = aboutTextField.text!
            
            // New SDK
            let randomIndex = Int(arc4random_uniform(UInt32(userProfileImageURLs.count)))
            let imageURL = userProfileImageURLs[randomIndex]
            let userImage = Image(url: imageURL)
            usr.images = ["profile": userImage]
            
            // Create New User
            appDel.rxTapglue.createUser(usr).subscribe { (event) in
                switch event {
                case .Next( _):
                    // Login new User
                    self.appDel.rxTapglue.loginUser(usr.username!, password: usr.password!).subscribe({ (event) in
                        switch event {
                        case .Next( _):
                            print("Go to next screen")
                        case .Error(let error):
                            self.appDel.printOutErrorMessageAndCode(error as? TapglueError)
                        case .Completed:
                            print("Completed Login")
                            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                                self.navigationController?.popToRootViewControllerAnimated(false)
                            })
                        }
                    }).addDisposableTo(self.appDel.disposeBag)
                case .Error(let error):
                    self.appDel.printOutErrorMessageAndCode(error as? TapglueError)
                case .Completed:
                    break
                }
            }.addDisposableTo(self.appDel.disposeBag)
            
        } else {
            print("Not enough characters")
        }
    }
}

extension LoginRegisterVC: UITextFieldDelegate {
    // Mark: TextField
{'fraction_non_alphanumeric': 0.0976980438776613, 'fraction_numerical': 0.014481789689830325, 'mean_word_length': 1.6862119013062409, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 22, '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': '5812694', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2447, 'n_words': 425}