Skip to content
This repository was archived by the owner on Jun 28, 2019. It is now read-only.

Commit 512e4c6

Browse files
committed
Kotlin Coroutinesの利用
1 parent d9b2896 commit 512e4c6

File tree

7 files changed

+48
-50
lines changed

7 files changed

+48
-50
lines changed

studyplus-android-sdk2/build.gradle

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ android {
3333
version = versionName
3434
group = "jp.studyplus.android.sdk"
3535

36+
consumerProguardFiles 'lib-proguard-rules.txt'
37+
3638
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3739
}
3840

@@ -44,9 +46,6 @@ android {
4446
initWith(buildTypes.debug)
4547
}
4648
release {
47-
minifyEnabled true
48-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
49-
5049
buildConfigField "String", "API_ENDPOINT", quote("https://external-api.studyplus.jp")
5150
}
5251
}
@@ -66,19 +65,18 @@ android {
6665
dependencies {
6766
implementation fileTree(dir: 'libs', include: ['*.jar'])
6867
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
68+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0'
69+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
6970

7071
implementation 'com.google.code.gson:gson:2.8.5'
71-
7272
implementation 'com.squareup.okhttp3:okhttp:3.11.0'
7373

7474
def retrofit = "2.4.0"
7575
implementation "com.squareup.retrofit2:retrofit:$retrofit"
76-
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit"
7776
implementation "com.squareup.retrofit2:converter-gson:$retrofit"
7877
testImplementation "com.squareup.retrofit2:retrofit-mock:$retrofit"
7978

80-
implementation "io.reactivex.rxjava2:rxjava:2.2.2"
81-
implementation "io.reactivex.rxjava2:rxandroid:2.1.0"
79+
implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2'
8280

8381
testImplementation 'junit:junit:4.12'
8482
}

studyplus-android-sdk2/proguard-rules.pro renamed to studyplus-android-sdk2/lib-proguard-rules.pro

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,14 @@
2020
# hide the original source file name.
2121
#-renamesourcefileattribute SourceFile
2222

23-
-keep public class jp.studyplus.android.sdk.* { public *; }
24-
-keep public class jp.studyplus.android.sdk.record.** { public *; }
23+
# ServiceLoader support
24+
-keepnames class kotlinx.coroutines.internal.MainDispatcherFactory {}
25+
-keepnames class kotlinx.coroutines.CoroutineExceptionHandler {}
26+
27+
# Most of volatile fields are updated with AFU and should not be mangled
28+
-keepclassmembernames class kotlinx.** {
29+
volatile <fields>;
30+
}
31+
32+
# Network Rsponse
33+
-keepnames class jp.studyplus.android.sdk.internal.api.response.** { *; }

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import android.app.Activity
44
import android.content.Context
55
import android.content.Intent
66
import android.util.Log
7-
import io.reactivex.android.schedulers.AndroidSchedulers
8-
import io.reactivex.schedulers.Schedulers
97
import jp.studyplus.android.sdk.internal.api.ApiClient
108
import jp.studyplus.android.sdk.internal.api.CertificationStore
119
import jp.studyplus.android.sdk.internal.auth.AuthTransit
1210
import jp.studyplus.android.sdk.record.StudyRecord
11+
import kotlinx.coroutines.runBlocking
1312

1413
class Studyplus private constructor() {
1514

@@ -72,13 +71,15 @@ class Studyplus private constructor() {
7271
throw IllegalStateException("Please check your application's authentication before this method call.")
7372
}
7473

75-
ApiClient.apiClient.postStudyRecords(context, studyRecord)
76-
.subscribeOn(Schedulers.newThread())
77-
.observeOn(AndroidSchedulers.mainThread())
78-
.subscribe(
79-
{ listener?.onResult(success = true, recordId = it.recordId) },
80-
{ listener?.onResult(success = false, throwable = it) }
81-
)
74+
runBlocking {
75+
try {
76+
val deferred = ApiClient.postStudyRecords(context, studyRecord)
77+
val result = deferred.await()
78+
listener?.onResult(success = true, recordId = result.recordId)
79+
} catch (t: Throwable) {
80+
listener?.onResult(success = false, throwable = t)
81+
}
82+
}
8283
}
8384

8485
companion object {
Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,14 @@
11
package jp.studyplus.android.sdk.internal.api
22

33
import android.content.Context
4-
import io.reactivex.Observable
54
import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse
65
import jp.studyplus.android.sdk.record.StudyRecord
7-
import retrofit2.Retrofit
6+
import kotlinx.coroutines.Deferred
87

9-
internal class ApiClient
10-
constructor(retrofit: Retrofit) {
8+
internal object ApiClient {
9+
private val apiService by lazy { ApiManager.retrofit.create(ApiService::class.java) }
1110

12-
companion object {
13-
val apiClient by lazy { ApiClient(ApiManager.retrofit) }
14-
lateinit var apiService: ApiService
15-
16-
private fun getOAuthAccessToken(context: Context): Observable<String> {
17-
return Observable.just(CertificationStore.create(context))
18-
.map { it.apiCertification() }
19-
.map { "OAuth $it" }
20-
}
21-
}
22-
23-
init {
24-
apiService = retrofit.create(ApiService::class.java)
25-
}
26-
27-
fun postStudyRecords(context: Context, studyRecord: StudyRecord): Observable<PostStudyRecordsResponse> {
28-
return getOAuthAccessToken(context).flatMap { apiService.postStudyRecords(it, studyRecord) }
11+
fun postStudyRecords(context: Context, studyRecord: StudyRecord): Deferred<PostStudyRecordsResponse> {
12+
return apiService.postStudyRecords(CertificationStore.create(context).getOAuthAccessToken(), studyRecord)
2913
}
30-
}
14+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package jp.studyplus.android.sdk.internal.api
22

3+
import com.jakewharton.retrofit2.adapter.kotlin.coroutines.CoroutineCallAdapterFactory
34
import jp.studyplus.android.sdk.BuildConfig
45
import okhttp3.OkHttpClient
56
import retrofit2.Retrofit
6-
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
77
import retrofit2.converter.gson.GsonConverterFactory
88
import java.util.concurrent.TimeUnit
99

1010
internal object ApiManager {
1111

12-
private val client by lazy {
12+
private val client: OkHttpClient by lazy {
1313
OkHttpClient.Builder()
1414
.connectTimeout(60, TimeUnit.SECONDS)
1515
.writeTimeout(60, TimeUnit.SECONDS)
1616
.readTimeout(60, TimeUnit.SECONDS)
1717
.build()
1818
}
1919

20-
val retrofit by lazy {
20+
val retrofit: Retrofit by lazy {
2121
Retrofit.Builder()
2222
.client(client)
2323
.baseUrl(BuildConfig.API_ENDPOINT)
24-
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
24+
.addCallAdapterFactory(CoroutineCallAdapterFactory())
2525
.addConverterFactory(GsonConverterFactory.create())
26-
.build()!!
26+
.build()
2727
}
2828
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package jp.studyplus.android.sdk.internal.api
22

3-
import io.reactivex.Observable
43
import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse
54
import jp.studyplus.android.sdk.record.StudyRecord
6-
import retrofit2.http.*
5+
import kotlinx.coroutines.Deferred
6+
import retrofit2.http.Body
7+
import retrofit2.http.Header
8+
import retrofit2.http.Headers
9+
import retrofit2.http.POST
710

811
internal interface ApiService {
912
@Headers(value = [
@@ -13,6 +16,6 @@ internal interface ApiService {
1316
@POST("/v1/study_records")
1417
fun postStudyRecords(
1518
@Header("Authorization") oauth: String,
16-
@Body studyRecord: StudyRecord)
17-
: Observable<PostStudyRecordsResponse>
19+
@Body studyRecord: StudyRecord
20+
): Deferred<PostStudyRecordsResponse>
1821
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ private constructor(private val preferences: SharedPreferences) {
2929
return !token.isNullOrEmpty()
3030
}
3131

32-
fun apiCertification(): String = preferences.getString(KEY_ACCESS_TOKEN, "")
32+
fun getOAuthAccessToken(): String {
33+
val certification = preferences.getString(KEY_ACCESS_TOKEN, "")
34+
return "OAuth $certification"
35+
}
3336

3437
fun update(data: Intent) {
3538
val code = data.getStringExtra(EXTRA_SP_AUTH_RESULT_CODE).orEmpty()

0 commit comments

Comments
 (0)