@@ -3,66 +3,90 @@ package jp.studyplus.android.sdk
3
3
import android.app.Activity
4
4
import android.content.Context
5
5
import android.content.Intent
6
+ import android.util.Log
6
7
import 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
11
8
import io.reactivex.schedulers.Schedulers
9
+ import jp.studyplus.android.sdk.internal.api.ApiClient
12
10
import jp.studyplus.android.sdk.internal.api.CertificationStore
11
+ import jp.studyplus.android.sdk.internal.auth.AuthTransit
12
+ import jp.studyplus.android.sdk.record.StudyRecord
13
13
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() {
24
15
25
16
private var consumerKey: String? = null
26
17
private var consumerSecret: String? = null
27
18
19
+ /* *
20
+ * ConsumerKey, ConsumerSecretKeyをStudyplus SDKに設定
21
+ *
22
+ * @since 2.0.0
23
+ */
28
24
fun setup (consumerKey : String , consumerSecret : String ) {
29
25
this .consumerKey = consumerKey
30
26
this .consumerSecret = consumerSecret
31
27
}
32
28
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()
36
37
38
+ /* *
39
+ * [setup]で設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
40
+ *
41
+ * @since 2.0.0
42
+ */
37
43
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." )
42
46
}
47
+
48
+ AuthTransit (consumerKey!! , consumerSecret!! ).start(activity, requestCode)
43
49
}
44
50
51
+ /* *
52
+ * [startAuth]の結果をStudyplusSDKに保存
53
+ *
54
+ * @since 2.0.0
55
+ */
45
56
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
48
60
}
61
+
62
+ CertificationStore .create(context.applicationContext).update(data)
49
63
}
50
64
65
+ /* *
66
+ * Studyplusとの認証情報を利用して学習記録をStudyplusへ投稿
67
+ *
68
+ * @since 2.0.0
69
+ */
51
70
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
+
52
75
ApiClient .apiClient.postStudyRecords(context, studyRecord)
53
76
.subscribeOn(Schedulers .newThread())
54
77
.observeOn(AndroidSchedulers .mainThread())
55
78
.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) }
66
81
)
67
82
}
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
+ }
68
92
}
0 commit comments