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. Motivation • Lots of activities (old) • Lots of fragments

    (new) • Loaders • Lifecycle fun hell • Fragment transitions • Back stack The Android Way
  2. 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
  3. Flow A small library that helps with describing an app

    as a collection of moderately independent screens.
  4. 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/
  5. The conversation view screen @Screen(ConversationView.class) public static class Conversation implements

    HasParent<ConversationList> { public final int conversationIndex; ! public Conversation(int conversationIndex) { this.conversationIndex = conversationIndex; } ! @Override public ConversationList getParent() { return new ConversationList(); } }
  6. App start Object firstScreen = new ConversationList(); Backstack backstack =

    Backstack.single(firstScreen); Flow flow = new Flow(backstack, this); go(flow.getBackstack(), Flow.Direction.FORWARD);
  7. On conversation tap Flow flow = ...; int position =

    <index of conversation>; flow.goTo(new Conversation(position));
  8. • 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
  9. • 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
  10. Mortar • View and controller destroyed in traditional android MVC

    • Only view destroyed with MVP under mortar. Presenter stays around. MVC MVP
  11. Mortar • Every Screen has an associated Dagger Module •

    Every View has an associated ViewPresenter Implementation http://animals.desktopnexus.com/wallpaper/1386356/
  12. Blueprint @Screen(ChatListView.class) public class ChatListScreen implements Blueprint { @Override public

    String getMortarScopeName() { return getClass().getName(); } @Override public Object getDaggerModule() { return new Module(); } }
  13. 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); } }
  14. 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
  15. 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