From 52ac40c8260cee5c990c36e88518461d6e3f6f6a Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sun, 22 Dec 2019 23:21:03 +0100 Subject: [PATCH] [API/Grades] Add data remove model for grades. --- .../idziennik/data/web/IdziennikWebGrades.kt | 16 +++++++--- .../data/web/IdziennikWebProposedGrades.kt | 8 +++-- .../data/api/LibrusApiBehaviourGrades.kt | 6 ++-- .../librus/data/api/LibrusApiGrades.kt | 31 +++++++++++++------ .../data/api/MobidziennikApiGrades.kt | 16 ++++++++-- .../data/web/MobidziennikWebGrades.kt | 16 ++++++---- .../vulcan/data/api/VulcanApiGrades.kt | 2 ++ .../data/api/models/DataRemoveModel.kt | 16 +++++++--- .../data/db/modules/grades/GradeDao.java | 6 ++++ 9 files changed, 87 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGrades.kt index 438d334e..dc3ca4f1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGrades.kt @@ -12,6 +12,7 @@ import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.DataIdziennik import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.ENDPOINT_IDZIENNIK_WEB_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.IdziennikWeb import pl.szczodrzynski.edziennik.data.api.models.ApiError +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.metadata.Metadata @@ -23,7 +24,7 @@ class IdziennikWebGrades(override val data: DataIdziennik, private const val TAG = "IdziennikWebGrades" } - init { + init { data.profile?.also { profile -> webApiGet(TAG, IDZIENNIK_WEB_GRADES, mapOf( "idPozDziennika" to data.registerId )) { result -> @@ -141,15 +142,22 @@ class IdziennikWebGrades(override val data: DataIdziennik, profileId, Metadata.TYPE_GRADE, id, - data.profile?.empty ?: false, - data.profile?.empty ?: false, + data.profile.empty, + data.profile.empty, addedDate )) } } + data.toRemove.addAll(listOf( + Grade.TYPE_NORMAL, + Grade.TYPE_SEMESTER1_FINAL, + Grade.TYPE_YEAR_FINAL + ).map { + DataRemoveModel.Grades.semesterWithType(profile.currentSemester, it) + }) data.setSyncNext(ENDPOINT_IDZIENNIK_WEB_GRADES, SYNC_ALWAYS) onSuccess() } - } + } ?: onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebProposedGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebProposedGrades.kt index 701620de..f82827bf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebProposedGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebProposedGrades.kt @@ -4,13 +4,14 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.web +import pl.szczodrzynski.edziennik.asJsonObjectList import pl.szczodrzynski.edziennik.data.api.ERROR_IDZIENNIK_WEB_REQUEST_NO_DATA import pl.szczodrzynski.edziennik.data.api.IDZIENNIK_WEB_MISSING_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.DataIdziennik import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.ENDPOINT_IDZIENNIK_WEB_PROPOSED_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.IdziennikWeb import pl.szczodrzynski.edziennik.data.api.models.ApiError -import pl.szczodrzynski.edziennik.asJsonObjectList +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_SEMESTER1_PROPOSED @@ -106,8 +107,11 @@ class IdziennikWebProposedGrades(override val data: DataIdziennik, } } + data.toRemove.addAll(listOf(TYPE_SEMESTER1_PROPOSED, TYPE_YEAR_PROPOSED).map { + DataRemoveModel.Grades.semesterWithType(profile.currentSemester, it) + }) data.setSyncNext(ENDPOINT_IDZIENNIK_WEB_PROPOSED_GRADES, SYNC_ALWAYS) onSuccess() } - }} + } ?: onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiBehaviourGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiBehaviourGrades.kt index 0e376bd9..8aa5cedf 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiBehaviourGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiBehaviourGrades.kt @@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_BEHAVIOUR_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.GradeCategory @@ -23,7 +24,7 @@ class LibrusApiBehaviourGrades(override val data: DataLibrus, private val nameFormat by lazy { DecimalFormat("#.##") } - init { data.profile?.let { profile -> + init { data.profile?.also { profile -> apiGet(TAG, "BehaviourGrades/Points") { json -> if (data.startPointsSemester1 > 0) { @@ -147,8 +148,9 @@ class LibrusApiBehaviourGrades(override val data: DataLibrus, )) } + data.toRemove.add(DataRemoveModel.Grades.semesterWithType(profile.currentSemester, Grade.TYPE_BEHAVIOUR)) data.setSyncNext(ENDPOINT_LIBRUS_API_BEHAVIOUR_GRADES, SYNC_ALWAYS) onSuccess() } - }} + } ?: onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiGrades.kt index 9cd0b267..17ae0d68 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiGrades.kt @@ -4,8 +4,10 @@ import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus import pl.szczodrzynski.edziennik.data.api.edziennik.librus.ENDPOINT_LIBRUS_API_NORMAL_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.* import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeCategory import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.utils.Utils @@ -17,7 +19,7 @@ class LibrusApiGrades(override val data: DataLibrus, const val TAG = "LibrusApiGrades" } - init { + init { data.profile?.also { profile -> apiGet(TAG, "Grades") { json -> val grades = json.getJsonArray("Grades").asJsonObjectList() @@ -68,15 +70,15 @@ class LibrusApiGrades(override val data: DataLibrus, when { grade.getBoolean("IsConstituent") ?: false -> - gradeObject.type = Grade.TYPE_NORMAL + gradeObject.type = TYPE_NORMAL grade.getBoolean("IsSemester") ?: false -> // semester final - gradeObject.type = if (gradeObject.semester == 1) Grade.TYPE_SEMESTER1_FINAL else Grade.TYPE_SEMESTER2_FINAL + gradeObject.type = if (gradeObject.semester == 1) TYPE_SEMESTER1_FINAL else TYPE_SEMESTER2_FINAL grade.getBoolean("IsSemesterProposition") ?: false -> // semester proposed - gradeObject.type = if (gradeObject.semester == 1) Grade.TYPE_SEMESTER1_PROPOSED else Grade.TYPE_SEMESTER2_PROPOSED + gradeObject.type = if (gradeObject.semester == 1) TYPE_SEMESTER1_PROPOSED else TYPE_SEMESTER2_PROPOSED grade.getBoolean("IsFinal") ?: false -> // year final - gradeObject.type = Grade.TYPE_YEAR_FINAL + gradeObject.type = TYPE_YEAR_FINAL grade.getBoolean("IsFinalProposition") ?: false -> // year final - gradeObject.type = Grade.TYPE_YEAR_PROPOSED + gradeObject.type = TYPE_YEAR_PROPOSED } grade.getJsonObject("Improvement")?.also { @@ -94,14 +96,25 @@ class LibrusApiGrades(override val data: DataLibrus, profileId, Metadata.TYPE_GRADE, id, - profile?.empty ?: false, - profile?.empty ?: false, + profile.empty, + profile.empty, addedDate )) } + data.toRemove.addAll(listOf( + TYPE_NORMAL, + TYPE_SEMESTER1_FINAL, + TYPE_SEMESTER2_FINAL, + TYPE_SEMESTER1_PROPOSED, + TYPE_SEMESTER2_PROPOSED, + TYPE_YEAR_FINAL, + TYPE_YEAR_PROPOSED + ).map { + DataRemoveModel.Grades.semesterWithType(profile.currentSemester, it) + }) data.setSyncNext(ENDPOINT_LIBRUS_API_NORMAL_GRADES, SYNC_ALWAYS) onSuccess() } - } + } ?: onSuccess() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiGrades.kt index 16e73363..61518aa3 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/api/MobidziennikApiGrades.kt @@ -5,12 +5,13 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.api import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.data.api.models.DataRemoveModel 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 { + init { data.profile?.also { profile -> run { data.db.gradeDao().getDetails( data.profileId, data.gradeAddedDates, @@ -73,6 +74,17 @@ class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { subjectId) gradeObject.type = type + data.toRemove.addAll(listOf( + TYPE_NORMAL, + TYPE_SEMESTER1_FINAL, + TYPE_SEMESTER2_FINAL, + TYPE_SEMESTER1_PROPOSED, + TYPE_SEMESTER2_PROPOSED, + TYPE_YEAR_FINAL, + TYPE_YEAR_PROPOSED + ).map { + DataRemoveModel.Grades.semesterWithType(profile.currentSemester, it) + }) data.gradeList.add(gradeObject) data.metadataList.add( Metadata( @@ -85,5 +97,5 @@ class MobidziennikApiGrades(val data: DataMobidziennik, rows: List) { )) addedDate++ } - }} + }}} } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt index 52db5bfa..ccb0edfe 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/mobidziennik/data/web/MobidziennikWebGrades.kt @@ -10,6 +10,7 @@ import pl.szczodrzynski.edziennik.data.api.Regexes import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.DataMobidziennik import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.ENDPOINT_MOBIDZIENNIK_WEB_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.mobidziennik.data.MobidziennikWeb +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.metadata.Metadata @@ -25,8 +26,10 @@ class MobidziennikWebGrades(override val data: DataMobidziennik, private const val TAG = "MobidziennikWebGrades" } - init { - webGet(TAG, "/dziennik/oceny?semestr=${profile?.currentSemester ?: 1}") { text -> + init { data.profile?.also { profile -> + val currentSemester = profile.currentSemester + + webGet(TAG, "/dziennik/oceny?semestr=$currentSemester") { text -> MobidziennikLuckyNumberExtractor(data, text) val doc = Jsoup.parse(text) @@ -90,7 +93,7 @@ class MobidziennikWebGrades(override val data: DataMobidziennik, ) val time = Time.fromH_m_s(it[4]) gradeAddedDateMillis = gradeAddedDate.combineWith(time) - gradeSemester = profile?.dateToSemester(gradeAddedDate) ?: 1 + gradeSemester = profile.dateToSemester(gradeAddedDate) } if (Regexes.MOBIDZIENNIK_GRADES_COUNT_TO_AVG.containsMatchIn(html)) { @@ -129,8 +132,8 @@ class MobidziennikWebGrades(override val data: DataMobidziennik, profileId, Metadata.TYPE_GRADE, gradeObject.id, - profile?.empty ?: false, - profile?.empty ?: false, + profile.empty, + profile.empty, gradeAddedDateMillis )) } @@ -143,8 +146,9 @@ class MobidziennikWebGrades(override val data: DataMobidziennik, } } + data.toRemove.add(DataRemoveModel.Grades.semesterWithType(currentSemester, Grade.TYPE_NORMAL)) data.setSyncNext(ENDPOINT_MOBIDZIENNIK_WEB_GRADES, SYNC_ALWAYS) onSuccess() } - } + }} } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiGrades.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiGrades.kt index d86d530a..93528a28 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiGrades.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/api/VulcanApiGrades.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.data.api.VULCAN_API_ENDPOINT_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.DataVulcan import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.ENDPOINT_VULCAN_API_GRADES import pl.szczodrzynski.edziennik.data.api.edziennik.vulcan.data.VulcanApi +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.metadata.Metadata @@ -109,6 +110,7 @@ class VulcanApiGrades(override val data: DataVulcan, val onSuccess: () -> Unit) )) } + data.toRemove.add(DataRemoveModel.Grades.semesterWithType(data.studentSemesterNumber, Grade.TYPE_NORMAL)) data.setSyncNext(ENDPOINT_VULCAN_API_GRADES, SYNC_ALWAYS) onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt index 2cbd67d9..46457a66 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/DataRemoveModel.kt @@ -27,17 +27,23 @@ open class DataRemoveModel { } } - class Grades(private val all: Boolean, private val semester: Int?) : DataRemoveModel() { + class Grades(private val all: Boolean, private val semester: Int?, private val type: Int?) : DataRemoveModel() { companion object { - fun all() = Grades(true, null) - fun semester(semester: Int) = Grades(false, semester) + fun all() = Grades(true, null, null) + fun allWithType(type: Int) = Grades(true, null, type) + fun semester(semester: Int) = Grades(false, semester, null) + fun semesterWithType(semester: Int, type: Int) = Grades(false, semester, type) } fun commit(profileId: Int, dao: GradeDao) { if (all) { - dao.clear(profileId) + if (type != null) dao.clearWithType(profileId, type) + else dao.clear(profileId) + } + semester?.let { + if (type != null) dao.clearForSemesterWithType(profileId, it, type) + else dao.clearForSemester(profileId, it) } - semester?.let { dao.clearForSemester(profileId, it) } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java index 43b1ce1c..02a6d7bd 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/grades/GradeDao.java @@ -27,9 +27,15 @@ public abstract class GradeDao { @Query("DELETE FROM grades WHERE profileId = :profileId") public abstract void clear(int profileId); + @Query("DELETE FROM grades WHERE profileId = :profileId AND gradeType = :type") + public abstract void clearWithType(int profileId, int type); + @Query("DELETE FROM grades WHERE profileId = :profileId AND gradeSemester = :semester") public abstract void clearForSemester(int profileId, int semester); + @Query("DELETE FROM grades WHERE profileId = :profileId AND gradeSemester = :semester AND gradeType = :type") + public abstract void clearForSemesterWithType(int profileId, int semester, int type); + @RawQuery(observedEntities = {Grade.class}) abstract LiveData> getAll(SupportSQLiteQuery query); public LiveData> getAll(int profileId, String filter, String orderBy) {