Skip to content
This repository was archived by the owner on Jun 28, 2019. It is now read-only.

Commit 9fb4a44

Browse files
authored
Merge pull request #7 from studyplus/develop
Docの追加、内部処理改善
2 parents a1bdc91 + 3f75847 commit 9fb4a44

File tree

14 files changed

+88
-101
lines changed

14 files changed

+88
-101
lines changed

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Studyplus-Android-SDK-V2
22

3+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/jp.studyplus.android.sdk/studyplus-android-sdk2/badge.svg)](https://maven-badges.herokuapp.com/maven-central/jp.studyplus.android.sdk/studyplus-android-sdk2)
4+
35
## Requirements
46
- Android 4.1+
57
- [Studyplus App 2.14+](https://play.google.com/store/apps/details?id=jp.studyplus.android.app)
@@ -8,23 +10,23 @@
810
## Import in your Project
911

1012
### Gradle file (app)
13+
1114
```
1215
repositories {
1316
mavenCentral()
1417
}
1518
dependencies {
16-
implementation ('jp.studyplus.android.sdk:studyplus-android-sdk2:2.0.0@aar') {
17-
transitive = true
18-
}
19+
implementation 'jp.studyplus.android.sdk:studyplus-android-sdk2:2.0.2'
1920
}
2021
```
2122

2223
### Maven
24+
2325
```
2426
<dependency>
2527
<groupId>jp.studyplus.android.sdk</groupId>
2628
<artifactId>studyplus-android-sdk2</artifactId>
27-
<version>2.0.0</version>
29+
<version>2.0.2</version>
2830
</dependency>
2931
```
3032

@@ -33,13 +35,15 @@ or download the latest JAR [via Central Repository](http://search.maven.org/#sea
3335
## Usage
3436

3537
### Setup
38+
3639
```
3740
Studyplus.instance.setup("consumer_key", "consumer_secret")
3841
```
3942

4043
### Authenticate
4144

4245
Open an Activity to connect with Studyplus.
46+
4347
```
4448
try {
4549
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
@@ -50,6 +54,7 @@ try {
5054
```
5155

5256
Then save its result.
57+
5358
```
5459
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
5560
when (requestCode) {
@@ -64,7 +69,9 @@ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
6469
```
6570

6671
### Post a record to Studyplus
72+
6773
Create a record and post.
74+
6875
```
6976
val record = StudyRecordBuilder()
7077
.setComment("勉強した!!!")
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
VERSION_MAJOR = 2
22
VERSION_MINOR = 0
3-
VERSION_PATCH = 1
3+
VERSION_PATCH = 2
44

5-
DEBUG_API_ENDPOINT =
5+
DEBUG_API_ENDPOINT =

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/Studyplus.kt

Lines changed: 57 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,90 @@ package jp.studyplus.android.sdk
33
import android.app.Activity
44
import android.content.Context
55
import android.content.Intent
6+
import android.util.Log
67
import io.reactivex.android.schedulers.AndroidSchedulers
7-
import jp.studyplus.android.sdk.internal.auth.AuthTransit
8-
import jp.studyplus.android.sdk.internal.api.ApiClient
9-
import jp.studyplus.android.sdk.internal.api.notNull
10-
import jp.studyplus.android.sdk.record.StudyRecord
118
import io.reactivex.schedulers.Schedulers
9+
import jp.studyplus.android.sdk.internal.api.ApiClient
1210
import jp.studyplus.android.sdk.internal.api.CertificationStore
11+
import jp.studyplus.android.sdk.internal.auth.AuthTransit
12+
import jp.studyplus.android.sdk.record.StudyRecord
1313

14-
class Studyplus
15-
private constructor() {
16-
companion object {
17-
interface OnPostRecordListener {
18-
fun onResult(success: Boolean, recordId: Long?, throwable: Throwable?)
19-
}
20-
21-
@JvmStatic
22-
val instance by lazy { Studyplus() }
23-
}
14+
class Studyplus private constructor() {
2415

2516
private var consumerKey: String? = null
2617
private var consumerSecret: String? = null
2718

19+
/**
20+
* ConsumerKey, ConsumerSecretKeyをStudyplus SDKに設定
21+
*
22+
* @since 2.0.0
23+
*/
2824
fun setup(consumerKey: String, consumerSecret: String) {
2925
this.consumerKey = consumerKey
3026
this.consumerSecret = consumerSecret
3127
}
3228

33-
fun isAuthenticated(context: Context): Boolean {
34-
return CertificationStore.create(context).isAuthenticated()
35-
}
29+
/**
30+
* [setup]で設定されたConsumerKeyがすでに認証済みかどうかを判定
31+
*
32+
* @return true: 認証済み、 false: 未認証
33+
* @since 2.0.0
34+
*/
35+
fun isAuthenticated(context: Context): Boolean =
36+
CertificationStore.create(context.applicationContext).isAuthenticated()
3637

38+
/**
39+
* [setup]で設定されたConsumerKey, ConsumerSecretKeyによるStudyplus連携認証
40+
*
41+
* @since 2.0.0
42+
*/
3743
fun startAuth(activity: Activity, requestCode: Int) {
38-
notNull(consumerKey, consumerSecret) { consumerKey, consumerSecret ->
39-
AuthTransit(consumerKey, consumerSecret).apply {
40-
start(activity, requestCode)
41-
}
44+
if (consumerKey == null || consumerSecret == null) {
45+
throw IllegalStateException("Please call setup method before this method call.")
4246
}
47+
48+
AuthTransit(consumerKey!!, consumerSecret!!).start(activity, requestCode)
4349
}
4450

51+
/**
52+
* [startAuth]の結果をStudyplusSDKに保存
53+
*
54+
* @since 2.0.0
55+
*/
4556
fun setAuthResult(context: Context, data: Intent?) {
46-
data?.also {
47-
CertificationStore.create(context).update(it)
57+
if (data == null) {
58+
Log.e("StudyplusSDK", "The data is null. Please check your code. If the data that received from Studyplus app is null, please contact Studyplus Dev team.")
59+
return
4860
}
61+
62+
CertificationStore.create(context.applicationContext).update(data)
4963
}
5064

65+
/**
66+
* Studyplusとの認証情報を利用して学習記録をStudyplusへ投稿
67+
*
68+
* @since 2.0.0
69+
*/
5170
fun postRecord(context: Context, studyRecord: StudyRecord, listener: OnPostRecordListener?) {
71+
if (!isAuthenticated(context)) {
72+
throw IllegalStateException("Please check your application's authentication before this method call.")
73+
}
74+
5275
ApiClient.apiClient.postStudyRecords(context, studyRecord)
5376
.subscribeOn(Schedulers.newThread())
5477
.observeOn(AndroidSchedulers.mainThread())
5578
.subscribe(
56-
{ response ->
57-
listener?.apply {
58-
onResult(true, response.recordId, null)
59-
}
60-
},
61-
{ throwable ->
62-
listener?.apply {
63-
onResult(false, null, throwable)
64-
}
65-
}
79+
{ listener?.onResult(success = true, recordId = it.recordId) },
80+
{ listener?.onResult(success = false, throwable = it) }
6681
)
6782
}
83+
84+
companion object {
85+
interface OnPostRecordListener {
86+
fun onResult(success: Boolean, recordId: Long? = null, throwable: Throwable? = null)
87+
}
88+
89+
@JvmStatic
90+
val instance by lazy { Studyplus() }
91+
}
6892
}

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/KotlinExtension.kt

Lines changed: 0 additions & 5 deletions
This file was deleted.

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/api/ApiClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse
66
import jp.studyplus.android.sdk.record.StudyRecord
77
import retrofit2.Retrofit
88

9-
class ApiClient
9+
internal class ApiClient
1010
constructor(retrofit: Retrofit) {
1111

1212
companion object {
@@ -15,7 +15,7 @@ constructor(retrofit: Retrofit) {
1515

1616
private fun getOAuthAccessToken(context: Context): Observable<String> {
1717
return Observable.just(CertificationStore.create(context))
18-
.map { it.apiCertification.accessToken }
18+
.map { it.apiCertification() }
1919
.map { "OAuth $it" }
2020
}
2121
}
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
package jp.studyplus.android.sdk.internal.api
22

3-
import com.google.gson.FieldNamingPolicy
4-
import com.google.gson.GsonBuilder
53
import jp.studyplus.android.sdk.BuildConfig
64
import okhttp3.OkHttpClient
7-
import okhttp3.logging.HttpLoggingInterceptor
85
import retrofit2.Retrofit
96
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
107
import retrofit2.converter.gson.GsonConverterFactory
118
import java.util.concurrent.TimeUnit
129

13-
object ApiManager {
14-
15-
private val loggingLevel: HttpLoggingInterceptor.Level
16-
get() = if (BuildConfig.DEBUG) {
17-
HttpLoggingInterceptor.Level.BODY
18-
} else {
19-
HttpLoggingInterceptor.Level.NONE
20-
}
21-
22-
private val gson by lazy {
23-
GsonBuilder()
24-
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
25-
.create()
26-
}
10+
internal object ApiManager {
2711

2812
private val client by lazy {
2913
OkHttpClient.Builder()
3014
.connectTimeout(60, TimeUnit.SECONDS)
3115
.writeTimeout(60, TimeUnit.SECONDS)
3216
.readTimeout(60, TimeUnit.SECONDS)
33-
.addInterceptor(HttpLoggingInterceptor().setLevel(loggingLevel))
3417
.build()
3518
}
3619

@@ -39,7 +22,7 @@ object ApiManager {
3922
.client(client)
4023
.baseUrl(BuildConfig.API_ENDPOINT)
4124
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
42-
.addConverterFactory(GsonConverterFactory.create(gson))
25+
.addConverterFactory(GsonConverterFactory.create())
4326
.build()!!
4427
}
4528
}

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/api/ApiService.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import jp.studyplus.android.sdk.internal.api.response.PostStudyRecordsResponse
55
import jp.studyplus.android.sdk.record.StudyRecord
66
import retrofit2.http.*
77

8-
interface ApiService {
8+
internal interface ApiService {
99
@Headers(value = [
1010
"Accept: application/json",
1111
"Content-type: application/json"

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/api/CertificationStore.kt

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ import android.content.Intent
55
import android.content.SharedPreferences
66
import android.net.Uri
77
import jp.studyplus.android.sdk.BuildConfig
8-
import jp.studyplus.android.sdk.internal.auth.AccessTokenNotFound
98

10-
interface ApiCertification {
11-
val accessToken: String
12-
}
13-
14-
class CertificationStore
9+
internal class CertificationStore
1510
private constructor(private val preferences: SharedPreferences) {
1611

1712
companion object {
@@ -34,17 +29,9 @@ private constructor(private val preferences: SharedPreferences) {
3429
return !token.isNullOrEmpty()
3530
}
3631

37-
val apiCertification: ApiCertification
38-
get() {
39-
val token = preferences.getString(KEY_ACCESS_TOKEN, "")
40-
if (token.isNullOrEmpty()) {
41-
throw AccessTokenNotFound()
42-
}
43-
return ApiCertificationImpl(token)
44-
}
32+
fun apiCertification(): String = preferences.getString(KEY_ACCESS_TOKEN, "")
4533

46-
fun update(data: Intent?) {
47-
if (data == null) { return }
34+
fun update(data: Intent) {
4835
val code = data.getStringExtra(EXTRA_SP_AUTH_RESULT_CODE).orEmpty()
4936
if (RESULT_CODE_AUTHENTICATED == code) {
5037
preferences.edit()?.apply {
@@ -53,7 +40,4 @@ private constructor(private val preferences: SharedPreferences) {
5340
}
5441
}
5542
}
56-
57-
private class ApiCertificationImpl
58-
constructor(override val accessToken: String) : ApiCertification
5943
}

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/api/response/PostStudyRecordsResponse.kt

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

33
import com.google.gson.annotations.SerializedName
44

5-
data class PostStudyRecordsResponse(
5+
internal data class PostStudyRecordsResponse(
66
@SerializedName("record_id")
77
val recordId: Long? = null
88
)

studyplus-android-sdk2/src/main/java/jp/studyplus/android/sdk/internal/auth/AuthTransit.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import android.content.Intent
55
import android.net.Uri
66
import jp.studyplus.android.sdk.R
77

8-
class AccessTokenNotFound : RuntimeException()
9-
10-
class AuthTransit
8+
internal class AuthTransit
119
/**
1210
* @param consumerKey for API
1311
* @param consumerSecret for API

0 commit comments

Comments
 (0)