|
1 | 1 | package jp.studyplus.android.sdk.record
|
2 | 2 |
|
| 3 | +import androidx.annotation.IntRange |
3 | 4 | import org.json.JSONObject
|
4 | 5 | import java.text.SimpleDateFormat
|
5 | 6 | import java.util.*
|
6 | 7 |
|
7 | 8 | /**
|
8 | 9 | * Studyplusに投稿する学習記録
|
9 | 10 | *
|
10 |
| - * @param duration 学習時間(s) |
| 11 | + * @param duration 学習時間(s), MAX 86400 (24h) |
11 | 12 | * @param amount 学習量
|
12 | 13 | * @param comment 学習に関するコメント
|
13 | 14 | * @param recordedTime 学習を終えた日時
|
14 | 15 | * @since 2.5.0
|
| 16 | + * @throws IllegalArgumentException duration exceeds 24h |
15 | 17 | */
|
16 | 18 | data class StudyRecord @JvmOverloads constructor(
|
| 19 | + @IntRange(from = 0L, to = DURATION_RANGE_MAX_24H) |
17 | 20 | val duration: Int,
|
18 | 21 | val amount: StudyRecordAmount? = null,
|
19 | 22 | val comment: String? = null,
|
20 | 23 | val recordedTime: Calendar = Calendar.getInstance(DATE_TIME_ZONE, DATE_LOCALE)
|
21 | 24 | ) {
|
22 | 25 |
|
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 | + } |
33 | 40 |
|
34 | 41 | companion object {
|
| 42 | + private const val DURATION_RANGE_MAX_24H = 24L * 60L * 60L |
| 43 | + |
35 | 44 | private const val DATE_FORMAT = "yyyy'-'MM'-'dd' 'HH':'mm':'ss"
|
36 | 45 | private val DATE_LOCALE: Locale = Locale.US
|
37 | 46 | private val DATE_TIME_ZONE: TimeZone = TimeZone.getTimeZone("UTC")
|
|
0 commit comments