Skip to content

Instantly share code, notes, and snippets.

Saravanan saru2020

Block or report user

Report or block saru2020

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View LoginViewController - Without ViewModel
//1
let email = emailTextField.rx.text.orEmpty.asObservable()
//2
let password = passwordTextField.rx.text.orEmpty.asObservable()
//3
Observable.combineLatest(email, password) { (email, password) in
return email.isValidEmail()
&& password.isValidPassword()
}
View LoginViewController - ViewModel
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,}"
View LoginViewModel struct
//1
struct LoginViewModel {
// 2
let email = BehaviorRelay<String>(value: "")
// 3
let password = BehaviorRelay<String>(value: "")
// 4
let isValid: Observable<Bool>
init() {
View BehaviorRelay in RxSwift
let disposeBag = DisposeBag()
//1
let behaviourRelay = BehaviorRelay<String>(value: "")
//2
let subscription1 = behaviourRelay.subscribe(onNext:{ string in
print("subscription1: ", string)
})
//3
subscription1.disposed(by: disposeBag)
//4
View DisposeBag in RxSwift
let disposeBag = DisposeBag()
let heySequence = Observable.from(["H","e","y"])
let subscription = heySequence.subscribe { event in
switch event {
case .next(let value):
print("onNext, Event: \(value)")
case .error(let error):
print(error)
case .completed:
View Events in RxSwift
let heySequence = Observable.from(["H","e","y"])
let subscription = heySequence.subscribe { event in
switch event {
case .next(let value):
print("onNext Event: \(value)")
case .error(let error):
print(error)
case .completed:
print("onCompleted")
}
View Observable Sequence in RxSwift
let heyThereSequence = Observable.of("Hey there!")
let subscription = heyThereSequence.subscribe { event in
print(event)
}
Result:
next("Hey there!")
completed
View LoginViewController - Outlets
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginButton: UIButton!
View Email & Password Extensions
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)
View reduce in Functional Programming
//Functional Programming
let totalDurationSpentByContacts = contactsDurationArr.reduce(0,+)
print(totalDurationSpentByContacts)
You can’t perform that action at this time.