@@ -5,105 +5,89 @@ import android.content.Context
55import android.content.Intent
66import android.os.Handler
77import 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
1311import 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}
0 commit comments