From 3cca5e8e9ae0aefb3f05371bdef9c7354d54df87 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Wed, 1 Jan 2020 18:46:43 +0100 Subject: [PATCH] [DataRemoveModel/Events] Add removing future except list of types. --- .../edziennik/librus/data/api/LibrusApiEvents.kt | 5 ++++- .../librus/data/api/LibrusApiPtMeetings.kt | 3 +++ .../edziennik/data/api/models/DataRemoveModel.kt | 8 +++++--- .../data/db/modules/events/EventDao.java | 15 +++++++++++++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt index 7b87c462..43cddd54 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiEvents.kt @@ -70,7 +70,10 @@ class LibrusApiEvents(override val data: DataLibrus, )) } - data.toRemove.add(DataRemoveModel.Events.futureExceptType(Event.TYPE_HOMEWORK)) + data.toRemove.add(DataRemoveModel.Events.futureExceptTypes(listOf( + Event.TYPE_HOMEWORK, + Event.TYPE_PT_MEETING + ))) data.setSyncNext(ENDPOINT_LIBRUS_API_EVENTS, SYNC_ALWAYS) onSuccess() diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt index a2a3789a..ab78a6e8 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/librus/data/api/LibrusApiPtMeetings.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_PT_MEETINGS 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.events.Event import pl.szczodrzynski.edziennik.data.db.modules.metadata.Metadata import pl.szczodrzynski.edziennik.utils.models.Date @@ -61,6 +62,8 @@ class LibrusApiPtMeetings(override val data: DataLibrus, )) } + data.toRemove.add(DataRemoveModel.Events.futureWithType(Event.TYPE_PT_MEETING)) + data.setSyncNext(ENDPOINT_LIBRUS_API_PT_MEETINGS, 12*HOUR) 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 46457a66..0b8f1265 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 @@ -47,15 +47,17 @@ open class DataRemoveModel { } } - class Events(private val type: Int?, private val exceptType: Int?) : DataRemoveModel() { + class Events(private val type: Int?, private val exceptType: Int?, private val exceptTypes: List?) : DataRemoveModel() { companion object { - fun futureExceptType(exceptType: Int) = Events(null, exceptType) - fun futureWithType(type: Int) = Events(type, null) + fun futureExceptType(exceptType: Int) = Events(null, exceptType, null) + fun futureExceptTypes(exceptTypes: List) = Events(null, null, exceptTypes) + fun futureWithType(type: Int) = Events(type, null, null) } fun commit(profileId: Int, dao: EventDao) { type?.let { dao.removeFutureWithType(profileId, Date.getToday(), it) } exceptType?.let { dao.removeFutureExceptType(profileId, Date.getToday(), it) } + exceptTypes?.let { dao.removeFutureExceptTypes(profileId, Date.getToday(), it) } } } } 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 12925c41..2a7560b3 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 @@ -130,8 +130,14 @@ public abstract class EventDao { @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") public abstract void removeNotManual(int profileId); - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate") - public abstract void removeFuture(int profileId, Date todayDate); + @RawQuery + abstract long removeFuture(SupportSQLiteQuery query); + @Transaction + public void removeFuture(int profileId, Date todayDate, String filter) { + removeFuture(new SimpleSQLiteQuery("DELETE FROM events WHERE profileId = " + profileId + + " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.getStringY_m_d() + "'" + + " AND " + filter)); + } @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); @@ -139,6 +145,11 @@ public abstract class EventDao { @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); + @Transaction + public void removeFutureExceptTypes(int profileId, Date todayDate, List exceptTypes) { + removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')); + } + @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);