[API/Librus] Add rest of the grade types and categories...

This commit is contained in:
Kacper Ziubryniewicz 2019-12-29 18:51:47 +01:00
parent 9b5cf3f636
commit 90d6fb56d1
13 changed files with 285 additions and 9 deletions

View File

@ -93,6 +93,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_grade_categories)
LibrusApiDescriptiveGradeCategories(data, onSuccess)
}
ENDPOINT_LIBRUS_API_TEXT_GRADE_CATEGORIES -> {
data.startProgress(R.string.edziennik_progress_endpoint_grade_categories)
LibrusApiTextGradeCategories(data, onSuccess)
}
ENDPOINT_LIBRUS_API_POINT_GRADE_CATEGORIES -> {
data.startProgress(R.string.edziennik_progress_endpoint_grade_categories)
LibrusApiPointGradeCategories(data, onSuccess)
}
ENDPOINT_LIBRUS_API_NORMAL_GRADE_COMMENTS -> {
data.startProgress(R.string.edziennik_progress_endpoint_grade_comments)
@ -115,7 +123,14 @@ class LibrusData(val data: DataLibrus, val onSuccess: () -> Unit) {
data.startProgress(R.string.edziennik_progress_endpoint_descriptive_grades)
LibrusApiDescriptiveGrades(data, onSuccess)
}
// TODO grades
ENDPOINT_LIBRUS_API_TEXT_GRADES -> {
data.startProgress(R.string.edziennik_progress_endpoint_descriptive_grades)
LibrusApiTextGrades(data, onSuccess)
}
ENDPOINT_LIBRUS_API_POINT_GRADES -> {
data.startProgress(R.string.edziennik_progress_endpoint_point_grades)
LibrusApiPointGrades(data, onSuccess)
}
ENDPOINT_LIBRUS_API_EVENT_TYPES -> {
data.startProgress(R.string.edziennik_progress_endpoint_event_types)

View File

@ -9,7 +9,6 @@ import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS
import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory
class LibrusApiDescriptiveGradeCategories(override val data: DataLibrus,
@ -39,7 +38,7 @@ class LibrusApiDescriptiveGradeCategories(override val data: DataLibrus,
data.gradeCategories.put(id, gradeCategoryObject)
}
data.setSyncNext(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES, SYNC_ALWAYS)
data.setSyncNext(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADE_CATEGORIES, 1 * DAY)
onSuccess()
}
}

View File

@ -8,8 +8,11 @@ import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel
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.Grade.TYPE_DESCRIPTIVE_TEXT
import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.TYPE_TEXT
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
@ -34,13 +37,13 @@ class LibrusApiDescriptiveGrades(override val data: DataLibrus,
?: grade.getJsonObject("Category")?.getLong("Id")
?: return@forEach
val type = when (grade.getJsonObject("Category")) {
null -> Grade.TYPE_DESCRIPTIVE
else -> Grade.TYPE_TEXT
null -> TYPE_DESCRIPTIVE_TEXT
else -> TYPE_TEXT
}
val category = data.gradeCategories.singleOrNull {
it.categoryId == categoryId && it.type == when (type) {
Grade.TYPE_DESCRIPTIVE -> GradeCategory.TYPE_DESCRIPTIVE
TYPE_DESCRIPTIVE_TEXT -> GradeCategory.TYPE_DESCRIPTIVE
else -> GradeCategory.TYPE_TEXT
}
}
@ -74,6 +77,13 @@ class LibrusApiDescriptiveGrades(override val data: DataLibrus,
))
}
data.toRemove.addAll(listOf(
TYPE_DESCRIPTIVE_TEXT,
TYPE_TEXT
).map {
DataRemoveModel.Grades.semesterWithType(profile.currentSemester, it)
})
data.setSyncNext(ENDPOINT_LIBRUS_API_DESCRIPTIVE_GRADES, SYNC_ALWAYS)
onSuccess()
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-12-29
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api
import android.graphics.Color
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_POINT_GRADE_CATEGORIES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory
class LibrusApiPointGradeCategories(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiPointGradeCategories"
}
init {
apiGet(TAG, "PointGrades/Categories") { json ->
json.getJsonArray("Categories")?.asJsonObjectList()?.forEach { category ->
val id = category.getLong("Id") ?: return@forEach
val name = category.getString("Name") ?: ""
val color = category.getJsonObject("Color")?.getInt("Id")
?.let { data.getColor(it) } ?: Color.BLUE
val countToAverage = category.getBoolean("CountToTheAverage") ?: true
val weight = if (countToAverage) category.getFloat("Weight") ?: 0f else 0f
val valueFrom = category.getFloat("ValueFrom") ?: 0f
val valueTo = category.getFloat("ValueTo") ?: 0f
val gradeCategoryObject = GradeCategory(
profileId,
id,
weight,
color,
name
).apply {
type = GradeCategory.TYPE_POINT
setValueRange(valueFrom, valueTo)
}
data.gradeCategories.put(id, gradeCategoryObject)
}
data.setSyncNext(ENDPOINT_LIBRUS_API_POINT_GRADE_CATEGORIES, 1 * DAY)
onSuccess()
}
}
}

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-12-29
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_POINT_GRADES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel
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.Grade.TYPE_POINT_AVG
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
class LibrusApiPointGrades(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiPointGrades"
}
init { data.profile?.also { profile ->
apiGet(TAG, "PointGrades") { json ->
json.getJsonArray("Grades")?.asJsonObjectList()?.forEach { grade ->
val id = grade.getLong("Id") ?: return@forEach
val teacherId = grade.getJsonObject("AddedBy")?.getLong("Id") ?: return@forEach
val semester = grade.getInt("Semester") ?: return@forEach
val subjectId = grade.getJsonObject("Subject")?.getLong("Id") ?: return@forEach
val name = grade.getString("Grade") ?: return@forEach
val value = grade.getFloat("GradeValue") ?: 0f
val categoryId = grade.getJsonObject("Category")?.getLong("Id") ?: return@forEach
val category = data.gradeCategories.singleOrNull {
it.categoryId == categoryId && it.type == GradeCategory.TYPE_POINT
}
val addedDate = Date.fromIso(grade.getString("AddDate") ?: return@forEach)
val gradeObject = Grade(
profileId,
id,
category?.text ?: "",
category?.color ?: -1,
"",
name,
value,
category?.weight ?: 0f,
semester,
teacherId,
subjectId
).apply {
type = TYPE_POINT_AVG
valueMax = category?.valueTo ?: 0f
}
data.gradeList.add(gradeObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_GRADE,
id,
profile.empty,
profile.empty,
addedDate
))
}
data.toRemove.add(DataRemoveModel.Grades.semesterWithType(profile.currentSemester, TYPE_POINT_AVG))
data.setSyncNext(ENDPOINT_LIBRUS_API_POINT_GRADES, SYNC_ALWAYS)
onSuccess()
}
} ?: onSuccess() }
}

View File

@ -0,0 +1,45 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-12-29
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api
import android.graphics.Color
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_TEXT_GRADE_CATEGORIES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory
class LibrusApiTextGradeCategories(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiTextGradeCategories"
}
init {
apiGet(TAG, "TextGrades/Categories") { json ->
json.getJsonArray("Categories")?.asJsonObjectList()?.forEach { category ->
val id = category.getLong("Id") ?: return@forEach
val name = category.getString("Name") ?: ""
val color = category.getJsonObject("Color")?.getInt("Id")
?.let { data.getColor(it) } ?: Color.BLUE
val gradeCategoryObject = GradeCategory(
profileId,
id,
-1f,
color,
name
).apply {
type = GradeCategory.TYPE_TEXT
}
data.gradeCategories.put(id, gradeCategoryObject)
}
data.setSyncNext(ENDPOINT_LIBRUS_API_TEXT_GRADE_CATEGORIES, 1 * DAY)
onSuccess()
}
}
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) Kacper Ziubryniewicz 2019-12-29
*/
package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.api
import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_TEXT_GRADES
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusApi
import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel
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.Grade.TYPE_DESCRIPTIVE
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
class LibrusApiTextGrades(override val data: DataLibrus,
val onSuccess: () -> Unit) : LibrusApi(data) {
companion object {
const val TAG = "LibrusApiTextGrades"
}
init { data.profile?.also { profile ->
apiGet(TAG, "DescriptiveGrades") { json ->
json.getJsonArray("Grades")?.asJsonObjectList()?.forEach { grade ->
val id = grade.getLong("Id") ?: return@forEach
val teacherId = grade.getJsonObject("AddedBy")?.getLong("Id") ?: return@forEach
val semester = grade.getInt("Semester") ?: return@forEach
val subjectId = grade.getJsonObject("Subject")?.getLong("Id") ?: return@forEach
val description = grade.getString("RealGradeValue") ?: grade.getString("Map") ?: ""
val categoryId = grade.getJsonObject("Skill")?.getLong("Id") ?: return@forEach
val category = data.gradeCategories.singleOrNull {
it.categoryId == categoryId && it.type == GradeCategory.TYPE_DESCRIPTIVE
}
val addedDate = Date.fromIso(grade.getString("AddDate") ?: return@forEach)
val gradeObject = Grade(
profileId,
id,
category?.text ?: "",
category?.color ?: -1,
"",
description,
0f,
0f,
semester,
teacherId,
subjectId
).apply {
type = TYPE_DESCRIPTIVE
}
data.gradeList.add(gradeObject)
data.metadataList.add(Metadata(
profileId,
Metadata.TYPE_GRADE,
id,
profile.empty,
profile.empty,
addedDate
))
}
data.toRemove.add(DataRemoveModel.Grades.semesterWithType(profile.currentSemester, TYPE_DESCRIPTIVE))
data.setSyncNext(ENDPOINT_LIBRUS_API_TEXT_GRADES, SYNC_ALWAYS)
onSuccess()
}
} ?: onSuccess() }
}

