├── .gitignore ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── manuelvicnt │ │ │ └── coroutinesflow │ │ │ ├── AppGraph.kt │ │ │ ├── fibonacci │ │ │ ├── ColdFibonacci.kt │ │ │ ├── NeverEndingFibonacci.kt │ │ │ └── impl │ │ │ │ ├── ColdFibonacciProducer.kt │ │ │ │ └── NeverEndingFibonacciProducer.kt │ │ │ ├── main │ │ │ ├── MainActivity.kt │ │ │ └── MainViewModel.kt │ │ │ └── user │ │ │ ├── UserActivity.kt │ │ │ ├── UserRepo.kt │ │ │ ├── UserViewModel.kt │ │ │ └── impl │ │ │ └── UserRepository.kt │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_main.xml │ │ └── activity_user.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── manuelvicnt │ └── coroutinesflow │ ├── LiveDataTestUtil.kt │ ├── MainCoroutineRule.kt │ ├── fibonacci │ ├── FakeColdFibonacciProducer.kt │ └── impl │ │ ├── ColdFibonacciProducerTest.kt │ │ └── NeverEndingFibonacciProducerTest.kt │ ├── main │ └── MainViewModelTest.kt │ └── user │ └── UserViewModelTest.kt ├── app_running.gif ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea 38 | .idea/workspace.xml 39 | .idea/tasks.xml 40 | .idea/gradle.xml 41 | .idea/assetWizardSettings.xml 42 | .idea/dictionaries 43 | .idea/libraries 44 | .idea/caches 45 | 46 | # Keystore files 47 | # Uncomment the following line if you do not want to check your keystore files in. 48 | #*.jks 49 | 50 | # External native build folder generated in Android Studio 2.2 and later 51 | .externalNativeBuild 52 | 53 | # Google Services (e.g. APIs or Firebase) 54 | google-services.json 55 | 56 | # Freeline 57 | freeline.py 58 | freeline/ 59 | freeline_project_description.json 60 | 61 | # fastlane 62 | fastlane/report.xml 63 | fastlane/Preview.html 64 | fastlane/screenshots 65 | fastlane/test_output 66 | fastlane/readme.md 67 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to become a contributor and submit your own code 2 | 3 | ## Contributor License Agreements 4 | 5 | We'd love to accept your sample apps and patches! Before we can take them, we 6 | have to jump a couple of legal hurdles. 7 | 8 | Please fill out either the individual or corporate Contributor License Agreement (CLA). 9 | 10 | * If you are an individual writing original source code and you're sure you 11 | own the intellectual property, then you'll need to sign an [individual CLA] 12 | (https://cla.developers.google.com). 13 | * If you work for a company that wants to allow you to contribute your work, 14 | then you'll need to sign a [corporate CLA] 15 | (https://cla.developers.google.com). 16 | 17 | Follow either of the two links above to access the appropriate CLA and 18 | instructions for how to sign and return it. Once we receive it, we'll be able to 19 | accept your pull requests. 20 | 21 | ## Contributing A Patch 22 | 23 | 1. Submit an issue describing your proposed change to the repo in question. 24 | 1. The repo owner will respond to your issue promptly. 25 | 1. If your proposed change is accepted, and you haven't already done so, sign a 26 | Contributor License Agreement (see details above). 27 | 1. Fork the desired repo, develop and test your code changes. 28 | 1. Ensure that your code adheres to the existing style in the sample to which 29 | you are contributing. Refer to the 30 | [Android Code Style Guide] 31 | (https://source.android.com/source/code-style.html) for the 32 | recommended coding standards for this organization. 33 | 1. Ensure that your code has an appropriate set of unit tests which all pass. 34 | 1. Submit a pull request. 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | -------------- 3 | 4 | Version 2.0, January 2004 5 | http://www.apache.org/licenses/ 6 | 7 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 8 | 9 | 1. Definitions. 10 | 11 | "License" shall mean the terms and conditions for use, reproduction, 12 | and distribution as defined by Sections 1 through 9 of this document. 13 | 14 | "Licensor" shall mean the copyright owner or entity authorized by 15 | the copyright owner that is granting the License. 16 | 17 | "Legal Entity" shall mean the union of the acting entity and all 18 | other entities that control, are controlled by, or are under common 19 | control with that entity. For the purposes of this definition, 20 | "control" means (i) the power, direct or indirect, to cause the 21 | direction or management of such entity, whether by contract or 22 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 23 | outstanding shares, or (iii) beneficial ownership of such entity. 24 | 25 | "You" (or "Your") shall mean an individual or Legal Entity 26 | exercising permissions granted by this License. 27 | 28 | "Source" form shall mean the preferred form for making modifications, 29 | including but not limited to software source code, documentation 30 | source, and configuration files. 31 | 32 | "Object" form shall mean any form resulting from mechanical 33 | transformation or translation of a Source form, including but 34 | not limited to compiled object code, generated documentation, 35 | and conversions to other media types. 36 | 37 | "Work" shall mean the work of authorship, whether in Source or 38 | Object form, made available under the License, as indicated by a 39 | copyright notice that is included in or attached to the work 40 | (an example is provided in the Appendix below). 41 | 42 | "Derivative Works" shall mean any work, whether in Source or Object 43 | form, that is based on (or derived from) the Work and for which the 44 | editorial revisions, annotations, elaborations, or other modifications 45 | represent, as a whole, an original work of authorship. For the purposes 46 | of this License, Derivative Works shall not include works that remain 47 | separable from, or merely link (or bind by name) to the interfaces of, 48 | the Work and Derivative Works thereof. 49 | 50 | "Contribution" shall mean any work of authorship, including 51 | the original version of the Work and any modifications or additions 52 | to that Work or Derivative Works thereof, that is intentionally 53 | submitted to Licensor for inclusion in the Work by the copyright owner 54 | or by an individual or Legal Entity authorized to submit on behalf of 55 | the copyright owner. For the purposes of this definition, "submitted" 56 | means any form of electronic, verbal, or written communication sent 57 | to the Licensor or its representatives, including but not limited to 58 | communication on electronic mailing lists, source code control systems, 59 | and issue tracking systems that are managed by, or on behalf of, the 60 | Licensor for the purpose of discussing and improving the Work, but 61 | excluding communication that is conspicuously marked or otherwise 62 | designated in writing by the copyright owner as "Not a Contribution." 63 | 64 | "Contributor" shall mean Licensor and any individual or Legal Entity 65 | on behalf of whom a Contribution has been received by Licensor and 66 | subsequently incorporated within the Work. 67 | 68 | 2. Grant of Copyright License. Subject to the terms and conditions of 69 | this License, each Contributor hereby grants to You a perpetual, 70 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 71 | copyright license to reproduce, prepare Derivative Works of, 72 | publicly display, publicly perform, sublicense, and distribute the 73 | Work and such Derivative Works in Source or Object form. 74 | 75 | 3. Grant of Patent License. Subject to the terms and conditions of 76 | this License, each Contributor hereby grants to You a perpetual, 77 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 78 | (except as stated in this section) patent license to make, have made, 79 | use, offer to sell, sell, import, and otherwise transfer the Work, 80 | where such license applies only to those patent claims licensable 81 | by such Contributor that are necessarily infringed by their 82 | Contribution(s) alone or by combination of their Contribution(s) 83 | with the Work to which such Contribution(s) was submitted. If You 84 | institute patent litigation against any entity (including a 85 | cross-claim or counterclaim in a lawsuit) alleging that the Work 86 | or a Contribution incorporated within the Work constitutes direct 87 | or contributory patent infringement, then any patent licenses 88 | granted to You under this License for that Work shall terminate 89 | as of the date such litigation is filed. 90 | 91 | 4. Redistribution. You may reproduce and distribute copies of the 92 | Work or Derivative Works thereof in any medium, with or without 93 | modifications, and in Source or Object form, provided that You 94 | meet the following conditions: 95 | 96 | (a) You must give any other recipients of the Work or 97 | Derivative Works a copy of this License; and 98 | 99 | (b) You must cause any modified files to carry prominent notices 100 | stating that You changed the files; and 101 | 102 | (c) You must retain, in the Source form of any Derivative Works 103 | that You distribute, all copyright, patent, trademark, and 104 | attribution notices from the Source form of the Work, 105 | excluding those notices that do not pertain to any part of 106 | the Derivative Works; and 107 | 108 | (d) If the Work includes a "NOTICE" text file as part of its 109 | distribution, then any Derivative Works that You distribute must 110 | include a readable copy of the attribution notices contained 111 | within such NOTICE file, excluding those notices that do not 112 | pertain to any part of the Derivative Works, in at least one 113 | of the following places: within a NOTICE text file distributed 114 | as part of the Derivative Works; within the Source form or 115 | documentation, if provided along with the Derivative Works; or, 116 | within a display generated by the Derivative Works, if and 117 | wherever such third-party notices normally appear. The contents 118 | of the NOTICE file are for informational purposes only and 119 | do not modify the License. You may add Your own attribution 120 | notices within Derivative Works that You distribute, alongside 121 | or as an addendum to the NOTICE text from the Work, provided 122 | that such additional attribution notices cannot be construed 123 | as modifying the License. 124 | 125 | You may add Your own copyright statement to Your modifications and 126 | may provide additional or different license terms and conditions 127 | for use, reproduction, or distribution of Your modifications, or 128 | for any such Derivative Works as a whole, provided Your use, 129 | reproduction, and distribution of the Work otherwise complies with 130 | the conditions stated in this License. 131 | 132 | 5. Submission of Contributions. Unless You explicitly state otherwise, 133 | any Contribution intentionally submitted for inclusion in the Work 134 | by You to the Licensor shall be under the terms and conditions of 135 | this License, without any additional terms or conditions. 136 | Notwithstanding the above, nothing herein shall supersede or modify 137 | the terms of any separate license agreement you may have executed 138 | with Licensor regarding such Contributions. 139 | 140 | 6. Trademarks. This License does not grant permission to use the trade 141 | names, trademarks, service marks, or product names of the Licensor, 142 | except as required for reasonable and customary use in describing the 143 | origin of the Work and reproducing the content of the NOTICE file. 144 | 145 | 7. Disclaimer of Warranty. Unless required by applicable law or 146 | agreed to in writing, Licensor provides the Work (and each 147 | Contributor provides its Contributions) on an "AS IS" BASIS, 148 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 149 | implied, including, without limitation, any warranties or conditions 150 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 151 | PARTICULAR PURPOSE. You are solely responsible for determining the 152 | appropriateness of using or redistributing the Work and assume any 153 | risks associated with Your exercise of permissions under this License. 154 | 155 | 8. Limitation of Liability. In no event and under no legal theory, 156 | whether in tort (including negligence), contract, or otherwise, 157 | unless required by applicable law (such as deliberate and grossly 158 | negligent acts) or agreed to in writing, shall any Contributor be 159 | liable to You for damages, including any direct, indirect, special, 160 | incidental, or consequential damages of any character arising as a 161 | result of this License or out of the use or inability to use the 162 | Work (including but not limited to damages for loss of goodwill, 163 | work stoppage, computer failure or malfunction, or any and all 164 | other commercial damages or losses), even if such Contributor 165 | has been advised of the possibility of such damages. 166 | 167 | 9. Accepting Warranty or Additional Liability. While redistributing 168 | the Work or Derivative Works thereof, You may choose to offer, 169 | and charge a fee for, acceptance of support, warranty, indemnity, 170 | or other liability obligations and/or rights consistent with this 171 | License. However, in accepting such obligations, You may act only 172 | on Your own behalf and on Your sole responsibility, not on behalf 173 | of any other Contributor, and only if You agree to indemnify, 174 | defend, and hold each Contributor harmless for any liability 175 | incurred by, or claims asserted against, such Contributor by reason 176 | of your accepting any such warranty or additional liability. 177 | 178 | END OF TERMS AND CONDITIONS 179 | 180 | APPENDIX: How to apply the Apache License to your work. 181 | 182 | To apply the Apache License to your work, attach the following 183 | boilerplate notice, with the fields enclosed by brackets "{}" 184 | replaced with your own identifying information. (Don't include 185 | the brackets!) The text should be enclosed in the appropriate 186 | comment syntax for the file format. We also recommend that a 187 | file or class name and description of purpose be included on the 188 | same "printed page" as the copyright notice for easier 189 | identification within third-party archives. 190 | 191 | Copyright {yyyy} {name of copyright owner} 192 | 193 | Licensed under the Apache License, Version 2.0 (the "License"); 194 | you may not use this file except in compliance with the License. 195 | You may obtain a copy of the License at 196 | 197 | http://www.apache.org/licenses/LICENSE-2.0 198 | 199 | Unless required by applicable law or agreed to in writing, software 200 | distributed under the License is distributed on an "AS IS" BASIS, 201 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 202 | See the License for the specific language governing permissions and 203 | limitations under the License. 204 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This sample showcases an Android app that uses both [Flow](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/) and [Channel](https://kotlinlang.org/docs/reference/coroutines/channels.html) from Kotlin Coroutines. 2 | 3 | It also includes tests! Very important to have a maintainable application. 4 | 5 | ## Differences between Flow and Channel 6 | 7 | - Flow has a cold behavior, every time an observer applies a terminal operator on it, it'll start 8 | executing the code from the beginning. Channels are hot and they run even if there are no observers 9 | listening for events. With a regular Channel, only one observer will get the element emitted from the Channel. 10 | With a BroadcastChannel, all observers get the same element emitted, it broadcasts the emission of the element. 11 | 12 | - Use Channels when the producer and the consumer have different lifetimes. For example, a View and a 13 | ViewModel have different lifetimes, you may not want to consume a Flow from the View because it'll start 14 | execution every time that the View gets created (for instance) and there's no way to continue execution or get 15 | the last emitted value. For that, use Channels. Not maintaining state is really bad for configuration changes. 16 | 17 | - Normally, when creating a Channel, you specify the Dispatcher it'll execute its code on. However, this is not 18 | true when creating a Flow. In a Flow, you don't specify the dispatcher because it will be executed in the 19 | consumer's dispatcher by default. In case you want to modify it, you have the [`flowOn`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow-on.html) operator. 20 | 21 | - Channels should be an implementation detail in your app. Even if you need to create one because producer and consumer have 22 | different lifetimes, NEVER expose a Channel, expose a Flow instead. You can use the [`asFlow()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/as-flow.html) operator. 23 | 24 | ## Flow and Channels in the app 25 | 26 | The behavior of the app showcases how `Flow` and `Channel` work: 27 | 28 | - [`ColdFibonnaci`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/ColdFibonacciProducer.kt) is implemented with a `Flow` and exposed to the View with a [`LiveData`](https://developer.android.com/topic/libraries/architecture/livedata). 29 | Therefore, whenever the view is no longer present, it'll unobserve the `LiveData` that will propagate that cancellation 30 | to the Flow. Whenever the View is present, the `LiveData` will start observing the `Flow` again, and because it has a 31 | cold observable behavior, it will start the sequence from the beginning. 32 | 33 | - [`NeverEndingFibonacci`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/NeverEndingFibonacciProducer.kt) is implemented with a `Channel` instead of a `Flow`. Since Channels are hot, 34 | it'll keep emitting Fibonacci numbers even if there are no consumers listening for the events. We create the loop 35 | to emit items within a `launch` coroutine because we just want to start and forget about it, we don't want to 36 | return anything, we'll send the elements to the Channel. The View will consume/subscribe to this Channel by means of the Flow interface. 37 | When listening for number updates, if it unsubscribes from the Flow, it's ok, nothing happens, it'll keep producing numbers. 38 | Whenever it collects again (maybe after a configuration change) from the Flow, the consumer will receive the last item emitted 39 | to the Channel and the new ones as they're produced. 40 | 41 | - [`UserRepository`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/user/impl/UserRepository.kt) 42 | has the use case of returning a deferred computation. However, although it's not fully implemented, it has the logic 43 | of how you could expose a stream of User objects. Imagine that you want to handle user sessions and want to expose 44 | to the rest of the application the User that is logged in at any point. As with `NeverEndingFibonacci`, this functionality 45 | is agnostic of View lifecycle events and has its own lifetime and that's why it's also implemented with a 46 | `ConflatedBroadcastChannel`. 47 | 48 | 49 | 50 | ## Launching coroutines 51 | 52 | There are two clearly-defined ways to create coroutines: 53 | 54 | - Launch: This is "fire and forget" kind of coroutine. It doesn't return any value. E.g. a coroutine 55 | that logs something to console. We use this in [`ColdFibonacciProducer.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/ColdFibonacciProducer.kt) 56 | to start our Fibonacci computation, here we don't need to return a value since we're sending the numbers to the `Channel`. 57 | 58 | - Async: creates a Coroutine that returns a value. E.g. a coroutine that returns the response 59 | of a network request. We use it in [`UserRepository.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/user/impl/UserRepository.kt) 60 | where we create a coroutine to obtain the user information. Why we create a coroutine? Retrieving that information can be expensive and we might want to do it on a background thread. 61 | 62 | ## Learnings 63 | 64 | - `async` creates a coroutine that returns a value. 65 | 66 | - `launch` creates a coroutine meant as to "fire and forget". 67 | 68 | - `channelFlow` is a `Flow` with a `Channel` built in. This gives you the behavior of a cold 69 | stream (starts the block of code every time there's an observer) with the flexibility of a `Channel` 70 | (being able to send elements between coroutines). We defined a `flowChannel` in the 71 | [`ColdFibonacciProducer.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/ColdFibonacciProducer.kt) file. We call `send` to emit the new calculated Fibonacci number to 72 | the flow's observer. 73 | 74 | - `ConflatedBroadcastChannel` re-emits the last value emitted by the Channel to a new consumer 75 | that opens a subscription. This is what we use at [`NeverEndingFibonacciProducer.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/NeverEndingFibonacciProducer.kt) to create the 76 | never-ending Fibonacci. The coroutine created by `launch` has its own scope so until you don't cancel 77 | it's job, it'll continue producing numbers. All the numbers produced are sent to the Channel that 78 | can be consumed from the outside. Whenever there's a new subscription, the observer will get the last 79 | item emitted by the channel plus the new ones. 80 | 81 | - `liveData` Coroutines builder. You can find the code in [`MainViewModel.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/main/MainViewModel.kt). This builder creates 82 | a new coroutine (with its own scope) so that now you can call suspend functions inside the builder. 83 | In the builder, we call collect on the flow to consume the elements. The way we emit to the exposed 84 | `LiveData` is with `emit`. We call `emit` every time we get an element from the flow. Notice that 85 | `LiveData` only works when there's a listener on the other end. When there's an observer, `LiveData` will 86 | start consuming the flow and the flow will start from the beginning of the sequence. Whenever the 87 | View gets destroyed, the `LiveData` won't be observed and will propagate cancellation to the flow too. 88 | This functionality is available in the `androidx.lifecycle:lifecycle-livedata-ktx` library. 89 | 90 | - `lifecycleScope.launchWhenX` methods are available in `LifecycleOwners` such as Activities. For 91 | example, in [`MainActivity.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/main/MainActivity.kt) we use `lifecycleScope.launchWhenStarted` to create a coroutine that 92 | will get executed when the LifecycleOwner is at least `Started`. Inside that coroutine, we can 93 | consume the elements from the ColdFibonacci flow. 94 | This functionality is available in the `androidx.lifecycle:lifecycle-runtime-ktx` library. 95 | 96 | - We don't use `GlobalScope` in [`NeverEndingFibonacciProducer.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/NeverEndingFibonacciProducer.kt), we create a custom scope that 97 | we can cancel (great for testing). If you create coroutines with `GlobalScope` you manually have to track down 98 | every coroutine you create, whereas with a custom scope you can track them all together. 99 | 100 | - `supervisorScope` & `coroutineScope`. You can find this in [`UserRepository.kt`](https://github.com/manuelvicnt/MathCoroutinesFlow/blob/master/app/src/main/java/com/manuelvicnt/coroutinesflow/user/impl/UserRepository.kt). If you notice, `getUserAsync()` 101 | is a suspend function; we use `supervisorScope` to create a new scope out of the one that is calling 102 | the method. And this is because we need a scope to create new coroutines! Find a `supervisorScope` vs `coroutineScope` 103 | comparison in that file. Another thing to notice is that both of these functions suspend and wait for its children coroutines to finish before resuming. 104 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | apply plugin: 'kotlin-android' 4 | 5 | apply plugin: 'kotlin-android-extensions' 6 | 7 | apply plugin: 'kotlin-kapt' 8 | 9 | android { 10 | 11 | compileSdkVersion 29 12 | buildToolsVersion "29.0.2" 13 | 14 | defaultConfig { 15 | applicationId "com.manuelvicnt.coroutinesflow" 16 | minSdkVersion 21 17 | targetSdkVersion 29 18 | versionCode 1 19 | versionName "1.0" 20 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 21 | } 22 | 23 | kotlinOptions { 24 | jvmTarget = '1.8' 25 | } 26 | 27 | buildTypes { 28 | release { 29 | minifyEnabled false 30 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 31 | } 32 | } 33 | } 34 | 35 | dependencies { 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" 37 | 38 | def support_library_version = "1.1.0" 39 | implementation "androidx.appcompat:appcompat:$support_library_version" 40 | implementation "androidx.fragment:fragment-ktx:$support_library_version" 41 | implementation "androidx.core:core-ktx:$support_library_version" 42 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 43 | 44 | // Android Architecture Components 45 | def lifecycle_version = "2.2.0" 46 | implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version" 47 | implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" 48 | implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" 49 | implementation "androidx.lifecycle:lifecycle-runtime-ktx:$lifecycle_version" 50 | kapt "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" 51 | 52 | // Kotlin coroutines 53 | def coroutines_version = "1.3.6" 54 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version" 55 | 56 | // Testing 57 | testImplementation 'junit:junit:4.13' 58 | testImplementation "androidx.arch.core:core-testing:2.1.0" 59 | testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_version" 60 | 61 | androidTestImplementation 'androidx.test:runner:1.2.0' 62 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 63 | } 64 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 17 | 18 | 21 | 22 | 30 | 32 | 33 | 34 | 35 | 36 | 37 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/AppGraph.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow 18 | 19 | import androidx.lifecycle.ViewModel 20 | import androidx.lifecycle.ViewModelProvider 21 | import com.manuelvicnt.coroutinesflow.AppDIGraph.coldFibonacciProducer 22 | import com.manuelvicnt.coroutinesflow.AppDIGraph.neverEndingFibonacciProducer 23 | import com.manuelvicnt.coroutinesflow.AppDIGraph.userRepository 24 | import com.manuelvicnt.coroutinesflow.fibonacci.impl.ColdFibonacciProducer 25 | import com.manuelvicnt.coroutinesflow.fibonacci.impl.NeverEndingFibonacciProducer 26 | import com.manuelvicnt.coroutinesflow.main.MainViewModel 27 | import com.manuelvicnt.coroutinesflow.user.impl.UserRepository 28 | import com.manuelvicnt.coroutinesflow.user.UserViewModel 29 | import kotlinx.coroutines.ExperimentalCoroutinesApi 30 | import kotlinx.coroutines.FlowPreview 31 | import java.lang.IllegalStateException 32 | 33 | @FlowPreview 34 | @ExperimentalCoroutinesApi 35 | object AppDIGraph { 36 | val coldFibonacciProducer by lazy { ColdFibonacciProducer() } 37 | val neverEndingFibonacciProducer by lazy { NeverEndingFibonacciProducer() } 38 | val userRepository by lazy { UserRepository() } 39 | } 40 | 41 | @FlowPreview 42 | @ExperimentalCoroutinesApi 43 | object ViewModelFactory : ViewModelProvider.Factory { 44 | 45 | @Suppress("UNCHECKED_CAST") 46 | override fun create(modelClass: Class): T { 47 | if (modelClass == MainViewModel::class.java) { 48 | return MainViewModel(coldFibonacciProducer, neverEndingFibonacciProducer) as T 49 | } else if (modelClass == UserViewModel::class.java) { 50 | return UserViewModel(userRepository) as T 51 | } 52 | throw IllegalStateException() 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/ColdFibonacci.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow.fibonacci 18 | 19 | import kotlinx.coroutines.ExperimentalCoroutinesApi 20 | import kotlinx.coroutines.flow.Flow 21 | 22 | @ExperimentalCoroutinesApi 23 | interface ColdFibonacci { 24 | fun fibonacci(): Flow 25 | } 26 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/NeverEndingFibonacci.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow.fibonacci 18 | 19 | import kotlinx.coroutines.flow.Flow 20 | 21 | interface NeverEndingFibonacci { 22 | 23 | fun fibonacci(): Flow 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/ColdFibonacciProducer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow.fibonacci.impl 18 | 19 | import com.manuelvicnt.coroutinesflow.fibonacci.ColdFibonacci 20 | import kotlinx.coroutines.ExperimentalCoroutinesApi 21 | import kotlinx.coroutines.channels.SendChannel 22 | import kotlinx.coroutines.delay 23 | import kotlinx.coroutines.flow.Flow 24 | import kotlinx.coroutines.flow.channelFlow 25 | 26 | @ExperimentalCoroutinesApi 27 | class ColdFibonacciProducer : ColdFibonacci { 28 | 29 | /** 30 | * `channelFlow` creates an instance of the cold [Flow] with elements 31 | * that are sent to a [SendChannel]. This gives you the behavior of a cold stream 32 | * (starts the block of code every time there's an observer) with the flexibility 33 | * of a Channel (being able to send elements between coroutines). 34 | * 35 | * Every time an observer starts listening to the Flow (using a terminator operator such as 36 | * collect), the block of code will start from the beginning: while the consumer is present and 37 | * it hasn't cancelled its execution yet, a new number is calculated and sent to the Channel 38 | * with the method `send`. Then we wait, and repeat the loop until the consumer is no longer 39 | * present. 40 | */ 41 | override fun fibonacci() = channelFlow { 42 | var first = 1L 43 | var second = 1L 44 | while (true) { 45 | if (isClosedForSend) break // Stop if the consumer is no longer there 46 | 47 | val next = first + second 48 | send(next) 49 | first = second 50 | second = next 51 | delay(2000) 52 | } 53 | } 54 | } 55 | 56 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/fibonacci/impl/NeverEndingFibonacciProducer.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow.fibonacci.impl 18 | 19 | import androidx.annotation.VisibleForTesting 20 | import com.manuelvicnt.coroutinesflow.fibonacci.NeverEndingFibonacci 21 | import kotlinx.coroutines.* 22 | import kotlinx.coroutines.channels.ConflatedBroadcastChannel 23 | import kotlinx.coroutines.flow.Flow 24 | import kotlinx.coroutines.flow.asFlow 25 | 26 | @FlowPreview 27 | @ExperimentalCoroutinesApi 28 | class NeverEndingFibonacciProducer : NeverEndingFibonacci { 29 | 30 | private var started = false 31 | 32 | /** 33 | * We expose a Flow because we don't want consumers to receive a ConflatedBroadcastChannel type from our 34 | * _neverEndingFibonacci variable. Channels should be an implementation detail. ALWAYS expose Flow. 35 | */ 36 | @Synchronized 37 | override fun fibonacci(): Flow { 38 | if (!started) startNeverEndingFibonacci() 39 | return _neverEndingFibonacci.asFlow() 40 | } 41 | 42 | /** 43 | * We use a ConflatedBroadcastChannel because we want the last item emitted to the Channel to 44 | * be received by the consumers when they start subscribing to it. For example, this is useful 45 | * when a View goes through a configuration change and wants to restore to the last state it was in. 46 | */ 47 | private val _neverEndingFibonacci by lazy { 48 | ConflatedBroadcastChannel() 49 | } 50 | 51 | /** 52 | * This will stop the never ending Fibonacci channel forever. 53 | * Once you cancel a Job, you cannot open it again. 54 | * 55 | * The job will cancel the scope in which it's used and it'll propagate the cancellation to 56 | * its children coroutines. 57 | */ 58 | @VisibleForTesting 59 | fun stopNeverEndingFibonacci() { 60 | neverEndingFibonacciJob.cancel() 61 | } 62 | 63 | /** 64 | * When we start the never ending fibonacci, we create a coroutine with `launch` 65 | * which is a "fire and forget" kind of coroutine. We don't have to return any 66 | * value, we're going send what it produces to the ConflatedBroadcastChannel. 67 | * 68 | * This execution will stop when the `neverEndingFibonacciScope` gets cancelled. 69 | * 70 | * @param dispatcher Dispatcher to use for calculating Fibonacci numbers. Very useful for testing. 71 | */ 72 | @VisibleForTesting 73 | @Synchronized 74 | fun startNeverEndingFibonacci( 75 | dispatcher: CoroutineDispatcher = Dispatchers.Default 76 | ) = neverEndingFibonacciScope.launch(dispatcher) { 77 | started = true 78 | var first = 1L 79 | var second = 1L 80 | while (true) { 81 | val next = first + second 82 | System.out.println(next.toString()) 83 | _neverEndingFibonacci.send(next) 84 | first = second 85 | second = next 86 | delay(3000) // Since delay is a suspend function that handles cancellation, 87 | // when the scope that started this coroutine (neverEndingFibonacciScope) 88 | // is cancelled and the coroutine execution comes to this suspension point, 89 | // the coroutine will stop and will finish executing. If this suspension 90 | // point weren't here and we wouldn't delay it, then you'd have to use the 91 | // property isActive and do something like `if (!isActive) break` 92 | } 93 | } 94 | } 95 | 96 | private val neverEndingFibonacciJob = SupervisorJob() 97 | private val neverEndingFibonacciScope = CoroutineScope(neverEndingFibonacciJob) 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/manuelvicnt/coroutinesflow/main/MainActivity.kt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 The Android Open Source Project 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.manuelvicnt.coroutinesflow.main 18 | 19 | import android.content.Intent 20 | import android.os.Bundle 21 | import android.widget.Button 22 | import android.widget.TextView 23 | import androidx.activity.viewModels 24 | import androidx.appcompat.app.AppCompatActivity 25 | import androidx.lifecycle.lifecycleScope 26 | import androidx.lifecycle.observe 27 | import com.manuelvicnt.coroutinesflow.R 28 | import com.manuelvicnt.coroutinesflow.ViewModelFactory 29 | import com.manuelvicnt.coroutinesflow.user.UserActivity 30 | import kotlinx.coroutines.ExperimentalCoroutinesApi 31 | import kotlinx.coroutines.FlowPreview 32 | import kotlinx.coroutines.flow.collect 33 | 34 | @FlowPreview 35 | @ExperimentalCoroutinesApi 36 | class MainActivity : AppCompatActivity() { 37 | 38 | private val viewModel: MainViewModel by viewModels { ViewModelFactory } 39 | 40 | override fun onCreate(savedInstanceState: Bundle?) { 41 | super.onCreate(savedInstanceState) 42 | setContentView(R.layout.activity_main) 43 | 44 | prepareUI() 45 | } 46 | 47 | private fun prepareUI() { 48 | prepareColdFibonacci() 49 | prepareNeverEndingFibonacci() 50 | setUpSettingsButton() 51 | } 52 | 53 | /** 54 | * Listens to the LiveData exposed by the ViewModel 55 | * 56 | * When the View is destroyed, it'll unsubscribe from the LiveData and the flow will stop. 57 | * Whenever the View is created again, the flow will also start and will emit the 58 | * Fibonacci sequence from the beginning. 59 | */ 60 | private fun prepareColdFibonacci() { 61 | val coldFibonacciText = findViewById(R.id.cold_fibonacci) 62 | viewModel.coldFibonacci.observe(this) { 63 | coldFibonacciText.text = it.toString() 64 | } 65 | } 66 | 67 | /** 68 | * Listens to the Channel exposed by the ViewModel 69 | * 70 | * When the View is destroyed, it'll unsubscribe from the LiveData and the flow will stop. 71 | * Whenever the View is created again, the flow will also start and will emit the 72 | * Fibonacci sequence from the beginning. 73 | */ 74 | private fun prepareNeverEndingFibonacci() { 75 | val neverEndingFibonacciText = findViewById(R.id.never_ending_fibonacci) 76 | lifecycleScope.launchWhenStarted { 77 | viewModel.neverEndingFibonacci.collect { 78 | neverEndingFibonacciText.text = it.toString() 79 | } 80 | } 81 | } 82 | 83 | private fun setUpSettingsButton() { 84 | findViewById