Skip to content

Commit d8ee85d

Browse files
Add Flow UseCases description to README.md
1 parent 88eeaf5 commit d8ee85d

File tree

1 file changed

+42
-11
lines changed

1 file changed

+42
-11
lines changed

README.md

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
![CoroutineUsecasesOnAndroid](documentation/images/Logo-new.png)
22

3-
# Kotlin Coroutines - Use Cases on Android
3+
# Kotlin Coroutines and Flow - Use Cases on Android
44

5-
🎓 Learning Kotlin Coroutines for Android by example.
5+
🎓 Learning Kotlin Coroutines and Flows for Android Development by example
66

7-
🚀 Sample implementations for real-world Android use cases.
7+
🚀 Sample implementations for real-world Android use cases
88

99
🛠 Unit tests included!
1010

11-
This repository is intended to be a "Playground Project". You can quickly look up and play around with the different Coroutine Android implementations.
12-
In the `playground` package you can play around with Coroutines examples that run directly on the JVM.
11+
This repository is intended to be a "Playground Project". You can quickly look up and play around with the different Coroutine and Flow Android implementations.
12+
In the `playground` package you can play around with Coroutines and Flow examples that run directly on the JVM.
1313

1414
## 🔧 Project Setup
1515

1616
Every use case is using its own `Activity` and `JetPack ViewModel`. The `ViewModel`s contain all the interesting Coroutine related code.
17-
`Activities` listen to `LiveData` events of the `ViewModel` and render received `UiState`s.
17+
`Activities` listen to `LiveData` or `StateFlow` events of the `ViewModel` and render received `UiState`s.
1818

1919
This project is using retrofit/okhttp together with a `MockNetworkInterceptor`. This lets you define how the API should behave.
2020
Everything can be configured: http status codes, response data and delays. Every use case defines a certain behaviour of the Mock API.
@@ -38,7 +38,7 @@ Unit Tests exist for most use cases.
3838
* Comparing Kotlin Coroutines with Callbacks and RxJava [[link](https://www.lukaslechner.com/comparing-kotlin-coroutines-with-callbacks-and-rxjava/)]
3939
* How to run an expensive calculation with Kotlin Coroutines on the Android Main Thread without freezing the UI [[link](https://www.lukaslechner.com/how-to-run-an-expensive-calculation-with-kotlin-coroutines-on-the-android-main-thread-without-freezing-the-ui/)]
4040

41-
Sign up to my [newsletter](https://www.lukaslechner.com/newsletter/) to never miss a new blog post. I will publish new blog posts about Coroutines on a regular basis.
41+
Sign up to my [newsletter](https://www.lukaslechner.com/newsletter/) to never miss new content. I will publish new blog posts and videos about Coroutines and Flow on a regular basis.
4242

4343
## 🎓 Online Course
4444

@@ -54,7 +54,7 @@ If you like this project, please tell other developers about it! ❤️
5454

5555
If you like, you can follow me on Twitter [**@LukasLechnerDev**](https://twitter.com/LukasLechnerDev).
5656

57-
## ⭐️ Use Cases
57+
## ⭐️ Coroutine Use Cases
5858
1. [Perform single network request](#1-perform-single-network-request)
5959
2. [Perform two sequential network requests](#2-perform-two-sequential-network-requests)
6060
3. [Perform several network requests concurrently](#3-perform-several-network-requests-concurrently)
@@ -73,7 +73,13 @@ If you like, you can follow me on Twitter [**@LukasLechnerDev**](https://twitter
7373
16. [Performance analysis of dispatchers, number of coroutines and yielding](#16-performance-analysis-of-dispatchers-number-of-coroutines-and-yielding)
7474
17. [Perform expensive calculation on Main Thread without freezing the UI](#17-perform-expensive-calculation-on-main-thread-without-freezing-the-ui)
7575

76-
## 📄 Description
76+
## ⭐ Flow Use Cases
77+
1. [Flow Basics](#1-flow-basics)
78+
2. [Basic Flow intermediate operators](#2-basic-flow-intermediate-operators)
79+
3. [Flow Exception Handling](#3-flow-exception-handling)
80+
4. [Exposing Flows in the ViewModel](#4-exposing-flows-in-the-viewmodel)
81+
82+
## 📄 Coroutine Use Cases Description
7783

7884
### 1. Perform single network request
7985

@@ -226,9 +232,34 @@ See [[this blog post](https://www.lukaslechner.com/how-to-run-an-expensive-calcu
226232

227233
You can play around and check the performance of different configurations!
228234

229-
## 👷‍♀️ Contributing 👷‍♂️
235+
## 📄 Flow Use Cases Description
236+
237+
### 1. Flow Basics
238+
239+
This simple use case shows how to consume values from a `DataSource` that emits live stock information and how to display them in the UI.
240+
241+
The datasource exposes a flow which is built with the `flow{}` flow builder. It fetches fresh stock information every 5 seconds from a mocked endpoint.
242+
243+
A `LiveData` property that exposes the `UiState` in the `ViewModel` is created by using the `.asLiveData()` terminal operator.
244+
This use case also shows how to use the `map` intermediate operator and the `onStart` lifecycle operator.
245+
246+
### 2. Basic Flow Intermediate Operators
247+
248+
The second use case is an extension of the first one.
249+
It uses some basic intermediate operators, like `withIndex`, `map`, `take` and `filter`.
250+
251+
### 3. Flow Exception Handling
252+
253+
The third use case shows how to properly implement exception handling with flows.
254+
255+
It uses the `catch` operator to handle exceptions of our flow in the `ViewModel`, and uses the `retry ` operator to retry failed network requests in the `DataSource`.
256+
257+
### 4. Exposing Flows in the ViewModel
258+
259+
This use case shows how to expose flows (a `StateFlow` to be precise) in the `ViewModel` instead of a `LiveData` property.
260+
The `statIn` operator is used to convert the ordinary, cold `Flow` into a hot `StateFlow`.
230261

231-
I am currently learning Coroutines myself. So if you have any ideas for or improvements or other use cases, feel free to create a pull request or an issue.
262+
In the `Activity`, the `repeadOnLifecycle` suspend function is used to collect emissions of the `StateFlow` in a lifecycle-aware manner.
232263

233264
## Author
234265

0 commit comments

Comments
 (0)