View File

@ -44,6 +44,7 @@ public class Grade {
public static final int TYPE_POINT_AVG = 10;
public static final int TYPE_POINT_SUM = 20;
public static final int TYPE_DESCRIPTIVE = 30;
public static final int TYPE_DESCRIPTIVE_TEXT = 31;
public static final int TYPE_TEXT = 40;
@ColumnInfo(name = "gradeType")
public int type = TYPE_NORMAL;

View File

@ -31,6 +31,7 @@ public class GradeCategory {
public static final int TYPE_BEHAVIOUR_COMMENT = 3;
public static final int TYPE_DESCRIPTIVE = 4;
public static final int TYPE_TEXT = 5;
public static final int TYPE_POINT = 6;
public GradeCategory(int profileId, long categoryId, float weight, int color, String text) {
this.profileId = profileId;

View File

@ -85,7 +85,7 @@ public class GradeDetailsDialog {
if (grade.weight < 0) {
grade.weight *= -1;
}
if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_TEXT || grade.type == Grade.TYPE_POINT_SUM) {
if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_DESCRIPTIVE_TEXT || grade.type == Grade.TYPE_TEXT || grade.type == Grade.TYPE_POINT_SUM) {
b.setWeightText(null);
grade.weight = 0;
}

View File

@ -84,7 +84,7 @@ public class GradesListAdapter extends RecyclerView.Adapter<GradesListAdapter.Vi
if (grade.weight < 0) {
grade.weight *= -1;
}
if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_TEXT || grade.type == Grade.TYPE_POINT_SUM) {
if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_DESCRIPTIVE_TEXT || grade.type == Grade.TYPE_TEXT || grade.type == Grade.TYPE_POINT_SUM) {
holder.gradesListWeight.setVisibility(View.GONE);
grade.weight = 0;
}

View File

@ -118,7 +118,7 @@ public class Colors {
else if (grade.type == Grade.TYPE_POINT_AVG) {
return Color.parseColor("#"+gradeValueToColorStr(grade.value/grade.valueMax*100));
}
else if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_TEXT) {
else if (grade.type == Grade.TYPE_DESCRIPTIVE || grade.type == Grade.TYPE_DESCRIPTIVE_TEXT || grade.type == Grade.TYPE_TEXT) {
return grade.color;
}
else {

View File

@ -1132,4 +1132,5 @@
<string name="edziennik_progress_login_edudziennik_web">Logowanie do Edudziennika</string>
<string name="dialog_error_details_title">Szczegóły błędu</string>
<string name="edziennik_progress_endpoint_descriptive_grades">Pobieranie ocen opisowych...</string>
<string name="edziennik_progress_endpoint_point_grades">Pobieranie ocen punktowych...</string>
</resources>