15
15
Add it in your root build.gradle at the end of repositories:
16
16
17
17
``` groovy
18
- allprojects {
19
- repositories {
20
- ...
21
- maven { url 'https://jitpack.io' }
18
+ allprojects {
19
+ repositories {
20
+ maven { url 'https://jitpack.io' }
22
21
}
23
22
}
24
23
```
25
24
26
25
``` 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
+ }
30
29
```
31
30
32
31
## Usage
33
32
34
33
### Setup
35
34
35
+ If you want to handle StudyplusSDK instance as a singleton, use [ Dagger] ( https://dagger.dev ) .
36
+
36
37
``` kotlin
37
- Studyplus .instance.setup(" consumer_key" , " consumer_secret" )
38
+ private val instance by lazy {
39
+ Studyplus (
40
+ context = this @MainActivity,
41
+ consumerKey = " consumer_key" ,
42
+ consumerSecret = " consumer_secret" ,
43
+ )
44
+ }
38
45
```
39
46
40
47
### Authenticate
41
48
42
49
Open an Activity to connect with Studyplus.
43
50
44
51
``` kotlin
45
- Studyplus . instance.startAuth(this @MainActivity, REQUEST_CODE_AUTH )
52
+ instance.startAuth(this @MainActivity, REQUEST_CODE_AUTH )
46
53
```
47
54
48
- Then save its result.
55
+ Then save the result.
49
56
50
57
``` kotlin
51
58
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
+ if (resultCode != RESULT_OK || data == null ) {
60
+ return
61
+ }
62
+
63
+ if (requestCode == REQUEST_CODE_AUTH ) {
64
+ instance.setAuthResult(data)
65
+ Toast .makeText( this @MainActivity, " Success!! " , Toast . LENGTH_LONG ).show()
59
66
}
60
67
}
61
68
```
62
69
63
70
### Unauth
64
71
65
72
``` kotlin
66
- Studyplus . instance.cancelAuth(this @MainActivity )
73
+ instance.cancelAuth()
67
74
```
68
75
69
76
### Post a record to Studyplus
@@ -76,19 +83,15 @@ val record = StudyRecord(
76
83
amount = StudyRecordAmountTotal (30 ),
77
84
comment = " 勉強した!!!" ,
78
85
)
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
- })
86
+ instance.postRecord(record, object : PostCallback {
87
+ override fun onSuccess () {
88
+ Toast .makeText(context, " Post Study Record!!" , Toast .LENGTH_LONG ).show()
89
+ }
90
+
91
+ override fun onFailure (e : StudyplusError ) {
92
+ Toast .makeText(context, " error!!" , Toast .LENGTH_LONG ).show()
93
+ }
94
+ })
92
95
```
93
96
94
97
### More
0 commit comments