Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

▪ Challenges ▪ Finding the right architecture fit ▪ Validation ▪ Practice makes perfect! ▪ Demo and Samples ▪ Cons ▪ Key Takeaways Structure of the talk

Slide 4

Slide 4 text

Available on Google Play Store Place your screenshot here Android App 1,825+ COURSES 18M+ LEARNERS 141+ PARTNERS

Slide 5

Slide 5 text

109% increase in mobile usage since last year 24 % use mobile only 40 % use mobile and desktop

Slide 6

Slide 6 text

Over 500k (and growing) new learners join Coursera every month

Slide 7

Slide 7 text

Cross team Tightly aligned, loosely coupled Faster MVPs Learn fast, fail fast Network Speeds Edge, 2G, Spotty

Slide 8

Slide 8 text

Who all has ever used MVC?

Slide 9

Slide 9 text

MVC

Slide 10

Slide 10 text

Where to Begin

Slide 11

Slide 11 text

MVP ? MVC ? MVVM ? VIPER ?

Slide 12

Slide 12 text

MODEL VIEW CONTROLLER Manage application data Render model Handle view events Interact with model Invoke next UI Table of Responsibilities

Slide 13

Slide 13 text

Mobile Web Desktop

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

Model public class CourseModel { Course getFromDatabase() { // fetch from db, or local cache } Course getFromNetwork() { // fetch from apis, cache when possible } }

Slide 17

Slide 17 text

View public class ExpandableCourseListActivity { void onSaveInstanceState(Bundle b) { // save all expanded states } }

Slide 18

Slide 18 text

Controller void onClick() {} void onModelUpdated() {} void onNewNotification() {} void onLocationUpdated(Location loc) {} void takePhoto(Activity context) {} void launchView(Activity context ) {}

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

MVC ??

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

Interactor?

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

View Model ?

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Presenter ?

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Data Source

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

FlowController ?

Slide 33

Slide 33 text

coursera-android://app/

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Apply Dependency Inversion

Slide 36

Slide 36 text

Data Source

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Espresso + Mocking JUnit + Mocking Android/ JUnit + Mocking Android/ JUnit + Mocks Data Source

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

@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

Slide 42

Slide 42 text

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.

Slide 43

Slide 43 text

Code Demo - Sample app

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Classes • MainActivity • FlowController • CatalogActivity • CatalogPresenter • CatalogViewModel • CatalogInteractor

Slide 46

Slide 46 text

MainActivity void onCreate() { FlowController.getInstance() .launchCatalogActivity(this); }

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

CatalogActivity void onResume() { Action1 action = ; presenter.subscribeToUpdates(action) }

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

CatalogPresenter CatalogInteractor interactor; CatalogPresenter(Context c) { viewModel = new CatalogViewModel(); interactor = new CatalogInteractor(c); loadCatalog(); }

Slide 52

Slide 52 text

CatalogViewModel // Parcelable public final Catalog catalog = new Catalog();

Slide 53

Slide 53 text

CatalogInteractor Context mContext; public CatalogInteractor(Context context) { mContext = context; } public Observable loadCatalog() { return catalogDataSource.loadCatalog(mContext.getApplication( )); }

Slide 54

Slide 54 text

CatalogPresenter void loadCatalog() { interactor.loadCourses().observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<>() { @Override public void call(List courses) { // update the view model } }); }

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

The Cons

Slide 57

Slide 57 text

Dealing with context Presenter(Context context) Interactor(Context context) NetworkClient(Context context) PersistenceClient(Context context) context.getSystemService(“SERVICE”) context.startActivity(intent) context.startActivityForResult(intent)

Slide 58

Slide 58 text

Tedium ~ 4 classes per feature/module

Slide 59

Slide 59 text

Adopting as a team Educating new hires

Slide 60

Slide 60 text

Key Takeaways

Slide 61

Slide 61 text

Ship the latest ideas

Slide 62

Slide 62 text

Refactor or Not?

Slide 63

Slide 63 text

Validate

Slide 64

Slide 64 text

Iterate, Iterate, Iterate

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

We Are Hiring! Apply

Slide 67

Slide 67 text

@richa123 [email protected] richk in/richak Thank You

Slide 68

Slide 68 text

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/

Slide 69

Slide 69 text

Questions ?