From 9b5cf3f6363fd5d8f64fd7aeae7f6b1a0b736d56 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 29 Dec 2019 17:48:54 +0100 Subject: [PATCH] [API/Librus] Add getting descriptive grades and its categories. --- .../api/edziennik/librus/data/LibrusData.kt | 10 +++ .../LibrusApiDescriptiveGradeCategories.kt | 46 +++++++++++ .../data/api/LibrusApiDescriptiveGrades.kt | 81 +++++++++++++++++++ .../data/db/modules/grades/GradeCategory.java | 2 + app/src/main/res/values/strings.xml | 1 + 5 files changed, 140 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGradeCategories.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGrades.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt index bccb86b3..9f0fdb63 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/LibrusData.kt @@ -89,6 +89,11 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_grade_categories) LibrusApiBehaviourGradeCategories(data, onSuccess) } + ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES -> { + data.startProgress(R.string.edziennik_progress_endpoint_grade_categories) + LibrusApiDescriptiveGradeCategories(data, onSuccess) + } + ENDPOINT_LIBRUS_API_NORMAL_GRADE_COMMENTS -> { data.startProgress(R.string.edziennik_progress_endpoint_grade_comments) LibrusApiGradeComments(data, onSuccess) @@ -97,6 +102,7 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_grade_comments) LibrusApiBehaviourGradeComments(data, onSuccess) } + ENDPOINT_LIBRUS_API_NORMAL_GRADES -> { data.startProgress(R.string.edziennik_progress_endpoint_grades) LibrusApiGrades(data, onSuccess) @@ -105,6 +111,10 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_behaviour_grades) LibrusApiBehaviourGrades(data, onSuccess) } + ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES -> { + data.startProgress(R.string.edziennik_progress_endpoint_descriptive_grades) + LibrusApiDescriptiveGrades(data, onSuccess) + } // TODO grades ENDPOINT_LIBRUS_API_EVENT_TYPES -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGradeCategories.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGradeCategories.kt new file mode 100644 index 00000000..1fa07381 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGradeCategories.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-29 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api + +import android.graphics.Color +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory + +class LibrusApiDescriptiveGradeCategories(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiDescriptiveGradeCategories" + } + + init { + apiGet(TAG, "DescriptiveTextGrades/Skills") { json -> + json.getJsonArray("Skills")?.asJsonObjectList()?.forEach { category -> + val id = category.getLong("Id") ?: return@forEach + val name = category.getString("Name") ?: "" + val color = category.getJsonObject("Color")?.getInt("Id") + ?.let { data.getColor(it) } ?: Color.BLUE + + val gradeCategoryObject = GradeCategory( + profileId, + id, + -1f, + color, + name + ).apply { + type = GradeCategory.TYPE_DESCRIPTIVE + } + + data.gradeCategories.put(id, gradeCategoryObject) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES, SYNC_ALWAYS) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGrades.kt new file mode 100644 index 00000000..208842cf --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiDescriptiveGrades.kt @@ -0,0 +1,81 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-29 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api + +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES +import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS +import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade +import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.utils.models.Date + +class LibrusApiDescriptiveGrades(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiDescriptiveGrades" + } + + init { data.profile?.also { profile -> + apiGet(TAG, "BaseTextGrades") { json -> + + json.getJsonArray("Grades")?.asJsonObjectList()?.forEach { grade -> + val id = grade.getLong("Id") ?: return@forEach + val teacherId = grade.getJsonObject("AddedBy")?.getLong("Id") ?: return@forEach + val semester = grade.getInt("Semester") ?: return@forEach + val subjectId = grade.getJsonObject("Subject")?.getLong("Id") ?: return@forEach + val description = grade.getString("Grade") + + val categoryId = grade.getJsonObject("Skill")?.getLong("Id") + ?: grade.getJsonObject("Category")?.getLong("Id") + ?: return@forEach + val type = when (grade.getJsonObject("Category")) { + null -> Grade.TYPE_DESCRIPTIVE + else -> Grade.TYPE_TEXT + } + + val category = data.gradeCategories.singleOrNull { + it.categoryId == categoryId && it.type == when (type) { + Grade.TYPE_DESCRIPTIVE -> GradeCategory.TYPE_DESCRIPTIVE + else -> GradeCategory.TYPE_TEXT + } + } + + val addedDate = Date.fromIso(grade.getString("AddDate") ?: return@forEach) + + val gradeObject = Grade( + profileId, + id, + category?.text ?: "", + category?.color ?: -1, + description, + " ", + 0f, + 0f, + semester, + teacherId, + subjectId + ).apply { + this.type = type + } + + data.gradeList.add(gradeObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_GRADE, + id, + profile.empty, + profile.empty, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES, SYNC_ALWAYS) + onSuccess() + } + } ?: onSuccess() } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeCategory.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeCategory.java index 076a952a..0b20ca6d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeCategory.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeCategory.java @@ -29,6 +29,8 @@ public class GradeCategory { public static final int TYPE_NORMAL_COMMENT = 1; public static final int TYPE_BEHAVIOUR = 2; public static final int TYPE_BEHAVIOUR_COMMENT = 3; + public static final int TYPE_DESCRIPTIVE = 4; + public static final int TYPE_TEXT = 5; public GradeCategory(int profileId, long categoryId, float weight, int color, String text) { this.profileId = profileId; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e1dd3e8e..10b5fee7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1131,4 +1131,5 @@ Użyj danych, którymi logujesz się do wersji komputerowej Edudziennika. Logowanie do Edudziennika Szczegóły błędu + Pobieranie ocen opisowych...