Skip to content

Commit 03f792b

Browse files
authored
Merge pull request #34 from studyplus/v3.0.0
v3.0.0
2 parents aa2d397 + 6a61fd6 commit 03f792b

File tree

25 files changed

+294
-343
lines changed

25 files changed

+294
-343
lines changed

Gemfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ GEM
1313
colored2 (3.1.2)
1414
cork (0.3.0)
1515
colored2 (~> 3.1)
16-
danger (8.2.2)
16+
danger (8.2.3)
1717
claide (~> 1.0)
1818
claide-plugins (>= 0.9.2)
1919
colored2 (~> 3.1)
@@ -25,7 +25,7 @@ GEM
2525
kramdown-parser-gfm (~> 1.0)
2626
no_proxy_fix
2727
octokit (~> 4.7)
28-
terminal-table (~> 1)
28+
terminal-table (>= 1, < 4)
2929
danger-android_lint (0.0.8)
3030
danger-plugin-api (~> 1.0)
3131
oga
@@ -64,7 +64,7 @@ GEM
6464
sawyer (0.8.2)
6565
addressable (>= 2.3.5)
6666
faraday (> 0.8, < 2.0)
67-
terminal-table (1.8.0)
67+
terminal-table (3.0.0)
6868
unicode-display_width (~> 1.1, >= 1.1.1)
6969
unicode-display_width (1.7.0)
7070

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
buildscript {
44
ext {
5-
kotlin_version = '1.4.30'
5+
kotlin_version = '1.4.31'
66
}
77
ext.versions = [
8-
'compileSdk': 29,
8+
'compileSdk': 30,
99
'minSdk' : 21,
10-
'targetSdk' : 29,
10+
'targetSdk' : 30,
1111
]
1212
repositories {
1313
google()

sdk-example-kt/build.gradle

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ android {
1313
}
1414
buildTypes {
1515
release {
16+
signingConfig signingConfigs.debug
17+
1618
minifyEnabled false
1719
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1820
}
@@ -28,8 +30,4 @@ dependencies {
2830

2931
implementation "androidx.appcompat:appcompat:1.2.0"
3032
implementation "androidx.constraintlayout:constraintlayout:2.0.4"
31-
32-
testImplementation "junit:junit:4.13"
33-
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
34-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
3533
}

sdk-example-kt/src/androidTest/java/jp/studyplus/android/sdk_example_kt/ExampleInstrumentedTest.kt

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

sdk-example-kt/src/main/java/jp/studyplus/android/sdk_example_kt/MainActivity.kt

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import android.view.View
88
import android.widget.TextView
99
import android.widget.Toast
1010
import androidx.appcompat.app.AppCompatActivity
11+
import jp.studyplus.android.sdk.PostCallback
1112
import jp.studyplus.android.sdk.Studyplus
13+
import jp.studyplus.android.sdk.StudyplusError
1214
import jp.studyplus.android.sdk.record.StudyRecord
1315
import jp.studyplus.android.sdk.record.StudyRecordAmountTotal
1416

@@ -18,19 +20,22 @@ class MainActivity : AppCompatActivity() {
1820
const val REQUEST_CODE_AUTH = 1
1921
}
2022

23+
private val instance by lazy {
24+
Studyplus(
25+
context = this,
26+
consumerKey = getString(R.string.consumer_key),
27+
consumerSecret = getString(R.string.consumer_secret),
28+
)
29+
}
30+
2131
override fun onCreate(savedInstanceState: Bundle?) {
2232
super.onCreate(savedInstanceState)
2333
setContentView(R.layout.activity_main)
2434

25-
Studyplus.instance.setup(
26-
getString(R.string.consumer_key),
27-
getString(R.string.consumer_secret)
28-
)
29-
3035
findViewById<View>(R.id.start_auth)?.apply {
3136
setOnClickListener {
3237
try {
33-
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
38+
instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
3439
} catch (e: ActivityNotFoundException) {
3540
e.printStackTrace()
3641
Toast.makeText(context, "Need for Studyplus 5.+", Toast.LENGTH_LONG).show()
@@ -40,7 +45,7 @@ class MainActivity : AppCompatActivity() {
4045

4146
findViewById<View>(R.id.cancel_auth)?.apply {
4247
setOnClickListener {
43-
Studyplus.instance.cancelAuth(this@MainActivity)
48+
instance.cancelAuth()
4449
updateAuthText()
4550
}
4651
}
@@ -52,19 +57,15 @@ class MainActivity : AppCompatActivity() {
5257
amount = StudyRecordAmountTotal(30),
5358
comment = "勉強した!!!"
5459
)
55-
Studyplus.instance.postRecord(this@MainActivity, record,
56-
object : Studyplus.Companion.OnPostRecordListener {
57-
override fun onResult(success: Boolean, recordId: Long?, throwable: Throwable?) {
58-
if (success) {
59-
Toast.makeText(context, "Post Study Record!! ($recordId)", Toast.LENGTH_LONG).show()
60-
} else {
61-
throwable?.apply {
62-
Toast.makeText(context, "error!!", Toast.LENGTH_LONG).show()
63-
printStackTrace()
64-
}
65-
}
66-
}
67-
})
60+
instance.postRecord(record, object : PostCallback {
61+
override fun onSuccess() {
62+
Toast.makeText(context, "Post Study Record!!", Toast.LENGTH_LONG).show()
63+
}
64+
65+
override fun onFailure(e: StudyplusError) {
66+
Toast.makeText(context, "error!!", Toast.LENGTH_LONG).show()
67+
}
68+
})
6869
}
6970
}
7071
}
@@ -76,18 +77,24 @@ class MainActivity : AppCompatActivity() {
7677

7778
private fun updateAuthText() {
7879
val authStatusText = findViewById<TextView>(R.id.auth_status)
79-
authStatusText.text = when (Studyplus.instance.isAuthenticated(this)) {
80+
authStatusText.text = when (instance.isAuthenticated()) {
8081
true -> getString(R.string.status_authenticated)
8182
else -> getString(R.string.status_unauthenticated)
8283
}
8384
}
8485

8586
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
87+
super.onActivityResult(requestCode, resultCode, data)
88+
if (resultCode != RESULT_OK || data == null) {
89+
Toast.makeText(this, "error!!", Toast.LENGTH_LONG).show()
90+
return
91+
}
92+
8693
when (requestCode) {
8794
REQUEST_CODE_AUTH -> {
8895
if (resultCode == Activity.RESULT_OK) {
89-
Studyplus.instance.setAuthResult(this, data)
90-
Toast.makeText(this@MainActivity, "Success!!", Toast.LENGTH_LONG).show()
96+
instance.setAuthResult(data)
97+
Toast.makeText(this, "Success!!", Toast.LENGTH_LONG).show()
9198
}
9299
}
93100
}

sdk-example-kt/src/test/java/jp/studyplus/android/sdk_example_kt/ExampleUnitTest.kt

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

studyplus-android-sdk/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ android {
3838
}
3939

4040
dependencies {
41-
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.1'
41+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
4242

4343
implementation "androidx.annotation:annotation:1.1.0"
4444

4545
implementation platform("com.squareup.okhttp3:okhttp-bom:4.9.1")
4646
implementation "com.squareup.okhttp3:okhttp"
4747

48-
testImplementation "junit:junit:4.13"
49-
testImplementation "org.robolectric:robolectric:4.3.1"
48+
testImplementation "junit:junit:4.13.2"
49+
testImplementation "org.robolectric:robolectric:4.5.1"
5050
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
5151
testImplementation "com.squareup.okhttp3:mockwebserver"
5252
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="jp.studyplus.android.sdk">
3+
4+
<queries>
5+
<package android:name="jp.studyplus.android.app.debug" />
6+
</queries>
7+
8+
</manifest>

studyplus-android-sdk/src/main/AndroidManifest.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@
33

44
<uses-permission android:name="android.permission.INTERNET" />
55

6+
<queries>
7+
<package android:name="jp.studyplus.android.app" />
8+
</queries>
9+
610
</manifest>

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
}

0 commit comments

Comments
 (0)