Created
December 16, 2018 02:34
-
-
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
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
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()) | |
} | |
} |
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
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