From 7961a7499549e1bf1860caba2f33f72fcc372d7b Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Mon, 25 Nov 2019 21:13:55 +0100 Subject: [PATCH] [APIv2/Events] Fix fetching events and homework. Add DataRemoveModel for events. --- .../idziennik/data/web/IdziennikWebExams.kt | 22 ++++++++++-------- .../api/v2/librus/data/api/LibrusApiEvents.kt | 3 +++ .../v2/librus/data/api/LibrusApiHomework.kt | 3 +++ .../data/synergia/LibrusSynergiaHomework.kt | 6 +++-- .../data/api/MobidziennikApiEvents.kt | 5 +++- .../data/api/MobidziennikApiHomework.kt | 5 +++- .../edziennik/api/v2/models/Data.kt | 2 +- .../api/v2/models/DataRemoveModel.kt | 23 +++++++++++++++---- .../api/v2/vulcan/data/api/VulcanApiEvents.kt | 11 +++++++-- .../data/db/modules/events/EventDao.java | 15 ++++++++---- 10 files changed, 71 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/web/IdziennikWebExams.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/web/IdziennikWebExams.kt index 3a9a3982..7e818653 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/web/IdziennikWebExams.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/idziennik/data/web/IdziennikWebExams.kt @@ -11,6 +11,7 @@ import pl.szczodrzynski.edziennik.api.v2.idziennik.DataIdziennik import pl.szczodrzynski.edziennik.api.v2.idziennik.ENDPOINT_IDZIENNIK_WEB_EXAMS import pl.szczodrzynski.edziennik.api.v2.idziennik.data.IdziennikWeb import pl.szczodrzynski.edziennik.api.v2.models.ApiError +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.lessons.Lesson @@ -34,14 +35,15 @@ class IdziennikWebExams(override val data: DataIdziennik, } private fun getExams() { - val param = JsonObject() - param.addProperty("strona", 1) - param.addProperty("iloscNaStrone", "99") - param.addProperty("iloscRekordow", -1) - param.addProperty("kolumnaSort", "ss.Nazwa,sp.Data_sprawdzianu") - param.addProperty("kierunekSort", 0) - param.addProperty("maxIloscZaznaczonych", 0) - param.addProperty("panelFiltrow", 0) + val param = JsonObject().apply { + addProperty("strona", 1) + addProperty("iloscNaStrone", "99") + addProperty("iloscRekordow", -1) + addProperty("kolumnaSort", "ss.Nazwa,sp.Data_sprawdzianu") + addProperty("kierunekSort", 0) + addProperty("maxIloscZaznaczonych", 0) + addProperty("panelFiltrow", 0) + } webApiGet(TAG, IDZIENNIK_WEB_EXAMS, mapOf( "idP" to data.registerId, @@ -106,9 +108,11 @@ class IdziennikWebExams(override val data: DataIdziennik, examsNextMonthChecked = true getExams() } else { + data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) + data.setSyncNext(ENDPOINT_IDZIENNIK_WEB_EXAMS, SYNC_ALWAYS) onSuccess() } } } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt index 512a8caa..1f6e0e15 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiEvents.kt @@ -9,6 +9,7 @@ import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_EVENTS import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata @@ -69,6 +70,8 @@ class LibrusApiEvents(override val data: DataLibrus, )) } + data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) + data.setSyncNext(ENDPOINT_LIBRUS_API_EVENTS, SYNC_ALWAYS) onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt index ba16ebe9..2dcec68b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/api/LibrusApiHomework.kt @@ -8,6 +8,7 @@ import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_API_HOMEWORK import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusApi +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.api.SYNC_ALWAYS import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata @@ -55,6 +56,8 @@ class LibrusApiHomework(override val data: DataLibrus, )) } + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_HOMEWORK)) + data.setSyncNext(ENDPOINT_LIBRUS_API_HOMEWORK, SYNC_ALWAYS) onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt index 860eb958..228001c7 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/librus/data/synergia/LibrusSynergiaHomework.kt @@ -11,6 +11,7 @@ import pl.szczodrzynski.edziennik.api.v2.POST import pl.szczodrzynski.edziennik.api.v2.librus.DataLibrus import pl.szczodrzynski.edziennik.api.v2.librus.ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK import pl.szczodrzynski.edziennik.api.v2.librus.data.LibrusSynergia +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.get @@ -66,8 +67,7 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () -> val notified = when (profile?.empty) { true -> true - false -> Date.getToday() < eventDate - else -> false + else -> eventDate < Date.getToday() } val eventObject = Event( @@ -96,6 +96,8 @@ class LibrusSynergiaHomework(override val data: DataLibrus, val onSuccess: () -> } } + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_HOMEWORK)) + // because this requires a synergia login (2 more requests) sync this every two hours or if explicit :D data.setSyncNext(ENDPOINT_LIBRUS_SYNERGIA_HOMEWORK, 2 * HOUR, DRAWER_ITEM_HOMEWORK) onSuccess() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiEvents.kt index 446b5576..2dea973f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiEvents.kt @@ -7,6 +7,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.api import androidx.core.util.contains import pl.szczodrzynski.edziennik.api.v2.Regexes import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.utils.models.Date @@ -74,5 +75,7 @@ class MobidziennikApiEvents(val data: DataMobidziennik, rows: List) { )) } } + + data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) } -} \ No newline at end of file +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiHomework.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiHomework.kt index 4e074f26..405dc9f1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiHomework.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/mobidziennik/data/api/MobidziennikApiHomework.kt @@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.mobidziennik.data.api import androidx.core.util.contains import pl.szczodrzynski.edziennik.api.v2.mobidziennik.DataMobidziennik +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.data.db.modules.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.utils.models.Date @@ -53,5 +54,7 @@ class MobidziennikApiHomework(val data: DataMobidziennik, rows: List) { )) } } + + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_HOMEWORK)) } -} \ 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 e0cea83d..e1f123fb 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 @@ -283,6 +283,7 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) when (model) { is DataRemoveModel.Timetable -> model.commit(profileId, db.timetableDao()) is DataRemoveModel.Grades -> model.commit(profileId, db.gradeDao()) + is DataRemoveModel.Events -> model.commit(profileId, db.eventDao()) } } @@ -305,7 +306,6 @@ open class Data(val app: App, val profile: Profile?, val loginStore: LoginStore) db.gradeDao().addAll(gradeList) } if (eventList.isNotEmpty()) { - db.eventDao().removeFuture(profile.id, Date.getToday()) db.eventDao().addAll(eventList) } if (noticeList.isNotEmpty()) { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/DataRemoveModel.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/DataRemoveModel.kt index c23b28d9..984e9292 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/DataRemoveModel.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/models/DataRemoveModel.kt @@ -4,6 +4,7 @@ package pl.szczodrzynski.edziennik.api.v2.models +import pl.szczodrzynski.edziennik.data.db.modules.events.EventDao import pl.szczodrzynski.edziennik.data.db.modules.grades.GradeDao import pl.szczodrzynski.edziennik.data.db.modules.timetable.TimetableDao import pl.szczodrzynski.edziennik.utils.models.Date @@ -15,21 +16,23 @@ open class DataRemoveModel { fun to(dateTo: Date) = Timetable(null, dateTo) fun between(dateFrom: Date, dateTo: Date) = Timetable(dateFrom, dateTo) } + fun commit(profileId: Int, dao: TimetableDao) { if (dateFrom != null && dateTo != null) { dao.clearBetweenDates(profileId, dateFrom, dateTo) - } - else { + } else { dateFrom?.let { dateFrom -> dao.clearFromDate(profileId, dateFrom) } dateTo?.let { dateTo -> dao.clearToDate(profileId, dateTo) } } } } - class Grades(val all: Boolean, val semester: Int?) : DataRemoveModel() { + + class Grades(private val all: Boolean, private val semester: Int?) : DataRemoveModel() { companion object { fun all() = Grades(true, null) fun semester(semester: Int) = Grades(false, semester) } + fun commit(profileId: Int, dao: GradeDao) { if (all) { dao.clear(profileId) @@ -37,4 +40,16 @@ open class DataRemoveModel { semester?.let { dao.clearForSemester(profileId, it) } } } -} \ No newline at end of file + + class Events(private val type: Int?, private val exceptType: Int?) : DataRemoveModel() { + companion object { + fun futureExceptType(exceptType: Int) = Events(null, exceptType) + fun futureWithType(type: Int) = Events(type, null) + } + + fun commit(profileId: Int, dao: EventDao) { + type?.let { dao.removeFutureWithType(profileId, Date.getToday(), it) } + exceptType?.let { dao.removeFutureExceptType(profileId, Date.getToday(), it) } + } + } +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt index d971118c..bcd02b6a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/api/v2/vulcan/data/api/VulcanApiEvents.kt @@ -6,6 +6,7 @@ package pl.szczodrzynski.edziennik.api.v2.vulcan.data.api import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_EVENTS import pl.szczodrzynski.edziennik.api.v2.VULCAN_API_ENDPOINT_HOMEWORK +import pl.szczodrzynski.edziennik.api.v2.models.DataRemoveModel import pl.szczodrzynski.edziennik.api.v2.vulcan.DataVulcan import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_EVENTS import pl.szczodrzynski.edziennik.api.v2.vulcan.ENDPOINT_VULCAN_API_HOMEWORK @@ -91,8 +92,14 @@ class VulcanApiEvents(override val data: DataVulcan, private val isHomework: Boo } when (isHomework) { - true -> data.setSyncNext(ENDPOINT_VULCAN_API_HOMEWORK, SYNC_ALWAYS) - false -> data.setSyncNext(ENDPOINT_VULCAN_API_EVENTS, SYNC_ALWAYS) + true -> { + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_HOMEWORK)) + data.setSyncNext(ENDPOINT_VULCAN_API_HOMEWORK, SYNC_ALWAYS) + } + false -> { + data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) + data.setSyncNext(ENDPOINT_VULCAN_API_EVENTS, SYNC_ALWAYS) + } } onSuccess() } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/events/EventDao.java b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/events/EventDao.java index 2d1e7d37..ae55ec3c 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/events/EventDao.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/modules/events/EventDao.java @@ -1,16 +1,17 @@ package pl.szczodrzynski.edziennik.data.db.modules.events; +import android.util.Log; + +import androidx.annotation.NonNull; import androidx.lifecycle.LiveData; -import androidx.sqlite.db.SimpleSQLiteQuery; -import androidx.sqlite.db.SupportSQLiteQuery; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.OnConflictStrategy; import androidx.room.Query; import androidx.room.RawQuery; import androidx.room.Transaction; -import androidx.annotation.NonNull; -import android.util.Log; +import androidx.sqlite.db.SimpleSQLiteQuery; +import androidx.sqlite.db.SupportSQLiteQuery; import java.util.List; @@ -130,6 +131,12 @@ public abstract class EventDao { @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate") public abstract void removeFuture(int profileId, Date todayDate); + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") + public abstract void removeFutureWithType(int profileId, Date todayDate, int type); + + @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType") + public abstract void removeFutureExceptType(int profileId, Date todayDate, int exceptType); + @Query("UPDATE metadata SET seen = :seen WHERE profileId = :profileId AND (thingType = "+TYPE_EVENT+" OR thingType = "+TYPE_LESSON_CHANGE+" OR thingType = "+TYPE_HOMEWORK+") AND thingId IN (SELECT eventId FROM events WHERE profileId = :profileId AND eventDate = :date)") public abstract void setSeenByDate(int profileId, Date date, boolean seen);