Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to coroutine stable and kotlin 1.3 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 37 additions & 33 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,15 @@ apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

kotlin {
experimental {
coroutines "enable"
}
}

android {
compileSdkVersion Vers.androidCompileSdk
compileSdkVersion 28
defaultConfig {
applicationId "com.elpassion.crweather"
minSdkVersion Vers.androidMinSdk
targetSdkVersion Vers.androidTargetSdk
minSdkVersion 19
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner Vers.androidTestRunnerClass
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
Expand All @@ -27,29 +21,39 @@ android {
}
}

repositories {
mavenCentral()
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation Deps.kotlinStdlib7
implementation Deps.kotlinxCoroutinesCore
implementation Deps.kotlinxCoroutinesAndroid
implementation Deps.androidSupportAppcompat
implementation Deps.androidSupportV4
implementation Deps.androidSupportDesign
implementation Deps.androidSupportRecyclerview
implementation Deps.androidSupportCardview
implementation Deps.androidSupportConstraint
implementation Deps.androidArchLifecycleExtensions
implementation Deps.retrofit
implementation Deps.retrofitMoshi
implementation Deps.okhttpLogging
kapt Deps.androidArchLifecycleCompiler
testImplementation Deps.junit
androidTestImplementation(Deps.androidEspresso, {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

//card view
implementation "com.android.support:cardview-v7:28.0.0"

//rxbinding
implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding-support-v4:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding-appcompat-v7:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding-design:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding-recyclerview-v7:2.1.1'
implementation 'com.jakewharton.rxbinding2:rxbinding-leanback-v17:2.1.1'

// multidex
implementation 'com.android.support:multidex:1.0.3'

// LiveData + ViewModel
implementation "android.arch.lifecycle:extensions:1.1.1"

//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
api 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.11.0'
implementation "com.squareup.retrofit2:converter-moshi:2.4.0"

//coroutine
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
}
27 changes: 16 additions & 11 deletions app/src/main/java/com/elpassion/crweather/CommonUtils.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.elpassion.crweather

import kotlinx.coroutines.experimental.CancellableContinuation
import kotlinx.coroutines.experimental.suspendCancellableCoroutine
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.suspendCancellableCoroutine
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.ArrayList
import java.util.*
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

@Suppress("unused") val Any?.unit get() = Unit
@Suppress("unused")
val Any?.unit
get() = Unit

operator fun StringBuilder.plusAssign(string: String) = append(string).unit

Expand All @@ -26,8 +30,8 @@ suspend fun <T> Call<T>.await(): T = suspendCancellableCoroutine { continuation
continuation.invokeOnCancellation { cancel() }

val callback = object : Callback<T> {
override fun onFailure(call: Call<T>, t: Throwable) = continuation.tryToResume { throw t }
override fun onResponse(call: Call<T>, response: Response<T>) = continuation.tryToResume {
override fun onFailure(call: Call<T>, t: Throwable) = continuation.invokeOnCancellation { throw t }
override fun onResponse(call: Call<T>, response: Response<T>) = continuation.tryResume {
response.isSuccessful || throw IllegalStateException("Http error ${response.code()}")
response.body() ?: throw IllegalStateException("Response body is null")
}
Expand All @@ -36,9 +40,10 @@ suspend fun <T> Call<T>.await(): T = suspendCancellableCoroutine { continuation
enqueue(callback)
}

private inline fun <T> CancellableContinuation<T>.tryToResume(getter: () -> T) {
isActive || return
try { resume(getter()) }
catch (exception: Throwable) { resumeWithException(exception) }
private fun <T> CancellableContinuation<T>.tryResume(getter: () -> T) {
try {
resume(getter())
} catch (exception: Throwable) {
resumeWithException(exception)
}
}

16 changes: 11 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.0'
ext.roomVersion = '1.1.1'
ext.archLifecycleVersion = '1.1.1'
repositories {
google()
jcenter()
maven { url 'https://maven.google.com' }
}
dependencies {
classpath Deps.kotlinGradlePlugin
classpath Deps.androidGradlePlugin
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}

allprojects {
repositories {
jcenter()
google()
jcenter()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}
}
1 change: 0 additions & 1 deletion buildSrc

This file was deleted.