├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── io │ │ └── github │ │ └── patrickyin │ │ └── coroutinesvsrx │ │ ├── MainActivity.kt │ │ └── data │ │ ├── CoroutineRestfulAPI.kt │ │ ├── HTTPClient.kt │ │ ├── RxRestfulAPI.kt │ │ └── model │ │ ├── Launch.kt │ │ ├── LaunchSite.kt │ │ ├── Rocket.kt │ │ └── RocketDetail.kt │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ └── activity_main.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 ├── 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 | 39 | # Keystore files 40 | # Uncomment the following line if you do not want to check your keystore files in. 41 | #*.jks 42 | 43 | # External native build folder generated in Android Studio 2.2 and later 44 | .externalNativeBuild 45 | 46 | # Google Services (e.g. APIs or Firebase) 47 | google-services.json 48 | 49 | # Freeline 50 | freeline.py 51 | freeline/ 52 | freeline_project_description.json 53 | 54 | # fastlane 55 | fastlane/report.xml 56 | fastlane/Preview.html 57 | fastlane/screenshots 58 | fastlane/test_output 59 | fastlane/readme.md 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - build-tools-28.0.2 6 | - android-28 7 | - extra-google-google_play_services 8 | - extra-google-m2repository 9 | - extra-android-m2repository 10 | - extra-android-support 11 | - addon-google_apis-google-28 12 | 13 | before_cache: 14 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 15 | - rm -fr $HOME/.gradle/caches/*/plugin-resolution/ 16 | cache: 17 | directories: 18 | - $HOME/.gradle/caches/ 19 | - $HOME/.gradle/wrapper/ 20 | - $HOME/.android/build-cache 21 | 22 | script: 23 | - ./gradlew build 24 | 25 | after_success: 26 | - bash <(curl -s https://codecov.io/bash) 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Patrick Yin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kotlin-coroutines-vs-rx-java 2 | 3 | [![Build Status](https://travis-ci.org/patrickyin/kotlin-coroutines-vs-rx.svg?branch=master)](https://travis-ci.org/patrickyin/kotlin-coroutines-vs-rx) 4 | 5 | This is a simple `Kotlin Coroutines vs. RxJava` sample. It calls SpaceX's api to get the next launch's deatil. 6 | 7 | ## Dependencies 8 | * RxJava2 9 | * RxAndroid 10 | * Kotlinx-coroutines 11 | * GSON 12 | * OKHTTP 13 | * Retrofit2 14 | * Retrofit2-adapter-rxjava2 15 | * Retrofit2-adapter-kotlin-coroutines 16 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | apply plugin: 'kotlin-android-extensions' 4 | apply plugin: 'kotlin-kapt' 5 | 6 | android { 7 | compileSdkVersion rootProject.ext.compile_sdk_version 8 | buildToolsVersion rootProject.ext.build_tools_version 9 | 10 | defaultConfig { 11 | applicationId rootProject.ext.application_id 12 | minSdkVersion rootProject.ext.mini_sdk_version 13 | targetSdkVersion rootProject.ext.target_sdk_version 14 | versionCode rootProject.ext.version_code 15 | versionName rootProject.ext.version_name 16 | testInstrumentationRunner rootProject.ext.test_runner 17 | } 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | useProguard false 22 | } 23 | } 24 | 25 | flavorDimensions "mode" 26 | 27 | productFlavors { 28 | def DEVELOPMENT_ENDPOINT = "https://api.spacexdata.com/v2/" 29 | development { 30 | dimension "mode" 31 | buildConfigField "String", "ENDPOINT", "\"" + DEVELOPMENT_ENDPOINT + "\"" 32 | } 33 | } 34 | } 35 | 36 | dependencies { 37 | implementation fileTree(dir: 'libs', include: ['*.jar']) 38 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" 39 | implementation "com.android.support:appcompat-v7:${rootProject.ext.support_library_version}" 40 | implementation "com.android.support:recyclerview-v7:${rootProject.ext.support_library_version}" 41 | implementation "com.android.support.constraint:constraint-layout:${rootProject.ext.constraint_layout_version}" 42 | 43 | implementation "com.squareup.retrofit2:retrofit:${rootProject.ext.retrofit2_version}" 44 | implementation "com.squareup.retrofit2:adapter-rxjava2:${rootProject.ext.retrofit2_version}" 45 | 46 | implementation "com.squareup.okhttp3:okhttp:${rootProject.ext.okhttp3_version}" 47 | 48 | implementation "com.google.code.gson:gson:${rootProject.ext.gson_version}" 49 | implementation "com.squareup.retrofit2:converter-gson:${rootProject.ext.retrofit2_version}" 50 | 51 | implementation "io.reactivex.rxjava2:rxjava:${rootProject.ext.rxjava2_version}" 52 | implementation "io.reactivex.rxjava2:rxandroid:${rootProject.ext.rxandroid_version}" 53 | 54 | implementation "com.jakewharton.retrofit:retrofit2-kotlin-coroutines-experimental-adapter:${rootProject.ext.retrofit2_coroutines_adapter_version}" 55 | implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:${rootProject.ext.coroutines_android_version}" 56 | 57 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 58 | testImplementation "junit:junit:${rootProject.ext.junit_version}" 59 | testImplementation "org.mockito:mockito-core:${rootProject.ext.mockito_version}" 60 | testImplementation "org.powermock:powermock-module-junit4:${rootProject.ext.powermock_version}" 61 | testImplementation "org.powermock:powermock-api-mockito2:${rootProject.ext.powermock_version}" 62 | androidTestImplementation "com.android.support.test:runner:${rootProject.ext.support_library_test_runner_version}" 63 | androidTestImplementation "com.android.support.test.espresso:espresso-core:${rootProject.ext.espresso_version}" 64 | } 65 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/patrick/Documents/android_sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx 2 | 3 | import android.os.Bundle 4 | import android.support.annotation.UiThread 5 | import android.support.v7.app.AppCompatActivity 6 | import io.github.patrickyin.coroutinesvsrx.data.HTTPClient 7 | import io.github.patrickyin.coroutinesvsrx.data.model.RocketDetail 8 | import io.reactivex.android.schedulers.AndroidSchedulers 9 | import io.reactivex.disposables.CompositeDisposable 10 | import io.reactivex.schedulers.Schedulers 11 | import kotlinx.android.synthetic.main.activity_main.* 12 | import kotlinx.coroutines.experimental.CommonPool 13 | import kotlinx.coroutines.experimental.android.UI 14 | import kotlinx.coroutines.experimental.cancel 15 | import kotlinx.coroutines.experimental.launch 16 | 17 | 18 | class MainActivity : AppCompatActivity() { 19 | 20 | private val disposables = CompositeDisposable() 21 | 22 | override fun onCreate(savedInstanceState: Bundle?) { 23 | super.onCreate(savedInstanceState) 24 | setContentView(R.layout.activity_main) 25 | 26 | clear.setOnClickListener { text_view.text = null } 27 | 28 | load_rx.setOnClickListener { fetchDataRx() } 29 | 30 | load_coroutines.setOnClickListener { fetchDataCoroutines() } 31 | } 32 | 33 | override fun onDestroy() { 34 | super.onDestroy() 35 | 36 | disposables.clear() 37 | CommonPool.cancel() 38 | } 39 | 40 | private fun fetchDataCoroutines() { 41 | val restfulAPI = HTTPClient.coroutineRestfulAPI 42 | launch(UI) { 43 | try { 44 | val nextLaunch = restfulAPI.getNextLaunch().await() 45 | val rocketDetail = restfulAPI.getRocket(nextLaunch.rocket?.rocketId ?: "").await() 46 | val result = formatString(rocketDetail) 47 | updateUI(result) 48 | } catch (e: Throwable) { 49 | showError(e) 50 | } 51 | } 52 | } 53 | 54 | private fun fetchDataRx() { 55 | val restfulAPI = HTTPClient.rxRestfulAPI 56 | 57 | val disposable = restfulAPI.getNextLaunch() 58 | .flatMap { restfulAPI.getRocket(it.rocket?.rocketId ?: "0") } 59 | .map { formatString(it) } 60 | .subscribeOn(Schedulers.io()) 61 | .observeOn(AndroidSchedulers.mainThread()) 62 | .subscribe({ text -> updateUI(text) }, { showError(it) }) 63 | disposables.add(disposable) 64 | } 65 | 66 | private fun formatString(rocketDetail: RocketDetail) = 67 | "Rocket name: ${rocketDetail.name}\nStages: ${rocketDetail.stages} \nCost per launch: ${rocketDetail.costPerLaunch}" 68 | 69 | private fun showError(throwable: Throwable) { 70 | text_view.text = throwable.localizedMessage 71 | } 72 | 73 | @UiThread 74 | private fun updateUI(text: String) { 75 | text_view.text = text 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/CoroutineRestfulAPI.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data 2 | 3 | import io.github.patrickyin.coroutinesvsrx.data.model.Launch 4 | import io.github.patrickyin.coroutinesvsrx.data.model.RocketDetail 5 | import kotlinx.coroutines.experimental.Deferred 6 | import retrofit2.http.GET 7 | import retrofit2.http.Path 8 | 9 | interface CoroutineRestfulAPI { 10 | @GET("launches/next") 11 | fun getNextLaunch(): Deferred 12 | 13 | @GET("rockets/{rocketId}") 14 | fun getRocket(@Path("rocketId") rocketId: String): Deferred 15 | } -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/HTTPClient.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data 2 | 3 | import com.jakewharton.retrofit2.adapter.kotlin.coroutines.experimental.CoroutineCallAdapterFactory 4 | import io.github.patrickyin.coroutinesvsrx.BuildConfig 5 | import retrofit2.CallAdapter 6 | import retrofit2.Retrofit 7 | import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory 8 | import retrofit2.converter.gson.GsonConverterFactory 9 | 10 | object HTTPClient { 11 | val rxRestfulAPI: RxRestfulAPI by lazy { 12 | initializeRetrofit(RxJava2CallAdapterFactory.create()) 13 | } 14 | 15 | val coroutineRestfulAPI: CoroutineRestfulAPI by lazy { 16 | initializeRetrofit(CoroutineCallAdapterFactory()) 17 | } 18 | 19 | private inline fun initializeRetrofit(retrofitFactory: CallAdapter.Factory): T { 20 | val retrofit = Retrofit.Builder() 21 | .baseUrl(BuildConfig.ENDPOINT) 22 | .addConverterFactory(GsonConverterFactory.create()) 23 | .addCallAdapterFactory(retrofitFactory) 24 | .build() 25 | 26 | return retrofit.create(T::class.java) 27 | } 28 | } -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/RxRestfulAPI.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data 2 | 3 | import io.github.patrickyin.coroutinesvsrx.data.model.Launch 4 | import io.github.patrickyin.coroutinesvsrx.data.model.RocketDetail 5 | import io.reactivex.Single 6 | import retrofit2.http.GET 7 | import retrofit2.http.Path 8 | 9 | interface RxRestfulAPI { 10 | @GET("launches/next") 11 | fun getNextLaunch(): Single 12 | 13 | @GET("rockets/{rocketId}") 14 | fun getRocket(@Path("rocketId") rocketId: String): Single 15 | } -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/model/Launch.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data.model 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | data class Launch( 6 | @SerializedName("rocket") 7 | var rocket: Rocket? = null, 8 | @SerializedName("launch_site") 9 | var launchSite: LaunchSite? = null 10 | ) 11 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/model/LaunchSite.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data.model 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | data class LaunchSite( 6 | @SerializedName("site_id") 7 | var siteId: String? = null, 8 | @SerializedName("site_name") 9 | var siteName: String? = null, 10 | @SerializedName("site_name_long") 11 | var siteNameLong: String? = null 12 | ) 13 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/model/Rocket.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data.model 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | data class Rocket( 6 | @SerializedName("rocket_id") 7 | var rocketId: String? = null 8 | ) 9 | -------------------------------------------------------------------------------- /app/src/main/java/io/github/patrickyin/coroutinesvsrx/data/model/RocketDetail.kt: -------------------------------------------------------------------------------- 1 | package io.github.patrickyin.coroutinesvsrx.data.model 2 | 3 | import com.google.gson.annotations.SerializedName 4 | 5 | data class RocketDetail( 6 | @SerializedName("name") 7 | var name: String? = null, 8 | @SerializedName("stages") 9 | var stages: String? = null, 10 | @SerializedName("cost_per_launch") 11 | var costPerLaunch: String? = null 12 | ) 13 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 22 | 23 |