-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add javadocs for new retrofit coroutines functions
- Loading branch information
Showing
8 changed files
with
376 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/com/livinglifetechway/k4kotlinsample/ApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.livinglifetechway.k4kotlinsample.RetrofitApi | ||
|
||
import com.livinglifetechway.k4kotlinsample.BuildConfig | ||
import okhttp3.OkHttpClient | ||
import okhttp3.logging.HttpLoggingInterceptor | ||
import retrofit2.Retrofit | ||
import retrofit2.converter.gson.GsonConverterFactory | ||
|
||
object ApiClient { | ||
val BASE_URL = "https://reqres.in/api/" | ||
private var retrofit: Retrofit? = null | ||
|
||
// add logging interceptor if DEBUG build | ||
private val client: Retrofit? | ||
get() { | ||
if (retrofit == null) { | ||
val builder = Retrofit.Builder() | ||
.baseUrl(BASE_URL) | ||
.addConverterFactory(GsonConverterFactory.create()) | ||
if (BuildConfig.DEBUG) { | ||
val client = OkHttpClient.Builder() | ||
val loggingInterceptor = HttpLoggingInterceptor() | ||
loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY | ||
client.addInterceptor(loggingInterceptor) | ||
builder.client(client.build()) | ||
} | ||
|
||
retrofit = builder.build() | ||
} | ||
return retrofit | ||
} | ||
|
||
val service: ApiInterface | ||
get() = client!!.create(ApiInterface::class.java) | ||
|
||
|
||
} |
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/livinglifetechway/k4kotlinsample/HomeActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.livinglifetechway.k4kotlinsample | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import com.livinglifetechway.k4kotlin.setBindingView | ||
import com.livinglifetechway.k4kotlinsample.databinding.ActivityHomeBinding | ||
|
||
class HomeActivity : AppCompatActivity() { | ||
|
||
lateinit var mBinding: ActivityHomeBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
mBinding = setBindingView(R.layout.activity_home) | ||
|
||
mBinding.btnMain.setOnClickListener { | ||
startActivity(Intent(this@HomeActivity, MainActivity::class.java)) | ||
} | ||
mBinding.btnAnim.setOnClickListener { | ||
startActivity(Intent(this@HomeActivity, AnimationActivity::class.java)) | ||
} | ||
mBinding.btnRetrofit.setOnClickListener { | ||
startActivity(Intent(this@HomeActivity, RetrofitActivity::class.java)) | ||
} | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
app/src/main/java/com/livinglifetechway/k4kotlinsample/RetrofitActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package com.livinglifetechway.k4kotlinsample | ||
|
||
import android.os.Bundle | ||
import android.support.v7.app.AppCompatActivity | ||
import com.livinglifetechway.k4kotlin.setBindingView | ||
import com.livinglifetechway.k4kotlin.show | ||
import com.livinglifetechway.k4kotlin.toast | ||
import com.livinglifetechway.k4kotlin_retrofit.* | ||
import com.livinglifetechway.k4kotlinsample.RetrofitApi.ApiClient | ||
import com.livinglifetechway.k4kotlinsample.databinding.ActivityRetrofitBinding | ||
import kotlinx.coroutines.experimental.android.UI | ||
import kotlinx.coroutines.experimental.async | ||
import kotlinx.coroutines.experimental.launch | ||
import kotlinx.coroutines.experimental.runBlocking | ||
|
||
class RetrofitActivity : AppCompatActivity() { | ||
|
||
lateinit var mBinding: ActivityRetrofitBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
mBinding = setBindingView(R.layout.activity_retrofit) | ||
|
||
// runBlocking { | ||
// val job = launch(UI) { | ||
// val enqueueAwait = ApiClient.service.getUserDetails().enqueueAwait(this@RetrofitActivity, RetrofitCallback { | ||
// progressView = mBinding.progressBar | ||
// | ||
// on200Ok { call, response -> | ||
// get original response object | ||
// } | ||
// }) | ||
// mBinding.tvResponse.text = enqueueAwait.toString() | ||
// } | ||
// } | ||
// mBinding.tvInfo.text = "This is runblocking" | ||
|
||
|
||
mBinding.btnEnqueue.setOnClickListener { | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
mBinding.tvInfo.append("Starting API call \n") | ||
ApiClient.service.getUserDetails().enqueue(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
on200Ok { call, response -> | ||
mBinding.tvInfo.append("Response Received \n") | ||
mBinding.tvResponse.text = response?.body()?.toString() | ||
} | ||
}) | ||
mBinding.tvInfo.append("API call enqueued \n") | ||
} | ||
|
||
// enqueue await | ||
mBinding.btnEnqueueAwait.setOnClickListener { | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
|
||
async(UI) { | ||
mBinding.tvInfo.append("Starting API call \n") | ||
val enqueueAwait = ApiClient.service.getUserDetails().enqueueAwait(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
|
||
onResponseCallback { call, response -> | ||
mBinding.tvInfo.append("Response Received \n") | ||
} | ||
|
||
}) | ||
mBinding.tvInfo.append("Await call complete \n") | ||
mBinding.tvResponse.text = enqueueAwait.toString() | ||
} | ||
} | ||
|
||
// enqueue await with error | ||
mBinding.btnEnqueueAwaitError.setOnClickListener { | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
|
||
val asyncTask = async(UI) { | ||
try { | ||
mBinding.tvInfo.append("Starting API call \n") | ||
val enqueueAwait = ApiClient.service.getUserDetailsError().enqueueAwait(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
|
||
onResponseCallback { call, response -> | ||
mBinding.tvInfo.append("Response Received \n") | ||
} | ||
|
||
}) | ||
mBinding.tvInfo.append("Await call complete \n") | ||
mBinding.tvResponse.text = enqueueAwait.toString() | ||
} catch (e: Exception) { | ||
mBinding.tvInfo.append("Crashed! \nException: ${e.message}") | ||
} | ||
} | ||
|
||
async(UI) { | ||
asyncTask.join() | ||
// you can check the state of the coroutine | ||
if (asyncTask.isCompletedExceptionally) toast("Exception") | ||
} | ||
} | ||
|
||
|
||
|
||
mBinding.btnEnqueueAwaitRunblocking.setOnClickListener { | ||
mBinding.progressBar.show() | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
|
||
runBlocking { | ||
launch(UI) { | ||
mBinding.tvInfo.append("Starting API call \n") | ||
val enqueueAwait = ApiClient.service.getUserDetails().enqueueAwait(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
|
||
on200Ok { call, response -> | ||
mBinding.tvInfo.append("Response received \n") | ||
} | ||
}) | ||
mBinding.tvInfo.append("Await call completed \n") | ||
mBinding.tvResponse.text = enqueueAwait.toString() | ||
} | ||
} | ||
mBinding.tvInfo.append("Run Blocking completed \n") | ||
} | ||
// | ||
// deferred response body | ||
mBinding.btnEnqueueDeferred.setOnClickListener { | ||
mBinding.progressBar.show() | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
|
||
async(UI) { | ||
mBinding.tvInfo.append("Starting API call \n") | ||
val enqueueDeferred = ApiClient.service.getUserDetails().enqueueDeferred(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
on200Ok { call, response -> | ||
mBinding.tvInfo.append("Response received \n") | ||
} | ||
}) | ||
mBinding.tvInfo.append("API called \n") | ||
val result = enqueueDeferred.await() | ||
mBinding.tvInfo.append("Await call complete \n") | ||
mBinding.tvResponse.text = result.toString() | ||
|
||
} | ||
} | ||
|
||
// deferred response | ||
mBinding.btnEnqueueDeferredResp.setOnClickListener { | ||
mBinding.progressBar.show() | ||
mBinding.tvResponse.text = "" | ||
mBinding.tvInfo.text = "" | ||
|
||
async(UI) { | ||
mBinding.tvInfo.append("Starting API call \n") | ||
val enqueueDeferredResponse = ApiClient.service.getUserDetails().enqueueDeferredResponse(this@RetrofitActivity, RetrofitCallback { | ||
progressView = mBinding.progressBar | ||
|
||
on200Ok { call, response -> | ||
mBinding.tvInfo.append("Response received \n") | ||
} | ||
}) | ||
mBinding.tvInfo.append("API called \n") | ||
val response = enqueueDeferredResponse.await() | ||
mBinding.tvInfo.append("Response Await Call Completed \n") | ||
val body = response.body() | ||
mBinding.tvResponse.text = body.toString() | ||
} | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<LinearLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_margin="8dp" | ||
android:gravity="center" | ||
android:orientation="vertical" | ||
tools:context="com.livinglifetechway.k4kotlinsample.HomeActivity"> | ||
|
||
<Button | ||
android:id="@+id/btn_main" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Core Functionality" /> | ||
|
||
<Button | ||
android:id="@+id/btn_anim" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Animation" /> | ||
|
||
<Button | ||
android:id="@+id/btn_retrofit" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:text="Retrofit" /> | ||
</LinearLayout> | ||
</layout> |
Oops, something went wrong.