Skip to content

Instantly share code, notes, and snippets.

@Sajjon
Last active November 26, 2018 14:31
Show Gist options
  • Save Sajjon/19096243ff15ce017ca802043f0c6a5e to your computer and use it in GitHub Desktop.
Save Sajjon/19096243ff15ce017ca802043f0c6a5e to your computer and use it in GitHub Desktop.
Medium article: SLC part 1 - ViewModel (simple)
// The cases should be named as if they were prefixed with the words
// "user intends to" or "user did"
enum AuthenticateNavigationStep {
case signIn
case signUp
}
final class AuthenticateViewModel: BaseViewModel<
AuthenticateNavigationStep,
AuthenticateViewModel.InputFromView,
AuthenticateViewModel.Output
> {
// MARK: - Overriding methods
override func transform(input: Input) -> Output {
func userIntends(to intention: AuthenticateNavigationStep) {
navigator.navigate(to: intention)
}
[
input.fromView.signUpTrigger
.do(onNext: { userIntends(to: .signUp) })
.drive(),
input.fromView.signInTrigger
.do(onNext: { userIntends(to: .signIn) })
.drive()
].forEach { $0.disposed(by: bag) }
return Output()
}
}
extension AuthenticateViewModel {
struct InputFromView {
let signUpTrigger: Driver<Void>
let signInTrigger: Driver<Void>
}
struct Output {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment