[APIv2/Vulcan] Add getting grades

This commit is contained in:
Kacper Ziubryniewicz 2019-10-20 14:07:34 +02:00
parent dd34e7d008
commit a0fe24ada0
3 changed files with 115 additions and 2 deletions

View File

@ -36,6 +36,7 @@ fun JsonObject?.getBoolean(key: String): Boolean? = get(key)?.let { if (it.isJso
fun JsonObject?.getString(key: String): String? = get(key)?.let { if (it.isJsonNull) null else it.asString }
fun JsonObject?.getInt(key: String): Int? = get(key)?.let { if (it.isJsonNull) null else it.asInt }
fun JsonObject?.getLong(key: String): Long? = get(key)?.let { if (it.isJsonNull) null else it.asLong }
fun JsonObject?.getFloat(key: String): Float? = get(key)?.let { if(it.isJsonNull) null else it.asFloat }
fun JsonObject?.getJsonObject(key: String): JsonObject? = get(key)?.let { if (it.isJsonNull) null else it.asJsonObject }
fun JsonObject?.getJsonArray(key: String): JsonArray? = get(key)?.let { if (it.isJsonNull) null else it.asJsonArray }
@ -43,6 +44,7 @@ fun JsonObject?.getBoolean(key: String, defaultValue: Boolean): Boolean = get(ke
fun JsonObject?.getString(key: String, defaultValue: String): String = get(key)?.let { if (it.isJsonNull) defaultValue else it.asString } ?: defaultValue
fun JsonObject?.getInt(key: String, defaultValue: Int): Int = get(key)?.let { if (it.isJsonNull) defaultValue else it.asInt } ?: defaultValue
fun JsonObject?.getLong(key: String, defaultValue: Long): Long = get(key)?.let { if (it.isJsonNull) defaultValue else it.asLong } ?: defaultValue
fun JsonObject?.getFloat(key: String, defaultValue: Float): Float = get(key)?.let { if(it.isJsonNull) defaultValue else it.asFloat } ?: defaultValue
fun JsonObject?.getJsonObject(key: String, defaultValue: JsonObject): JsonObject = get(key)?.let { if (it.isJsonNull) defaultValue else it.asJsonObject } ?: defaultValue
fun JsonObject?.getJsonArray(key: String, defaultValue: JsonArray): JsonArray = get(key)?.let { if (it.isJsonNull) defaultValue else it.asJsonArray } ?: defaultValue
@ -224,4 +226,4 @@ operator fun MatchResult.get(group: Int): String {
if (group >= groupValues.size)
return ""
return groupValues[group]
}
}

View File

@ -4,12 +4,16 @@
package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_GRADES
import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan
import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_GRADES
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.getJsonArray
import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade
import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata
import java.text.DecimalFormat
import kotlin.math.roundToInt
class VulcanApiGrades(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) {
companion object {
@ -20,6 +24,87 @@ class VulcanApiGrades(override val data: DataVulcan, val onSuccess: () -> Unit)
apiGet(TAG, VULCAN_API_ENDPOINT_GRADES) { json, _ ->
val grades = json.getJsonArray("Data")
grades?.forEach { gradeEl ->
val grade = gradeEl.asJsonObject
val id = grade.getLong("Id") ?: return@forEach
val categoryId = grade.getLong("IdKategoria") ?: -1
val category = data.gradeCategories.singleOrNull{ it.categoryId == categoryId }?.text
?: ""
val teacherId = grade.getLong("IdPracownikD") ?: -1
val subjectId = grade.getLong("IdPrzedmiot") ?: -1
val description = grade.getString("Opis")
val comment = grade.getString("Komentarz")
var value = grade.getFloat("Wartosc")
var weight = grade.getFloat("WagaOceny") ?: 0.0f
val modificatorValue = grade.getFloat("WagaModyfikatora")
val numerator = grade.getFloat("Licznik")
val denominator = grade.getFloat("Mianownik")
val addedDate = (grade.getLong("DataModyfikacji") ?: return@forEach) * 1000
var finalDescription = ""
var name = when (numerator != null && denominator != null) {
true -> {
value = numerator / denominator
finalDescription += DecimalFormat("#.##").format(numerator) +
"/" + DecimalFormat("#.##").format(denominator)
weight = 0.0f
(value * 100).roundToInt().toString() + "%"
}
else -> {
if (value != null) modificatorValue?.also { value += it }
else weight = 0.0f
grade.getString("Wpis") ?: ""
}
}
comment?.also {
if (name == "") name = it
else finalDescription = (if (finalDescription == "") "" else " ") + it
}
description?.also {
finalDescription = (if (finalDescription == "") "" else " - ") + it
}
val color = when (name) {
"1-", "1", "1+" -> 0xffd65757
"2-", "2", "2+" -> 0xff9071b3
"3-", "3", "3+" -> 0xffd2ab24
"4-", "4", "4+" -> 0xff50b6d6
"5-", "5", "5+" -> 0xff2cbd92
"6-", "6", "6+" -> 0xff91b43c
else -> 0xff3D5F9C
}.toInt()
val gradeObject = Grade(
profileId,
id,
category,
color,
finalDescription,
name,
value ?: 0.0f,
weight,
data.studentSemesterNumber,
teacherId,
subjectId
)
data.gradeList.add(gradeObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_GRADE,
id,
profile?.empty ?: false,
profile?.empty ?: false,
addedDate
))
}
data.setSyncNext(ENDPOINT_VULCAN_API_GRADES, SYNC_ALWAYS)
onSuccess()
}

View File

@ -0,0 +1,26 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-10-20
*/
package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api
import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_GRADES
import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan
import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_GRADES
import pl.szczodrzynski.edziennik.api.v2.vulcan.data.VulcanApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.getJsonArray
class VulcanApiTemplate(override val data: DataVulcan, val onSuccess: () -> Unit) : VulcanApi(data) {
companion object {
const val TAG = "VulcanApi"
}
init {
/* apiGet(TAG, VULCAN_API_ENDPOINT_) { json, _ ->
data.setSyncNext(ENDPOINT_VULCAN_API_, SYNC_ALWAYS)
onSuccess()
} */
}
}