11package jp.studyplus.android.sdk_example_kt
22
3- import android.app.Activity
43import android.content.ActivityNotFoundException
54import android.content.Intent
65import android.os.Bundle
7- import android.view.View
8- import android.widget.TextView
96import android.widget.Toast
107import androidx.appcompat.app.AppCompatActivity
8+ import androidx.lifecycle.Lifecycle
9+ import androidx.lifecycle.LifecycleEventObserver
10+ import androidx.lifecycle.LifecycleOwner
1111import jp.studyplus.android.sdk.PostCallback
1212import jp.studyplus.android.sdk.Studyplus
1313import jp.studyplus.android.sdk.StudyplusError
1414import jp.studyplus.android.sdk.record.StudyRecord
1515import 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