1
1
package jp.studyplus.android.sdk_example_kt
2
2
3
- import android.app.Activity
4
3
import android.content.ActivityNotFoundException
5
4
import android.content.Intent
6
5
import android.os.Bundle
7
- import android.view.View
8
- import android.widget.TextView
9
6
import android.widget.Toast
10
7
import androidx.appcompat.app.AppCompatActivity
8
+ import androidx.lifecycle.Lifecycle
9
+ import androidx.lifecycle.LifecycleEventObserver
10
+ import androidx.lifecycle.LifecycleOwner
11
11
import jp.studyplus.android.sdk.PostCallback
12
12
import jp.studyplus.android.sdk.Studyplus
13
13
import jp.studyplus.android.sdk.StudyplusError
14
14
import jp.studyplus.android.sdk.record.StudyRecord
15
15
import jp.studyplus.android.sdk.record.StudyRecordAmountTotal
16
+ import jp.studyplus.android.sdk_example_kt.databinding.ActivityMainBinding
16
17
17
- class MainActivity : AppCompatActivity () {
18
+ class MainActivity : AppCompatActivity (R .layout.activity_main ) {
18
19
19
20
companion object {
20
- const val REQUEST_CODE_AUTH = 1
21
+ const val REQUEST_CODE_AUTH = 112
21
22
}
22
23
23
24
private val instance by lazy {
@@ -30,56 +31,55 @@ class MainActivity : AppCompatActivity() {
30
31
31
32
override fun onCreate (savedInstanceState : Bundle ? ) {
32
33
super .onCreate(savedInstanceState)
33
- setContentView(R .layout.activity_main)
34
+ val binding = ActivityMainBinding .inflate(layoutInflater)
35
+ setContentView(binding.root)
34
36
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
+ }
42
46
}
43
47
}
44
- }
48
+ })
45
49
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()
50
58
}
51
59
}
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()
70
63
}
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
+ }
77
76
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
+ })
83
83
}
84
84
}
85
85
@@ -90,13 +90,14 @@ class MainActivity : AppCompatActivity() {
90
90
return
91
91
}
92
92
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()
100
96
}
101
97
}
98
+
99
+ private fun currentAuthState () = when (instance.isAuthenticated()) {
100
+ true -> getString(R .string.status_authenticated)
101
+ else -> getString(R .string.status_unauthenticated)
102
+ }
102
103
}
0 commit comments