<commit_before>//
//  ViewController.swift
//  Sample
//
//  Created by <NAME> on 25/01/2016.
//  Copyright © 2016 Future Workshops. All rights reserved.
//

import UIKit
import FWTNotifiable
import SVProgressHUD

class ViewController: UIViewController {
    
    let FWTDeviceListSegue = "FWTDeviceListSegue"
    var manager:FWTNotifiableManager!
    var token:NSData?
    
    typealias FWTRegisterCompleted = (token:NSData!)->Void;
    var registerCompleted:FWTRegisterCompleted?
    
    @IBOutlet weak var onSiteSwitch: UISwitch!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "registerDeviceSuccess:", name: FWTNotifiableDidRegisterDeviceWithAPNSNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "failedToRegisterDevice:", name: FWTNotifiableFailedToRegisterDeviceWithAPNSNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "registerForRemoteNotification:", name: FWTNotifiableApplicationDidRegisterForRemoteNotifications, object: nil)
        self.updateScreen()
    }
    
    func updateScreen() {
        guard let information = self.manager.currentDevice?.information?["onsite"] as? NSNumber else {
            onSiteSwitch.on = false
            return
        }
        
        onSiteSwitch.on = information.boolValue
    }
    
    func registerDeviceSuccess(notification:NSNotification) {
        print("Device registered")
    }
    
    func failedToRegisterDevice(notification:NSNotification) {
        print("Device failed to register")
    }

    func registerForRemoteNotification(notification:NSNotification) {
        let token = notification.userInfo?[FWTNotifiableNotificationDeviceToken] as? NSData
        self.registerCompleted?(token: token)
    }
}

//MARK - Register

extension ViewController {
    
    func registeredForNotificationsWithToken(token:NSData) {
        self.token = token
        self.registerCompleted?(token: token)
        self.manager.updateDeviceToken(token, deviceName: "device", userAlias: "user", location: NSLocale.currentLocale(), deviceInformation: ["onsite":true]) { (device, error) -> Void in
            
        }
    }
    
    @IBAction func registerAnonymous(sender: AnyObject) {
        self._registerForNotifications { [weak self] (token) in
            self?._registerAnonymousToken(token)
        }
    }
    
    @IBAction func registerToUser(sender: AnyObject) {
        let alertController = UIAlertController(title: "User", message: "Please, insert the user name", preferredStyle: .Alert)
        alertController.addTextFieldWithConfigurationHandler {
            $0.placeholder = "User Name"
        }
        alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
        alertController.addAction(UIAlertAction(title: "Ok", style: .Default) { [weak self] (alertAction) -> Void in
            guard let userName = alertController.textFields?.first?.text else {
                return
            }
            self?._registerWithUser(userName)
        })
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    private func _registerWithUser(user:String) {
        self._registerForNotifications { [weak self] (token) -> Void in
            self?._registerToken(token, user: user)
        }
    }
    
    private func _registerAnonymousToken(token:NSData) {
        self.manager.registerAnonymousToken(token) { (device, error) in
            if let error = error {
                SVProgressHUD.showErrorWithStatus(error.fwt_debugMessage())
            } else {
                SVProgressHUD.showSuccessWithStatus("Anonymous device registered")
            }
        }
    }
    
    private func _registerToken(token:NSData, user:String) {
        let deviceName = UIDevice.currentDevice().name
        self.manager.registerToken(token, withUserAlias: user, deviceName: deviceName) { (device, error) in
            if let error = error {
                SVProgressHUD.showErrorWithStatus(error.fwt_debugMessage())
            } else {
                SVProgressHUD.showSuccessWithStatus("Device registered to user \(user)")
            }
        }
    }
    
    private func _registerForNotifications(completion:FWTRegisterCompleted) {
        self.registerCompleted = completion
        SVProgressHUD.show()
        let notificationSettings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
    }
}

//MARK - User

extension ViewController {
    @IBAction func associateToUser(sender: AnyObject) {
        let alertController = UIAlertController(title: "User", message: "Please, insert the user name", preferredStyle: .Alert)
        alertController.addTextFieldWithConfigurationHandler {<commit_msg>Force the device locale as en<commit_after>//
//  ViewController.swift
//  Sample
//
//  Created by <NAME> on 25/01/2016.
//  Copyright © 2016 Future Workshops. All rights reserved.
//

import UIKit
import FWTNotifiable
import SVProgressHUD

class ViewController: UIViewController {
    
    let FWTDeviceListSegue = "FWTDeviceListSegue"
    var manager:FWTNotifiableManager!
    var token:NSData?
    
    typealias FWTRegisterCompleted = (token:NSData!)->Void;
    var registerCompleted:FWTRegisterCompleted?
    
    @IBOutlet weak var onSiteSwitch: UISwitch!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "registerDeviceSuccess:", name: FWTNotifiableDidRegisterDeviceWithAPNSNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "failedToRegisterDevice:", name: FWTNotifiableFailedToRegisterDeviceWithAPNSNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "registerForRemoteNotification:", name: FWTNotifiableApplicationDidRegisterForRemoteNotifications, object: nil)
        self.updateScreen()
    }
    
    func updateScreen() {
        guard let information = self.manager.currentDevice?.information?["onsite"] as? NSNumber else {
            onSiteSwitch.on = false
            return
        }
        
        onSiteSwitch.on = information.boolValue
    }
    
    func registerDeviceSuccess(notification:NSNotification) {
        print("Device registered")
    }
    
    func failedToRegisterDevice(notification:NSNotification) {
        print("Device failed to register")
    }

    func registerForRemoteNotification(notification:NSNotification) {
        let token = notification.userInfo?[FWTNotifiableNotificationDeviceToken] as? NSData
        self.registerCompleted?(token: token)
    }
}

//MARK - Register

extension ViewController {
    
