Skip to content

Commit eec8ceb

Browse files
committed
Update SDK
1 parent aa2d397 commit eec8ceb

File tree

14 files changed

+173
-205
lines changed

14 files changed

+173
-205
lines changed

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

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,105 +5,89 @@ import android.content.Context
55
import android.content.Intent
66
import android.os.Handler
77
import android.os.Looper
8-
import android.util.Log
9-
import jp.studyplus.android.sdk.internal.api.ApiClient
10-
import jp.studyplus.android.sdk.internal.api.CertificationStore
11-
import jp.studyplus.android.sdk.internal.api.PostCallback
12-
import jp.studyplus.android.sdk.internal.auth.AuthTransit
8+
import jp.studyplus.android.sdk.internal.api.Repository
9+
import jp.studyplus.android.sdk.internal.auth.Preference
10+
import jp.studyplus.android.sdk.internal.auth.Transit
1311
import jp.studyplus.android.sdk.record.StudyRecord
14-
import java.io.IOException
1512

16-
class Studyplus private constructor() {
13+
class Studyplus(
14+
context: Context,
15+
private val consumerKey: String,
16+
private val consumerSecret: String,
17+
) {
1718

18-
private var consumerKey: String? = null
19-
private var consumerSecret: String? = null
19+
private val store = Preference(context)
2020

2121
/**
22-
* ConsumerKey, ConsumerSecretKeyをStudyplus SDKに設定
23-
*
24-
* @since 2.0.0
25-
*/
26-
fun setup(consumerKey: String, consumerSecret: String) {
27-
this.consumerKey = consumerKey
28-
this.consumerSecret = consumerSecret
29-
}
30-
31-
/**
32-
* [setup]で設定されたConsumerKeyがすでに認証済みかどうかを判定
22+
* 設定されたConsumerKeyがすでに認証済みかどうかを判定
3323
*
3424
* @return true: 認証済み、 false: 未認証
3525
* @since 2.0.0
3626
*/
37-
fun isAuthenticated(context: Context): Boolean =
38-
CertificationStore.create(context.applicationContext).isAuthenticated()
27+
@SuppressWarnings("unused")
28+
fun isAuthenticated() = store.isAuthenticated
3929

4030
/**
41-
* [setup]で設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
31+
* 設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
4232
*
4333
* @since 2.0.0
4434
*/
45-
fun startAuth(activity: Activity, requestCode: Int) {
46-
check(consumerKey != null && consumerSecret != null) { "Please call setup method before this method call." }
47-
48-
AuthTransit(consumerKey!!, consumerSecret!!).start(activity, requestCode)
49-
}
35+
@SuppressWarnings("unused")
36+
fun startAuth(activity: Activity, requestCode: Int) =
37+
Transit(consumerKey, consumerSecret).start(activity, requestCode)
5038

5139
/**
5240
* [startAuth]の結果をStudyplusSDKに保存
5341
*
5442
* @since 2.0.0
5543
*/
56-
fun setAuthResult(context: Context, data: Intent?) {
57-
if (data == null) {
58-
Log.e("StudyplusSDK", "The data is null. Please check your code. If the data that received from Studyplus app is null, please contact Studyplus Dev team.")
59-
return
60-
}
61-
62-
CertificationStore.create(context.applicationContext).update(data)
63-
}
44+
@SuppressWarnings("unused")
45+
fun setAuthResult(data: Intent) = store.update(data)
6446

6547
/**
6648
* Studyplus連携認証解除
6749
*
6850
* @since 2.6.1
6951
*/
70-
fun cancelAuth(context: Context) {
71-
CertificationStore.remove(context)
72-
}
52+
@SuppressWarnings("unused")
53+
fun cancelAuth() = store.remove()
7354

7455
/**
7556
* Studyplusとの認証情報を利用して学習記録をStudyplusへ投稿
7657
*
7758
* @since 2.0.0
7859
*/
79-
fun postRecord(context: Context, studyRecord: StudyRecord, listener: OnPostRecordListener?) {
80-
check(isAuthenticated(context)) { "Please check your application's authentication before this method call." }
60+
@SuppressWarnings("unused")
61+
fun postRecord(record: StudyRecord, callback: PostCallback) {
62+
if (!isAuthenticated()) {
63+
callback.onFailure(StudyplusError.LoginRequired)
64+
return
65+
}
8166

82-
ApiClient.postStudyRecords(context, studyRecord, object : PostCallback {
83-
override fun onSuccess(recordId: Long?) {
84-
runOnUiThread {
85-
listener?.onResult(success = true, recordId = recordId)
67+
Repository().post(
68+
store = store,
69+
record = record,
70+
callback = object : PostCallback {
71+
override fun onSuccess() {
72+
runOnUiThread {
73+
callback.onSuccess()
74+
}
8675
}
87-
}
8876

89-
override fun onFailure(e: IOException) {
90-
runOnUiThread {
91-
listener?.onResult(success = false, throwable = e)
77+
override fun onFailure(e: StudyplusError) {
78+
runOnUiThread {
79+
callback.onFailure(e)
80+
}
9281
}
93-
}
94-
})
82+
})
9583
}
9684

9785
private fun runOnUiThread(task: () -> Unit) {
9886
Handler(Looper.getMainLooper()).post { task() }
9987
}
88+
}
10089

101-
companion object {
102-
interface OnPostRecordListener {
103-
fun onResult(success: Boolean, recordId: Long? = null, throwable: Throwable? = null)
104-
}
105-
106-
@JvmStatic
107-
val instance by lazy { Studyplus() }
108-
}
90+
interface PostCallback {
91+
fun onSuccess()
92+
fun onFailure(e: StudyplusError)
10993
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package jp.studyplus.android.sdk
2+
3+
sealed class StudyplusError {
4+
class IOException(val throwable: Throwable) : StudyplusError();
5+
object BadRequest : StudyplusError();
6+
object LoginRequired : StudyplusError();
7+
object ServerError : StudyplusError();
8+
object Unknown : StudyplusError();
9+
10+
companion object {
11+
fun byCode(code: Int) = when (code) {
12+
400 -> BadRequest
13+
401 -> LoginRequired
14+
in 500..599 -> ServerError
15+
else -> Unknown
16+
}
17+
}
18+
}

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

Lines changed: 0 additions & 13 deletions
This file was deleted.

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

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

Lines changed: 0 additions & 58 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package jp.studyplus.android.sdk.internal.api
33
import okhttp3.OkHttpClient
44
import java.util.concurrent.TimeUnit
55

6-
internal object ApiManager {
6+
internal object Manager {
77
val client: OkHttpClient = OkHttpClient.Builder()
88
.connectTimeout(60, TimeUnit.SECONDS)
99
.writeTimeout(60, TimeUnit.SECONDS)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package jp.studyplus.android.sdk.internal.api
2+
3+
import jp.studyplus.android.sdk.BuildConfig
4+
import jp.studyplus.android.sdk.PostCallback
5+
import jp.studyplus.android.sdk.StudyplusError
6+
import jp.studyplus.android.sdk.internal.auth.Preference
7+
import jp.studyplus.android.sdk.record.StudyRecord
8+
import okhttp3.Call
9+
import okhttp3.Callback
10+
import okhttp3.MediaType.Companion.toMediaType
11+
import okhttp3.OkHttpClient
12+
import okhttp3.Request
13+
import okhttp3.RequestBody.Companion.toRequestBody
14+
import okhttp3.Response
15+
import java.io.IOException
16+
17+
internal class Repository(private val client: OkHttpClient = Manager.client) {
18+
fun post(store: Preference, record: StudyRecord, callback: PostCallback) {
19+
val body = record
20+
.json()
21+
.toRequestBody("application/json; charset=utf-8".toMediaType())
22+
23+
val request = Request.Builder()
24+
.header("Accept", "application/json")
25+
.header("Content-type", "application/json")
26+
.header("Authorization", "OAuth ${store.accessToken}")
27+
.url("${BuildConfig.API_ENDPOINT}/v1/study_records")
28+
.post(body)
29+
.build()
30+
31+
client.newCall(request).enqueue(object : Callback {
32+
override fun onFailure(call: Call, e: IOException) {
33+
callback.onFailure(StudyplusError.IOException(e))
34+
}
35+
36+
override fun onResponse(call: Call, response: Response) {
37+
if (!response.isSuccessful) {
38+
callback.onFailure(StudyplusError.byCode(response.code))
39+
return
40+
}
41+
42+
callback.onSuccess()
43+
}
44+
})
45+
}
46+
}

0 commit comments

Comments
 (0)