Skip to content

Commit

Permalink
Add javadocs for new retrofit coroutines functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kirtan403 committed Dec 27, 2017
1 parent e4a8df6 commit c8dc215
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 8 deletions.
15 changes: 12 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.livinglifetechway.k4kotlinsample"
minSdkVersion 15
minSdkVersion 17
targetSdkVersion 27
versionCode 1
versionName "1.0"
Expand All @@ -32,13 +32,22 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.0.1'
implementation 'com.android.support:support-v4:27.0.1'
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
kapt 'com.android.databinding:compiler:3.0.0'
implementation 'org.jetbrains.anko:anko:0.10.2'
implementation project(':k4kotlin')

// for retrofit
implementation project(':k4kotlin-retrofit')
implementation "com.squareup.retrofit2:retrofit:2.3.0"
implementation "com.squareup.retrofit2:converter-gson:2.3.0"
implementation 'com.squareup.okhttp3:logging-interceptor:3.7.0'

implementation 'com.google.android:flexbox:0.3.1'
}


Expand Down
8 changes: 6 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.livinglifetechway.k4kotlinsample">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -11,14 +13,16 @@
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize">
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" />
<activity android:name=".AnimationActivity" />
<activity android:name=".HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".AnimationActivity"></activity>
<activity android:name=".RetrofitActivity" />
</application>

</manifest>
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)


}
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))
}
}
}
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()
}
}


}
}
31 changes: 31 additions & 0 deletions app/src/main/res/layout/activity_home.xml
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>
Loading

0 comments on commit c8dc215

Please sign in to comment.