    func registeredForNotificationsWithToken(token:NSData) {
        self.token = token
        self.registerCompleted?(token: token)
        self.manager.updateDeviceToken(token, deviceName: "device", userAlias: "user", location: NSLocale.currentLocale(), deviceInformation: ["onsite":true]) { (device, error) -> Void in
            
        }
    }
    
    @IBAction func registerAnonymous(sender: AnyObject) {
        self._registerForNotifications { [weak self] (token) in
            self?._registerAnonymousToken(token)
        }
    }
    
    @IBAction func registerToUser(sender: AnyObject) {
        let alertController = UIAlertController(title: "User", message: "Please, insert the user name", preferredStyle: .Alert)
        alertController.addTextFieldWithConfigurationHandler {
            $0.placeholder = "User Name"
        }
        alertController.addAction(UIAlertAction(title: "Cancel", style: .Default, handler: nil))
        alertController.addAction(UIAlertAction(title: "Ok", style: .Default) { [weak self] (alertAction) -> Void in
            guard let userName = alertController.textFields?.first?.text else {
                return
            }
            self?._registerWithUser(userName)
        })
        self.presentViewController(alertController, animated: true, completion: nil)
    }
    
    private func _registerWithUser(user:String) {
        self._registerForNotifications { [weak self] (token) -> Void in
            self?._registerToken(token, user: user)
        }
    }
    
    private func _registerAnonymousToken(token:NSData) {
        let deviceName = UIDevice.currentDevice().name
        self.manager.registerAnonymousToken(token, deviceName: deviceName, withLocale: NSLocale(localeIdentifier: "en"), deviceInformation: [:]) { (device, error) in
            if let error = error {
                SVProgressHUD.showErrorWithStatus(error.fwt_debugMessage())
            } else {
                SVProgressHUD.showSuccessWithStatus("Anonymous device registered")
            }
        }
    }
    
    private func _registerToken(token:NSData, user:String) {
        let deviceName = UIDevice.currentDevice().name
        self.manager.registerToken(token, deviceName: deviceName, withUserAlias: user, locale: NSLocale(localeIdentifier: "en"), deviceInformation: [:]) { (device, error) in
            if let error = error {
                SVProgressHUD.showErrorWithStatus(error.fwt_debugMessage())
            } else {
                SVProgressHUD.showSuccessWithStatus("Device registered to user \(user)")
            }
        }
    }
    
    private func _registerForNotifications(completion:FWTRegisterCompleted) {
        self.registerCompleted = completion
        SVProgressHUD.show()
        let notificationSettings = UIUserNotificationSettings(forTypes: .Badge, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
    }
}

//MARK - User

extension ViewController {
    @IBAction func associateToUser(sender: AnyObject) {
        let alertController = UIAlertController(title: "User", message: "Please, insert the user name", preferredStyle: .Alert)
        alertController.addTextFieldWithConfigurationHandler {
{'fraction_non_alphanumeric': 0.09092700226578662, 'fraction_numerical': 0.002561324007486947, 'mean_word_length': 3.113452188006483, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 13, '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': '7569002', 'n_tokens_mistral': 2658, 'n_tokens_neox': 2607, 'n_words': 556}