Skip to content

Commit 89fb704

Browse files
committed
Remove Kotlin Coroutines
1 parent 5a072fb commit 89fb704

File tree

9 files changed

+76
-74
lines changed

9 files changed

+76
-74
lines changed

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
buildscript {
44
ext {
55
kotlin_version = '1.3.71'
6-
kotlin_cotoutines_version = '1.3.5'
76
}
87
ext.versions = [
98
'compileSdk' : 29,

studyplus-android-sdk/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,8 @@ android {
3838
}
3939

4040
dependencies {
41-
implementation fileTree(dir: 'libs', include: ['*.jar'])
4241
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
4342

44-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_cotoutines_version"
45-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_cotoutines_version"
46-
4743
implementation "androidx.annotation:annotation:$versions.annotation"
4844

4945
def okhttp = "4.4.1"

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/Studyplus.kt

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package jp.studyplus.android.sdk
33
import android.app.Activity
44
import android.content.Context
55
import android.content.Intent
6+
import android.os.Handler
7+
import android.os.Looper
68
import android.util.Log
79
import jp.studyplus.android.sdk.internal.api.ApiClient
810
import jp.studyplus.android.sdk.internal.api.CertificationStore
11+
import jp.studyplus.android.sdk.internal.api.PostCallback
912
import jp.studyplus.android.sdk.internal.auth.AuthTransit
1013
import jp.studyplus.android.sdk.record.StudyRecord
11-
import kotlinx.coroutines.runBlocking
14+
import java.io.IOException
1215

1316
class Studyplus private constructor() {
1417

@@ -32,7 +35,7 @@ class Studyplus private constructor() {
3235
* @since 2.0.0
3336
*/
3437
fun isAuthenticated(context: Context): Boolean =
35-
CertificationStore.create(context.applicationContext).isAuthenticated()
38+
CertificationStore.create(context.applicationContext).isAuthenticated()
3639

3740
/**
3841
* [setup]で設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
@@ -76,15 +79,23 @@ class Studyplus private constructor() {
7679
fun postRecord(context: Context, studyRecord: StudyRecord, listener: OnPostRecordListener?) {
7780
check(isAuthenticated(context)) { "Please check your application's authentication before this method call." }
7881

79-
runBlocking {
80-
try {
81-
val deferred = ApiClient.postStudyRecords(context, studyRecord)
82-
val recordId = deferred.await()
83-
listener?.onResult(success = true, recordId = recordId)
84-
} catch (t: Throwable) {
85-
listener?.onResult(success = false, throwable = t)
82+
ApiClient.postStudyRecords(context, studyRecord, object : PostCallback {
83+
override fun onSuccess(recordId: Long?) {
84+
runOnUiThread {
85+
listener?.onResult(success = true, recordId = recordId)
86+
}
8687
}
87-
}
88+
89+
override fun onFailure(e: IOException) {
90+
runOnUiThread {
91+
listener?.onResult(success = false, throwable = e)
92+
}
93+
}
94+
})
95+
}
96+
97+
private fun runOnUiThread(task: () -> Unit) {
98+
Handler(Looper.getMainLooper()).post { task() }
8899
}
89100

90101
companion object {

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/internal/api/ApiClient.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package jp.studyplus.android.sdk.internal.api
22

33
import android.content.Context
44
import jp.studyplus.android.sdk.record.StudyRecord
5-
import kotlinx.coroutines.Deferred
65

76
internal object ApiClient {
8-
fun postStudyRecords(context: Context, studyRecord: StudyRecord): Deferred<Long?> {
9-
return ApiService(ApiManager.client).post(CertificationStore.create(context).getOAuthAccessToken(), studyRecord.toJson())
10-
}
7+
fun postStudyRecords(context: Context, studyRecord: StudyRecord, callback: PostCallback) =
8+
ApiService(ApiManager.client).post(
9+
CertificationStore.create(context).getOAuthAccessToken(),
10+
studyRecord.toJson(),
11+
callback
12+
)
1113
}

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/internal/api/ApiManager.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import okhttp3.OkHttpClient
44
import java.util.concurrent.TimeUnit
55

66
internal object ApiManager {
7-
val client: OkHttpClient by lazy {
8-
OkHttpClient.Builder()
9-
.connectTimeout(60, TimeUnit.SECONDS)
10-
.writeTimeout(60, TimeUnit.SECONDS)
11-
.readTimeout(60, TimeUnit.SECONDS)
12-
.build()
13-
}
7+
val client: OkHttpClient = OkHttpClient.Builder()
8+
.connectTimeout(60, TimeUnit.SECONDS)
9+
.writeTimeout(60, TimeUnit.SECONDS)
10+
.readTimeout(60, TimeUnit.SECONDS)
11+
.build()
1412
}
Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package jp.studyplus.android.sdk.internal.api
22

33
import jp.studyplus.android.sdk.BuildConfig
4-
import kotlinx.coroutines.CompletableDeferred
5-
import kotlinx.coroutines.Deferred
64
import okhttp3.*
75
import okhttp3.MediaType.Companion.toMediaTypeOrNull
86
import okhttp3.RequestBody.Companion.toRequestBody
97
import org.json.JSONObject
108
import java.io.IOException
119

10+
internal interface PostCallback {
11+
fun onSuccess(recordId: Long?)
12+
fun onFailure(e: IOException)
13+
}
14+
1215
internal class ApiService(private val client: OkHttpClient) {
13-
fun post(auth: String, json: String): Deferred<Long?> {
16+
fun post(auth: String, json: String, callback: PostCallback) {
1417
val body = createPostBody(json)
1518
val request = Request.Builder()
1619
.header("Accept", HEADER_JSON)
@@ -20,7 +23,7 @@ internal class ApiService(private val client: OkHttpClient) {
2023
.post(body)
2124
.build()
2225

23-
return execute(client.newCall(request))
26+
execute(client, request, callback)
2427
}
2528

2629
companion object {
@@ -30,41 +33,35 @@ internal class ApiService(private val client: OkHttpClient) {
3033
}
3134

3235
private val JSON_MEDIA_TYPE = "application/json; charset=utf-8".toMediaTypeOrNull()
36+
private const val EMPTY_JSON = "{}"
37+
private const val RECORD_ID = "record_id"
38+
private const val INVALID_RECORD_ID = -1L
3339

3440
internal fun createPostBody(json: String) = json.toRequestBody(JSON_MEDIA_TYPE)
3541

36-
internal fun execute(call: Call): Deferred<Long?> {
37-
val deferred = CompletableDeferred<Long?>()
38-
deferred.invokeOnCompletion {
39-
if (deferred.isCancelled) {
40-
call.cancel()
41-
}
42-
}
43-
44-
call.enqueue(object : Callback {
45-
private val EMPTY_JSON = "{}"
46-
private val RECORD_ID = "record_id"
47-
private val INVALID_RECORD_ID = -1L
48-
42+
internal fun execute(client: OkHttpClient, request: Request, callback: PostCallback) {
43+
client.newCall(request).enqueue(object : Callback {
4944
override fun onFailure(call: Call, e: IOException) {
50-
deferred.completeExceptionally(e)
45+
callback.onFailure(e)
5146
}
5247

5348
override fun onResponse(call: Call, response: Response) {
54-
if (response.isSuccessful) {
55-
val parsedJson = JSONObject(response.body?.string() ?: EMPTY_JSON)
56-
val recordId = parsedJson.optLong(RECORD_ID, INVALID_RECORD_ID)
49+
if (!response.isSuccessful) {
50+
callback.onFailure(IOException(response.toString()))
51+
return
52+
}
5753

58-
if (recordId != INVALID_RECORD_ID) {
59-
deferred.complete(recordId)
60-
} else {
61-
deferred.complete(null)
62-
}
54+
val recordId = parseResponse(response)
55+
if (recordId != INVALID_RECORD_ID) {
56+
callback.onSuccess(recordId)
6357
} else {
64-
deferred.completeExceptionally(IOException(response.toString()))
58+
callback.onSuccess(null)
6559
}
6660
}
6761
})
62+
}
6863

69-
return deferred
64+
private fun parseResponse(response: Response): Long {
65+
val parsedJson = JSONObject(response.body?.string() ?: EMPTY_JSON)
66+
return parsedJson.optLong(RECORD_ID, INVALID_RECORD_ID)
7067
}

studyplus-android-sdk/src/test/java/jp/studyplus/android/sdk/ApiUnitTest.kt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package jp.studyplus.android.sdk
22

33
import android.os.Build
44
import jp.studyplus.android.sdk.internal.api.MockApiClient
5+
import jp.studyplus.android.sdk.internal.api.PostCallback
56
import jp.studyplus.android.sdk.record.StudyRecord
6-
import kotlinx.coroutines.runBlocking
77
import org.junit.Test
88
import org.junit.runner.RunWith
99
import org.robolectric.RobolectricTestRunner
1010
import org.robolectric.annotation.Config
11+
import java.io.IOException
1112
import kotlin.test.assertEquals
12-
import kotlin.test.assertNull
13+
import kotlin.test.fail
1314

1415
@RunWith(RobolectricTestRunner::class)
1516
@Config(sdk = [Build.VERSION_CODES.P])
@@ -18,15 +19,14 @@ class ApiUnitTest {
1819
@Test
1920
fun mockApi() {
2021
val record = StudyRecord(2 * 60)
21-
runBlocking {
22-
try {
23-
val deferred = MockApiClient.postStudyRecords(null, record)
24-
val recordId = deferred.await()
25-
22+
MockApiClient.postStudyRecords(null, record, object : PostCallback {
23+
override fun onSuccess(recordId: Long?) {
2624
assertEquals(recordId, 9999L)
27-
} catch (t: Throwable) {
28-
assertNull(t)
2925
}
30-
}
26+
27+
override fun onFailure(e: IOException) {
28+
fail("Failed Network Request")
29+
}
30+
})
3131
}
3232
}

studyplus-android-sdk/src/test/java/jp/studyplus/android/sdk/internal/api/MockApiClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ import android.content.Context
44
import jp.studyplus.android.sdk.record.StudyRecord
55

66
internal object MockApiClient {
7-
fun postStudyRecords(context: Context?, studyRecord: StudyRecord) =
8-
MockApiService(ApiManager.client).post(studyRecord.toJson())
7+
fun postStudyRecords(context: Context?, studyRecord: StudyRecord, callback: PostCallback) =
8+
MockApiService(ApiManager.client).post(studyRecord.toJson(), callback)
99
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package jp.studyplus.android.sdk.internal.api
22

3-
import kotlinx.coroutines.Deferred
43
import okhttp3.OkHttpClient
54
import okhttp3.Request
65
import okhttp3.mockwebserver.MockResponse
@@ -9,18 +8,18 @@ import org.json.JSONObject
98

109
internal class MockApiService(private val client: OkHttpClient) {
1110

12-
fun post(json: String): Deferred<Long?> {
11+
fun post(json: String, callback: PostCallback) {
1312
val server = MockWebServer()
14-
server.enqueue(MockResponse().setBody(JSONObject().apply { put("record_id", 9999L) }.toString()))
13+
server.enqueue(MockResponse().setBody(JSONObject().apply { put("record_id", 9999L) }
14+
.toString()))
1515
server.start()
1616

1717
val body = createPostBody(json)
1818
val request = Request.Builder()
19-
.url(server.url("/v1/study_records"))
20-
.post(body)
21-
.build()
22-
val call = client.newCall(request)
19+
.url(server.url("/v1/study_records"))
20+
.post(body)
21+
.build()
2322

24-
return execute(call)
23+
execute(client, request, callback)
2524
}
2625
}

0 commit comments

Comments
 (0)