Skip to content

Instantly share code, notes, and snippets.

@nanoxd
Created December 16, 2018 02:34
Show Gist options
  • Save nanoxd/0f41499f43dc20d241a2df0f202d7386 to your computer and use it in GitHub Desktop.
Save nanoxd/0f41499f43dc20d241a2df0f202d7386 to your computer and use it in GitHub Desktop.
[ViewModelType] Provide a clear contract between inputs/outputs desired in a view model
final class HelloWorldViewModel: ViewModelType {
let input: Input
let output: Output
struct Input {
let name: Anyobserver<String>
}
struct Output {
let greeting: Driver<String>
}
private let nameSubject = ReplaySubject<String>.create(bufferSize: 1)
init() {
let greeting = nameSubject
.map { name in
return "Hello \(name)"
}
.asDriver(onErrorJustReturn: "Hello Doe")
self.output = Output(greeting: greeting)
self.input = Input(nameSubject.asObserver())
}
}
protocol ViewModelType {
associatedtype Input
associatedtype Output
var input: Input { get }
var output: Output { get }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment