From b13257cb786d9c19ca1781163092bebdce8df61b Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 13 May 2020 17:33:42 +0200 Subject: [PATCH] [API/Podlasie] Add getting grades. --- .../podlasie/data/api/PodlasieApiGrades.kt | 72 +++++++++++++++++++ .../podlasie/data/api/PodlasieApiMain.kt | 1 + 2 files changed, 73 insertions(+) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt new file mode 100644 index 00000000..d5696567 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiGrades.kt @@ -0,0 +1,72 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2020-5-13 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.data.api + +import android.graphics.Color +import com.google.gson.JsonObject +import pl.szczodrzynski.edziennik.data.api.edziennik.podlasie.DataPodlasie +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel +import pl.szczodrzynski.edziennik.data.db.entity.Grade +import pl.szczodrzynski.edziennik.data.db.entity.Metadata +import pl.szczodrzynski.edziennik.getFloat +import pl.szczodrzynski.edziennik.getInt +import pl.szczodrzynski.edziennik.getLong +import pl.szczodrzynski.edziennik.getString +import pl.szczodrzynski.edziennik.utils.models.Date + +class PodlasieApiGrades(val data: DataPodlasie, val rows: List) { + init { + rows.forEach { grade -> + val id = grade.getLong("ExternalId") ?: return@forEach + val name = grade.getString("Mark") ?: return@forEach + val value = data.app.gradesManager.getGradeValue(name) + val weight = grade.getFloat("Weight") ?: 0f + val includeToAverage = grade.getInt("IncludeToAverage") != 0 + val color = grade.getString("Color")?.let { Color.parseColor(it) } ?: -1 + val category = grade.getString("Category") ?: "" + val comment = grade.getString("Comment") ?: "" + val semester = grade.getString("TermShortcut")?.length ?: data.currentSemester + + val teacherFirstName = grade.getString("TeacherFirstName") ?: return@forEach + val teacherLastName = grade.getString("TeacherLastName") ?: return@forEach + val teacher = data.getTeacher(teacherFirstName, teacherLastName) + + val subjectName = grade.getString("SchoolSubject") ?: return@forEach + val subject = data.getSubject(subjectName) + + val addedDate = grade.getString("ReceivedDate")?.let { Date.fromY_m_d(it).inMillis } + ?: System.currentTimeMillis() + + val gradeObject = Grade( + profileId = data.profileId, + id = id, + name = name, + type = Grade.TYPE_NORMAL, + value = value, + weight = if (includeToAverage) weight else 0f, + color = color, + category = category, + description = null, + comment = comment, + semester = semester, + teacherId = teacher.id, + subjectId = subject.id, + addedDate = addedDate + ) + + data.gradeList.add(gradeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_GRADE, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false + )) + } + + data.toRemove.add(DataRemoveModel.Grades.allWithType(Grade.TYPE_NORMAL)) + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt index 0ddff4e9..aa4dc9f2 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/podlasie/data/api/PodlasieApiMain.kt @@ -24,6 +24,7 @@ class PodlasieApiMain(override val data: DataPodlasie, data.getTeam() // Save the class team when it doesn't exist. json.getJsonArray("Timetable")?.asJsonObjectList()?.let { PodlasieApiTimetable(data, it) } + json.getJsonArray("Marks")?.asJsonObjectList()?.let { PodlasieApiGrades(data, it) } data.setSyncNext(ENDPOINT_PODLASIE_API_MAIN, SYNC_ALWAYS) onSuccess(ENDPOINT_PODLASIE_API_MAIN)