[API/Podlasie] Add getting grades.

This commit is contained in:
Kacper Ziubryniewicz 2020-05-13 17:33:42 +02:00
parent fcffa2afeb
commit b13257cb78
2 changed files with 73 additions and 0 deletions

View File

@ -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<JsonObject>) {
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))
}
}

View File

@ -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)