Skip to content

Commit cfa2029

Browse files
authored
Merge pull request #19 from studyplus/feature/set_duration_range
duration must be 24 hours or less
2 parents 9de746f + 00d2153 commit cfa2029

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

studyplus-android-sdk/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dependencies {
4545
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines"
4646
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines"
4747

48+
implementation 'androidx.annotation:annotation:1.1.0'
49+
4850
def okhttp = "3.14.2"
4951
implementation "com.squareup.okhttp3:okhttp:$okhttp"
5052

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/record/StudyRecord.kt

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

3+
import androidx.annotation.IntRange
34
import org.json.JSONObject
45
import java.text.SimpleDateFormat
56
import java.util.*
67

78
/**
89
* Studyplusに投稿する学習記録
910
*
10-
* @param duration 学習時間(s)
11+
* @param duration 学習時間(s), MAX 86400 (24h)
1112
* @param amount 学習量
1213
* @param comment 学習に関するコメント
1314
* @param recordedTime 学習を終えた日時
1415
* @since 2.5.0
16+
* @throws IllegalArgumentException duration exceeds 24h
1517
*/
1618
data class StudyRecord @JvmOverloads constructor(
19+
@IntRange(from = 0L, to = DURATION_RANGE_MAX_24H)
1720
val duration: Int,
1821
val amount: StudyRecordAmount? = null,
1922
val comment: String? = null,
2023
val recordedTime: Calendar = Calendar.getInstance(DATE_TIME_ZONE, DATE_LOCALE)
2124
) {
2225

23-
internal fun toJson(): String = JSONObject().apply {
24-
put("recorded_at", formatTime(recordedTime))
25-
put("duration", duration)
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-
}
32-
}.toString()
26+
internal fun toJson(): String = if (duration > DURATION_RANGE_MAX_24H) {
27+
throw IllegalArgumentException("duration must be 24 hours or less")
28+
} else {
29+
JSONObject().apply {
30+
put("recorded_at", formatTime(recordedTime))
31+
put("duration", duration)
32+
comment?.let { put("comment", it) }
33+
amount?.let { studyRecordAmount ->
34+
studyRecordAmount.amountTotal?.let { put("amount", it) }
35+
studyRecordAmount.startPosition?.let { put("start_position", it) }
36+
studyRecordAmount.endPosition?.let { put("end_position", it) }
37+
}
38+
}.toString()
39+
}
3340

3441
companion object {
42+
private const val DURATION_RANGE_MAX_24H = 24L * 60L * 60L
43+
3544
private const val DATE_FORMAT = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
3645
private val DATE_LOCALE: Locale = Locale.US
3746
private val DATE_TIME_ZONE: TimeZone = TimeZone.getTimeZone("UTC")

0 commit comments

Comments
 (0)