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

Clean Android Architecture

B1b15a8df73879b1d5eb247fee2028eb?s=47 Richa
April 10, 2015

Clean Android Architecture

Diving into problems with MVC and refactoring it into Clean Architecture - VIPER + MVVM, and talking about pros and cons of this architecture.

B1b15a8df73879b1d5eb247fee2028eb?s=128

Richa

April 10, 2015
Tweet

Transcript

  1. CLEAN ARCHITECTURE

  2. Richa Khandelwal rkhandelwal@coursera.org @richa123

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

  9. None
  10. None
  11. MODEL VIEW CONTROLLER Interact with DB Render Model Handle view

    events Update model Invoke next UI
  12. Mobile Web Desktop Mobile is diffrent, don’t generalize based on

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

    events Interact with API/cloud code Manage view state Update model Invoke another view Interact with system components Handle system events Update view based on system events
  15. Model public class CourseModel { public Course getFromDatabase() { ….

    } public void getFromNetwork() { …. } }
  16. View public class ExpandableCourseListActivity { public void onPause() { //

    save all expanded states } public void onResume() { // restore all expanded states } }
  17. Controller public void onClick() {} public void onModelUpdated() {} public

    void onNewNotification() {} public void onLocationUpdated(Location location) {} public void takePhoto(Activity activityContext) {} public void launchProfilePage(Activity activityContext ) {}
  18. None
  19. None
  20. None
  21. None
  22. MODEL VIEW CONTROLLER Interact with DB Render Model Handle view

    events Interact with API/cloud code Manage view state Update model Invoke another view Interact with system components Handle system events Update view based on system events
  23. Interactor?

  24. JUnit + Mocking

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

    Handle view events Interact with external entities Retrieve data from API/cloud code Manage view state Invoke another view 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 Encapsulate view state and behavior Retrieve data from API/ cloud code Invoke another view Update view state
  29. Presenter ?

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

    Render View Model Handle view events Interact with external entities Encapsulate view state and behavior Retrieve data from API/ cloud code Invoke another view Update view state
  31. Espresso + Mocking JUnit + Mocking JUnit + Mocking Android

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

    Sources Render View Model Handle view events Interact with external entities Encapsulate view state and behavior Invoke another view Update view state
  33. Router ?

  34. Android + Mocks

  35. ENTITIES VIEW PRESENTER INTERACTOR VIEW MODEL ROUTER Retrieve data from

    Data Sources Render View Model Handle view events Interact with external entities Encapsulate view state and behavior Invoke another view Update view state
  36. None
  37. Apply Dependency Inversion

  38. None
  39. None
  40. None
  41. Clean Android Architecture Architecture is about Intents not Frameworks -

    Uncle Bob
  42. None
  43. App to list courses on coursera using Catalog API Github

    Link -
  44. Classes • MainActivity • CatalogRouter • CatalogActivity • CatalogPresenter •

    CatalogViewModel • CatalogInteractor
  45. MainActivity public void onCreate() { CatalogRouter.getInstance().launchCatalogActivity(); }

  46. CatalogRouter public void launchCatalogActivity(Context context) { Intent intent = …;

    context.startActivity(intent) }
  47. CatalogActivity CatalogPresenter presenter; Action1<Throwable> onError = new Action1<Throwable>() { public

    void call(Throwable throwable) { // handle error } });
  48. CatalogActivity public void onCreate() { presenter = new CatalogPresenter(this); presenter.loadCatalog(onError);

    }
  49. CatalogActivity public void onResume() { Action1<Catalog> catalogUpdateAction = ; presenter.subscribeToCatalogUpdates(catalogUpdat

    eAction) }
  50. CatalogActivity public void onPause() { presenter.unsubscribeToCatalogUpdates(); }

  51. CatalogPresenter CatalogInteractor interactor; public CatalogPresenter(Context context) { viewModel = new

    CatalogViewModel(); interactor = new CatalogInteractor(context) }
  52. CatalogPresenter public void loadCatalog() { interactor.loadInteractor() .subscribe(new Action1<List<Course>>() { @Override

    public void call(List<Course> courses) { getData().mCourseList.onNext(courses); } }); }
  53. CatalogPresenter public void subscribeToCatalogUpdates(Action1 action) { viewModel.subscribeToUpdates(action); } public void

    unsubscribeToCatalogUpdates() { viewModel.subscribeToUpdates(); }
  54. CatalogViewModel public final BehaviorSubject<Catalog> catalog = BehaviorSubject.create(); public void subscribeToCatalog(Action1

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

    } public Observable loadCatalog() { return catalogDataSource.loadCatalog(); }
  56. None
  57. None
  58. None
  59. None
  60. Dealing with Context

  61. After thoughts

  62. Build features in new architecture rather than refactor Validate Iterate

    - learn and apply Don’t refactor all the time
  63. None
  64. Coursera is hiring!

  65. Questions ? Thank You!