Skip to content

Commit

Permalink
Add RetrofitExtension to the lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgr committed Aug 29, 2016
1 parent 3abe36a commit 7924825
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
18 changes: 0 additions & 18 deletions app/src/main/kotlin/co/metalab/asyncawaitsample/AwaitExtensions.kt

This file was deleted.

43 changes: 28 additions & 15 deletions app/src/main/kotlin/co/metalab/asyncawaitsample/GithubActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package co.metalab.asyncawaitsample

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import co.metalab.asyncawait.AsyncController
import co.metalab.asyncawait.RetrofitHttpError
import co.metalab.asyncawait.async
import co.metalab.asyncawait.awaitSuccessful
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_github.*
import retrofit2.Call
Expand Down Expand Up @@ -33,42 +34,54 @@ class GitHubActivity : AppCompatActivity() {
}

private fun refreshRepos() = async {
txtRepos.text = ""
progressBar.isIndeterminate = true
progressBar.visibility = View.VISIBLE
btnGetRepos.isEnabled = false
txtStatus.text = "Loading repos list..."
showLoadingUi()

val userName = txtUserName.text.toString()
reposList = await(github.listRepos(userName))
reposList = awaitSuccessful(github.listRepos(userName))
showRepos(reposList)

progressBar.isIndeterminate = false
reposList.forEachIndexed { i, repo ->
txtStatus.text = "Loading info for ${repo.name}..."
progressBar.progress = i * 100 / reposList.size
val repoDetails = await(github.repoDetails(userName, repo.name))
val repoDetails = awaitSuccessful(github.repoDetails(userName, repo.name))
repo.stars = repoDetails.stargazers_count
showRepos(reposList)
}

hideLoadingUi()
}.onError {
progressBar.visibility = View.INVISIBLE
btnGetRepos.isEnabled = true
txtStatus.text = "Done."
}.onError {

val errorMessage = getErrorMessage(it)

txtStatus.text = errorMessage
Log.e(TAG, errorMessage, it)
}

private fun showLoadingUi() {
txtRepos.text = ""
progressBar.isIndeterminate = true
progressBar.visibility = View.VISIBLE
btnGetRepos.isEnabled = false
txtStatus.text = "Loading repos list..."
}

private fun hideLoadingUi() {
progressBar.visibility = View.INVISIBLE
btnGetRepos.isEnabled = true
txtStatus.text = "Done."
}

val errorMessage = if (it is RetrofitHttpException) {
private fun getErrorMessage(it: Exception): String {
return if (it is RetrofitHttpError) {
val httpErrorCode = it.errorResponse.code()
val errorResponse = Gson().fromJson(it.errorResponse.errorBody().string(), GithubErrorResponse::class.java)
"[$httpErrorCode] ${errorResponse.message}"
} else {
"Couldn't load repos (${it.message})"
}

txtStatus.text = errorMessage
Log.e(TAG, errorMessage, it)
}

private fun showRepos(reposResponse: List<Repo>) {
Expand Down
2 changes: 2 additions & 0 deletions asyncawait/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ android {

defaultConfig {
minSdkVersion 15
//noinspection OldTargetApi
targetSdkVersion 23
versionCode 1
versionName "1"
Expand All @@ -38,6 +39,7 @@ android {

dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.squareup.retrofit2:retrofit:2.1.0'
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.1.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package co.metalab.asyncawait

import retrofit2.Call
import retrofit2.Response

suspend fun <V> AsyncController.await(call: Call<V>, machine: Continuation<Response<V>>) {
this.await({ call.execute() }, machine)
}

suspend fun <V> AsyncController.awaitSuccessful(call: Call<V>, machine: Continuation<V>) {
this.await({
val response = call.execute()
if (response.isSuccessful) {
response.body()
} else {
throw RetrofitHttpError(response)
}
}, machine)
}

class RetrofitHttpError(val errorResponse: Response<*>) : RuntimeException("${errorResponse.code()}")

0 comments on commit 7924825

Please sign in to comment.