Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Composite ViewModels

Composite ViewModels

ViewModels are the brains behind our views. Growing in length as the view complexity increases, threatening to become monolithic madness with every added line. In this talk we'll explore the concept of Composite ViewModels, a strategy to break down those monsters into less scary bits.

Nicole Terc

August 26, 2020
Tweet

More Decks by Nicole Terc

Other Decks in Programming

Transcript

  1. ViewModels View Model ViewModel Basic principles: • Handle all the

    business logic in the ViewModel • Keep the View as dumb as possible and handle only UI logic
  2. ViewModels - Unidirectional Data Flow Pattern Two basic concepts: •

    View State ◦ Holds the data that will be used to show content in the View. ◦ Ej: LoadingState, DataLoadedState, etc. • Events
  3. ViewModels - Unidirectional Data Flow Pattern Two basic concepts: •

    View State ◦ Holds the data that will be used to show content in the View. ◦ Ej: LoadingState, DataLoadedState, etc. • Events ◦ User & system triggered actions. ◦ Ej: button clicks, lifecycle events (onResume, onStop...), etc.
  4. ViewModels - Unidirectional Data Flow Pattern View Creates Event Streams

    Observes View State & reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State
  5. Composite ViewModels Article by the same name from Sergej Shafarenka.

    Link here: https://www.halfbit.de/posts/composite-viewmodel/
  6. Normal ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State
  7. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State
  8. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State Load Delegate Click Delegate
  9. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State Load Delegate Load Event Click Delegate Click Event
  10. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Handles Events & reacts to them Updates View State Events State Load Delegate Load Event Click Delegate Click Event
  11. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Updates View State Events State Load Delegate Load Event Click Delegate Click Event
  12. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Updates View State Events State Load Delegate State Load Event Click Delegate State Click Event
  13. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Updates View State Events State Load Delegate State Load Event Click Delegate State Click Event
  14. Composite ViewModel View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Dispatch state updates Events State Load Delegate State Load Event Click Delegate State Click Event
  15. Why is this nice? • Smaller classes. • Each delegate

    only handles the events that has implemented. • Adding new functionality just adds more delegates, the VM remains the same.
  16. Small note: The Knot way Reactive State Container Library for

    Android https://github.com/beworker/knot/
  17. Channel Implementation View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Dispatch state updates Events State Load Delegate State Load Event Click Delegate State Click Event
  18. Channel Implementation View Creates Event Streams Observes View State &

    reacts to its changes. ViewModel Dispatch the events Dispatch state updates Events State Load Delegate State Load Event Click Delegate State Click Event
  19. Wanna learn more? • Unidirectional Data Flow Pattern ◦ Android

    Unidirectional Data Flow with LiveData: https://medium.com/hackernoon/android-unidirectional-flow-with-livedata-bf24119 e747 • Composite ViewModel ◦ Talk Code Repository: https://github.com/nicole-terc/compositeVMChannel ◦ Sergej Shafarenka Article: https://www.halfbit.de/posts/composite-viewmodel/#composite-viewmodel-1 • Knot library https://github.com/beworker/knot/