From fb945470c0ede3436fa39d04841b2b476f81ebe0 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 13 Oct 2019 15:44:52 +0200 Subject: [PATCH] [APIv2/Librus] Add getting attendances and their types --- .../edziennik/api/v2/librus/LibrusFeatures.kt | 8 +- .../api/v2/librus/data/LibrusData.kt | 8 ++ .../data/api/LibrusApiAttendanceTypes.kt | 42 ++++++++++ .../librus/data/api/LibrusApiAttendances.kt | 83 +++++++++++++++++++ .../edziennik/api/v2/models/Data.kt | 5 +- 5 files changed, 140 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendanceTypes.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendances.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt index 46ba4706..a71b74d7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/LibrusFeatures.kt @@ -35,7 +35,7 @@ const val ENDPOINT_LIBRUS_API_HOMEWORK = 1050 const val ENDPOINT_LIBRUS_API_LUCKY_NUMBER = 1060 const val ENDPOINT_LIBRUS_API_NOTICES = 1070 const val ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES = 1080 -const val ENDPOINT_LIBRUS_API_ATTENDANCE = 1081 +const val ENDPOINT_LIBRUS_API_ATTENDANCES = 1081 const val ENDPOINT_LIBRUS_API_ANNOUNCEMENTS = 1090 const val ENDPOINT_LIBRUS_API_PT_MEETINGS = 1100 const val ENDPOINT_LIBRUS_API_TEACHER_FREE_DAYS = 1110 @@ -85,8 +85,8 @@ val LibrusFeatures = listOf( ENDPOINT_LIBRUS_API_NOTICES to LOGIN_METHOD_LIBRUS_API ), listOf(LOGIN_METHOD_LIBRUS_API)), Feature(LOGIN_TYPE_LIBRUS, FEATURE_ATTENDANCE, listOf( - ENDPOINT_LIBRUS_API_ATTENDANCE to LOGIN_METHOD_LIBRUS_API, - ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES to LOGIN_METHOD_LIBRUS_API + ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES to LOGIN_METHOD_LIBRUS_API, + ENDPOINT_LIBRUS_API_ATTENDANCES to LOGIN_METHOD_LIBRUS_API ), listOf(LOGIN_METHOD_LIBRUS_API)), Feature(LOGIN_TYPE_LIBRUS, FEATURE_ANNOUNCEMENTS, listOf( ENDPOINT_LIBRUS_API_ANNOUNCEMENTS to LOGIN_METHOD_LIBRUS_API @@ -140,4 +140,4 @@ val LibrusFeatures = listOf( Feature(LOGIN_TYPE_LIBRUS, FEATURE_MESSAGES_SENT, listOf( ENDPOINT_LIBRUS_MESSAGES_SENT to LOGIN_METHOD_LIBRUS_MESSAGES ), listOf(LOGIN_METHOD_LIBRUS_MESSAGES)) -) \ No newline at end of file +) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt index 734f71a9..c4e867f9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/LibrusData.kt @@ -55,6 +55,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.sync_action_syncing_homework) LibrusApiHomework(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES -> { + data.startProgress(R.string.sync_action_syncing_attendance_types) + LibrusApiAttendanceTypes(data) { onSuccess() } + } + ENDPOINT_LIBRUS_API_ATTENDANCES -> { + data.startProgress(R.string.sync_action_syncing_attendance) + LibrusApiAttendances(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendanceTypes.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendanceTypes.kt new file mode 100644 index 00000000..aa268a80 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendanceTypes.kt @@ -0,0 +1,42 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-13 + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS + +class LibrusApiAttendanceTypes(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiAttendanceTypes" + } + + init { + apiGet(TAG, "Attendances/Types") { json -> + val attendanceTypes = json.getJsonArray("Types") + + attendanceTypes?.forEach { attendanceTypeEl -> + val attendanceType = attendanceTypeEl.asJsonObject + + val id = attendanceType.getInt("Id") ?: return@forEach + val standardId = when (attendanceType.getBoolean("Standard") ?: false) { + true -> id + false -> attendanceType.getJsonObject("StandardType")?.getInt("Id") + ?: return@forEach + } + + val name = attendanceType.getString("Name") ?: "" + + data.attendanceTypes.put(id, Pair(standardId, name)) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCE_TYPES, SYNC_ALWAYS) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendances.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendances.kt new file mode 100644 index 00000000..28635ded --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiAttendances.kt @@ -0,0 +1,83 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-10-13 + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_ATTENDANCES +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.Utils +import pl.szczodrzynski.edziennik.utils.models.Date + +class LibrusApiAttendances(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiAttendances" + } + + init { + apiGet(TAG, "Attendances") { json -> + val attendances = json.getJsonArray("Attendances") + + attendances?.forEach { attendanceEl -> + val attendance = attendanceEl.asJsonObject + + val teacherId = attendance.getJsonObject("AddedBy")?.getLong("Id") ?: -1 + val lessonNo = attendance.getInt("LessonNo") ?: return@forEach + val startTime = data.lessonRanges.get(lessonNo).startTime + val lessonDate = Date.fromY_m_d(attendance.getString("Date") ?: return@forEach) + val semester = attendance.getInt("Semester") ?: return@forEach + var type = attendance.getJsonObject("Type")?.getInt("Id") ?: return@forEach + val attendanceType = data.attendanceTypes.get(type) + val topic = attendanceType.second + + val id = Utils.strToInt((attendance.getString("Id") ?: return@forEach) + .replace("[^\\d.]".toRegex(), "")).toLong() + + val subjectId = data.lessonList.singleOrNull { + it.weekDay == lessonDate.weekDay && it.startTime.value == startTime.value + }?.subjectId ?: -1 + + type = when(type) { + 1 -> Attendance.TYPE_ABSENT + 2 -> Attendance.TYPE_BELATED + 3 -> Attendance.TYPE_ABSENT_EXCUSED + 4 -> Attendance.TYPE_RELEASED + else -> Attendance.TYPE_PRESENT + } + + val attendanceObject = Attendance( + profileId, + id, + teacherId, + subjectId, + semester, + topic, + lessonDate, + startTime, + type + ) + + val addedDate = Date.fromIso(attendance.getString("AddDate") ?: return@forEach) + + data.attendanceList.add(attendanceObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_ATTENDANCE, + id, + profile?.empty ?: false, + profile?.empty ?: false, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_ATTENDANCES, SYNC_ALWAYS) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index dd4e7fe9..bbff319c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -103,6 +103,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val teamList = LongSparseArray() val lessonRanges = SparseArray() val gradeCategories = LongSparseArray() + val attendanceTypes = SparseArray>() private var mTeamClass: Team? = null var teamClass: Team? @@ -129,7 +130,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) var noticesToRemove: DataRemoveModel? = null val noticeList = mutableListOf() - var attendanceToRemove: DataRemoveModel? = null + var attendancesToRemove: DataRemoveModel? = null val attendanceList = mutableListOf() var announcementsToRemove: DataRemoveModel? = null @@ -304,4 +305,4 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) fun startProgress(stringRes: Int) { callback.onStartProgress(stringRes) } -} \ No newline at end of file +}