|
1 | 1 | package jp.studyplus.android.sdk.record |
2 | 2 |
|
3 | | -import jp.studyplus.android.sdk.record.StudyRecord.Companion.getTime |
4 | 3 | import org.json.JSONObject |
5 | | -import java.security.InvalidParameterException |
6 | 4 | import java.text.SimpleDateFormat |
7 | 5 | import java.util.* |
8 | 6 |
|
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 | +) { |
11 | 22 |
|
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)) |
20 | 25 | 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 | + } |
25 | 32 | }.toString() |
26 | 33 |
|
27 | 34 | companion object { |
28 | 35 | private const val DATE_FORMAT = "yyyy'-'MM'-'dd' 'HH':'mm':'ss" |
29 | 36 | private val DATE_LOCALE: Locale = Locale.US |
30 | 37 | private val DATE_TIME_ZONE: TimeZone = TimeZone.getTimeZone("UTC") |
31 | 38 |
|
32 | | - internal fun getTime(calendar: Calendar): String { |
| 39 | + internal fun formatTime(calendar: Calendar): String { |
33 | 40 | val format = SimpleDateFormat(DATE_FORMAT, DATE_LOCALE) |
34 | 41 | format.timeZone = calendar.timeZone |
35 | 42 | return format.format(calendar.time) |
36 | 43 | } |
37 | 44 | } |
38 | | -} |
39 | 45 |
|
40 | | -class StudyRecordBuilder { |
41 | | - |
42 | | - private val studyRecord = StudyRecord() |
43 | | - |
44 | | - fun build(): StudyRecord { |
45 | | - return studyRecord |
46 | | - } |
| 46 | +} |
47 | 47 |
|
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 { |
52 | 54 |
|
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? |
57 | 58 |
|
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 |
61 | 69 | } |
62 | 70 |
|
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 |
70 | 82 | } |
71 | 83 |
|
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 | | - } |
80 | 84 | } |
0 commit comments