Slide 1

Slide 1 text

Introduction to Reactive Programming and Dagger Kaushal Dhruw @drulabs (twitter, GitHub, medium)

Slide 2

Slide 2 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Contents • MVP brief intro • Reactive programming • Retrofit with RxJava2 • Room with RxJava2 • Dependency injection & Dagger • Putting it all together 2

Slide 3

Slide 3 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. What’s NOT covered x Room x Retrofit x Advanced concepts x Testing x MVP, MVVM from scratch x Step by step walk through x In-depth knowledge of RxJava2 and Dagger2 3

Slide 4

Slide 4 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. MVP architectural pattern

Slide 5

Slide 5 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Quiz App (walkthrough video)

Slide 6

Slide 6 text

Reactive Programming

Slide 7

Slide 7 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming • Wikipedia – It is an asynchronous programming paradigm concerned with data flows and propagation of change. • In other words: – Reactive programming is programming with asynchronous data streams.

Slide 8

Slide 8 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming is… Observer pattern Iterator pattern Functional support And more…

Slide 9

Slide 9 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming is… Observer pattern Iterator pattern Functional support Concurrency Resilience And more…

Slide 10

Slide 10 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming Observer pattern Iterator pattern Functional support

Slide 11

Slide 11 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming Observer pattern Functional support Iterator pattern

Slide 12

Slide 12 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive programming Observer pattern Iterator pattern Functional support Create Combine Transform Chain Listen Filter Reduce Map

Slide 13

Slide 13 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive Extensions (Rx) • Reactive extensions (or Rx) is a library for composing asynchronous and event-based programs by using observable sequences

Slide 14

Slide 14 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive Extensions (Rx) • Reactive extensions (or Rx) is a library for composing and programs by using sequences simultaneous operations. perform some operation(s) when event is triggered or received. think of it as asynchronous immutable array.

Slide 15

Slide 15 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Reactive Extensions (Rx)

Slide 16

Slide 16 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. RxJava & RxAndroid • RxJava is a Java VM implementation of ReactiveX. • RxAndroid provides android specific bindings and extensions. So RxJava can be used in android applications

Slide 17

Slide 17 text

Reactive Programming Components

Slide 18

Slide 18 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Components • Source (emits data) – Emits data streams • Subscriber (the observer or consumer) – Consumes data emitted from source • Scheduler (threads and concurrency) – Where to process and consume data

Slide 19

Slide 19 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Components Source (observable) Subscriber (observer) Scheduler (concurrency) subscribe dispose Time onNext(1) onNext(2) Error / complete onNext(3)

Slide 20

Slide 20 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Source Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3)

Slide 21

Slide 21 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Source & Subscriber Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3) Subscriber (observer) subscribe dispose

Slide 22

Slide 22 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) Thread t = new Thread(…); t.start(); Wait() Notify()

Slide 23

Slide 23 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency)

Slide 24

Slide 24 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Schedulers Source (observable) Time Subscriber (observer) Scheduler (concurrency) • io() • computation() • newThread() • from(Executer)

Slide 25

Slide 25 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Source, Subscriber & Scheduler Source (observable) Time onNext(1) onNext(2) Error / complete onNext(3) Subscriber (observer) subscribe dispose Scheduler (concurrency)

Slide 26

Slide 26 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Components Source (observable) Subscriber (observer) Scheduler (concurrency) subscribe dispose Time onNext(1) onNext(2) Error / complete onNext(3)

Slide 27

Slide 27 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Source • Observable • Flowable • Single • Maybe • Completable • ObservableSource • Publisher • SingleSource • MaybeSource • CompletableSource

Slide 28

Slide 28 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Observable • Observable • Flowable • Single • Maybe • Completable Emits 0 to N items terminates with success or exception

Slide 29

Slide 29 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Flowable • Observable • Flowable • Single • Maybe • Completable Emits 0 to N items terminates with success or exception. Supports backpressure

Slide 30

Slide 30 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Single • Observable • Flowable • Single • Maybe • Completable Emits single item or error. Has no completion indicator

Slide 31

Slide 31 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Maybe • Observable • Flowable • Single • Maybe • Completable Succeeds with 0 or 1 item, or fails. Has completion indicator. Reactive optional.

Slide 32

Slide 32 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Completable • Observable • Flowable • Single • Maybe • Completable Completes with success or fails. No emission.

Slide 33

Slide 33 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. RxJava Operators • Creational – Create – Just – From • Transformational – Map – Flatmap – Group by • Filter – Filter – Distinct – Skip • Combine – Concat – Merge – Zip

Slide 34

Slide 34 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Rx Marbles Interactive diagram of Rx Observables

Slide 35

Slide 35 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Operator chaining Map Filter Merge

Slide 36

Slide 36 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Operator chaining Filter Reduce

Slide 37

Slide 37 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Retrofit with RxJava2 Add retrofit RxJava2 adapter dependency And of course RxAndroid dependency

Slide 38

Slide 38 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Retrofit with RxJava Use RxJava2 with retrofit

Slide 39

Slide 39 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Room & RxJava Add Room and RxJava support dependency Supports Flowable, Single and Maybe

Slide 40

Slide 40 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Room & RxJava Supports Flowable, Single and Maybe Regular RxJava2 RxJava2 ????? ?????

Slide 41

Slide 41 text

Dependency injection Dagger2

Slide 42

Slide 42 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dependency injection Got all the ingredients for pizza except the base Should I create pizza base myself? You are gonna mess it up let me do it

Slide 43

Slide 43 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dependency injection Think in terms of objects Avoid this

Slide 44

Slide 44 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dependency injection Get dependencies from outside Injecting the dependency

Slide 45

Slide 45 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dependency injection Instead of having hidden dependencies, pass the dependent objects • loose coupling • Testable code • Reusable code • Readable code • Enforces SRP

Slide 46

Slide 46 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dagger2 Dagger is a compile time dependency injection framework for both Java and Android. I will inject the dependencies just tell me: • What dependencies to inject - @Component • How to create it - @Module, @Provides • Where to inject - @Inject • Which one to inject - @Qualifier, @Named

Slide 47

Slide 47 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Dagger dependency graph Pizza Pizza base Chicken sausage Cheese Flour Chicken Milk salt Processed chicken Toppings Tomato chilli flakes oregano

Slide 48

Slide 48 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Types of injection • Field injection • Constructor injection • Method injection

Slide 49

Slide 49 text

Dagger code and demo

Slide 50

Slide 50 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Summary • Brief introduction of MVP • Reactive programming – What, why and how? – Various operators – With Room and Retrofit • Dagger • How these work together

Slide 51

Slide 51 text

Copyright © 2016 Talentica Software (I) Pvt Ltd. All rights reserved. Thank you Kaushal Dhruw @drulabs (twitter, medium, github) Q & A

Slide 52

Slide 52 text

Backup