Android Architecture

B1b15a8df73879b1d5eb247fee2028eb?s=47 Richa
August 13, 2016

Android Architecture

Android Architecture talk presented at 360Andev

B1b15a8df73879b1d5eb247fee2028eb?s=128

Richa

August 13, 2016
Tweet

Transcript

  1. Effective Android Architecture Richa Khandelwal Growth/Android/Front-End Engineer at Coursera

  2. None
  3. ▪ Challenges ▪ Finding the right architecture fit ▪ Validation

    ▪ Practice makes perfect! ▪ Demo and Samples ▪ Cons ▪ Key Takeaways Structure of the talk
  4. Available on Google Play Store Place your screenshot here Android

    App 1,825+ COURSES 18M+ LEARNERS 141+ PARTNERS
  5. 109% increase in mobile usage since last year 24 %

    use mobile only 40 % use mobile and desktop
  6. Over 500k (and growing) new learners join Coursera every month

  7. Cross team Tightly aligned, loosely coupled Faster MVPs Learn fast,

    fail fast Network Speeds Edge, 2G, Spotty
  8. Who all has ever used MVC?

  9. MVC

  10. Where to Begin

  11. MVP ? MVC ? MVVM ? VIPER ?

  12. MODEL VIEW CONTROLLER Manage application data Render model Handle view

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

  14. None
  15. MODEL VIEW CONTROLLER Manage data from network Render model Handle

    view events Manage data from local storage Manage view state Interact with model Manage model consistency Navigation Interact with system components Interact with system events Update view on model changes or 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. MVC ??

  21. MODEL VIEW CONTROLLER Manage data from local storage Render model

    Handle view events Manage data from network Manage view state Interact with model Manage model consistency Navigation Interact with system components Interact with system events Update view on model changes or system events
  22. Interactor?

  23. None
  24. MODEL VIEW CONTROLLER INTERACTOR Manage data from local storage Render

    view data Handle view events Interact with external entities Manage data from network Manage view data/ updates Navigation Manage model consistency Forward view data updates to view
  25. View Model ?

  26. None
  27. MODEL VIEW CONTROLLER INTERACTOR VIEW MODEL Manage data from local

    storage Render view model Handle view events Interact with external entities Manage view data Manage data from network Navigation Manage model consistency Manage view model
  28. Presenter ?

  29. MODEL VIEW PRESENTER INTERACTOR VIEW MODEL Manage data from local

    storage Render view model Handle view events Interact with external entities Manage view data Manage data from network Navigation Manage model consistency Manage view model
  30. Data Source

  31. ENTITIES VIEW PRESENTER INTERACTOR VIEW MODEL Represent model objects Render

    view model Handle view events Interact with external entities Manage view data Navigation Manage view model
  32. FlowController ?

  33. coursera-android://app/<feature_url>

  34. ENTITIES VIEW PRESENTER INTERACTOR VIEW MODEL FLOW CONTROLLER Represent model

    objects Render view model Manage view events and view model Interact with external entities Manage view data Navigation
  35. Apply Dependency Inversion

  36. Data Source

  37. None
  38. None
  39. Espresso + Mocking JUnit + Mocking Android/ JUnit + Mocking

    Android/ JUnit + Mocks Data Source
  40. None
  41. @AutoValue @AutoParcel https://github.com/google/auto V2 - and beyond ▪ Good developers

    write code, great developers generate! ▪ When code is generated, why write tests? ▪ Use code generation and annotation processing heavily. ▹ Custom annotation processors - Data source policies, Routing, Model, Event Tracking generation. https://github.com/rharter/auto-value-gson
  42. V2 - and beyond ▪ Data Source layer can evolve

    separately from the rest of the application. ▪ Load all data on critical path. ▪ Leverage common interfaces like `onLoad` in the Presenters to prefetch data on critical path of user experience.
  43. Code Demo - Sample app

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

  45. Classes • MainActivity • FlowController • CatalogActivity • CatalogPresenter •

    CatalogViewModel • CatalogInteractor
  46. MainActivity void onCreate() { FlowController.getInstance() .launchCatalogActivity(this); }

  47. FlowController void launchCatalogActivity(Context c) { Intent intent = …; c.startActivity(intent)

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

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

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

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

    interactor = new CatalogInteractor(c); loadCatalog(); }
  52. CatalogViewModel // Parcelable public final Catalog catalog = new Catalog();

  53. CatalogInteractor Context mContext; public CatalogInteractor(Context context) { mContext = context;

    } public Observable loadCatalog() { return catalogDataSource.loadCatalog(mContext.getApplication( )); }
  54. CatalogPresenter void loadCatalog() { interactor.loadCourses().observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<>() { @Override public

    void call(List<Course> courses) { // update the view model } }); }
  55. None
  56. The Cons

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

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

  59. Adopting as a team Educating new hires

  60. Key Takeaways

  61. Ship the latest ideas

  62. Refactor or Not?

  63. Validate

  64. Iterate, Iterate, Iterate

  65. None
  66. We Are Hiring! Apply

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

  68. References ▪ Android dialogs - https://www.youtube.com/watch?v=VTaguVtvuYI ▪ https://github.com/square/okhttp ▪ https://github.com/square/retrofit

    ▪ http://google.github.io/dagger/ ▪ https://github.com/ReactiveX/RxAndroid ▪ http://360andev.com/sessions/100-libraries-i-wish-i-knew-about-when-i- started/ ▪http://360andev.com/sessions/100-eliminateboilerplate/ ▪ https://github.com/googlesamples/android-architecture ▪ http://360andev.com/sessions/100-intro-to-rxjava/
  69. Questions ?