From 25f504cadf106932527fc12826267d28b3b144d7 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Tue, 3 Dec 2019 23:42:01 +0100 Subject: [PATCH] [APIv2/Librus] Add getting behaviour grades and its categories. --- .../api/v2/librus/data/LibrusData.kt | 8 ++ .../api/LibrusApiBehaviourGradeCategories.kt | 46 ++++++ .../data/api/LibrusApiBehaviourGrades.kt | 136 ++++++++++++++++++ .../data/db/modules/grades/GradeCategory.java | 1 + .../utils/models/ItemGradesSubjectModel.java | 6 +- app/src/main/res/values/strings.xml | 1 + 6 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGradeCategories.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGrades.kt 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 c919cd52..35fb5d7b 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 @@ -93,6 +93,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) { data.startProgress(R.string.edziennik_progress_endpoint_grade_categories) LibrusApiGradeCategories(data, onSuccess) } + ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES -> { + data.startProgress(R.string.edziennik_progress_endpoint_behaviour_grades) + LibrusApiBehaviourGrades(data, onSuccess) + } + ENDPOINT_LIBRUS_API_BEHAVIOUR_GC -> { + data.startProgress(R.string.edziennik_progress_endpoint_grade_categories) + LibrusApiBehaviourGradeCategories(data, onSuccess) + } // TODO grades ENDPOINT_LIBRUS_API_EVENT_TYPES -> { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGradeCategories.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGradeCategories.kt new file mode 100644 index 00000000..a431f145 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGradeCategories.kt @@ -0,0 +1,46 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-3 + */ + +package pl.szczodrzynski.edziennik.api.v2.librus.data.api + +import android.graphics.Color +import pl.szczodrzynski.edziennik.* +import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus +import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_BEHAVIOUR_GC +import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory + +class LibrusApiBehaviourGradeCategories(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiBehaviourGradeCategories" + } + + init { + apiGet(TAG, "BehaviourGrades/Points/Categories") { json -> + json.getJsonArray("Categories")?.asJsonObjectList()?.forEach { category -> + val id = category.getLong("Id") ?: return@forEach + val name = category.getString("Name") ?: "" + val valueFrom = category.getFloat("ValueFrom") ?: 0f + val valueTo = category.getFloat("ValueTo") ?: 0f + + val gradeCategoryObject = GradeCategory( + profileId, + id, + -1f, + Color.BLUE, + name + ).apply { + type = GradeCategory.TYPE_BEHAVIOUR + setValueRange(valueFrom, valueTo) + } + + data.gradeCategories.put(id, gradeCategoryObject) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_BEHAVIOUR_GC, 1 * WEEK) + onSuccess() + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGrades.kt new file mode 100644 index 00000000..1a47f7b7 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiBehaviourGrades.kt @@ -0,0 +1,136 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-3 + */ + +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_BEHAVIOUR_GRADES +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.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 +import java.text.DecimalFormat + +class LibrusApiBehaviourGrades(override val data: DataLibrus, + val onSuccess: () -> Unit) : LibrusApi(data) { + companion object { + const val TAG = "LibrusApiBehaviourGrades" + } + + private val nameFormat by lazy { DecimalFormat("#.##") } + + init { data.profile?.let { profile -> + apiGet(TAG, "BehaviourGrades/Points") { json -> + + val semester1StartGradeObject = Grade( + profileId, + -1, + data.app.getString(R.string.grade_start_points), + 0xffbdbdbd.toInt(), + data.app.getString(R.string.grade_start_points_format, 1), + nameFormat.format(data.startPointsSemester1), + data.startPointsSemester1.toFloat(), + -1f, + 1, + -1, + 1 + ).apply { type = Grade.TYPE_BEHAVIOUR } + + data.gradeList.add(semester1StartGradeObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_GRADE, + -1, + true, + true, + profile.getSemesterStart(1).inMillis + )) + + val semester2StartGradeObject = Grade( + profileId, + -2, + data.app.getString(R.string.grade_start_points), + 0xffbdbdbd.toInt(), + data.app.getString(R.string.grade_start_points_format, 2), + nameFormat.format(data.startPointsSemester2), + data.startPointsSemester2.toFloat(), + -1f, + 2, + -1, + 1 + ).apply { type = Grade.TYPE_BEHAVIOUR } + + data.gradeList.add(semester2StartGradeObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_GRADE, + -2, + true, + true, + profile.getSemesterStart(2).inMillis + )) + + json.getJsonArray("Grades")?.asJsonObjectList()?.forEach { grade -> + val id = grade.getLong("Id") ?: return@forEach + val value = grade.getFloat("Value") + val shortName = grade.getString("ShortName") + val semester = grade.getInt("Semester") ?: profile.currentSemester + val teacherId = grade.getJsonObject("AddedBy")?.getLong("Id") ?: -1 + val addedDate = grade.getString("AddDate")?.let { Date.fromIso(it) } + ?: System.currentTimeMillis() + + val name = when { + value != null -> (if (value >= 0) "+" else "") + nameFormat.format(value) + shortName != null -> shortName + else -> return@forEach + } + + val color = data.getColor(when { + value == null || value == 0f -> 12 + value > 0 -> 16 + value < 0 -> 26 + else -> 12 + }) + + val categoryId = grade.getJsonObject("Category")?.getLong("Id") ?: -1 + val category = data.gradeCategories.singleOrNull { + it.categoryId == categoryId && it.type == GradeCategory.TYPE_BEHAVIOUR + } + + val gradeObject = Grade( + profileId, + id, + category?.text ?: "", + color, + "", + name, + value ?: category?.valueFrom ?: 0f, + -1f, + semester, + teacherId, + 1 + ).apply { + type = Grade.TYPE_BEHAVIOUR + valueMax = category?.valueTo ?: 0f + } + + data.gradeList.add(gradeObject) + data.metadataList.add(Metadata( + profileId, + Metadata.TYPE_GRADE, + id, + profile.empty, + profile.empty, + addedDate + )) + } + + data.setSyncNext(ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES, SYNC_ALWAYS) + 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 442a384a..b96689c8 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 @@ -27,6 +27,7 @@ public class GradeCategory { public static final int TYPE_NORMAL = 0; public static final int TYPE_COMMENT = 1; + public static final int TYPE_BEHAVIOUR = 2; public GradeCategory(int profileId, long categoryId, float weight, int color, String text) { this.profileId = profileId; diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/ItemGradesSubjectModel.java b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/ItemGradesSubjectModel.java index 17c45b7b..6a437d14 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/ItemGradesSubjectModel.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/utils/models/ItemGradesSubjectModel.java @@ -17,15 +17,15 @@ public class ItemGradesSubjectModel { public int semester1Unread = 0; public int semester2Unread = 0; - public float semester1Average = -1; + public float semester1Average = 0; public GradeFull semester1Proposed = null; public GradeFull semester1Final = null; - public float semester2Average = -1; + public float semester2Average = 0; public GradeFull semester2Proposed = null; public GradeFull semester2Final = null; - public float yearAverage = -1; + public float yearAverage = 0; public GradeFull yearProposed = null; public GradeFull yearFinal = null; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2242a76e..956cfa57 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1058,4 +1058,5 @@ PozostaƂo lekcji: %d - do %s z %s %s%s + Pobieranie ocen z zachowania...