Skip to content

Commit 9a083b8

Browse files
authored
Merge pull request #35 from studyplus/preparing_for_release
Preparing for release
2 parents 03f792b + ff37971 commit 9a083b8

File tree

4 files changed

+99
-86
lines changed

4 files changed

+99
-86
lines changed

README.md

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,64 @@
1515
Add it in your root build.gradle at the end of repositories:
1616

1717
```groovy
18-
allprojects {
19-
repositories {
20-
...
21-
maven { url 'https://jitpack.io' }
18+
allprojects {
19+
repositories {
20+
maven { url 'https://jitpack.io' }
2221
}
2322
}
2423
```
2524

2625
```groovy
27-
dependencies {
28-
implementation 'com.github.studyplus:Studyplus-Android-SDK:2.7.0'
29-
}
26+
dependencies {
27+
implementation 'com.github.studyplus:Studyplus-Android-SDK:3.0.0'
28+
}
3029
```
3130

3231
## Usage
3332

3433
### Setup
3534

35+
If you want to handle StudyplusSDK instance as a singleton, use [Dagger](https://dagger.dev).
36+
3637
```kotlin
37-
Studyplus.instance.setup("consumer_key", "consumer_secret")
38+
private val instance by lazy {
39+
Studyplus(
40+
context = this,
41+
consumerKey = "consumer_key",
42+
consumerSecret = "consumer_secret",
43+
)
44+
}
3845
```
3946

4047
### Authenticate
4148

4249
Open an Activity to connect with Studyplus.
4350

4451
```kotlin
45-
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
52+
instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
4653
```
4754

48-
Then save its result.
55+
Then save the result.
4956

5057
```kotlin
5158
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
52-
when (requestCode) {
53-
REQUEST_CODE_AUTH -> {
54-
if (resultCode == Activity.RESULT_OK) {
55-
Studyplus.instance.setAuthResult(this, data)
56-
Toast.makeText(this@MainActivity, "Success!!", Toast.LENGTH_LONG).show()
57-
}
58-
}
59+
super.onActivityResult(requestCode, resultCode, data)
60+
if (resultCode != RESULT_OK || data == null) {
61+
Toast.makeText(this, "error!!", Toast.LENGTH_LONG).show()
62+
return
63+
}
64+
65+
if (requestCode == REQUEST_CODE_AUTH) {
66+
instance.setAuthResult(data)
67+
Toast.makeText(this, "Success!!", Toast.LENGTH_LONG).show()
5968
}
6069
}
6170
```
6271

6372
### Unauth
6473

6574
```kotlin
66-
Studyplus.instance.cancelAuth(this@MainActivity)
75+
instance.cancelAuth()
6776
```
6877

6978
### Post a record to Studyplus
@@ -76,19 +85,15 @@ val record = StudyRecord(
7685
amount = StudyRecordAmountTotal(30),
7786
comment = "勉強した!!!",
7887
)
79-
Studyplus.instance.postRecord(this@MainActivity, record,
80-
object : Studyplus.Companion.OnPostRecordListener {
81-
override fun onResult(success: Boolean, recordId: Long?, throwable: Throwable?) {
82-
if (success) {
83-
Toast.makeText(context, "Post Study Record!! ($recordId)", Toast.LENGTH_LONG).show()
84-
} else {
85-
throwable?.apply {
86-
Toast.makeText(context, "error!!", Toast.LENGTH_LONG).show()
87-
printStackTrace()
88-
}
89-
}
90-
}
91-
})
88+
instance.postRecord(record, object : PostCallback {
89+
override fun onSuccess() {
90+
Toast.makeText(context, "Post Study Record!!", Toast.LENGTH_LONG).show()
91+
}
92+
93+
override fun onFailure(e: StudyplusError) {
94+
Toast.makeText(context, "error!!", Toast.LENGTH_LONG).show()
95+
}
96+
})
9297
```
9398

9499
### More

sdk-example-kt/build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ apply plugin: 'kotlin-android'
33

44
android {
55
compileSdkVersion versions.compileSdk
6+
7+
buildFeatures {
8+
viewBinding true
9+
}
10+
611
defaultConfig {
712
applicationId "jp.studyplus.android.sdk_example_kt"
813
minSdkVersion versions.minSdk
@@ -11,6 +16,7 @@ android {
1116
versionName "1.0"
1217
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1318
}
19+
1420
buildTypes {
1521
release {
1622
signingConfig signingConfigs.debug
@@ -19,6 +25,7 @@ android {
1925
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2026
}
2127
}
28+
2229
compileOptions {
2330
sourceCompatibility = 1.8
2431
targetCompatibility = 1.8
Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package jp.studyplus.android.sdk_example_kt
22

3-
import android.app.Activity
43
import android.content.ActivityNotFoundException
54
import android.content.Intent
65
import android.os.Bundle
7-
import android.view.View
8-
import android.widget.TextView
96
import android.widget.Toast
107
import androidx.appcompat.app.AppCompatActivity
8+
import androidx.lifecycle.Lifecycle
9+
import androidx.lifecycle.LifecycleEventObserver
10+
import androidx.lifecycle.LifecycleOwner
1111
import jp.studyplus.android.sdk.PostCallback
1212
import jp.studyplus.android.sdk.Studyplus
1313
import jp.studyplus.android.sdk.StudyplusError
1414
import jp.studyplus.android.sdk.record.StudyRecord
1515
import jp.studyplus.android.sdk.record.StudyRecordAmountTotal
16+
import jp.studyplus.android.sdk_example_kt.databinding.ActivityMainBinding
1617

17-
class MainActivity : AppCompatActivity() {
18+
class MainActivity : AppCompatActivity(R.layout.activity_main) {
1819

1920
companion object {
20-
const val REQUEST_CODE_AUTH = 1
21+
const val REQUEST_CODE_AUTH = 112
2122
}
2223

2324
private val instance by lazy {
@@ -30,56 +31,55 @@ class MainActivity : AppCompatActivity() {
3031

3132
override fun onCreate(savedInstanceState: Bundle?) {
3233
super.onCreate(savedInstanceState)
33-
setContentView(R.layout.activity_main)
34+
val binding = ActivityMainBinding.inflate(layoutInflater)
35+
setContentView(binding.root)
3436

35-
findViewById<View>(R.id.start_auth)?.apply {
36-
setOnClickListener {
37-
try {
38-
instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
39-
} catch (e: ActivityNotFoundException) {
40-
e.printStackTrace()
41-
Toast.makeText(context, "Need for Studyplus 5.+", Toast.LENGTH_LONG).show()
37+
lifecycle.addObserver(object : LifecycleEventObserver {
38+
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
39+
when (event) {
40+
Lifecycle.Event.ON_RESUME -> {
41+
binding.authStatus.text = currentAuthState()
42+
}
43+
else -> {
44+
// nop
45+
}
4246
}
4347
}
44-
}
48+
})
4549

46-
findViewById<View>(R.id.cancel_auth)?.apply {
47-
setOnClickListener {
48-
instance.cancelAuth()
49-
updateAuthText()
50+
binding.startAuth.setOnClickListener {
51+
try {
52+
instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
53+
} catch (e: ActivityNotFoundException) {
54+
e.printStackTrace()
55+
Toast
56+
.makeText(this@MainActivity, "Need for Studyplus 5.+", Toast.LENGTH_LONG)
57+
.show()
5058
}
5159
}
52-
53-
findViewById<View>(R.id.post_study_record)?.apply {
54-
setOnClickListener {
55-
val record = StudyRecord(
56-
duration = 2 * 60,
57-
amount = StudyRecordAmountTotal(30),
58-
comment = "勉強した!!!"
59-
)
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-
})
69-
}
60+
binding.cancelAuth.setOnClickListener {
61+
instance.cancelAuth()
62+
binding.authStatus.text = currentAuthState()
7063
}
71-
}
72-
73-
override fun onResume() {
74-
super.onResume()
75-
updateAuthText()
76-
}
64+
binding.postStudyRecord.setOnClickListener {
65+
val record = StudyRecord(
66+
duration = 2 * 60,
67+
amount = StudyRecordAmountTotal(30),
68+
comment = "勉強した!!!",
69+
)
70+
instance.postRecord(record, object : PostCallback {
71+
override fun onSuccess() {
72+
Toast
73+
.makeText(this@MainActivity, "Post Study Record!!", Toast.LENGTH_LONG)
74+
.show()
75+
}
7776

78-
private fun updateAuthText() {
79-
val authStatusText = findViewById<TextView>(R.id.auth_status)
80-
authStatusText.text = when (instance.isAuthenticated()) {
81-
true -> getString(R.string.status_authenticated)
82-
else -> getString(R.string.status_unauthenticated)
77+
override fun onFailure(e: StudyplusError) {
78+
Toast
79+
.makeText(this@MainActivity, "error!!", Toast.LENGTH_LONG)
80+
.show()
81+
}
82+
})
8383
}
8484
}
8585

@@ -90,13 +90,14 @@ class MainActivity : AppCompatActivity() {
9090
return
9191
}
9292

93-
when (requestCode) {
94-
REQUEST_CODE_AUTH -> {
95-
if (resultCode == Activity.RESULT_OK) {
96-
instance.setAuthResult(data)
97-
Toast.makeText(this, "Success!!", Toast.LENGTH_LONG).show()
98-
}
99-
}
93+
if (requestCode == REQUEST_CODE_AUTH) {
94+
instance.setAuthResult(data)
95+
Toast.makeText(this, "Success!!", Toast.LENGTH_LONG).show()
10096
}
10197
}
98+
99+
private fun currentAuthState() = when (instance.isAuthenticated()) {
100+
true -> getString(R.string.status_authenticated)
101+
else -> getString(R.string.status_unauthenticated)
102+
}
102103
}

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/StudyplusError.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jp.studyplus.android.sdk
22

33
sealed class StudyplusError {
4-
class IOException(val throwable: Throwable) : StudyplusError()
4+
class IOException(val e: java.io.IOException) : StudyplusError()
55
object BadRequest : StudyplusError()
66
object LoginRequired : StudyplusError()
77
object ServerError : StudyplusError()

0 commit comments

Comments
 (0)