Slide 77
Slide 77 text
fun onStart() {
events.publish { events ->
Observable.merge(
events.filterIsInstance()
.switchMap { event ->
appService.setName(event.newName).map { response ->
when {
response.isValid -> NameValid(response.name)
response.isInvalid -> NameInvalid.also { navigator.goTo(response) }
}
}
.startWith(NameValid(event.newName))
}
)
}
.mergeWith(appService.profile().map(::ProfileResult))
.scan(ViewModel(stuff = null, prefixedName = PrefixedName("", "")),
BiFunction { previous: ViewModel, result: Result ->
when (result) {
is NameValid -> previous.copy(prefixedName = previous.prefixedName.copy(name = result.name))
is NameInvalid -> previous
is ProfileResult -> previous.copy(
stuff = result.profile,
prefixedName = PrefixedName(result.profile.prefix, result.profile.name)
)
}
})
.subscribe { viewModel ->
nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else ""
nameView.text = viewModel.prefixedName.compute()
// rendering other stuff
}