Loading repository dataβ¦
Loading repository dataβ¦
LukasLechnerDev / repository
π Learning Kotlin Coroutines and Flows for Android by example. π Sample implementations for real-world Android use cases. π Unit tests included!
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
π Learning Kotlin Coroutines and Flows for Android Development by Example
π Sample implementations for real-world Android use cases
π Unit tests included!
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.
In the playground package you can play around with Coroutines and Flow examples that run directly on the JVM.
Every use case is using its own Activity and JetPack ViewModel. The ViewModels contain most of the interesting Coroutine-related code.
Activities listen to LiveData or StateFlow events of the ViewModel and render received UiStates.
This project is using retrofit/okhttp together with a MockNetworkInterceptor. This lets you define how the API should behave.
Everything can be configured: http status codes, response data and delays. Every use case defines a certain behaviour of the Mock API.
The API has 2 endpoints. One returns the names of the most recent Android versions and the other one returns the features of a certain
Android version.
Unit Tests exist for most use cases.
Sign up to my newsletter to never miss new content. I will publish new blog posts and videos about Coroutines and Flow on a regular basis.
This project is the foundation of a comprehensive Online Course about Kotlin Coroutines and Flow for Android Development In the course, we are going to implement the use cases of this repository together, as well as talk about all the necessary concepts that you need to know.
If you like this project, please tell other developers about it! β€οΈ
If you like, you can follow me on Twitter @LukasLechnerDev.
This use case performs a single network request to get the latest Android Versions and displays them on the screen.
[code]
This use case performs two network requests sequentially. First, it retrieves recent Android Versions and then it requests the features of the latest version.
There are also 2 alternative implementations included. One is using old-school callbacks. The other one uses RxJava. You can compare each implementation. If you compare all three implementations, it is really interesting to see, in my opinion, how simple the Coroutine-based version actually is.
[code]
Performs three network requests concurrently. It loads the feature information of the 3 most recent Android Versions. Additionally, an implementation that performs the requests sequentially is included. The UI shows how much time each implementation takes to load the data so you can see that the network requests in the concurrent version are indeed performed in parallel. The included unit test is also interesting, as it shows how you can use virtual time to verify that the concurrent version gets performed in parallel.
[code]
Demonstrates the simple usage of map() to perform a dynamic amount of network requests. At first, this use case performs a network request to load all Android versions.
Then it performs a network request for each Android version to load its features. It contains an implementation that performs the network requests sequentially and another one that performs them concurrently.
[code]
This use case uses the suspending function withTimeout() from the coroutines-core library. It throws a TimeoutCancellationException if the timeout was exceeded.
You can set the duration of the request in the UI and check the behaviour when the response time is bigger than the timeout.
General networking timeouts can also be configured in the okhttp client.
[code]
Demonstrates the usage of higher-order functions together with coroutines. The higher-order function retry() retries a certain suspending operation for a given amount of times.
It uses an exponential backoff for retries, which means that the delay between retries increases exponentially. The behavior of the Mock API is defined in a way that it responses
with 2 unsuccessful responses followed by a successful response.
[code]
Unit tests verify the amount of requests that are performed in different scenarios. Furthermore, they check if the exponential backoff is working properly by asserting the amount of elapsed virtual time.
Composes higher level functions retry() and withTimeout(). Demonstrates how simple and readable code written with Coroutines can be.
The mock API first responds after the timeout and then returns an unsuccessful response. The third attempt is then successful.
Take a look at the included callback-based implementation to see how tricky this use case is to implement without Coroutines.
I also implemented the use case with RxJava.
[code]
This example stores the response data of each network request in a Room database. This is essential for any "offline-first" app.
If the View requests data, the ViewModel first checks if there is data available in the database. If so, this data is returned before performing
a network request to get fresh data.
[code]
This is not really a use case, but I wanted to show how you can add additional debug information about the Coroutine that is currently running to your logs.
It will add the Coroutine name next to the thread name when calling Thread.currentThread.name()
This is done by enabling Coroutine Debu