Forked from pallavtrivedi03/CombineLatestOperator.swift
Created
February 11, 2022 18:13
-
-
Save EvolverSwiftUI/9d063f304aa158f59f8c0ad795c451f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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