2019-11-14 06:01:06 +01:00
|
|
|
package com.topjohnwu.magisk.data.database
|
|
|
|
|
|
|
|
import androidx.room.*
|
|
|
|
import com.topjohnwu.magisk.model.entity.MagiskLog
|
2020-07-09 13:49:14 +02:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.withContext
|
2019-11-14 06:01:06 +01:00
|
|
|
import java.util.*
|
|
|
|
|
2020-02-22 08:40:20 +01:00
|
|
|
@Database(version = 1, entities = [MagiskLog::class], exportSchema = false)
|
2019-11-14 06:01:06 +01:00
|
|
|
abstract class SuLogDatabase : RoomDatabase() {
|
|
|
|
|
|
|
|
abstract fun suLogDao(): SuLogDao
|
|
|
|
}
|
|
|
|
|
|
|
|
@Dao
|
|
|
|
abstract class SuLogDao(private val db: SuLogDatabase) {
|
|
|
|
|
|
|
|
private val twoWeeksAgo =
|
|
|
|
Calendar.getInstance().apply { add(Calendar.WEEK_OF_YEAR, -2) }.timeInMillis
|
|
|
|
|
2020-07-09 13:49:14 +02:00
|
|
|
suspend fun deleteAll() = withContext(Dispatchers.IO) { db.clearAllTables() }
|
2019-11-14 06:01:06 +01:00
|
|
|
|
2020-07-09 13:49:14 +02:00
|
|
|
suspend fun fetchAll(): MutableList<MagiskLog> {
|
|
|
|
deleteOutdated()
|
|
|
|
return fetch()
|
|
|
|
}
|
2019-11-14 06:01:06 +01:00
|
|
|
|
|
|
|
@Query("SELECT * FROM logs ORDER BY time DESC")
|
2020-07-09 13:49:14 +02:00
|
|
|
protected abstract suspend fun fetch(): MutableList<MagiskLog>
|
2019-11-14 06:01:06 +01:00
|
|
|
|
|
|
|
@Query("DELETE FROM logs WHERE time < :timeout")
|
2020-07-09 13:49:14 +02:00
|
|
|
protected abstract suspend fun deleteOutdated(timeout: Long = twoWeeksAgo)
|
|
|
|
|
|
|
|
@Insert
|
|
|
|
abstract suspend fun insert(log: MagiskLog)
|
2019-11-14 06:01:06 +01:00
|
|
|
|
|
|
|
}
|