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

Commit 832aa17

Browse files
committed
[change]StudyRecordの仕様変更
1 parent 96fb796 commit 832aa17

File tree

9 files changed

+149
-140
lines changed

9 files changed

+149
-140
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
7373
Create a record and post.
7474

7575
```
76-
val record = StudyRecordBuilder()
77-
.setComment("勉強した!!!")
78-
.setAmountTotal(30)
79-
.setDurationSeconds(2 * 60)
80-
.build()
76+
val record = StudyRecord(
77+
duration = 2 * 60,
78+
comment = "勉強した!!!",
79+
amount = StudyRecordAmount(30)
80+
)
8181
Studyplus.instance.postRecord(this@MainActivity, record,
8282
object : Studyplus.Companion.OnPostRecordListener {
8383
override fun onResult(success: Boolean, recordId: Long?, throwable: Throwable?) {

sdk-example-java/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ android {
2020
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2121
}
2222
}
23+
compileOptions {
24+
sourceCompatibility = 1.8
25+
targetCompatibility = 1.8
26+
}
2327

2428
}
2529

sdk-example-java/src/main/java/jp/studyplus/android/sdk_example_java/MainActivity.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@
44
import android.content.ActivityNotFoundException;
55
import android.content.Intent;
66
import android.os.Bundle;
7-
import android.view.View;
87
import android.widget.Button;
98
import android.widget.TextView;
109
import android.widget.Toast;
1110
import androidx.annotation.Nullable;
1211
import androidx.appcompat.app.AppCompatActivity;
1312
import jp.studyplus.android.sdk.Studyplus;
1413
import jp.studyplus.android.sdk.record.StudyRecord;
15-
import jp.studyplus.android.sdk.record.StudyRecordBuilder;
14+
import jp.studyplus.android.sdk.record.StudyRecordAmount;
15+
16+
import java.util.Locale;
1617

1718
public class MainActivity extends AppCompatActivity {
1819

@@ -28,44 +29,34 @@ protected void onCreate(Bundle savedInstanceState) {
2829
getString(R.string.consumer_secret));
2930

3031
Button startAuthButton = findViewById(R.id.start_auth);
31-
startAuthButton.setOnClickListener(new View.OnClickListener() {
32-
@Override
33-
public void onClick(View view) {
34-
try {
35-
Studyplus.getInstance().startAuth(MainActivity.this, REQUEST_CODE_AUTH);
36-
} catch (ActivityNotFoundException e) {
37-
e.printStackTrace();
38-
Toast.makeText(MainActivity.this, "Need for Studyplus 2.14.0+", Toast.LENGTH_LONG).show();
39-
}
32+
startAuthButton.setOnClickListener(view -> {
33+
try {
34+
Studyplus.getInstance().startAuth(MainActivity.this, REQUEST_CODE_AUTH);
35+
} catch (ActivityNotFoundException e) {
36+
e.printStackTrace();
37+
Toast.makeText(MainActivity.this, "Need for Studyplus 2.14.0+", Toast.LENGTH_LONG).show();
4038
}
4139
});
4240

4341
Button postRecordButton = findViewById(R.id.post_study_record);
44-
postRecordButton.setOnClickListener(new View.OnClickListener() {
45-
@Override
46-
public void onClick(View view) {
47-
StudyRecord record = new StudyRecordBuilder()
48-
.setComment("勉強した!!!")
49-
.setAmountTotal(30)
50-
.setDurationSeconds(2 * 60)
51-
.build();
52-
Studyplus.getInstance().postRecord(MainActivity.this, record,
53-
new Studyplus.Companion.OnPostRecordListener() {
54-
@Override
55-
public void onResult(boolean success, Long recordId, Throwable throwable) {
56-
if (success) {
57-
Toast.makeText(MainActivity.this,
58-
String.format("Post Study Record!! (%d)", recordId), Toast.LENGTH_LONG).show();
59-
} else {
60-
Toast.makeText(MainActivity.this, "error!!", Toast.LENGTH_LONG).show();
61-
if (throwable != null) {
62-
throwable.printStackTrace();
63-
}
64-
}
42+
postRecordButton.setOnClickListener(view -> {
43+
StudyRecord record = new StudyRecord(2 * 60);
44+
record.setAmount(new StudyRecordAmount(30));
45+
record.setComment("勉強した!!!");
46+
47+
Studyplus.getInstance().postRecord(MainActivity.this, record,
48+
(success, recordId, throwable) -> {
49+
if (success) {
50+
Toast.makeText(MainActivity.this,
51+
String.format(Locale.US, "Post Study Record!! (%d)", recordId), Toast.LENGTH_LONG).show();
52+
} else {
53+
Toast.makeText(MainActivity.this, "error!!", Toast.LENGTH_LONG).show();
54+
if (throwable != null) {
55+
throwable.printStackTrace();
6556
}
66-
});
57+
}
58+
});
6759

68-
}
6960
});
7061
}
7162

sdk-example-kt/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ android {
2020
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
2121
}
2222
}
23+
compileOptions {
24+
sourceCompatibility = 1.8
25+
targetCompatibility = 1.8
26+
}
2327
}
2428

2529
repositories {

sdk-example-kt/src/main/java/jp/studyplus/android/sdk_example_kt/MainActivity.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import android.widget.TextView
99
import android.widget.Toast
1010
import androidx.appcompat.app.AppCompatActivity
1111
import jp.studyplus.android.sdk.Studyplus
12-
import jp.studyplus.android.sdk.record.StudyRecordBuilder
12+
import jp.studyplus.android.sdk.record.StudyRecord
13+
import jp.studyplus.android.sdk.record.StudyRecordAmount
1314

1415
class MainActivity : AppCompatActivity() {
1516

@@ -39,11 +40,11 @@ class MainActivity : AppCompatActivity() {
3940

4041
findViewById<View>(R.id.post_study_record)?.apply {
4142
setOnClickListener {
42-
val record = StudyRecordBuilder()
43-
.setComment("勉強した!!!")
44-
.setAmountTotal(30)
45-
.setDurationSeconds(2 * 60)
46-
.build()
43+
val record = StudyRecord(
44+
duration = 2 * 60,
45+
comment = "勉強した!!!",
46+
amount = StudyRecordAmount(30)
47+
)
4748
Studyplus.instance.postRecord(this@MainActivity, record,
4849
object : Studyplus.Companion.OnPostRecordListener {
4950
override fun onResult(success: Boolean, recordId: Long?, throwable: Throwable?) {

studyplus-android-sdk2/build.gradle

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ android {
3030
lintOptions {
3131
abortOnError false
3232
}
33+
34+
compileOptions {
35+
sourceCompatibility = 1.8
36+
targetCompatibility = 1.8
37+
}
3338
}
3439

3540
dependencies {
@@ -40,11 +45,12 @@ dependencies {
4045
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
4146
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
4247

43-
def okhttp = "3.14.1"
48+
def okhttp = "3.14.2"
4449
implementation "com.squareup.okhttp3:okhttp:$okhttp"
4550

4651
testImplementation 'junit:junit:4.12'
4752
testImplementation 'org.robolectric:robolectric:4.0.2'
53+
testImplementation 'org.json:json:20180813'
4854
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
4955
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp"
5056
}
Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,84 @@
11
package jp.studyplus.android.sdk.record
22

3-
import jp.studyplus.android.sdk.record.StudyRecord.Companion.getTime
43
import org.json.JSONObject
5-
import java.security.InvalidParameterException
64
import java.text.SimpleDateFormat
75
import java.util.*
86

9-
class StudyRecord {
10-
var recordedTime: String = getTime(GregorianCalendar(DATE_TIME_ZONE, DATE_LOCALE))
7+
/**
8+
* Studyplusに投稿する学習記録
9+
*
10+
* @param duration 学習時間(s)
11+
* @param recordedTime 学習を終えた日時
12+
* @param comment 学習に関するコメント
13+
* @param amount 学習量
14+
* @since 2.3.0
15+
*/
16+
data class StudyRecord @JvmOverloads constructor(
17+
var duration: Int,
18+
var recordedTime: Calendar = Calendar.getInstance(DATE_TIME_ZONE, DATE_LOCALE),
19+
var comment: String? = null,
20+
var amount: StudyRecordAmount? = null
21+
) {
1122

12-
var duration: Int = 0
13-
var comment: String = ""
14-
var amountTotal: Int? = null
15-
var startPosition: Int? = null
16-
var endPosition: Int? = null
17-
18-
fun toJson() = JSONObject().apply {
19-
put("recorded_at", recordedTime)
23+
internal fun toJson(): String = JSONObject().apply {
24+
put("recorded_at", formatTime(recordedTime))
2025
put("duration", duration)
21-
put("comment", comment)
22-
put("amount", amountTotal)
23-
put("start_position", startPosition)
24-
put("end_position", endPosition)
26+
comment?.let { put("comment", it) }
27+
amount?.let { studyRecordAmount ->
28+
studyRecordAmount.amountTotal?.let { put("amount", it) }
29+
studyRecordAmount.startPosition?.let { put("start_position", it) }
30+
studyRecordAmount.endPosition?.let { put("end_position", it) }
31+
}
2532
}.toString()
2633

2734
companion object {
2835
private const val DATE_FORMAT = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
2936
private val DATE_LOCALE: Locale = Locale.US
3037
private val DATE_TIME_ZONE: TimeZone = TimeZone.getTimeZone("UTC")
3138

32-
internal fun getTime(calendar: Calendar): String {
39+
internal fun formatTime(calendar: Calendar): String {
3340
val format = SimpleDateFormat(DATE_FORMAT, DATE_LOCALE)
3441
format.timeZone = calendar.timeZone
3542
return format.format(calendar.time)
3643
}
3744
}
38-
}
3945

40-
class StudyRecordBuilder {
41-
42-
private val studyRecord = StudyRecord()
43-
44-
fun build(): StudyRecord {
45-
return studyRecord
46-
}
46+
}
4747

48-
fun setRecordedTime(calendar: Calendar): StudyRecordBuilder {
49-
studyRecord.recordedTime = getTime(calendar)
50-
return this
51-
}
48+
/**
49+
* Studyplusに投稿する学習記録の学習量
50+
*
51+
* @since 2.3.0
52+
*/
53+
class StudyRecordAmount {
5254

53-
fun setDurationSeconds(duration: Int): StudyRecordBuilder {
54-
studyRecord.duration = duration
55-
return this
56-
}
55+
val amountTotal: Int?
56+
val startPosition: Int?
57+
val endPosition: Int?
5758

58-
fun setComment(comment: String): StudyRecordBuilder {
59-
studyRecord.comment = comment
60-
return this
59+
/**
60+
* 学習記録の合計学習量
61+
*
62+
* @param amountTotal 合計学習量
63+
* @since 2.3.0
64+
*/
65+
constructor(amountTotal: Int) {
66+
this.amountTotal = amountTotal
67+
this.startPosition = null
68+
this.endPosition = null
6169
}
6270

63-
fun setAmountTotal(amount: Int): StudyRecordBuilder {
64-
if (studyRecord.startPosition != null
65-
|| studyRecord.endPosition != null) {
66-
throw InvalidParameterException()
67-
}
68-
studyRecord.amountTotal = amount
69-
return this
71+
/**
72+
* 学習記録の学習範囲
73+
*
74+
* @param startPosition 学習量の起点
75+
* @param endPosition 学習量の終点
76+
* @since 2.3.0
77+
*/
78+
constructor(startPosition: Int, endPosition: Int) {
79+
this.amountTotal = null
80+
this.startPosition = startPosition
81+
this.endPosition = endPosition
7082
}
7183

72-
fun setAmountRange(start: Int, end: Int): StudyRecordBuilder {
73-
if (studyRecord.amountTotal != null) {
74-
throw InvalidParameterException()
75-
}
76-
studyRecord.startPosition = start
77-
studyRecord.endPosition = end
78-
return this
79-
}
8084
}

studyplus-android-sdk2/src/test/java/jp/studyplus/android/sdk/ApiUnitTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package jp.studyplus.android.sdk
22

33
import jp.studyplus.android.sdk.internal.api.MockApiClient
4-
import jp.studyplus.android.sdk.record.StudyRecordBuilder
4+
import jp.studyplus.android.sdk.record.StudyRecord
55
import kotlinx.coroutines.runBlocking
66
import org.junit.Test
77
import org.junit.runner.RunWith
@@ -14,7 +14,7 @@ class ApiUnitTest {
1414

1515
@Test
1616
fun mockApi() {
17-
val record = StudyRecordBuilder().build()
17+
val record = StudyRecord(2 * 60)
1818
runBlocking {
1919
try {
2020
val deferred = MockApiClient.postStudyRecords(null, record)
@@ -26,4 +26,4 @@ class ApiUnitTest {
2626
}
2727
}
2828
}
29-
}
29+
}

0 commit comments

Comments
 (0)