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

Intro to Flow and Mortar

Intro to Flow and Mortar

Two awesome libraries by Square that I used to build SUPER (super.me).

Thorben Primke

May 11, 2015
Tweet

More Decks by Thorben Primke

Other Decks in Programming

Transcript

  1. Highlights • Background • Fragments vs Views • Flow /

    Mortar • Deep dive • Flow / Mortar Helpers
  2. Jelly /SUPER • Jelly was built using Flow + Mortar

    • Made building a unique and animated UI • Discussed Fragments vs Flow + Mortar
  3. Fragments vs Plain Views • Fragments and nested fragments •

    Complicated lifecycle • Suppose to make things easier and reusable • Be cautious checking if the state is valid whenever switching fragments • Can be clutted with both view and business logic
  4. Flow • Used to describe screens and handle the navigation

    between them • Each screen is described by a state • Handles screen history • Handles saving the “screens” or paths into the Bundle
  5. Flow • Flows can be extended to handle to nested

    / sub flows • Nested flows - Similar to nested Fragments when used to encapsulate a specific flow within an app. • Nested flows are better because a parents flow and inject data into the child flow such as the state during a compose flow
  6. Mortar • Overlay on the application life cycle • Integrates

    with the Context’s getSystemService • Dependency injection • Provides global access to the persistence bundle • MortarScope
  7. Mortar • BundleService - Any class has access to the

    Activity’s persistence bundle • Provides a Presenter / ViewPresenter • Integrates with Bundler • Can easily persist the Presenter data (onSave method)
  8. Flow And Mortar Together • Screens all extend: Path •

    Each Screen in turn has its own Module (adds to Object Graph) • This module is added to a parent Module and its contents can be injected in the Presenter and View • Per MVP concept, both Presenter and View have an instance of one another • The View notifies the Presenter when it is ready • The injected Model is used by the Presenter to init the View
  9. Flow Navigation • Provides navigation for setting / adding to

    the history • Provides back navigation in case there is more than one screen on the history stack
  10. Screen Transitions • No default screen animations are provided •

    Flow’s PathContainer needs to be extended • Implement PathContainer’s performTraversal method
  11. Flow History / State Persistence • Flow provides both the

    Flow and FlowDelegate classes • Flow holds the navigation history • Each Entry contains the Screen/Path object • FlowDelegate propagates a onSaveInstanceState call to the Flow which persists the history • Each Screen/Path is converted into a Parcelable object • Parcelable converter could serialize the Path into JSON
  12. Lifecycle Events • Sometimes its necessary to have access to

    onPause, onResume, onActivityResult • Examples: Sign on integrations, external resizing activity, custom Camera integration • Easy way - set explicit callbacks on *Your*Activity • Better - Manager that Presenters can register and unregister for lifecycle events
  13. Settings • Leverage injection to provide a setting to any

    class • Unique Annotation for each setting • Separate SettingsModule that is included in the RootModule • No SettingsManager needed and each settings value can be provided as needed
  14. Good (and Bad) - No support for new Material style

    view animations - Both projects are still actively changing + Presenters could be tested + Mortar with Dagger make DI easy - Screen/Path’s modules (or components)
  15. Questions? Thorben Primke www.super.me
 [email protected] - @tprimke • Flow -

    https://github.com/square/flow • Mortar - https://github.com/square/mortar • Example - In the Mortar repo • Flow / Mortar Helpers - https://github.com/ thorbenprimke/flow-mortar-helpers