Skip to content

Commit ff37971

Browse files
committed
Use ViewBinding
1 parent 36bef10 commit ff37971

File tree

3 files changed

+66
-56
lines changed

3 files changed

+66
-56
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ If you want to handle StudyplusSDK instance as a singleton, use [Dagger](https:/
3737
```kotlin
3838
private val instance by lazy {
3939
Studyplus(
40-
context = this@MainActivity,
40+
context = this,
4141
consumerKey = "consumer_key",
4242
consumerSecret = "consumer_secret",
4343
)
@@ -56,13 +56,15 @@ Then save the result.
5656

5757
```kotlin
5858
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
59+
super.onActivityResult(requestCode, resultCode, data)
5960
if (resultCode != RESULT_OK || data == null) {
61+
Toast.makeText(this, "error!!", Toast.LENGTH_LONG).show()
6062
return
6163
}
6264

6365
if (requestCode == REQUEST_CODE_AUTH) {
6466
instance.setAuthResult(data)
65-
Toast.makeText(this@MainActivity, "Success!!", Toast.LENGTH_LONG).show()
67+
Toast.makeText(this, "Success!!", Toast.LENGTH_LONG).show()
6668
}
6769
}
6870
```

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
}

0 commit comments

Comments
 (0)