<commit_before>//
//  ControllerViewController.swift
//  BoostRemote
//
//  Created by <NAME> on 2017/08/01.
//  Copyright © 2017 <EMAIL>. All rights reserved.
//

import UIKit
import ReactiveCocoa
import ReactiveSwift
import Result
import ReSwift

class ControllerViewController: UIViewController, StoreSubscriber {
    
    @IBOutlet weak var leftSlider: UISlider!
    @IBOutlet weak var rightSlider: UISlider!
    
    @IBOutlet weak var centerSlider: UISlider!
    @IBOutlet weak var centerLabel: UILabel!
    
    @IBOutlet weak var connectButton: UIButton!
    
    let connectionState = MutableProperty(ConnectionState.disconnected)
    
    var leftMotor: Motor? = Motor(port: .A)
    var rightMotor: Motor? = Motor(port: .B)
    var centerMotor: Motor? {
        didSet {
            if let motor = centerMotor {
                centerSlider.isHidden = false
                centerLabel.isHidden = false
                centerLabel.text = motor.port.description
            } else {
                centerSlider.isHidden = true
                centerLabel.isHidden = true
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUp(slider: leftSlider)
        setUp(slider: rightSlider)
        setUp(slider: centerSlider)
        
        setUp(button: connectButton)
        
        signal(for: leftSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.leftMotor, power: value)
        }
        signal(for: rightSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.rightMotor, power: value)
        }
        signal(for: centerSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.centerMotor, power: value)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        store.subscribe(self)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        store.unsubscribe(self)
    }
    
    func newState(state: State) {
        connectionState.value = state.connectionState
        
        centerMotor = state.portState
            .flatMap { (port, type) -> Motor? in
                return type == .interactiveMotor ? Motor(port: port) : nil
            }
            .first
    }
    
    private func setUp(slider: UISlider) {
        slider.setThumbImage(UIImage(named: "thumb")?.withRenderingMode(.alwaysTemplate), for: .normal)
        slider.setMinimumTrackImage(UIImage(named: "left")?.withRenderingMode(.alwaysTemplate), for: .normal)
        slider.setMaximumTrackImage(UIImage(named: "right")?.withRenderingMode(.alwaysTemplate), for: .normal)
        
        slider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * -0.5))
        
        //slider.reactive.isEnabled <~ connectionState.map { $0 == .connected }
    }
    
    private func setUp(button: UIButton) {<commit_msg>Show the center slider only when connecting<commit_after>//
//  ControllerViewController.swift
//  BoostRemote
//
//  Created by <NAME> on 2017/08/01.
//  Copyright © 2017 brick<EMAIL>. All rights reserved.
//

import UIKit
import ReactiveCocoa
import ReactiveSwift
import Result
import ReSwift

class ControllerViewController: UIViewController, StoreSubscriber {
    
    @IBOutlet weak var leftSlider: UISlider!
    @IBOutlet weak var rightSlider: UISlider!
    
    @IBOutlet weak var centerSlider: UISlider!
    @IBOutlet weak var centerLabel: UILabel!
    
    @IBOutlet weak var connectButton: UIButton!
    
    let connectionState = MutableProperty(ConnectionState.disconnected)
    
    var leftMotor: Motor? = Motor(port: .A)
    var rightMotor: Motor? = Motor(port: .B)
    var centerMotor: Motor? {
        didSet {
            if let motor = centerMotor {
                centerSlider.isHidden = false
                centerLabel.isHidden = false
                centerLabel.text = motor.port.description
            } else {
                centerSlider.isHidden = true
                centerLabel.isHidden = true
            }
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUp(slider: leftSlider)
        setUp(slider: rightSlider)
        setUp(slider: centerSlider)
        
        setUp(button: connectButton)
        
        signal(for: leftSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.leftMotor, power: value)
        }
        signal(for: rightSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.rightMotor, power: value)
        }
        signal(for: centerSlider).observeValues { [weak self] (value) in
            self?.sendCommand(motor: self?.centerMotor, power: value)
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        store.subscribe(self)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        store.unsubscribe(self)
    }
    
    func newState(state: State) {
        connectionState.value = state.connectionState
        
        if state.connectionState == .connected {
            centerMotor = state.portState
                .flatMap { (port, type) -> Motor? in
                    return type == .interactiveMotor ? Motor(port: port) : nil
                }
                .first
        } else {
            centerMotor = nil
        }
    }
    
    private func setUp(slider: UISlider) {
        slider.setThumbImage(UIImage(named: "thumb")?.withRenderingMode(.alwaysTemplate), for: .normal)
        slider.setMinimumTrackImage(UIImage(named: "left")?.withRenderingMode(.alwaysTemplate), for: .normal)
        slider.setMaximumTrackImage(UIImage(named: "right")?.withRenderingMode(.alwaysTemplate), for: .normal)
        
        slider.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi * -0.5))
    }
    
    private func setUp(button: UIButton) {
{'fraction_non_alphanumeric': 0.09598948060486522, 'fraction_numerical': 0.004766600920447075, 'mean_word_length': 2.257494646680942, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 9, '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': '1357039', 'n_tokens_mistral': 1812, 'n_tokens_neox': 1743, 'n_words': 386}