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 Slide

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

    View Slide

  3. Motivation

    View Slide

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

    View Slide

  5. 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

  9. 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 Slide

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

    View Slide

  11. 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 Slide

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

    View Slide

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

    View 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
    http://www.catster.com/molz/happy-cat-is-happy

    View Slide

  15. • 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 Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  20. 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 Slide

  21. 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 Slide

  22. 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 Slide