[Events] Fix preserving isDone value. Improve DataRemoveModel method of keeping items.

This commit is contained in:
Kuba Szczodrzyński 2020-03-30 22:37:48 +02:00
parent 328c07eaf4
commit fcc3c55110
9 changed files with 66 additions and 28 deletions

View File

@ -284,7 +284,7 @@ abstract class Data(val app: App, val profile: Profile?, val loginStore: LoginSt
db.gradeDao().addAll(gradeList) db.gradeDao().addAll(gradeList)
} }
if (eventList.isNotEmpty()) { if (eventList.isNotEmpty()) {
db.eventDao().upsertAll(eventList) db.eventDao().upsertAll(eventList, removeNotKept = true)
} }
if (noticeList.isNotEmpty()) { if (noticeList.isNotEmpty()) {
db.noticeDao().clear(profile.id) db.noticeDao().clear(profile.id)

View File

@ -56,9 +56,9 @@ open class DataRemoveModel {
} }
fun commit(profileId: Int, dao: EventDao) { fun commit(profileId: Int, dao: EventDao) {
type?.let { dao.removeFutureWithType(profileId, Date.getToday(), it) } type?.let { dao.dontKeepFutureWithType(profileId, Date.getToday(), it) }
exceptType?.let { dao.removeFutureExceptType(profileId, Date.getToday(), it) } exceptType?.let { dao.dontKeepFutureExceptType(profileId, Date.getToday(), it) }
exceptTypes?.let { dao.removeFutureExceptTypes(profileId, Date.getToday(), it) } exceptTypes?.let { dao.dontKeepFutureExceptTypes(profileId, Date.getToday(), it) }
} }
} }

View File

@ -43,7 +43,7 @@ import pl.szczodrzynski.edziennik.data.db.migration.*
LibrusLesson::class, LibrusLesson::class,
TimetableManual::class, TimetableManual::class,
Metadata::class Metadata::class
], version = 82) ], version = 83)
@TypeConverters( @TypeConverters(
ConverterTime::class, ConverterTime::class,
ConverterDate::class, ConverterDate::class,
@ -167,7 +167,8 @@ abstract class AppDb : RoomDatabase() {
Migration79(), Migration79(),
Migration80(), Migration80(),
Migration81(), Migration81(),
Migration82() Migration82(),
Migration83()
).allowMainThreadQueries().build() ).allowMainThreadQueries().build()
} }
} }

View File

@ -8,9 +8,10 @@ import androidx.lifecycle.LiveData
import androidx.room.* import androidx.room.*
import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SimpleSQLiteQuery
import androidx.sqlite.db.SupportSQLiteQuery import androidx.sqlite.db.SupportSQLiteQuery
import pl.szczodrzynski.edziennik.data.db.entity.Keepable
@Dao @Dao
interface BaseDao<T, F> { interface BaseDao<T : Keepable, F : T> {
@RawQuery @RawQuery
fun getRaw(query: SupportSQLiteQuery): LiveData<List<F>> fun getRaw(query: SupportSQLiteQuery): LiveData<List<F>>
fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query)) fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query))
@ -21,6 +22,9 @@ interface BaseDao<T, F> {
fun getOneNow(query: SupportSQLiteQuery): F? fun getOneNow(query: SupportSQLiteQuery): F?
fun getOneNow(query: String) = getOneNow(SimpleSQLiteQuery(query)) fun getOneNow(query: String) = getOneNow(SimpleSQLiteQuery(query))
@Query("DELETE FROM events WHERE keep = 0")
fun removeNotKept()
/** /**
* INSERT an [item] into the database, * INSERT an [item] into the database,
* ignoring any conflicts. * ignoring any conflicts.
@ -81,7 +85,7 @@ interface BaseDao<T, F> {
* @return a [LongArray] of IDs of newly inserted items or -1L if the item existed before * @return a [LongArray] of IDs of newly inserted items or -1L if the item existed before
*/ */
@Transaction @Transaction
fun upsertAll(items: List<T>): LongArray { fun upsertAll(items: List<T>, removeNotKept: Boolean = false): LongArray {
val insertResult = addAll(items) val insertResult = addAll(items)
val updateList = mutableListOf<T>() val updateList = mutableListOf<T>()
@ -90,6 +94,7 @@ interface BaseDao<T, F> {
} }
if (updateList.isNotEmpty()) updateAll(items) if (updateList.isNotEmpty()) updateAll(items)
if (removeNotKept) removeNotKept()
return insertResult return insertResult
} }
} }

View File

@ -90,34 +90,34 @@ abstract class EventDao : BaseDao<Event, EventFull> {
@get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1") @get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1")
abstract val blacklistedIds: List<Long> abstract val blacklistedIds: List<Long>
@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?) abstract fun convertOlderToManual(profileId: Int, date: Date?)
@Query("DELETE FROM events WHERE teamId = :teamId AND eventId = :id") @Query("DELETE FROM events WHERE teamId = :teamId AND eventId = :id")
abstract fun removeByTeamId(teamId: Long, id: Long) abstract fun removeByTeamId(teamId: Long, id: Long)
@Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0") @Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0")
abstract fun removeNotManual(profileId: Int) abstract fun removeNotManual(profileId: Int)*/
@RawQuery @RawQuery
abstract fun removeFuture(query: SupportSQLiteQuery?): Long abstract fun dontKeepFuture(query: SupportSQLiteQuery?): Long
@Transaction @Transaction
open fun removeFuture(profileId: Int, todayDate: Date, filter: String) { open fun dontKeepFuture(profileId: Int, todayDate: Date, filter: String) {
removeFuture(SimpleSQLiteQuery("DELETE FROM events WHERE profileId = " + profileId dontKeepFuture(SimpleSQLiteQuery("UPDATE events SET keep = 0 WHERE profileId = " + profileId
+ " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.stringY_m_d + "'" + + " AND eventAddedManually = 0 AND eventDate >= '" + todayDate.stringY_m_d + "'" +
" AND " + filter)) " AND " + filter))
} }
@Query("DELETE FROM events WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type") @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType = :type")
abstract fun removeFutureWithType(profileId: Int, todayDate: Date, type: Long) 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") @Query("UPDATE events SET keep = 0 WHERE profileId = :profileId AND eventAddedManually = 0 AND eventDate >= :todayDate AND eventType != :exceptType")
abstract fun removeFutureExceptType(profileId: Int, todayDate: Date, exceptType: Long) abstract fun dontKeepFutureExceptType(profileId: Int, todayDate: Date, exceptType: Long)
@Transaction @Transaction
open fun removeFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List<Long>) { open fun dontKeepFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List<Long>) {
removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')')) 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)") @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)")

View File

@ -42,7 +42,7 @@ open class Event(
var teacherId: Long, var teacherId: Long,
var subjectId: Long, var subjectId: Long,
var teamId: Long var teamId: Long
) { ) : Keepable() {
companion object { companion object {
const val TYPE_UNDEFINED = -2L const val TYPE_UNDEFINED = -2L
const val TYPE_HOMEWORK = -1L const val TYPE_HOMEWORK = -1L

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -17,10 +17,7 @@ import java.io.File
import javax.annotation.processing.* import javax.annotation.processing.*
import javax.lang.model.SourceVersion import javax.lang.model.SourceVersion
import javax.lang.model.element.* import javax.lang.model.element.*
import javax.lang.model.type.MirroredTypeException import javax.lang.model.type.*
import javax.lang.model.type.MirroredTypesException
import javax.lang.model.type.TypeKind
import javax.lang.model.type.TypeMirror
import javax.lang.model.util.ElementFilter import javax.lang.model.util.ElementFilter
import javax.tools.Diagnostic import javax.tools.Diagnostic
import kotlin.reflect.KClass import kotlin.reflect.KClass
@ -146,14 +143,26 @@ class FileGenerator : AbstractProcessor() {
val primaryKeys = annotation.primaryKeys val primaryKeys = annotation.primaryKeys
val skippedColumns = annotation.skippedColumns val skippedColumns = annotation.skippedColumns
val members = processingEnv.elementUtils.getAllMembers(paramTypeElement)
var members = processingEnv.elementUtils.getAllMembers(paramTypeElement)
val allFields = ElementFilter.fieldsIn(members) 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 { it.getAnnotation(Ignore::class.java) != null }
allFields.removeAll { field -> field.modifiers.any { it == Modifier.STATIC || it == Modifier.FINAL } } 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 fields = allFieldsDistinct.filterNot { primaryKeys.contains(it.name()) }
val primaryFields = allFields.filter { primaryKeys.contains(it.name()) } val primaryFields = allFieldsDistinct.filter { primaryKeys.contains(it.name()) }
val fieldNames = fields.map { it.name() } val fieldNames = fields.map { it.name() }
val primaryFieldNames = primaryFields.map { it.name() } val primaryFieldNames = primaryFields.map { it.name() }