Slide 1

Slide 1 text

Bye bye RxJava Building flexible SDKs with Kotlin and Coroutines

Slide 2

Slide 2 text

Our journey with reactive streams

Slide 3

Slide 3 text

Step 1 - MVP UI Network stream Presenter commands actions

Slide 4

Slide 4 text

Step 2 - clean architecture UI Network stream Presenter commands actions Repository Use case stream stream

Slide 5

Slide 5 text

Step 3 - SDKs UI Network callbacks Presenter commands actions Repository Use case stream stream SDK

Slide 6

Slide 6 text

SDK Step 4 - fully reactive (experiment*) UI Network request
 stream MVI state stream action stream Repository Use case response
 stream stream response
 stream request
 stream

Slide 7

Slide 7 text

It worked nicely…

Slide 8

Slide 8 text

… on a small scale

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

SDK design issues

Slide 11

Slide 11 text

Streaming is overused

Slide 12

Slide 12 text

It started with the network

Slide 13

Slide 13 text

Network ops interface MessageService { @GET(“messages") fun getMessages(): Observable> }

Slide 14

Slide 14 text

Network ops interface MessageService { @GET(“messages") fun getMessages(): Observable> }

Slide 15

Slide 15 text

Network ops interface MessageService { @GET(“messages") fun getMessages(): Single> }

Slide 16

Slide 16 text

HTTP network calls are not streams

Slide 17

Slide 17 text

Rx mostly provided easy background networking

Slide 18

Slide 18 text

Rx and Callback APIs

Slide 19

Slide 19 text

Callback APIs data class GetPatientRequest(val patientId: String) interface GetPatientOutput { fun onGetPatientSuccess(patient: Patient) } interface BabylonUserApi { fun getPatient( request: GetPatientRequest, output: GetPatientOutput ): Disposable }

Slide 20

Slide 20 text

Rx APIs @Experimental interface BabylonRxUserApi { fun getPatient(): ObservableTransformer }

Slide 21

Slide 21 text

Rx APIs // From RxJava sources public interface ObservableTransformer { ObservableSource apply(Observable upstream); }

Slide 22

Slide 22 text

Rx APIs @Experimental interface BabylonRxUserApi { fun getPatient(): ObservableTransformer }

Slide 23

Slide 23 text

Rx APIs sealed class GetPatientStatus { data class Ready(val patient: Patient) : GetPatientStatus() object Loading : GetPatientStatus() data class Error(val throwable: Throwable) : GetPatientStatus() }

Slide 24

Slide 24 text

Rx APIs internal class GetPatientUseCase ( ... ) : ObservableTransformer() { override fun apply( upstream: Observable ): Observable = upstream.switchMap { patientRepository.getPatient(it.patientId) .map { patient -> GetPatientStatus.Ready(patient) } .onErrorReturn { throwable -> GetPatientStatus.Error(throwable) } .startWith(GetPatientStatus.Loading) } }

Slide 25

Slide 25 text

Rx APIs upstream.switchMap { patientRepository.getPatient(it.patientId) .map { patient -> GetPatientStatus.Ready(patient) } .onErrorReturn { throwable -> GetPatientStatus.Error(throwable) } .startWith(GetPatientStatus.Loading) }

Slide 26

Slide 26 text

Rx APIs upstream.switchMap { patientRepository.getPatient(it.patientId) .map { patient -> GetPatientStatus.Ready(patient) } .onErrorReturn { throwable -> GetPatientStatus.Error(throwable) } .startWith(GetPatientStatus.Loading) }

Slide 27

Slide 27 text

Rx APIs upstream.switchMap { patientRepository.getPatient(it.patientId) .map { patient -> GetPatientStatus.Ready(patient) } .onErrorReturn { throwable -> GetPatientStatus.Error(throwable) } .startWith(GetPatientStatus.Loading) }

Slide 28

Slide 28 text

Overengineering

Slide 29

Slide 29 text

class GetImportantMessagesUseCase @Inject constructor( private val loadSessionConfigurationUseCase: LoadSessionConfigurationUseCase, private val getAppointmentsUseCase: GetAppointmentsUseCase, private val getLoggedInPatientUseCase: GetLoggedInPatientUseCase, private val getNotificationsUseCase: GetNotificationsUseCase, private val isHealthCheckCompletedUseCase: IsHealthCheckCompletedUseCase, private val systemUtil: SystemUtil ) : AuthenticatedUseCase() { override fun applyUseCase(upstream: Observable): Observable { return upstream.switchMap { Observable.just(GetLoggedInPatientRequest()) .compose(getLoggedInPatientUseCase) .toReadyData() .toObservable() .switchMap { patient -> Observable.combineLatest( Observable.just(LoadSessionConfigurationRequest()) .compose(loadSessionConfigurationUseCase), Observable.just(GetAppointmentsRequest(patient.id !!)) .compose(getAppointmentsUseCase), Observable.just(GetNotificationsRequest) .compose(getNotificationsUseCase), Observable.just(IsHealthCheckCompletedRequest) .compose(isHealthCheckCompletedUseCase), Function4 { sessionConfigurationStatus, getAppointmentsStatus, getNotificationsStatus, isHealthCheckCompletedStatus -> when { getAppointmentsStatus is GetAppointmentsStatus.Error -> GetImportantMessagesStatus.Error(getAppointmentsStatus.throwable) sessionConfigurationStatus is LoadSessionConfigurationStatus.Error -> GetImportantMessagesStatus.Error(sessionConfigurationStatus.throwable) getNotificationsStatus is GetNotificationsStatus.Error -> GetImportantMessagesStatus.Error(getNotificationsStatus.throwable) isHealthCheckCompletedStatus is IsHealthCheckCompletedStatus.Error ->

Slide 30

Slide 30 text

Complexity bubbles upwards

Slide 31

Slide 31 text

Reactive stream reuse is a double edged sword

Slide 32

Slide 32 text

“Bad” consistency

Slide 33

Slide 33 text

Not enough due diligence

Slide 34

Slide 34 text

value < cost

Slide 35

Slide 35 text

Reactive streams are powerful and not to be used lightly!

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Lessons learned

Slide 39

Slide 39 text

Look before you leap

Slide 40

Slide 40 text

Understand both the value and its cost

Slide 41

Slide 41 text

Monitor & iterate

Slide 42

Slide 42 text

Problems snowball with time

Slide 43

Slide 43 text

Keep It Simple Stupid

Slide 44

Slide 44 text

Our latest SDK architecture

Slide 45

Slide 45 text

Guiding principles

Slide 46

Slide 46 text

KISS

Slide 47

Slide 47 text

Design for the majority of problems - not the minority

Slide 48

Slide 48 text

Reserve streams for true streams to avoid ambiguity

Slide 49

Slide 49 text

Be consistent thoughtfully

Slide 50

Slide 50 text

Design

Slide 51

Slide 51 text

SDK Before - Fully reactive SDK UI Network request
 stream MVI state stream action stream Repository Use case response
 stream stream response
 stream request
 stream

Slide 52

Slide 52 text

SDK After - V2 SDK architecture UI Network Call MVI State stream action stream Repository Use case

Slide 53

Slide 53 text

Lower layers as simple as possible

Slide 54

Slide 54 text

Coroutines

Slide 55

Slide 55 text

Lower layers as simple as possible internal interface PaymentCardsService { @GET(PAYMENT_CARD_ENDPOINT) suspend fun getPaymentCards(): List } Retrofit 2.6.0+

Slide 56

Slide 56 text

Lower layers as simple as possible internal interface PaymentCardsRepository { suspend fun getPaymentCards(): List }

Slide 57

Slide 57 text

Lower layers as simple as possible class NetworkPaymentCardsRepository( private val paymentCardsService: PaymentCardsService ) : PaymentCardsRepository { override suspend fun getPaymentCards(): List = paymentCardsService.getPaymentCards() .map { it.toDomainEntity() } }

Slide 58

Slide 58 text

Topmost layer takes care of high-level decisions

Slide 59

Slide 59 text

interface PaymentV2Api { fun getPaymentCards(): Call> }

Slide 60

Slide 60 text

interface PaymentV2Api { fun getPaymentCards(): Call> }

Slide 61

Slide 61 text

interface Call { fun execute(): T fun enqueue(callback: Callback) fun cancel() } interface Callback { fun onResult(data: T) fun onException(throwable: Throwable) }

Slide 62

Slide 62 text

class PaymentV2ApiImpl( private val paymentCardsRepository: PaymentCardsRepository ) : PaymentV2Api { override fun getPaymentCards() = wrapCall { paymentCardsRepository.getPaymentCards() } }

Slide 63

Slide 63 text

class PaymentV2ApiImpl( private val paymentCardsRepository: PaymentCardsRepository ) : PaymentV2Api { override fun getPaymentCards() = wrapCall { paymentCardsRepository.getPaymentCards() } }

Slide 64

Slide 64 text

Before class GetPrescriptionsUseCase ( private val prescriptionsRepository: PrescriptionsRepository, private val userAccountsRepository: UserAccountsRepository ) : AuthenticatedUseCase() { override fun applyUseCase(upstream: Observable): Observable { return upstream.switchMap { request -> getPrescriptions(request) .toObservable() .map { GetPrescriptionsStatus.Ready(request.location, it) } .startWith(GetPrescriptionsStatus.Loading) .onErrorReturn { GetPrescriptionsStatus.Error(it) } } } private fun getPrescriptions(request: GetPrescriptionsRequest): Single> { return userAccountsRepository.getLoggedInUser() .flatMapObservable { prescriptionsRepository.getPrescriptions(it) } } }

Slide 65

Slide 65 text

After class GetPrescriptionsUseCase ( private val prescriptionsRepository: PrescriptionsRepository, private val userAccountsRepository: UserAccountsRepository ) { suspend operator fun invoke(): List { val userAccount = userAccountsRepository.getLoggedInUser() return prescriptionsRepository.getPrescriptions(userAccount) } }

Slide 66

Slide 66 text

Extensions provide flexibility

Slide 67

Slide 67 text

suspend fun Call.asSuspend(): T { ... // Coroutine directly returning the result } fun Call.asSingle(): Single { ... // Rx single emitting the result } fun Call.asStatusObservable(): Observable> { ... // Rx stream with updates on the status of the execution }

Slide 68

Slide 68 text

Hiding implementation details

Slide 69

Slide 69 text

Notes on rx and coroutine compatibility

Slide 70

Slide 70 text

fun getSomething(): Single val nonce = getSomething().await()

Slide 71

Slide 71 text

suspend fun getSomething(): String rxSingle { getSomething() }

Slide 72

Slide 72 text

suspend fun getSomething(): String rxSingle(Dispatchers.Unconfined) { getSomething() }

Slide 73

Slide 73 text

Execution

Slide 74

Slide 74 text

Prototyping

Slide 75

Slide 75 text

Testing the behaviour

Slide 76

Slide 76 text

Conventions & howtos

Slide 77

Slide 77 text

Enforce conventions

Slide 78

Slide 78 text

Migration

Slide 79

Slide 79 text

–Dalai Lama “Forget the failures. Keep the lessons.”

Slide 80

Slide 80 text

Mikolaj Leszczynski @TheAngroid Twitter / Medium Careers: http://tiny.cc/babylon