Slide 1

Slide 1 text

CLEAN ARCHITECTURE

Slide 2

Slide 2 text

Richa Khandelwal [email protected] @richa123

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

Who all has ever used MVC?

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

MODEL VIEW CONTROLLER Interact with DB Render Model Handle view events Update model Invoke next UI

Slide 12

Slide 12 text

Mobile Web Desktop Mobile is diffrent, don’t generalize based on this

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Model public class CourseModel { public Course getFromDatabase() { …. } public void getFromNetwork() { …. } }

Slide 16

Slide 16 text

View public class ExpandableCourseListActivity { public void onPause() { // save all expanded states } public void onResume() { // restore all expanded states } }

Slide 17

Slide 17 text

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 ) {}

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

Interactor?

Slide 24

Slide 24 text

JUnit + Mocking

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

View Model ?

Slide 27

Slide 27 text

JUnit + Mocking Android + Mocking Espresso + Mocking

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Presenter ?

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Espresso + Mocking JUnit + Mocking JUnit + Mocking Android + Mocking

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Router ?

Slide 34

Slide 34 text

Android + Mocks

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

Apply Dependency Inversion

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Clean Android Architecture Architecture is about Intents not Frameworks - Uncle Bob

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

App to list courses on coursera using Catalog API Github Link -

Slide 44

Slide 44 text

Classes • MainActivity • CatalogRouter • CatalogActivity • CatalogPresenter • CatalogViewModel • CatalogInteractor

Slide 45

Slide 45 text

MainActivity public void onCreate() { CatalogRouter.getInstance().launchCatalogActivity(); }

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

CatalogActivity CatalogPresenter presenter; Action1 onError = new Action1() { public void call(Throwable throwable) { // handle error } });

Slide 48

Slide 48 text

CatalogActivity public void onCreate() { presenter = new CatalogPresenter(this); presenter.loadCatalog(onError); }

Slide 49

Slide 49 text

CatalogActivity public void onResume() { Action1 catalogUpdateAction = ; presenter.subscribeToCatalogUpdates(catalogUpdat eAction) }

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

CatalogPresenter CatalogInteractor interactor; public CatalogPresenter(Context context) { viewModel = new CatalogViewModel(); interactor = new CatalogInteractor(context) }

Slide 52

Slide 52 text

CatalogPresenter public void loadCatalog() { interactor.loadInteractor() .subscribe(new Action1>() { @Override public void call(List courses) { getData().mCourseList.onNext(courses); } }); }

Slide 53

Slide 53 text

CatalogPresenter public void subscribeToCatalogUpdates(Action1 action) { viewModel.subscribeToUpdates(action); } public void unsubscribeToCatalogUpdates() { viewModel.subscribeToUpdates(); }

Slide 54

Slide 54 text

CatalogViewModel public final BehaviorSubject catalog = BehaviorSubject.create(); public void subscribeToCatalog(Action1 action) { catalog.subscribe(action); } public void unsubscribeToCatalog() { catalog.unsubscribe(action); }

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

Dealing with Context

Slide 61

Slide 61 text

After thoughts

Slide 62

Slide 62 text

Build features in new architecture rather than refactor Validate Iterate - learn and apply Don’t refactor all the time

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

Coursera is hiring!

Slide 65

Slide 65 text

Questions ? Thank You!