Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Effective Reactive Architecture

Effective Reactive Architecture

Video: https://youtu.be/7fE5MDQ_Sc4

Your relationship with RxJava doesn't have to be complicated. You may have found many ways to use it wrong but it is powerful when used properly.In fact, RxJava can guide you in shaping a sound architecture for your app. To do this, you only have to follow a few but decisive principles.

In this talk, you'll:
- Explore how side-effect isolation can help avoiding bugs.
- Learn how to model your data and share it as one unique stream between your view and your presenter.
- Discover the ways a unidirectional data flow makes adding new functionality easy and safe.
- Look at how you can easily deal with collateral side effects, such as navigation.

After this talk, you’ll be able to write a robust and reactive architecture for your app, taking full advantage of RxJava.

Benoît Quenaudon

October 24, 2019
Tweet

More Decks by Benoît Quenaudon

Other Decks in Programming

Transcript

  1. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { } }
  2. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { nameSubject .switchMap { name -> appService.profile() .map { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) } }
  3. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { nameSubject .switchMap { name -> appService.profile() .map { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) } }
  4. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { nameSubject .switchMap { name -> appService.profile() .map { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } }
  5. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { nameSubject .switchMap { name -> appService.profile() .map { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } }
  6. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { nameSubject .switchMap { name -> appService.profile() .map { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) }
  7. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) } }
  8. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  9. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  10. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  11. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  12. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  13. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  14. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  15. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  16. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  17. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  18. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  19. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  20. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  21. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  22. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  23. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  24. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  25. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  26. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  27. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  28. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  29. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  30. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  31. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  32. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  33. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  34. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  35. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  36. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  37. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  38. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  39. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  40. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  41. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  42. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  43. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  44. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  45. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  46. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  47. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  48. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  49. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  50. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  51. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  52. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  53. fun onStart() { nameSubject .switchMap { name -> appService.profile() .map

    { profile -> profile.prefix } .map { profile -> if (name.isEmpty()) "" else "$prefix $name" } } .subscribe(nameView::setText) // hint grows the view in width so hide it if unnecessary nameSubject .map { name -> if (name.isEmpty()) "Set your name" else "" } .subscribe(nameView::setHint) appService.profile().subscribe(this::populateProfile) } fun populateProfile(profile: Profile) { nameSubject.onNext(profile.name) // rendering other stuff } fun onSetNameDialogResult(newName: String) = setName(newName) fun setName(newName: String) { nameSubject.onNext(newName) appService.setName(newName) .subscribe { response -> when { response.isValid -> nameSubject.onNext(response.name) response.isInvalid -> navigator.goTo(response.error) } } }
  54. class ProfileView { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { } }
  55. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { } }
  56. sealed class Result { data class ProfileResult(val profile: Profile) :

    Result() data class NameValid(val name: String) : Result() object NameInvalid : Result() }
  57. data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName )

    { data class PrefixedName( val prefix: String, val name: String ) { fun compute(name:String): String = if (name.isEmpty()) "" else "$prefix $name" } }
  58. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() fun onStart() { } }
  59. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> } }
  60. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> .subscribe { viewModel -> } } }
  61. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  62. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .switchMap { event -> } ) } .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  63. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .switchMap { event -> appService.setName(event.newName).map { response -> when { response.isValid -> NameValid(response.name) response.isInvalid -> NameInvalid.also { navigator.goTo(response) } } } } ) } .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  64. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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)) } ) } .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } }
  65. class ProfileViewRemix { val navigator: Navigator val nameView: TextView val

    appService: AppService val nameSubject = BehaviorSubject.create<String>() val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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)) .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  66. val nameView: TextView val appService: AppService val nameSubject = BehaviorSubject.create<String>()

    val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 -> }) .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  67. val nameView: TextView val appService: AppService val nameSubject = BehaviorSubject.create<String>()

    val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 -> }) .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  68. val nameView: TextView val appService: AppService val nameSubject = BehaviorSubject.create<String>()

    val events = PublishSubject.create<Event>() fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 -> }) .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  69. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } }) .subscribe { viewModel -> nameView.hint = if (viewModel.prefixedName.name.isEmpty()) "Set your name" else "" nameView.text = viewModel.prefixedName.compute() // rendering other stuff } } }
  70. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 }
  71. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  72. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  73. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  74. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  75. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  76. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  77. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  78. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  79. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  80. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  81. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  82. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  83. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  84. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  85. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  86. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  87. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  88. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  89. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  90. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  91. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  92. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  93. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  94. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  95. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  96. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  97. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  98. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  99. fun onStart() { events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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 } } fun onSetNameDialogResult(newName: String) { events.onNext(EditName(newName)) }
  100. events .subscribe { viewModel -> } class ProfilePresenter : Consumer<Event>

    { override fun accept(event: Event) { } } View Presenter
  101. class ProfilePresenter : Consumer<Event> { override fun accept(event: Event) {

    event.flatMap {} } } View Presenter events.subscribe(presenter) presenter.something().subscribe { viewModel -> }
  102. presenter.viewModels(events) .subscribe { viewModel -> } class ProfilePresenter { fun

    viewModels(events: Observable<Event>) { events.publish { events -> } } } View Presenter
  103. presenter.viewmodels() .subscribe { viewModel -> } class ProfilePresenter { fun

    viewmodels(): Observable<ViewModel> { } } View Presenter
  104. presenter .subscribe { viewModel -> } class ProfilePresenter: Observable<ViewModel>() {

    override fun subscribeActual(observer: Observer<in ViewModel>) { } } View Presenter
  105. presenter .subscribe(object : Observer<ViewModel> { override fun onComplete() {} override

    fun onSubscribe(d: Disposable) {} override fun onNext(t: ViewModel) {} override fun onError(e: Throwable) {} }) class ProfilePresenter: ObservableSource<ViewModel> { override fun subscribe(observer: Observer<in ViewModel>) { } } View Presenter
  106. Observable.wrap(presenter) .subscribe { viewModel -> } class ProfilePresenter: ObservableSource<ViewModel> {

    override fun subscribe(observer: Observer<in ViewModel>) { } } View Presenter
  107. interface ObservableTransformer<Upstream, Downstream> { /** * Applies a function to

    the upstream Observable and returns an ObservableSource with * optionally different element type. */ fun apply(upstream: Observable<Upstream>): ObservableSource<Downstream> }
  108. class Presenter: ObservableTransformer<Event, ViewModel> { fun apply(events: Observable<Event>): ObservableSource<ViewModel> }

    val testObserver = events // PublishSubject<Event> .compose(presenter) .test() // TestObserver<ViewModel> Test Presenter
  109. class Presenter: ObservableTransformer<Event, ViewModel> { fun apply(events: Observable<Event>): ObservableSource<ViewModel> }

    val testObserver = events // PublishSubject<Event> .compose(presenter) .test() // TestObserver<ViewModel> events.onNext(EditName("Georges")) testObserver.assertValue(ViewModel()) Test Presenter
  110. Presenter • Don't break the stream • Don't start work

    until observed • Doesn't handle disposables* • Has one entry point • Has one exit point
  111. class ProfilePresenter { val events = PublishSubject.create<Event>() fun passEvents(events: Observable<Event>)

    { events.subscribe(this.events) } fun viewModels() { return events.businessLogic() } } View Presenter
  112. class ProfilePresenter { val events = PublishSubject.create<Event>() fun passEvents(events: Observable<Event>)

    { events.subscribe(this.events) } private val vmObservable : Observable<MatchesViewState> by lazy(NONE) { events.businessLogic() .replay(1) .autoConnect(0) } fun viewModels() = vmObservable } View Presenter
  113. presenter.viewModels() .subscribe { viewModel -> } View Presenter class ProfilePresenter

    { val events = PublishSubject.create<Event>() fun passEvents(events: Observable<Event>) { events.subscribe(this.events) } private val vmObservable : Observable<MatchesViewState> by lazy(NONE) { events.businessLogic() .replay(1) .autoConnect(0) } fun viewModels() = vmObservable }
  114. presenter.viewModels() .subscribe { viewModel -> } presenter.passEvents(events) View Presenter class

    ProfilePresenter { val events = PublishSubject.create<Event>() fun passEvents(events: Observable<Event>) { events.subscribe(this.events) } private val vmObservable : Observable<MatchesViewState> by lazy(NONE) { events.businessLogic() .replay(1) .autoConnect(0) } fun viewModels() = vmObservable }
  115. sealed class Event { data class EditName(val newName: String) :

    Event() } data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName ) class ProfileViewRemix { fun onStart() { events .compose(presenter) .subscribe { viewModel -> // rendering view model } } }
  116. sealed class Event { data class EditName(val newName: String) :

    Event() } data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName ) class ProfileViewRemix { fun onStart() { events .compose(presenter) .subscribe { viewModel -> // rendering view model } } }
  117. sealed class Event { data class EditName(val newName: String) :

    Event() data class ChangeAvatar(val picture: File) : Event() } data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName ) class ProfileViewRemix { fun onStart() { events .compose(presenter) .subscribe { viewModel -> // rendering view model } } }
  118. sealed class Event { data class EditName(val newName: String) :

    Event() data class ChangeAvatar(val picture: File) : Event() } data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName, val avatarUri: Uri ) class ProfileViewRemix { fun onStart() { events .compose(presenter) .subscribe { viewModel -> // rendering view model } } }
  119. sealed class Event { data class EditName(val newName: String) :

    Event() data class ChangeAvatar(val picture: File) : Event() } data class ViewModel( val stuff: Any?, val prefixedName: PrefixedName, val avatarUri: Uri ) class ProfileViewRemix { fun onStart() { events .compose(presenter) .subscribe { viewModel -> // rendering view model Picasso.get().load(viewModel.avatarUri).into(avatarView) } } }
  120. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>() .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) ) } }) } }
  121. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>().logic() ) } .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) ) } }) } }
  122. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>().logic() events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } } ) } .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) ) } }) } }
  123. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>().logic() events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } .onErrorReturn { throwable -> AvatarInvalid(throwable) } } ) } .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) ) } }) } }
  124. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>().logic() events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } .onErrorReturn { throwable -> AvatarInvalid(throwable) } } ) } .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) ) is AvatarValid -> previous.copy(avatarUri = result.uri) } }) } }
  125. class ProfilePresenter : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>):

    ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<EditName>().logic() events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } .onErrorReturn { throwable -> AvatarInvalid(throwable) } } ) } .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) ) is AvatarValid -> previous.copy(avatarUri = result.uri) is AvatarInvalid -> TODO() } }) } }
  126. Architecture forced us to separate concerns No need to touch

    existing code Easy to test Easy to review
  127. events.filterIsInstance<ChangeAvatar>().changeAvatar() private fun Observable<ChangeAvatar>.changeAvatar(): Observable<Result> { return switchMap { event

    -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } } } events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } }
  128. events.filterIsInstance<ChangeAvatar>().changeAvatar() private fun Observable<ChangeAvatar>.changeAvatar(): Observable<Result> { return switchMap { event

    -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } } } events.filterIsInstance<ChangeAvatar>() .switchMap { event -> appService.changeAvatar(event.picture) .map { response -> AvatarValid(response.uri) } }
  129. interface Navigator { fun goTo(screen: Any) } interface Launcher {

    fun shareText(text: String): Boolean fun launchUrl(url: String): Boolean fun launchApp(packageName: String): Boolean }
  130. interface Navigator { fun goTo(screen: Any) } class ProfilePresenter( private

    val navigator: Navigator ) : ObservableTransformer<Event, ViewModel> { }
  131. interface Navigator { fun goTo(screen: Any) } class ProfilePresenter( private

    val navigator: Navigator ) : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>): ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<GoToEvent>().goTo() ) } } }
  132. interface Navigator { fun goTo(screen: Any) } class ProfilePresenter( private

    val navigator: Navigator ) : ObservableTransformer<Event, ViewModel> { override fun apply(events: Observable<Event>): ObservableSource<ViewModel> { return events.publish { events -> Observable.merge( events.filterIsInstance<GoToEvent>().goTo() ) } } private fun Observable<GoToEvent>.goTo(): Observable<ViewModel> { return doOnNext { navigator.goTo(SomeScreen) } .ignoreElements() .toObservable() } }
  133. inline fun <T : Any, R : Any> Observable<T>.consumeOnNext( crossinline

    sideEffect: (T) -> Unit ): Observable<R> { return doOnNext { sideEffect(it) } .ignoreElements() .toObservable() } private fun Observable<GoToEvent>.goTo(): Observable<ViewModel> { return consumeOnNext { navigator.goTo(SomeScreen) } }
  134. class FakeNavigator : Navigator { private val navigatedScreens = ArrayDeque<Any>()

    override fun goTo(screen: Any) { navigatedScreens.add(screen) } fun takeNext() = navigatedScreens.pop()!! fun isEmpty() = navigatedScreens.isEmpty() } events.accept(Event) assertThat(navigator.takeNextScreen()).isEqualTo(ExpectedScreen)