From 93d55969423aa9281e145b7c2382dca15fb73344 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Fri, 11 Oct 2019 22:07:50 +0200 Subject: [PATCH] [APIv2/Librus] Add events syncing and fix NPE in grades --- .../api/v2/librus/data/LibrusData.kt | 6 ++ .../api/v2/librus/data/api/LibrusApiEvents.kt | 61 +++++++++++++++++-- .../api/v2/librus/data/api/LibrusApiGrades.kt | 27 ++++---- 3 files changed, 74 insertions(+), 20 deletions(-) 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 5e355bae..08a32a06 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 @@ -5,10 +5,12 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_EVENTS import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_ME import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_NORMAL_GRADES import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_SCHOOLS import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiEvents import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiGrades import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiMe import pl.szczodrzynski.edziennik.api.v2.librus.data.api.LibrusApiSchools @@ -52,6 +54,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_grades) LibrusApiGrades(data) { onSuccess() } } + ENDPOINT_LIBRUS_API_EVENTS -> { + data.startProgress(R.string.sync_action_syncing_events) + LibrusApiEvents(data) { onSuccess() } + } else -> onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt index 7beeaace..2d9211de 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -4,8 +4,15 @@ package pl.szczodrzynski.edziennik.api.v2.librus.data.api +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.ENDPOINT_LIBRUS_API_EVENTS import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus 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.events.Event +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date +import pl.szczodrzynski.edziennik.utils.models.Time class LibrusApiEvents(override val data: DataLibrus, val onSuccess: () -> Unit) : LibrusApi(data) { @@ -14,13 +21,55 @@ class LibrusApiEvents(override val data: DataLibrus, } init { - /*apiGet(LibrusApiMe.TAG, "") { json -> + apiGet(TAG, "HomeWorks") { json -> + val events = json.getJsonArray("HomeWorks") - // on error - data.error(TAG, ERROR_LIBRUS_API_, response, json) + events?.forEach { eventEl -> + val event = eventEl.asJsonObject - data.setSyncNext(ENDPOINT_LIBRUS_API_, 2 * DAY) + val id = event.getLong("Id") ?: return@forEach + val eventDate = Date.fromY_m_d(event.getString("Date") ?: return@forEach) + val topic = event.getString("Content") ?: return@forEach + val type = event.getJsonObject("Category")?.getInt("Id") ?: -1 + val teacherId = event.getJsonObject("CreatedBy")?.getLong("Id") ?: -1 + val subjectId = event.getJsonObject("Subject")?.getLong("Id") ?: -1 + val teamId = event.getJsonObject("Class")?.getLong("Id") ?: -1 + + val lessonNo = event.getInt("LessonNo") + val lessonRange = data.lessonRanges.singleOrNull { it.lessonNumber == lessonNo } + val startTime = lessonRange?.startTime + ?: Time.fromH_m(event.getString("TimeFrom") ?: return@forEach) + + val eventObject = Event( + profileId, + id, + eventDate, + startTime, + topic, + -1, + type, + false, + teacherId, + subjectId, + teamId + ) + + val addedDate = Date.fromIso(event.get("AddDate").asString) + + data.eventList.add(eventObject) + data.metadataList.add( + Metadata( + profileId, + Metadata.TYPE_EVENT, + id, + profile?.empty ?: false, + profile?.empty ?: false, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_EVENTS, SYNC_ALWAYS) onSuccess() - }*/ + } } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt index f95a6647..65ba5628 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiGrades.kt @@ -21,16 +21,15 @@ class LibrusApiGrades(override val data: DataLibrus, apiGet(TAG, "Grades") { json -> val grades = json.getJsonArray("Grades") - grades?.forEach { gradeEl -> val grade = gradeEl.asJsonObject - val id = grade.get("Id").asLong - val categoryId = grade.get("Category").asJsonObject.get("Id").asLong - val name = grade.get("Grade").asString - val semester = grade.get("Semester").asInt - val teacherId = grade.get("AddedBy").asJsonObject.get("Id").asLong - val subjectId = grade.get("Subject").asJsonObject.get("Id").asLong + val id = grade.getLong("Id") ?: return@forEach + val categoryId = grade.getJsonObject("Category")?.getLong("Id") ?: return@forEach + val name = grade.getString("Grade") ?: return@forEach + val semester = grade.getInt("Semester") ?: return@forEach + val teacherId = grade.getJsonObject("AddedBy")?.getLong("Id") ?: -1 + val subjectId = grade.getJsonObject("Subject")?.getLong("Id") ?: -1 val category = data.gradeCategories.singleOrNull { it.categoryId == categoryId } val categoryName = category?.text ?: "" @@ -60,20 +59,20 @@ class LibrusApiGrades(override val data: DataLibrus, ) when { - grade.get("IsConstituent").asBoolean -> + grade.getBoolean("IsConstituent") ?: false -> gradeObject.type = Grade.TYPE_NORMAL - grade.get("IsSemester").asBoolean -> // semester final + grade.getBoolean("IsSemester") ?: false -> // semester final gradeObject.type = if (gradeObject.semester == 1) Grade.TYPE_SEMESTER1_FINAL else Grade.TYPE_SEMESTER2_FINAL - grade.get("IsSemesterProposition").asBoolean -> // semester proposed + grade.getBoolean("IsSemesterProposition") ?: false -> // semester proposed gradeObject.type = if (gradeObject.semester == 1) Grade.TYPE_SEMESTER1_PROPOSED else Grade.TYPE_SEMESTER2_PROPOSED - grade.get("IsFinal").asBoolean -> // year final + grade.getBoolean("IsFinal") ?: false -> // year final gradeObject.type = Grade.TYPE_YEAR_FINAL - grade.get("IsFinalProposition").asBoolean -> // year final + grade.getBoolean("IsFinalProposition") ?: false -> // year final gradeObject.type = Grade.TYPE_YEAR_PROPOSED } grade.getJsonObject("Improvement")?.also { - val historicalId = it.get("Id").asLong + val historicalId = it.getLong("Id") data.gradeList.firstOrNull { grade -> grade.id == historicalId }?.also { grade -> grade.parentId = gradeObject.id if (grade.name == "nb") grade.weight = 0f @@ -95,7 +94,7 @@ class LibrusApiGrades(override val data: DataLibrus, )) } - data.setSyncNext(ENDPOINT_LIBRUS_API_NORMAL_GRADES, SYNC_ALWAYS, MainActivity.DRAWER_ITEM_GRADES) + data.setSyncNext(ENDPOINT_LIBRUS_API_NORMAL_GRADES, SYNC_ALWAYS) onSuccess() } }