Skip to content

Instantly share code, notes, and snippets.

@pallavtrivedi03
Created December 20, 2021 03:43
Show Gist options
  • Save pallavtrivedi03/07f522e3b90f23975b219d4cf721c379 to your computer and use it in GitHub Desktop.
Save pallavtrivedi03/07f522e3b90f23975b219d4cf721c379 to your computer and use it in GitHub Desktop.
import UIKit
import Combine
class ViewController: UIViewController {
@IBOutlet private var userNameTextField: UITextField!
@IBOutlet private var passwordTextField: UITextField!
@IBOutlet private var tncSwitch: UISwitch!
@IBOutlet private var signupButton: SignUpButton!
@Published private var isTnCAccepted: Bool = false
@Published private var username: String = ""
@Published private var password: String = ""
private let viewModel: SignUpViewModel = SignUpViewModel()
private var subscriptions: Set<AnyCancellable> = Set<AnyCancellable>()
private var signupValidationPublisher: AnyPublisher<Bool, Never> {
return Publishers.CombineLatest3($isTnCAccepted, $username, $password)
.map { isTnCAccepted, username, password in
isTnCAccepted && !username.isEmpty && !password.isEmpty
}
.eraseToAnyPublisher()
}
override func viewDidLoad() {
super.viewDidLoad()
signupValidationPublisher
.receive(on: RunLoop.main)
.assign(to: \.isEnabled, on: signupButton)
.store(in: &subscriptions)
}
@IBAction private func didToggleTnCSwitch(_ sender: UISwitch) {
isTnCAccepted = sender.isOn
}
@IBAction private func didChangeUsername(_ sender: UITextField) {
username = sender.text ?? ""
}
@IBAction private func didChangePassword(_ sender: UITextField) {
password = sender.text ?? ""
}
@IBAction private func didClickOnSubmitButton(_ sender: SignUpButton) {
//To be implemented in Zip Operator video
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment