Effective Android Architecture - MWDC

B1b15a8df73879b1d5eb247fee2028eb?s=47 Richa
July 16, 2015

Effective Android Architecture - MWDC

Yet another talk on Android architecture at MWDC 2015

B1b15a8df73879b1d5eb247fee2028eb?s=128

Richa

July 16, 2015
Tweet

Transcript

  1. EFFECTIVE ANDROID ARCHITECTURE

  2. Hi, I am Richa

  3. EFFECTIVE ANDROID ARCHITECTURE

  4. None
  5. None
  6. None
  7. None
  8. None
  9. Who all has ever used MVC?

  10. MVC

  11. MVP ? MVC ? MVVM ?

  12. MODEL VIEW CONTROLLER Interact with DB Render Model Handle view

    events Update model Invoke next UI Table of Responsibilities
  13. Mobile Web Desktop

  14. None
  15. MODEL VIEW CONTROLLER Interact with DB Render Model Handle view

    events Interact with network Manage view state Update model Navigation Interact with system components Handle system events Update view on system events
  16. Model public class CourseModel { Course getFromDatabase() { // fetch

    from db, or local cache } Course getFromNetwork() { // fetch from apis, cache when possible } }
  17. View public class ExpandableCourseListActivity { void onSaveInstanceState(Bundle b) { //

    save all expanded states } }
  18. Controller void onClick() {} void onModelUpdated() {} void onNewNotification() {}

    void onLocationUpdated(Location loc) {} void takePhoto(Activity context) {} void launchView(Activity context ) {}
  19. None
  20. None
  21. MVC ??

  22. MODEL VIEW CONTROLLER Interact with DB Render Model Handle view

    events Interact with network Manage view state Update model Navigation Interact with system components Handle system events Update view on system events
  23. Interactor?

  24. JUnit + Mocking

  25. MODEL VIEW CONTROLLER INTERACTOR Retrieve data from DB Render Model

    Handle view events Interact with external entities Retrieve data from network Manage view state Navigation Forward system events to view Forward model updates to view
  26. View Model ?

  27. JUnit + Mocking Android + Mocking Espresso + Mocking

  28. MODEL VIEW CONTROLLER INTERACTOR VIEW MODEL Retrieve data from Network

    Render View Model Handle view events Interact with external entities View state / behavior Retrieve data from network Navigation Update view state
  29. Presenter ?

  30. MODEL VIEW PRESENTER INTERACTOR VIEW MODEL Retrieve data from DB

    Render View Model Handle view events Interact with external entities View state / behavior Retrieve data from network Navigation Update view state
  31. Espresso + Mocking JUnit + Mocking JUnit + Mocking Android

    + Mocking Data Source
  32. ENTITIES VIEW PRESENTER INTERACTOR VIEW MODEL Get data from Data

    Sources Render View Model Handle view events Interact with external entities View state / behavior Navigation Update view state
  33. FlowController ?

  34. Android + Mocks

  35. ENTITIES VIEW PRESENTER INTERACTOR VIEW MODEL FLOW CONTROLLER Get data

    from Data Sources Render View Model Handle view events Update view state Interact with external entities View state / behavior Navigation
  36. None
  37. Apply Dependency Inversion

  38. None
  39. None
  40. None
  41. S ingle Responsibility Principle O pen/Closed Principle L iskov Substitution

    Principle I nterface Segregation Principle D ependency Inversion Principle
  42. Architecture is about Intents not Frameworks - Uncle Bob

  43. None
  44. Implementation in Practice

  45. Activity Video Module Activity Quiz Module Navigation Main Activity Shell

    App Catalog Activity Catalog Module url Intent Intent Register with url Modules
  46. Deep links Shell App Video Activity Video Module Catalog Activity

    Catalog Module register register url Intent Intent
  47. ViewModels Save/Restore View Models Presenter Retainable Fragments Parcelables onSaveInstanceState onSaveInstanceState

  48. Chain of Responsibility View Presenter Interactor Network Persistence •Result •Error

  49. None
  50. The Cons

  51. Dealing with context Presenter(Context context) Interactor(Context context) NetworkClient(Context context) PersistenceClient(Context

    context) context.getSystemService(“SERVICE”) context.startActivity(intent) context.startActivityForResult(intent)
  52. Tedium ~ 7 classes per feature/module

  53. Adopting as a team Educating new hires

  54. Key Takeaways

  55. Ship the latest ideas

  56. Refactor or Not?

  57. Validate

  58. Iterate, Iterate, Iterate

  59. Code Demo - Sample app

  60. Source Code https://github.com/richk/ CourseraDemoApp

  61. Classes • MainActivity • CatalogFlowController • CatalogActivity • CatalogPresenter •

    CatalogViewModel • CatalogInteractor
  62. MainActivity void onCreate() { CatalogFlowController.getInstance() .launchCatalogActivity(); }

  63. CatalogFlowController void launchCatalogActivity(Context c) { Intent intent = …; c.startActivity(intent)

    }
  64. CatalogActivity CatalogPresenter presenter; void onCreate() { presenter = new CatalogPresenter(this);

    }
  65. CatalogActivity void onResume() { Action1<Catalog> action = ; presenter.subscribeToUpdates(action) }

  66. CatalogActivity void onPause() { presenter.unsubscribeToUpdates(); }

  67. CatalogPresenter CatalogInteractor interactor; CatalogPresenter(Context c) { viewModel = new CatalogViewModel();

    interactor = new CatalogInteractor(c); loadCatalog(); }
  68. CatalogPresenter void loadCatalog() { interactor.loadCourses() .subscribe(new Action1<>() { @Override public

    void call(List<Course> courses) { // action } }); }
  69. CatalogPresenter void subscribeToUpdates(Action1 a) { viewModel.subscribeToUpdates(a); } void unsubscribeToUpdates() {

    viewModel.unsubscribeToUpdates(); }
  70. CatalogViewModel public final BehaviorSubject<Catalog> catalog = BehaviorSubject.create(); public void subscribeToCatalog(Action1

    action) { catalog.subscribe(action); } public void unsubscribeToCatalog() { catalog.unsubscribe(); }
  71. CatalogInteractor Context mContext; public CatalogInteractor(Context context) { mContext = context;

    } public Observable loadCatalog() { return catalogDataSource.loadCatalog(); }
  72. None
  73. None
  74. Coursera is hiring!

  75. Questions ?

  76. @richa123 rkhandelwal@coursera.org richk in/richak Thank You