From bbaa405c5940f2abfd8af08e09f7d4ce32588cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 8 Oct 2019 19:16:19 +0200 Subject: [PATCH] [APIv2/Mobidziennik] Add Notices, Grades, fix Attendance --- .../api/v2/mobidziennik/DataMobidziennik.kt | 5 ++ .../mobidziennik/data/web/MobidziennikApi.kt | 4 +- .../web/apidata/MobidziennikApiAttendance.kt | 5 +- .../data/web/apidata/MobidziennikApiGrades.kt | 88 +++++++++++++++++++ .../web/apidata/MobidziennikApiNotices.kt | 57 ++++++++++++ .../edziennik/api/v2/models/Data.kt | 2 +- 6 files changed, 155 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt index 9cb8c9c5..f4232e94 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/DataMobidziennik.kt @@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik import android.util.LongSparseArray import android.util.SparseArray +import android.util.SparseIntArray import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.api.v2.LOGIN_METHOD_MOBIDZIENNIK_WEB import pl.szczodrzynski.edziennik.api.v2.models.Data @@ -33,6 +34,10 @@ class DataMobidziennik(app: App, profile: Profile?, loginStore: LoginStore) : Da val teachersMap = LongSparseArray() val subjectsMap = LongSparseArray() + val gradeAddedDates = SparseArray() + val gradeAverages = SparseArray() + val gradeColors = SparseIntArray() + private var mLoginServerName: String? = null var loginServerName: String? get() { mLoginServerName = mLoginServerName ?: loginStore.getLoginData("serverName", null); return mLoginServerName } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt index a804a78b..83b55f36 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/MobidziennikApi.kt @@ -40,9 +40,9 @@ class MobidziennikApi(override val data: DataMobidziennik, 14 -> MobidziennikApiGradeCategories(data, rows) 15 -> MobidziennikApiLessons(data, rows) 16 -> MobidziennikApiAttendance(data, rows) - /*17 -> MobidziennikApiNotices(data, rows) + 17 -> MobidziennikApiNotices(data, rows) 18 -> MobidziennikApiGrades(data, rows) - 21 -> MobidziennikApiEvents(data, rows) + /*21 -> MobidziennikApiEvents(data, rows) 23 -> MobidziennikApiHomework(data, rows) 24 -> MobidziennikApiTimetable(data, rows)*/ } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiAttendance.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiAttendance.kt index a281497c..b3d2b509 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiAttendance.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiAttendance.kt @@ -5,7 +5,6 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.apidata import androidx.core.util.contains -import pl.szczodrzynski.edziennik.App.profileId import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance.* @@ -36,7 +35,7 @@ class MobidziennikApiAttendance(val data: DataMobidziennik, rows: List) val semester = data.profile?.dateToSemester(lesson.date) ?: 1 val attendanceObject = Attendance( - profileId, + data.profileId, id, lesson.teacherId, lesson.subjectId, @@ -49,7 +48,7 @@ class MobidziennikApiAttendance(val data: DataMobidziennik, rows: List) data.attendanceList.add(attendanceObject) data.metadataList.add( Metadata( - profileId, + data.profileId, Metadata.TYPE_ATTENDANCE, id, data.profile?.empty ?: false, diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt new file mode 100644 index 00000000..05eb9663 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiGrades.kt @@ -0,0 +1,88 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2019-10-8. + */ + +package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.apidata + +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade +import pl.szczodrzynski.edziennik.data.db.modules.grades.Grade.* +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata + +class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { + init { run { + data.db.gradeDao().getDetails( + data.profileId, + data.gradeAddedDates, + data.gradeAverages, + data.gradeColors + ) + var addedDate = System.currentTimeMillis() + for (row in rows) { + if (row.isEmpty()) + continue + val cols = row.split("|") + + val studentId = cols[2].toInt() + if (studentId != data.studentId) + return@run + + val id = cols[0].toLong() + val categoryId = cols[6].toLongOrNull() ?: -1 + val categoryColumn = cols[10].toIntOrNull() ?: 1 + val name = cols[7] + val value = cols[11].toFloat() + val semester = cols[5].toInt() + val teacherId = cols[2].toLong() + val subjectId = cols[3].toLong() + var type = when (cols[8]) { + "3" -> if (semester == 1) TYPE_SEMESTER1_PROPOSED else TYPE_SEMESTER2_PROPOSED + "1" -> if (semester == 1) TYPE_SEMESTER1_FINAL else TYPE_SEMESTER2_FINAL + "4" -> TYPE_YEAR_PROPOSED + "2" -> TYPE_YEAR_FINAL + else -> TYPE_NORMAL + } + + var weight = 0.0f + var category = "" + var description = "" + var color = -1 + data.gradeCategories.get(categoryId)?.let { gradeCategory -> + weight = gradeCategory.weight + category = gradeCategory.text + description = gradeCategory.columns[categoryColumn-1] + color = gradeCategory.color + } + + // fix for "0" value grades, so they're not counted in the average + if (value == 0.0f/* && data.app.appConfig.dontCountZeroToAverage*/) { + weight = 0.0f + } + + val gradeObject = Grade( + data.profileId, + id, + category, + color, + description, + name, + value, + weight, + semester, + teacherId, + subjectId) + + data.gradeList.add(gradeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_GRADE, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false, + addedDate + )) + addedDate++ + } + }} +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt new file mode 100644 index 00000000..416d3f6d --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/web/apidata/MobidziennikApiNotices.kt @@ -0,0 +1,57 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2019-10-8. + */ + +package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.web.apidata + +import pl.szczodrzynski.edziennik.App +import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.data.db.modules.attendance.Attendance +import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata +import pl.szczodrzynski.edziennik.data.db.modules.notices.Notice +import pl.szczodrzynski.edziennik.data.db.modules.teachers.Teacher +import pl.szczodrzynski.edziennik.utils.models.Date + +class MobidziennikApiNotices(val data: DataMobidziennik, rows: List) { + init { run { + for (row in rows) { + if (row.isEmpty()) + continue + val cols = row.split("|") + + val studentId = cols[2].toInt() + if (studentId != data.studentId) + return@run + + val id = cols[0].toLong() + val text = cols[4] + val semester = cols[6].toInt() + val type = when (cols[3]) { + "0" -> Notice.TYPE_NEGATIVE + "1" -> Notice.TYPE_POSITIVE + else -> Notice.TYPE_NEUTRAL + } + val teacherId = cols[5].toLong() + val addedDate = Date.fromYmd(cols[7]).inMillis + + val noticeObject = Notice( + data.profileId, + id, + text, + semester, + type, + teacherId) + + data.noticeList.add(noticeObject) + data.metadataList.add( + Metadata( + data.profileId, + Metadata.TYPE_NOTICE, + id, + data.profile?.empty ?: false, + data.profile?.empty ?: false, + addedDate + )) + } + }} +} \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt index e24608e6..e642623e 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/Data.kt @@ -141,7 +141,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) val metadataList = mutableListOf() val messageMetadataList = mutableListOf() - private val db by lazy { app.db } + val db by lazy { app.db } init {