From fcc3c55110289b31c94a8b082d894d98a6c69911 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Mon, 30 Mar 2020 22:37:48 +0200 Subject: [PATCH] [Events] Fix preserving isDone value. Improve DataRemoveModel method of keeping items. --- .../edziennik/data/api/models/Data.kt | 2 +- .../data/api/models/DataRemoveModel.kt | 6 ++--- .../szczodrzynski/edziennik/data/db/AppDb.kt | 5 ++-- .../edziennik/data/db/dao/BaseDao.kt | 9 +++++-- .../edziennik/data/db/dao/EventDao.kt | 22 ++++++++-------- .../edziennik/data/db/entity/Event.kt | 2 +- .../edziennik/data/db/entity/Keepable.kt | 9 +++++++ .../data/db/migration/Migration83.kt | 14 +++++++++++ .../edziennik/codegen/FileGenerator.kt | 25 +++++++++++++------ 9 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Keepable.kt create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration83.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt index 1292e799..753160ac 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/models/Data.kt @@ -284,7 +284,7 @@ abstract class Data(val app: App, val profile: Profile?, val loginStore: LoginSt db.gradeDao().addAll(gradeList) } if (eventList.isNotEmpty()) { - db.eventDao().upsertAll(eventList) + db.eventDao().upsertAll(eventList, removeNotKept = true) } if (noticeList.isNotEmpty()) { db.noticeDao().clear(profile.id) 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 ccccc00e..f247ee19 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 @@ -56,9 +56,9 @@ open class DataRemoveModel { } 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) } + type?.let { dao.dontKeepFutureWithType(profileId, Date.getToday(), it) } + exceptType?.let { dao.dontKeepFutureExceptType(profileId, Date.getToday(), it) } + exceptTypes?.let { dao.dontKeepFutureExceptTypes(profileId, Date.getToday(), it) } } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt index 49afa827..dfbf8cbe 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/AppDb.kt @@ -43,7 +43,7 @@ import pl.szczodrzynski.edziennik.data.db.migration.* LibrusLesson::class, TimetableManual::class, Metadata::class -], version = 82) +], version = 83) @TypeConverters( ConverterTime::class, ConverterDate::class, @@ -167,7 +167,8 @@ abstract class AppDb : RoomDatabase() { Migration79(), Migration80(), Migration81(), - Migration82() + Migration82(), + Migration83() ).allowMainThreadQueries().build() } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt index 4b82fd01..8c13dca9 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/BaseDao.kt @@ -8,9 +8,10 @@ import androidx.lifecycle.LiveData import androidx.room.* import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery +import pl.szczodrzynski.edziennik.data.db.entity.Keepable @Dao -interface BaseDao { +interface BaseDao { @RawQuery fun getRaw(query: SupportSQLiteQuery): LiveData> fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query)) @@ -21,6 +22,9 @@ interface BaseDao { fun getOneNow(query: SupportSQLiteQuery): F? fun getOneNow(query: String) = getOneNow(SimpleSQLiteQuery(query)) + @Query("DELETE FROM events WHERE keep = 0") + fun removeNotKept() + /** * INSERT an [item] into the database, * ignoring any conflicts. @@ -81,7 +85,7 @@ interface BaseDao { * @return a [LongArray] of IDs of newly inserted items or -1L if the item existed before */ @Transaction - fun upsertAll(items: List): LongArray { + fun upsertAll(items: List, removeNotKept: Boolean = false): LongArray { val insertResult = addAll(items) val updateList = mutableListOf() @@ -90,6 +94,7 @@ interface BaseDao { } if (updateList.isNotEmpty()) updateAll(items) + if (removeNotKept) removeNotKept() return insertResult } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt index bbd7e8e9..2610537a 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/dao/EventDao.kt @@ -90,34 +90,34 @@ abstract class EventDao : BaseDao { @get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1") abstract val blacklistedIds: List - @Query("UPDATE events SET eventAddedManually = 1 WHERE profileId = :profileId AND eventDate < :date") + /*@Query("UPDATE events SET eventAddedManually = 1 WHERE profileId = :profileId AND eventDate < :date") abstract fun convertOlderToManual(profileId: Int, date: Date?) @Query("DELETE FROM events WHERE teamId = :teamId AND eventId = :id") abstract fun removeByTeamId(teamId: Long, id: Long) @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") - abstract fun removeNotManual(profileId: Int) + abstract fun removeNotManual(profileId: Int)*/ @RawQuery - abstract fun removeFuture(query: SupportSQLiteQuery?): Long + abstract fun dontKeepFuture(query: SupportSQLiteQuery?): Long @Transaction - open fun removeFuture(profileId: Int, todayDate: Date, filter: String) { - removeFuture(SimpleSQLiteQuery("DELETE FROM events WHERE profileId = " + profileId + open fun dontKeepFuture(profileId: Int, todayDate: Date, filter: String) { + dontKeepFuture(SimpleSQLiteQuery("UPDATE events SET keep = 0 WHERE profileId = " + profileId + " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.stringY_m_d + "'" + " AND " + filter)) } - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") - abstract fun removeFutureWithType(profileId: Int, todayDate: Date, type: Long) + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") + abstract fun dontKeepFutureWithType(profileId: Int, todayDate: Date, type: Long) - @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType") - abstract fun removeFutureExceptType(profileId: Int, todayDate: Date, exceptType: Long) + @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType") + abstract fun dontKeepFutureExceptType(profileId: Int, todayDate: Date, exceptType: Long) @Transaction - open fun removeFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List) { - removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')) + open fun dontKeepFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List) { + dontKeepFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')) } @Query("UPDATE metadata SET seen = :seen WHERE profileId = :profileId AND (thingType = " + Metadata.TYPE_EVENT + " OR thingType = " + Metadata.TYPE_LESSON_CHANGE + " OR thingType = " + Metadata.TYPE_HOMEWORK + ") AND thingId IN (SELECT eventId FROM events WHERE profileId = :profileId AND eventDate = :date)") diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt index f14ac32e..ce8cc3ca 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Event.kt @@ -42,7 +42,7 @@ open class Event( var teacherId: Long, var subjectId: Long, var teamId: Long -) { +) : Keepable() { companion object { const val TYPE_UNDEFINED = -2L const val TYPE_HOMEWORK = -1L diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Keepable.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Keepable.kt new file mode 100644 index 00000000..eb0da986 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/entity/Keepable.kt @@ -0,0 +1,9 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2020-3-30. + */ + +package pl.szczodrzynski.edziennik.data.db.entity + +abstract class Keepable { + var keep: Boolean = true +} diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration83.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration83.kt new file mode 100644 index 00000000..340d5768 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/db/migration/Migration83.kt @@ -0,0 +1,14 @@ +/* + * Copyright (c) Kuba SzczodrzyƄski 2020-3-30. + */ + +package pl.szczodrzynski.edziennik.data.db.migration + +import androidx.room.migration.Migration +import androidx.sqlite.db.SupportSQLiteDatabase + +class Migration83 : Migration(82, 83) { + override fun migrate(database: SupportSQLiteDatabase) { + database.execSQL("ALTER TABLE events ADD COLUMN keep INTEGER NOT NULL DEFAULT 1") + } +} diff --git a/codegen/src/main/java/pl/szczodrzynski/edziennik/codegen/FileGenerator.kt b/codegen/src/main/java/pl/szczodrzynski/edziennik/codegen/FileGenerator.kt index dee1d098..d1a9fefa 100644 --- a/codegen/src/main/java/pl/szczodrzynski/edziennik/codegen/FileGenerator.kt +++ b/codegen/src/main/java/pl/szczodrzynski/edziennik/codegen/FileGenerator.kt @@ -17,10 +17,7 @@ import java.io.File import javax.annotation.processing.* import javax.lang.model.SourceVersion import javax.lang.model.element.* -import javax.lang.model.type.MirroredTypeException -import javax.lang.model.type.MirroredTypesException -import javax.lang.model.type.TypeKind -import javax.lang.model.type.TypeMirror +import javax.lang.model.type.* import javax.lang.model.util.ElementFilter import javax.tools.Diagnostic import kotlin.reflect.KClass @@ -146,14 +143,26 @@ class FileGenerator : AbstractProcessor() { val primaryKeys = annotation.primaryKeys val skippedColumns = annotation.skippedColumns - val members = processingEnv.elementUtils.getAllMembers(paramTypeElement) + + var members = processingEnv.elementUtils.getAllMembers(paramTypeElement) val allFields = ElementFilter.fieldsIn(members) - allFields.removeAll { skippedColumns.contains(it.simpleName.toString()) } + + // check all super classes + var superType = paramTypeElement.superclass + while (superType !is NoType) { + val superTypeElement = processingEnv.typeUtils.asElement(superType) as TypeElement + members = processingEnv.elementUtils.getAllMembers(superTypeElement) + allFields += ElementFilter.fieldsIn(members) + superType = superTypeElement.superclass + } + + allFields.removeAll { skippedColumns.contains(it.name()) } allFields.removeAll { it.getAnnotation(Ignore::class.java) != null } allFields.removeAll { field -> field.modifiers.any { it == Modifier.STATIC || it == Modifier.FINAL } } + val allFieldsDistinct = allFields.distinct() - val fields = allFields.filterNot { primaryKeys.contains(it.name()) } - val primaryFields = allFields.filter { primaryKeys.contains(it.name()) } + val fields = allFieldsDistinct.filterNot { primaryKeys.contains(it.name()) } + val primaryFields = allFieldsDistinct.filter { primaryKeys.contains(it.name()) } val fieldNames = fields.map { it.name() } val primaryFieldNames = primaryFields.map { it.name() }