[APIv2/Librus] Add getting behaviour grades and its categories.

This commit is contained in:
Kacper Ziubryniewicz 2019-12-03 23:42:01 +01:00
parent 07ce718e3c
commit 25f504cadf
6 changed files with 195 additions and 3 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1058,4 +1058,5 @@
<string name="home_timetable_lessons_remaining">Pozostało lekcji: %d - do %s</string>
<string name="grade_subject_format">z %s</string>
<string name="concat_2_strings" translatable="false">%s%s</string>
<string name="edziennik_progress_endpoint_behaviour_grades">Pobieranie ocen z zachowania...</string>
</resources>