Created
September 3, 2017 10:19
-
-
Save MartinMoizard/4d66528a9959cbbdefa6d50394d2bfb1 to your computer and use it in GitHub Desktop.
This file contains 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 SayHelloViewModel: ViewModelType { | |
let input: Input | |
let output: Output | |
struct Input { | |
let name: AnyObserver<String> | |
let validate: AnyObserver<Void> | |
} | |
struct Output { | |
let greeting: Driver<String> | |
} | |
private let nameSubject = ReplaySubject<String>.create(bufferSize: 1) | |
private let validateSubject = PublishSubject<Void>() | |
init() { | |
let greeting = validateSubject | |
.withLatestFrom(nameSubject) | |
.map { name in | |
return "Hello \(name)!" | |
} | |
.asDriver(onErrorJustReturn: ":-(") | |
self.output = Output(greeting: greeting) | |
self.input = Input(name: nameSubject.asObserver(), validate: validateSubject.asObserver()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I had to abandon the
BaseViewModel
class, as well as the parameters and functions of theSomeViewModel
class that should be used in closures. And also theinit
function turns out to be rather cumbersome because nothing can be taken out of it into other functions withoutself
.I got something like this, but I'm not sure if this approach is suitable for all cases:
Maybe you have ideas how to improve it and avoid cumbersome?