Created
November 4, 2019 03:48
-
-
Save fmo91/b4000799d2d9cddc01b0410d02e0555b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ViewController: UIViewController { | |
// MARK: - Views - | |
@IBOutlet private weak var firstNumberTextField: UITextField! | |
@IBOutlet private weak var secondNumberTextField: UITextField! | |
@IBOutlet private weak var operationTextField: UITextField! | |
@IBOutlet private weak var resultLabel: UILabel! | |
private var operationPickerView: UIPickerView! | |
// MARK: - Attributes - | |
private let viewModel = ViewModel() | |
private var cancellables: [AnyCancellable] = [] | |
// MARK: - Life Cycle - | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
firstNumberTextField.addTarget(self, | |
action: #selector(firstNumberTextFieldChanged), | |
for: .editingChanged | |
) | |
secondNumberTextField.addTarget(self, | |
action: #selector(secondNumberTextFieldChanged), | |
for: .editingChanged | |
) | |
operationPickerView = UIPickerView() | |
operationPickerView.delegate = self | |
operationPickerView.dataSource = self | |
operationTextField.inputView = operationPickerView | |
bindToViewModel() | |
} | |
// MARK: - Private methods - | |
private func bindToViewModel() { | |
viewModel.$operationText | |
.assign(to: \.text, on: operationTextField) | |
.add(to: &cancellables) | |
viewModel.$result | |
.assign(to: \.text, on: resultLabel) | |
.add(to: &cancellables) | |
} | |
// MARK: - Actions - | |
@objc private func firstNumberTextFieldChanged() { | |
viewModel.firstNumberText = firstNumberTextField.text | |
} | |
@objc private func secondNumberTextFieldChanged() { | |
viewModel.secondNumberText = secondNumberTextField.text | |
} | |
} | |
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource { | |
func numberOfComponents(in pickerView: UIPickerView) -> Int { | |
return 1 | |
} | |
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { | |
return MathOperation.allCases.count | |
} | |
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { | |
return MathOperation.allCases[row].rawValue | |
} | |
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { | |
viewModel.operation = MathOperation.allCases[row] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment