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

Effective Android Architecture

B1b15a8df73879b1d5eb247fee2028eb?s=47 Richa
May 09, 2015

Effective Android Architecture

B1b15a8df73879b1d5eb247fee2028eb?s=128

Richa

May 09, 2015
Tweet

Transcript

  1. EFFECTIVE ANDROID ARCHITECTURE

  2. Hi, I am Richa

  3. Hi, I am Kelvin

  4. EFFECTIVE ANDROID ARCHITECTURE

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

  11. MVC

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

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

  15. None
  16. 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
  17. Model public class CourseModel { Course getFromDatabase() { // fetch

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

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

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

  23. 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
  24. Interactor?

  25. JUnit + Mocking

  26. 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
  27. View Model ?

  28. JUnit + Mocking Android + Mocking Espresso + Mocking

  29. 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
  30. Presenter ?

  31. 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
  32. Espresso + Mocking JUnit + Mocking JUnit + Mocking Android

    + Mocking Data Source
  33. 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
  34. FlowController ?

  35. Android + Mocks

  36. 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
  37. None
  38. Apply Dependency Inversion

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

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

  44. None
  45. Implementation in Practice

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

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

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

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

  50. None
  51. The Cons

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

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

  54. Adopting as a team Educating new hires

  55. Key Takeaways

  56. Ship the latest ideas

  57. Refactor or Not?

  58. Validate

  59. Iterate, Iterate, Iterate

  60. Code Demo - Sample app

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

  62. Classes • MainActivity • CatalogFlowController • CatalogActivity • CatalogPresenter •

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

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

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

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

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

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

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

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

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

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

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

  76. Questions ?

  77. @richa123 rkhandelwal@coursera.org, kwong@coursera.org richk in/richak , in/kelvinswwong Thank You