milos marinkovic "modular android ui"

41

Upload: it-event

Post on 23-Jan-2018

251 views

Category:

Education


8 download

TRANSCRIPT

Page 1: Milos Marinkovic "Modular Android UI"
Page 2: Milos Marinkovic "Modular Android UI"

A visual introduction

What does it mean to have a Modular UI?

Page 3: Milos Marinkovic "Modular Android UI"

Responsive layouts

Page 4: Milos Marinkovic "Modular Android UI"

Adjust padding and View positions on larger screens

Page 5: Milos Marinkovic "Modular Android UI"

Different item lists on different devices

Page 6: Milos Marinkovic "Modular Android UI"

List presented as Grid on larger screens

Page 7: Milos Marinkovic "Modular Android UI"

Runtime-configurable layouts

Page 8: Milos Marinkovic "Modular Android UI"

Different list arrangement on larger screens

Page 9: Milos Marinkovic "Modular Android UI"

Displaying the same content in different containers

Page 10: Milos Marinkovic "Modular Android UI"

Compress layout vertically in split-screen scenario

Page 11: Milos Marinkovic "Modular Android UI"

Different navigation on different devices

Page 12: Milos Marinkovic "Modular Android UI"

Architecture & Layering

Page 13: Milos Marinkovic "Modular Android UI"

Android SDK helps.. with some stuff

● Screen sizes: small, normal, large, xlarge● Screen aspect ratio: long, notlong● Device orientation: land, port● Screen densities: ldpi, mdpi, hdpi, xhdpi,

xxhdpi, xxxhdpi● Density pixels (dp) instead of pixels (px)● Precise screen sizes: w__dp, h__dp, sw__dp

Page 14: Milos Marinkovic "Modular Android UI"

Phones

Page 15: Milos Marinkovic "Modular Android UI"

Small Tablets (7”, 8”)

Page 16: Milos Marinkovic "Modular Android UI"

Large Tablets

Page 17: Milos Marinkovic "Modular Android UI"

Android layouts

● Less layouts; more styles● Print and slice UI on paper● Sub-layouts belong to static containers● Use phone UI in tablet containers

Page 18: Milos Marinkovic "Modular Android UI"

MVC

Model:View:Controller

● Model - SQLite, Shared prefs

● View - main_activity.xml, ListFragment.java

● Controller - MyController.java, MainActivity.java (don’t)

Page 19: Milos Marinkovic "Modular Android UI"

MVVM

Model:View:ViewModel

● Model - SQLite, Shared prefs

● View - main_activity.xml, ViewGroup or View,

AccountListFragment.java● ViewModel - AccountListFragmentUiModel.java

Page 20: Milos Marinkovic "Modular Android UI"

MVP

Model:View:Presenter

● Model - SQLite, Shared prefs

● View - main_activity.xml, ViewGroup or View

● Presenter - MainActivity.java ListFragment.java

Page 21: Milos Marinkovic "Modular Android UI"

MVP - Variations

Passive View

● View not aware of the Model● Presenter does the hard work

Supervising Controller

● View interacts directly with the Model● Presenter handles extreme cases

Page 22: Milos Marinkovic "Modular Android UI"

Components for Android

Page 23: Milos Marinkovic "Modular Android UI"

How we dividedAndroid components

Page 24: Milos Marinkovic "Modular Android UI"

Lifecycle

● Service, Activity, Fragment, Dialog, Application - all different

● Create a custom UI entity? I do it anyway● Unreliability, bugs, appcompat, gotchas● API levels…● Fragment caching mechanism…

Solution: Move away from Android APIs?

Page 25: Milos Marinkovic "Modular Android UI"

Fragment lifecycle...

Page 26: Milos Marinkovic "Modular Android UI"

Android Lists with RecyclerView

● Decouple!● Android: Recycler View, Adapter, Layout

Manager, Item Animator, Item Decorator, Recycled Pool, Cache, Item Decoration

Advice: Split “adapter” component (1) data provider and (2) recycler adapter

Page 27: Milos Marinkovic "Modular Android UI"

Recycler overview

Page 28: Milos Marinkovic "Modular Android UI"

Asynchronous work

● UI events must go to a UI thread● Long operations go on a background thread● Thread or AsyncTask or RxJava● Events? Otto or GreenRobot or others● A swarm of events

Advice: Keep it minimal for your needs

Page 29: Milos Marinkovic "Modular Android UI"

Self-contained UI parts (Pages)

● Keep pages focused on the UI● Presenters prepare data for pages● Pages handle dialogs internally● Complex dialogs? Contain pages inside them● Persistent presenters or Presenter pool?

Advice: Instantiate in one place

Page 30: Milos Marinkovic "Modular Android UI"

Android Activity

● Context is everywhere● Difficult to decouple● Components: LoaderManager, Cursor,

FragmentManager, Fragment backstack, Fragment caching, Content Providers, Intents, Menus, etc.

Advice: It’s worth decoupling, try to do it

Page 31: Milos Marinkovic "Modular Android UI"

Intents

● Handle all intents in one place● Allow the IntentHandler to navigate● Use an ActivityResolver● Allow easy Activity switching

Advice: React in onResume() method if possible

Page 32: Milos Marinkovic "Modular Android UI"

Connecting the dots

Page 33: Milos Marinkovic "Modular Android UI"

Rules for a ‘happy’ UI

● Enumerate pages somehow (enum optimized?)● Pages need a common navigation API● Framework creates (and caches) pages● Follow the Activity lifecycle

Example of a ‘happy’ UI code:

navigate().with(myBundle).to(Pages.LOGIN);

Page 34: Milos Marinkovic "Modular Android UI"

Show and hide pages (Page Manager)

● Static containers or dynamic containers● Watch out: component lifecycles● Decoupled mechanism for show/hide● The dark pit of FragmentManager

Advice: Small app - use FragmentManager Big app - build your own PageManager

Page 35: Milos Marinkovic "Modular Android UI"

Layout and navigation (The Coordinator)

● Decoupled navigation & layout component● Uses the PageManager to show/hide● Handles back and “up”● Holds the IntentHandler component

Advice: Make Coordinator also a page, a ‘super-page’ that holds all other pages

Page 36: Milos Marinkovic "Modular Android UI"

Visible UI on Phones Actual layers on Phones

The Coordinator

Content Page

Navigation drawer page

Page 37: Milos Marinkovic "Modular Android UI"

The CoordinatorBranding barList pageDetails pageFAB

List PageToolbar & menuList - Item photo - Item name

Details PageToolbar & menuLarge titleContent

Actual layers on Tablets

Page 38: Milos Marinkovic "Modular Android UI"

Page Modularity

External configuration causes trouble

● A lot of if-else branches in pages● Custom Views rule the app (learning curve)

Solution: Have a distinct page for each UIAdvice: Create a PageResolver component

Page 39: Milos Marinkovic "Modular Android UI"

Drawbacks

● A lot of decoupling and cosmetic work● A lot of method references● Difficult to switch to● Makes sense only for complex UIs● Team education● Limited support (open-source?)

Page 40: Milos Marinkovic "Modular Android UI"

To recap...

● Self-maintained UI components (pages)● Data is always prepared (presenters)● Intent handling happens in one place● Page Resolver helps with modular UIs● Separate navigation mechanism● Wrap it all up in a Coordinator● Have a Coordinator for each screen variant

Page 41: Milos Marinkovic "Modular Android UI"

Finally done.Modular Android UI

GitHub / Twitter / LinkedIn

@ milosmns

Blog

www.angrybyte.me

Questions?