From 621a7ac6426c18c5a7af3dbf089efb4be161e39e Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 21 Feb 2021 22:28:54 +0100 Subject: [PATCH] [Vulcan/Hebe] Add getting attendance. --- .../edziennik/data/api/Constants.kt | 1 + .../api/edziennik/vulcan/VulcanFeatures.kt | 4 + .../api/edziennik/vulcan/data/VulcanData.kt | 4 + .../vulcan/data/hebe/VulcanHebeAttendance.kt | 125 ++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAttendance.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt index d44aa78a..aac3f498 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt @@ -133,6 +133,7 @@ const val VULCAN_HEBE_ENDPOINT_ADDRESSBOOK = "api/mobile/addressbook" const val VULCAN_HEBE_ENDPOINT_EXAMS = "api/mobile/exam" const val VULCAN_HEBE_ENDPOINT_GRADES = "api/mobile/grade" const val VULCAN_HEBE_ENDPOINT_HOMEWORK = "api/mobile/homework" +const val VULCAN_HEBE_ENDPOINT_ATTENDANCE = "api/mobile/lesson" const val VULCAN_HEBE_ENDPOINT_MESSAGES = "api/mobile/message" const val EDUDZIENNIK_USER_AGENT = "Szkolny.eu/${BuildConfig.VERSION_NAME}" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/VulcanFeatures.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/VulcanFeatures.kt index 5b8944f6..9e247575 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/VulcanFeatures.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/VulcanFeatures.kt @@ -26,6 +26,7 @@ const val ENDPOINT_VULCAN_HEBE_TIMETABLE = 3020 const val ENDPOINT_VULCAN_HEBE_EXAMS = 3030 const val ENDPOINT_VULCAN_HEBE_GRADES = 3040 const val ENDPOINT_VULCAN_HEBE_HOMEWORK = 3060 +const val ENDPOINT_VULCAN_HEBE_ATTENDANCE = 3080 const val ENDPOINT_VULCAN_HEBE_MESSAGES_INBOX = 3090 const val ENDPOINT_VULCAN_HEBE_MESSAGES_SENT = 3100 @@ -67,6 +68,9 @@ val VulcanFeatures = listOf( Feature(LOGIN_TYPE_VULCAN, FEATURE_ATTENDANCE, listOf( ENDPOINT_VULCAN_API_ATTENDANCE to LOGIN_METHOD_VULCAN_API ), listOf(LOGIN_METHOD_VULCAN_API)), + Feature(LOGIN_TYPE_VULCAN, FEATURE_ATTENDANCE, listOf( + ENDPOINT_VULCAN_HEBE_ATTENDANCE to LOGIN_METHOD_VULCAN_HEBE + ), listOf(LOGIN_METHOD_VULCAN_HEBE)), // messages Feature(LOGIN_TYPE_VULCAN, FEATURE_MESSAGES_INBOX, listOf( ENDPOINT_VULCAN_API_MESSAGES_INBOX to LOGIN_METHOD_VULCAN_API diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt index af15be8e..6b733e33 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanData.kt @@ -134,6 +134,10 @@ class VulcanData(val data: DataVulcan, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_messages_outbox) VulcanHebeMessages(data, lastSync, onSuccess).getMessages(Message.TYPE_SENT) } + ENDPOINT_VULCAN_HEBE_ATTENDANCE -> { + data.startProgress(R.string.edziennik_progress_endpoint_attendance) + VulcanHebeAttendance(data, lastSync, onSuccess) + } else -> onSuccess(endpointId) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAttendance.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAttendance.kt new file mode 100644 index 00000000..c423d634 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAttendance.kt @@ -0,0 +1,125 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2021-2-21 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.hebe + +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.VULCAN_HEBE_ENDPOINT_ATTENDANCE +import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan +import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanHebe +import pl.szczodrzynski.edziennik.data.db.entity.Attendance +import pl.szczodrzynski.edziennik.data.db.entity.Metadata + +class VulcanHebeAttendance( + override val data: DataVulcan, + override val lastSync: Long?, + val onSuccess: (endpointId: Int) -> Unit +) : VulcanHebe(data, lastSync) { + + companion object { + const val TAG = "VulcanHebeAttendance" + } + + init { + apiGetList( + TAG, + VULCAN_HEBE_ENDPOINT_ATTENDANCE, + HebeFilterType.BY_PUPIL, + lastSync = lastSync + ) { list, _ -> + list.forEach { attendance -> + val id = attendance.getLong("AuxPresenceId") ?: return@forEach + val type = attendance.getJsonObject("PresenceType") ?: return@forEach + val baseType = getBaseType(type) + val typeName = type.getString("Name") ?: return@forEach + val typeCategoryId = type.getLong("CategoryId") ?: return@forEach + val typeSymbol = type.getString("Symbol") ?: return@forEach + val typeShort = when (typeCategoryId.toInt()) { + 6, 8 -> typeSymbol + else -> data.app.attendanceManager.getTypeShort(baseType) + } + val typeColor = when (typeCategoryId.toInt()) { + 1 -> 0xffffffff // obecność + 2 -> 0xffffa687 // nieobecność + 3 -> 0xfffcc150 // nieobecność usprawiedliwiona + 4 -> 0xffede049 // spóźnienie + 5 -> 0xffbbdd5f // spóźnienie usprawiedliwione + 6 -> 0xffa9c9fd // nieobecny z przyczyn szkolnych + 7 -> 0xffddbbe5 // zwolniony + 8 -> 0xffffffff // usunięty wpis + else -> null + }?.toInt() + val date = getDate(attendance, "Day") ?: return@forEach + val lessonRange = getLessonRange(attendance, "TimeSlot") + val startTime = lessonRange?.startTime + val semester = profile?.dateToSemester(date) ?: return@forEach + val teacherId = attendance.getJsonObject("TeacherPrimary")?.getLong("Id") ?: -1 + val subjectId = attendance.getJsonObject("Subject")?.getLong("Id") ?: -1 + val addedDate = getDateTime(attendance, "DateModify") + val lessonNumber = lessonRange?.lessonNumber + val isCounted = attendance.getBoolean("CalculatePresence") + ?: (baseType != Attendance.TYPE_RELEASED) + + val attendanceObject = Attendance( + profileId = profileId, + id = id, + baseType = baseType, + typeName = typeName, + typeShort = typeShort, + typeSymbol = typeSymbol, + typeColor = typeColor, + date = date, + startTime = startTime, + semester = semester, + teacherId = teacherId, + subjectId = subjectId, + addedDate = addedDate + ).also { + it.lessonNumber = lessonNumber + it.isCounted = isCounted + } + + data.attendanceList.add(attendanceObject) + if (baseType != Attendance.TYPE_PRESENT) { + data.metadataList.add( + Metadata( + profileId, + Metadata.TYPE_ATTENDANCE, + attendanceObject.id, + profile?.empty ?: true || baseType == Attendance.TYPE_PRESENT_CUSTOM || baseType == Attendance.TYPE_UNKNOWN, + profile?.empty ?: true || baseType == Attendance.TYPE_PRESENT_CUSTOM || baseType == Attendance.TYPE_UNKNOWN + ) + ) + } + } + } + } + + fun getBaseType(attendanceType: JsonObject): Int { + val absent = attendanceType.getBoolean("Absence") ?: false + val excused = attendanceType.getBoolean("AbsenceJustified") ?: false + return if (absent) { + if (excused) + Attendance.TYPE_ABSENT_EXCUSED + else + Attendance.TYPE_ABSENT + } else { + val belated = attendanceType.getBoolean("Late") ?: false + val released = attendanceType.getBoolean("LegalAbsence") ?: false + val present = attendanceType.getBoolean("Presence") ?: true + if (belated) + if (excused) + Attendance.TYPE_BELATED_EXCUSED + else + Attendance.TYPE_BELATED + else if (released) + Attendance.TYPE_RELEASED + else if (present) + Attendance.TYPE_PRESENT + else + Attendance.TYPE_UNKNOWN + } + } +}