mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-06-13 06:00:46 +02:00
[Events] Fix preserving isDone value. Improve DataRemoveModel method of keeping items.
This commit is contained in:
@ -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)
|
||||
|
@ -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) }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
@ -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<T, F> {
|
||||
interface BaseDao<T : Keepable, F : T> {
|
||||
@RawQuery
|
||||
fun getRaw(query: SupportSQLiteQuery): LiveData<List<F>>
|
||||
fun getRaw(query: String) = getRaw(SimpleSQLiteQuery(query))
|
||||
@ -21,6 +22,9 @@ interface BaseDao<T, F> {
|
||||
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<T, F> {
|
||||
* @return a [LongArray] of IDs of newly inserted items or -1L if the item existed before
|
||||
*/
|
||||
@Transaction
|
||||
fun upsertAll(items: List<T>): LongArray {
|
||||
fun upsertAll(items: List<T>, removeNotKept: Boolean = false): LongArray {
|
||||
val insertResult = addAll(items)
|
||||
val updateList = mutableListOf<T>()
|
||||
|
||||
@ -90,6 +94,7 @@ interface BaseDao<T, F> {
|
||||
}
|
||||
|
||||
if (updateList.isNotEmpty()) updateAll(items)
|
||||
if (removeNotKept) removeNotKept()
|
||||
return insertResult
|
||||
}
|
||||
}
|
||||
|
@ -90,34 +90,34 @@ abstract class EventDao : BaseDao<Event, EventFull> {
|
||||
@get:Query("SELECT eventId FROM events WHERE eventBlacklisted = 1")
|
||||
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?)
|
||||
|
||||
@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<Long>) {
|
||||
removeFuture(profileId, todayDate, "eventType NOT IN " + exceptTypes.toString().replace('[', '(').replace(']', ')'))
|
||||
open fun dontKeepFutureExceptTypes(profileId: Int, todayDate: Date, exceptTypes: List<Long>) {
|
||||
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)")
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
@ -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")
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user