<commit_before>
//
//  DevicesTableViewController.swift
//  Glowb
//
//  Created by <NAME> on 12/5/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

class DeviceSelectionTableViewController: BaseTableViewController {
    
    var items: [SelectableViewModel<Device>] = []
    var selectionStyle: SelectionStyle = .single
    weak var delegate: SelectableTableViewControllerDelegate?
    
    
    // MARK: - Life cycle
    
    init(items: [SelectableViewModel<Device>]) {
        self.items = items
        
        super.init(style: .plain)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }
    
    // MARK: - Setup
    
    private func setup() {
        setupNavigationItem()
        setupNotifications()
        setupTableView()
    }
    
    private func setupNavigationItem() {
        title = "Device"
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addDeviceButtonTapped))
    }
    
    private func setupNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(handleNewDeviceConnected(notification:)), name: .particleDeviceConnected, object: nil)
    }
    
    private func setupTableView() {
        tableView.register(cellType: DeviceSelectionTableViewCell.self)
    }
    
    
    // MARK: - Notification
    
    @objc private func handleNewDeviceConnected(notification: Notification) {
        if let id = notification.userInfo?["device_id"] as? String,
            let name = notification.userInfo?["device_name"] as? String {
            Device.create(deviceId: id, name: name).then { device -> Void in
                self.refreshDevices(device)
                self.dismiss(animated: true, completion: nil)
            }.catch { error in
                print(error)
            }
        }
    }
    
    fileprivate func refreshDevices(_ device: Device) {
        var devices = User.current.devices.filter { $0.id != device.id }
        devices.append(device)
        User.current.devices = devices
        
        let selectableDevices = User.current.devices.map { (item) -> SelectableViewModel<Device> in
            let state: SelectedState = (device == item) ? .selected : .deselected
            return SelectableViewModel(model: item, selectedState: state)
        }
        
        items = selectableDevices
        
        self.tableView.reloadData()
    }
    
    
    // MARK: - Actions
    
    @objc private func addDeviceButtonTapped() {
        let viewController = WizardIntroViewController.initFromStoryboard()
        viewController.delegate = self
        let navigationController = BaseNavigationController(rootViewController: viewController)
        navigationController.transitioningDelegate = CardTransitioningDelegate.shared
        navigationController.modalPresentationStyle = .custom
        
        present(navigationController, animated: true, completion: nil)
    }
}<commit_msg>auto select newly connected device<commit_after>
//
//  DevicesTableViewController.swift
//  Glowb
//
//  Created by <NAME> on 12/5/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import UIKit

class DeviceSelectionTableViewController: BaseTableViewController {
    
    var items: [SelectableViewModel<Device>] = []
    var selectionStyle: SelectionStyle = .single
    weak var delegate: SelectableTableViewControllerDelegate?
    
    
    // MARK: - Life cycle
    
    init(items: [SelectableViewModel<Device>]) {
        self.items = items
        
        super.init(style: .plain)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setup()
    }
    
    // MARK: - Setup
    
    private func setup() {
        setupNavigationItem()
        setupNotifications()
        setupTableView()
    }
    
    private func setupNavigationItem() {
        title = "Device"
        navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addDeviceButtonTapped))
    }
    
    private func setupNotifications() {
        NotificationCenter.default.addObserver(self, selector: #selector(handleNewDeviceConnected(notification:)), name: .particleDeviceConnected, object: nil)
    }
    
    private func setupTableView() {
        tableView.register(cellType: DeviceSelectionTableViewCell.self)
    }
    
    
    // MARK: - Notification
    
    @objc private func handleNewDeviceConnected(notification: Notification) {
        if let id = notification.userInfo?["device_id"] as? String,
            let name = notification.userInfo?["device_name"] as? String {
            Device.create(deviceId: id, name: name).then { device -> Void in
                self.refreshDevices(device)
                self.dismiss(animated: true, completion: nil)
            }.catch { error in
                print(error)
            }
        }
    }
    
    fileprivate func refreshDevices(_ device: Device) {
        var devices = User.current.devices.filter { $0.id != device.id }
        devices.append(device)
        User.current.devices = devices
        
        let selectableDevices = User.current.devices.map { (item) -> SelectableViewModel<Device> in
            let state: SelectedState = (device == item) ? .selected : .deselected
            return SelectableViewModel(model: item, selectedState: state)
        }
        
        items = selectableDevices
        
        self.tableView.reloadData()
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            self.delegate?.selectableTableViewController(viewController: self, didSelectSelection: SelectableViewModel(model: device, selectedState: .selected))
        }
    }
        
    
    // MARK: - Actions
    
    @objc private func addDeviceButtonTapped() {
        let viewController = WizardIntroViewController.initFromStoryboard()
        viewController.delegate = self
        let navigationController = BaseNavigationController(rootViewController: viewController)
        navigationController.transitioningDelegate = CardTransitioningDelegate.shared
        navigationController.modalPresentationStyle = .custom
        
        present(navigationController, animated: true, completion: nil)
    }
}
{'fraction_non_alphanumeric': 0.09228418640183346, 'fraction_numerical': 0.0033613445378151263, 'mean_word_length': 2.441640378548896, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, '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': '3226438', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1692, 'n_words': 376}