Created
May 22, 2019 10:01
-
-
Save saru2020/d253ac09440f6ddddfaac63eee9ed76f 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 RxSwift | |
import RxCocoa | |
extension String { | |
/// Used to validate if the given string is valid email or not | |
/// | |
/// - Returns: Boolean indicating if the string is valid email or not | |
func isValidEmail() -> Bool { | |
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}" | |
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx) | |
print("emailTest.evaluate(with: self): \(emailTest.evaluate(with: self))") | |
return emailTest.evaluate(with: self) | |
} | |
/// Used to validate if the given string matches the password requirements | |
/// | |
/// - Returns: Boolean indicating the comparison result | |
func isValidPassword() -> Bool { | |
print("self.count >= 6: \(self.count >= 6)") | |
return self.count >= 6 | |
} | |
} | |
//1 | |
struct LoginViewModel { | |
// 2 | |
let email = BehaviorRelay<String>(value: "") | |
// 3 | |
let password = BehaviorRelay<String>(value: "") | |
// 4 | |
let isValid: Observable<Bool> | |
init() { | |
// 5 | |
isValid = Observable.combineLatest(self.email.asObservable(), self.password.asObservable()) | |
{ (email, password) in | |
// 6 | |
return email.isValidEmail() | |
&& password.isValidPassword() | |
} | |
} | |
} | |
class ViewController: UIViewController { | |
@IBOutlet weak var emailTextField: UITextField! | |
@IBOutlet weak var passwordTextField: UITextField! | |
@IBOutlet weak var loginButton: UIButton! | |
let viewModel = LoginViewModel() | |
let disposeBag = DisposeBag() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// 1 | |
emailTextField.rx.text.orEmpty.bind(to: viewModel.email) | |
.disposed(by: disposeBag) | |
// 2 | |
passwordTextField.rx.text.orEmpty.bind(to: viewModel.password) | |
.disposed(by: disposeBag) | |
// 3 | |
viewModel.isValid.map { $0 } | |
.bind(to: loginButton.rx.isEnabled) | |
.disposed(by: disposeBag) | |
} | |
@IBAction func loginButtonTapped(_ sender: Any) { | |
let alert = UIAlertController(title: "Login", message: "Login Button Enabled/Tapped!", preferredStyle: .alert) | |
let okAction = UIAlertAction(title: "OK", style: .default, handler: nil) | |
alert.addAction(okAction) | |
self.present(alert, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment