@@ -5,105 +5,89 @@ import android.content.Context
5
5
import android.content.Intent
6
6
import android.os.Handler
7
7
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
13
11
import jp.studyplus.android.sdk.record.StudyRecord
14
- import java.io.IOException
15
12
16
- class Studyplus private constructor() {
13
+ class Studyplus (
14
+ context : Context ,
15
+ private val consumerKey : String ,
16
+ private val consumerSecret : String ,
17
+ ) {
17
18
18
- private var consumerKey: String? = null
19
- private var consumerSecret: String? = null
19
+ private val store = Preference (context)
20
20
21
21
/* *
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がすでに認証済みかどうかを判定
33
23
*
34
24
* @return true: 認証済み、 false: 未認証
35
25
* @since 2.0.0
36
26
*/
37
- fun isAuthenticated ( context : Context ): Boolean =
38
- CertificationStore .create(context.applicationContext). isAuthenticated()
27
+ @SuppressWarnings( " unused " )
28
+ fun isAuthenticated () = store. isAuthenticated
39
29
40
30
/* *
41
- * [setup]で設定されたConsumerKey , ConsumerSecretKeyによるStudyplus連携認証
31
+ * 設定されたConsumerKey , ConsumerSecretKeyによるStudyplus連携認証
42
32
*
43
33
* @since 2.0.0
44
34
*/
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)
50
38
51
39
/* *
52
40
* [startAuth]の結果をStudyplusSDKに保存
53
41
*
54
42
* @since 2.0.0
55
43
*/
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)
64
46
65
47
/* *
66
48
* Studyplus連携認証解除
67
49
*
68
50
* @since 2.6.1
69
51
*/
70
- fun cancelAuth (context : Context ) {
71
- CertificationStore .remove(context)
72
- }
52
+ @SuppressWarnings(" unused" )
53
+ fun cancelAuth () = store.remove()
73
54
74
55
/* *
75
56
* Studyplusとの認証情報を利用して学習記録をStudyplusへ投稿
76
57
*
77
58
* @since 2.0.0
78
59
*/
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
+ }
81
66
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
+ }
86
75
}
87
- }
88
76
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
+ }
92
81
}
93
- }
94
- })
82
+ })
95
83
}
96
84
97
85
private fun runOnUiThread (task : () -> Unit ) {
98
86
Handler (Looper .getMainLooper()).post { task() }
99
87
}
88
+ }
100
89
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 )
109
93
}
0 commit comments