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

Android Apps with Mortar and Flow

Edward Dale
January 21, 2014

Android Apps with Mortar and Flow

Edward Dale

January 21, 2014
Tweet

More Decks by Edward Dale

Other Decks in Programming

Transcript

  1. Android Apps
    with
    Mortar and Flow
    Edward Dale
    @scompt

    View full-size slide

  2. Agenda
    • Motivation
    • Flow
    • Mortar
    http://knowyourmeme.com/memes/business-cat

    View full-size slide

  3. Motivation
    • Lots of activities (old)
    • Lots of fragments (new)
    • Loaders
    • Lifecycle fun hell
    • Fragment transitions
    • Back stack
    The Android Way

    View full-size slide

  4. Motivation
    • Lots of activities (old)
    • Lots of fragments (new)
    • Loaders
    • Lifecycle fun hell
    • Fragment transitions
    • Back stack
    The Android Way
    • Few activities
    • No fragments
    • Fatter views
    • Dependency injection
    • RxJava
    The Square Way

    View full-size slide

  5. Sample
    App
    https://github.com/square/flow

    View full-size slide

  6. Sample
    App
    https://github.com/square/flow

    View full-size slide

  7. Flow
    A small library that helps with describing an app
    as a collection of moderately independent
    screens.

    View full-size slide

  8. Flow
    • A screen in a POJO with the
    @Screen annotation saying
    which view or layout should
    be used to display it.
    • The Backstack is a stack of
    these objects.
    • The Flow is the interface for
    navigating the history of your
    app.
    http://www.rluxemburg.com/2013/09/01/kitten-flow-chart/

    View full-size slide

  9. The conversation list screen
    @Screen(ConversationListView.class)
    public class ConversationList {
    }

    View full-size slide

  10. The conversation view
    screen
    @Screen(ConversationView.class)
    public static class Conversation
    implements HasParent {
    public final int conversationIndex;
    !
    public Conversation(int conversationIndex) {
    this.conversationIndex = conversationIndex;
    }
    !
    @Override public ConversationList getParent() {
    return new ConversationList();
    }
    }

    View full-size slide

  11. App start
    Object firstScreen = new ConversationList();
    Backstack backstack = Backstack.single(firstScreen);
    Flow flow = new Flow(backstack, this);
    go(flow.getBackstack(), Flow.Direction.FORWARD);

    View full-size slide

  12. On conversation tap
    Flow flow = ...;
    int position = ;
    flow.goTo(new Conversation(position));

    View full-size slide

  13. • Controller layer is a bit 

    too ad-hoc
    But not quite enough
    This is nice
    • No fragments
    • Unified interface for
    navigating in the app
    • Power over the back stack
    http://www.catster.com/molz/happy-cat-is-happy

    View full-size slide

  14. • Controller layer is a bit 

    too ad-hoc
    But not quite enough
    This is nice
    • No fragments
    • Unified interface for
    navigating in the app
    • Power over the back stack

    View full-size slide

  15. Mortar
    Mortar eases the use of Dagger to divide Android
    apps into composable modules.

    View full-size slide

  16. Mortar
    • View and controller destroyed in traditional
    android MVC
    • Only view destroyed with MVP under mortar.
    Presenter stays around.
    MVC MVP

    View full-size slide

  17. Mortar
    • Every Screen has an
    associated Dagger Module
    • Every View has an associated
    ViewPresenter
    Implementation
    http://animals.desktopnexus.com/wallpaper/1386356/

    View full-size slide

  18. Blueprint
    @Screen(ChatListView.class)
    public class ChatListScreen implements Blueprint {
    @Override public String getMortarScopeName() {
    return getClass().getName();
    }
    @Override public Object getDaggerModule() {
    return new Module();
    }
    }

    View full-size slide

  19. ViewPresenter
    public class ChatListView extends ListView
    implements ChatListScreen.View {
    @Inject ChatListScreen.Presenter presenter;
    !
    public ChatListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    Mortar.inject(context, this);
    }
    !
    @Override protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    presenter.takeView(this);
    }
    }

    View full-size slide

  20. http://www.catster.com/molz/happy-cat-is-happy
    This is nice
    • Screens are self-contained
    and injected, facilitating
    testing
    • Insulated from lifecycle events
    • Memory efficient

    View full-size slide

  21. Mortar
    Flow
    A small library that helps with describing an app as a
    collection of moderately independent screens.
    Mortar eases the use of Dagger to divide
    Android apps into composable modules.
    Questions?
    Edward Dale
    @scompt

    View full-size slide