Share
Embed
Transcript
34 slides
-
Outline Flow Introduction 01 Application 02 Flow Testing Methods 03
Experiences and Pitfalls 04 -
What is Flow? • A new stream processing API developed
by JetBrains. • Flow ◦ Simple ◦ Kotlin language features support ◦ Flows are cold -
How to Use it? flowOf("Pikachu", "John", "Blabla") .collect { name
-> println(name) } -
How to Use it? flow { emit(6) } .collect {
number -> println(number) } -
How to Use it? flowOf("Pikachu", "John", "Blabla") .map { names
-> names[5] } .catch { e -> println(e) } .onCompletion { println("Completed") } .collect { ch -> println(ch) } -
Threading • Threading Operators ◦ flowOn • Dispatchers ◦ IO
◦ Main ◦ Default -
Threading fun getUserFromServer(id: Long): Flow<User> { ... } getUserFromServer(id) .flowOn(Dispatchers.IO)
.collect { Log.d("test", "I got $it") } -
Flow + UI Events • We can consider UI inputs
as a flow of UI events. findViewById<Button>(R.id.button) .clicks() .onEach { Log.d("logTag", "clicked!") } .launchIn(lifecycleScope) -
Advantages • Binds to lifecycle automatically. • Makes code more
readable. • Avoids callback hell & boilerplates. • You can write everything related to UI in Flow. -
Do it! • Write it by yourself ◦ Extension methods
• Libs ◦ FlowBinding ◦ Corbind -
Scenarios • Some cases we can use flow binding… ◦
To implement API search by TextWatcher. ◦ To detect gestures with onTouch events. ◦ Avoid quick double clicks on buttons. -
Flow Binding view.findViewById<EditText>(R.id.edit_text) .textChanges() .skipInitialValue() .sample(2000L) .map { it.toString() }
.onEach { Log.i("logTag", "Text changes, $it.")} .launchIn(lifecycleScope) -
Flow + Retrofit • Wrap Retrofit calls in call adapter
factory. • Use libs. Retrofit.Builder() .baseUrl(BASE_DOMAIN) .client(okhttpClient) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(MyRetrofitFlowFactory.create()) .build() -
How to Test Flow? • Use Mocking Libraries ◦ Mockito
◦ MockK • Flow Assert / Turbine -
Using Mocking Libs • Flow can be easily tested by
using mocking libraries. • Use Mocking Libs ◦ Mockito-kotlin ◦ MockK -
Preparation data class User(val name: String, val id: Long) interface
Service { fun getUser(): User } class ApiService : Service { override fun getUser() = User("John", 1L) } -
Preparation class UserRepository(private val service: Service) { fun getUser(): Flow<User>
= flow { emit(service.getUser()) } } -
Using MockK to Test Flow @Test fun `Test getting an
user info by MockK`() = runBlocking { } val fakeUser = User("Doe", 2L) val mockApiService = mockk<ApiService>(relaxed = true) every { mockApiService.getUser() } returns fakeUser val userRepository = UserRepository(mockApiService) userRepository.getUser().collect { assertEquals(it, fakeUser) } -
Flow Assert / Turbine • It is originally from SqlDelight
lib ◦ And it was published as Turbine. • It is an extension function of Flow • Makes expressions more specific ◦ expectItem() ◦ expectError() ◦ expectComplete() • Takes advantage of Channels API and Coroutines API -
The Extension suspend fun <T> Flow<T>.test( timeout: Duration = 1.seconds,
validate: suspend FlowTurbine<T>.() -> Unit ) { ... } Source: https://github.com/cashapp/turbine/blob/trunk/src/commonMain/kotlin/app/cash/turbine/FlowTurbine.kt -
Implementation interface FlowTurbine<T> { suspend fun expectItem(): T suspend fun
expectComplete() suspend fun expectError(): Throwable } Source: https://github.com/cashapp/turbine/blob/trunk/src/commonMain/kotlin/app/cash/turbine/FlowTurbine.kt -
How does it Work? Flow Items Unlimited Buffer Channel FlowTurbine
expectItem() expectError() expectComplete() Query the Channel Reference: https://proandroiddev.com/kotlin-flow-assert-ff45465c01c0 -
Flow Assert / Turbine @Test fun `Test getting an user
info by Flow Assert`() = runBlocking { } val fakeUser = User("Pikachu", 3L) val fakeApiService = provideFakeService(fakeUser) val userRepository = UserRepository(fakeApiService) userRepository.getUser().test { assertEquals(expectItem(), fakeUser) expectComplete() } -
Threading Operators • In RxJava, we declare start and modify
chain below. observeSomething() .subscribeOn(io()) .observe(mainThread()) .subscribeOn(computation()) .subscribe { result -> println(result) } -
Threading Operators • In Flow, we have end declared and
can modify chain above. CouroutineScope(Job() + Dispatchers.Main).launch { Reader.flowRead<RssStandardChannel>(url) .flowOn(Dispathcers.IO) .map { channel -> channel.getResult() } .flowOn(Dispatchers.Default) .collect { println(it) } } -
Retrofit + Flow • When you retry your API calls,
you will get an exception. ◦ java.lang.IllegalStateException: Already executed. -
Retrofit + Flow • Simply copy the call before you
execute it in your CallAdapterFactory. override fun adapt(call: Call<T>): Flow<Response<T>> = flow { val newCall = call.takeIf { !it.isExecuted } ?: call.clone() emit(newCall.awaitResponse()) } -
Experience • From RxJava to Flow ◦ We can use
a tool to transform stream types. • Apply flow to UI can actually be really convenient. ◦ View extensions (avoid quick clicks, text watchers, and so on) ◦ Use it with sealed classes. • Flow is simple. -
CREDITS: This presentation template was created by Slidesgo, including icons
by Flaticon, and infographics & images by Freepik. Thanks KtRssReader!