@@ -3,66 +3,90 @@ package jp.studyplus.android.sdk
33import android.app.Activity
44import android.content.Context
55import android.content.Intent
6+ import android.util.Log
67import io.reactivex.android.schedulers.AndroidSchedulers
7- import jp.studyplus.android.sdk.internal.auth.AuthTransit
8- import jp.studyplus.android.sdk.internal.api.ApiClient
9- import jp.studyplus.android.sdk.internal.api.notNull
10- import jp.studyplus.android.sdk.record.StudyRecord
118import io.reactivex.schedulers.Schedulers
9+ import jp.studyplus.android.sdk.internal.api.ApiClient
1210import jp.studyplus.android.sdk.internal.api.CertificationStore
11+ import jp.studyplus.android.sdk.internal.auth.AuthTransit
12+ import jp.studyplus.android.sdk.record.StudyRecord
1313
14- class Studyplus
15- private constructor () {
16- companion object {
17- interface OnPostRecordListener {
18- fun onResult (success : Boolean , recordId : Long? , throwable : Throwable ? )
19- }
20-
21- @JvmStatic
22- val instance by lazy { Studyplus () }
23- }
14+ class Studyplus private constructor() {
2415
2516 private var consumerKey: String? = null
2617 private var consumerSecret: String? = null
2718
19+ /* *
20+ * ConsumerKey, ConsumerSecretKeyをStudyplus SDKに設定
21+ *
22+ * @since 2.0.0
23+ */
2824 fun setup (consumerKey : String , consumerSecret : String ) {
2925 this .consumerKey = consumerKey
3026 this .consumerSecret = consumerSecret
3127 }
3228
33- fun isAuthenticated (context : Context ): Boolean {
34- return CertificationStore .create(context).isAuthenticated()
35- }
29+ /* *
30+ * [setup]で設定されたConsumerKeyがすでに認証済みかどうかを判定
31+ *
32+ * @return true: 認証済み、 false: 未認証
33+ * @since 2.0.0
34+ */
35+ fun isAuthenticated (context : Context ): Boolean =
36+ CertificationStore .create(context.applicationContext).isAuthenticated()
3637
38+ /* *
39+ * [setup]で設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
40+ *
41+ * @since 2.0.0
42+ */
3743 fun startAuth (activity : Activity , requestCode : Int ) {
38- notNull(consumerKey, consumerSecret) { consumerKey, consumerSecret ->
39- AuthTransit (consumerKey, consumerSecret).apply {
40- start(activity, requestCode)
41- }
44+ if (consumerKey == null || consumerSecret == null ) {
45+ throw IllegalStateException (" Please call setup method before this method call." )
4246 }
47+
48+ AuthTransit (consumerKey!! , consumerSecret!! ).start(activity, requestCode)
4349 }
4450
51+ /* *
52+ * [startAuth]の結果をStudyplusSDKに保存
53+ *
54+ * @since 2.0.0
55+ */
4556 fun setAuthResult (context : Context , data : Intent ? ) {
46- data?.also {
47- CertificationStore .create(context).update(it)
57+ if (data == null ) {
58+ Log .e(" StudyplusSDK" , " The data is null. Please check your code. If the received data is already null, please contact Studyplus Dev team." )
59+ return
4860 }
61+
62+ CertificationStore .create(context.applicationContext).update(data)
4963 }
5064
65+ /* *
66+ * Studyplusとの認証情報を利用して学習記録をStudyplusへ投稿
67+ *
68+ * @since 2.0.0
69+ */
5170 fun postRecord (context : Context , studyRecord : StudyRecord , listener : OnPostRecordListener ? ) {
71+ if (! isAuthenticated(context)) {
72+ throw IllegalStateException (" Please check your application's authentication before this method call." )
73+ }
74+
5275 ApiClient .apiClient.postStudyRecords(context, studyRecord)
5376 .subscribeOn(Schedulers .newThread())
5477 .observeOn(AndroidSchedulers .mainThread())
5578 .subscribe(
56- { response ->
57- listener?.apply {
58- onResult(true , response.recordId, null )
59- }
60- },
61- { throwable ->
62- listener?.apply {
63- onResult(false , null , throwable)
64- }
65- }
79+ { listener?.onResult(success = true , recordId = it.recordId) },
80+ { listener?.onResult(success = false , throwable = it) }
6681 )
6782 }
83+
84+ companion object {
85+ interface OnPostRecordListener {
86+ fun onResult (success : Boolean , recordId : Long? = null, throwable : Throwable ? = null)
87+ }
88+
89+ @JvmStatic
90+ val instance by lazy { Studyplus () }
91+ }
6892}
0 commit comments