Migrate to LiveData
Benefit includes: no more flush after changing nickname. Yep.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package be.mygod.vpnhotspot.room
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.room.*
|
||||
|
||||
@Entity
|
||||
@@ -8,14 +9,23 @@ data class ClientRecord(@PrimaryKey
|
||||
var nickname: CharSequence = "",
|
||||
var blocked: Boolean = false) {
|
||||
@androidx.room.Dao
|
||||
interface Dao {
|
||||
abstract class Dao {
|
||||
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
|
||||
fun lookupOrNull(mac: Long): ClientRecord?
|
||||
abstract fun lookup(mac: Long): ClientRecord?
|
||||
|
||||
fun lookupOrDefault(mac: Long) = lookup(mac) ?: ClientRecord(mac)
|
||||
|
||||
@Query("SELECT * FROM `ClientRecord` WHERE `mac` = :mac")
|
||||
abstract fun lookupSync(mac: Long): LiveData<ClientRecord>
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun updateInternal(value: ClientRecord): Long
|
||||
protected abstract fun updateInternal(value: ClientRecord): Long
|
||||
fun update(value: ClientRecord) = check(updateInternal(value) == value.mac)
|
||||
|
||||
@Transaction
|
||||
open fun upsert(mac: Long, operation: ClientRecord.() -> Unit) = lookupOrDefault(mac).apply {
|
||||
operation()
|
||||
update(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun ClientRecord.Dao.lookup(mac: Long) = lookupOrNull(mac) ?: ClientRecord(mac)
|
||||
fun ClientRecord.Dao.update(value: ClientRecord) = check(updateInternal(value) == value.mac)
|
||||
|
||||
@@ -7,7 +7,8 @@ import java.net.InetAddress
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
class Converters {
|
||||
object Converters {
|
||||
@JvmStatic
|
||||
@TypeConverter
|
||||
fun persistCharSequence(cs: CharSequence): ByteArray {
|
||||
val p = Parcel.obtain()
|
||||
@@ -19,6 +20,7 @@ class Converters {
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@TypeConverter
|
||||
fun unpersistCharSequence(data: ByteArray): CharSequence {
|
||||
val p = Parcel.obtain()
|
||||
@@ -31,9 +33,11 @@ class Converters {
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@TypeConverter
|
||||
fun persistInetAddress(address: InetAddress): ByteArray = address.address
|
||||
|
||||
@JvmStatic
|
||||
@TypeConverter
|
||||
fun unpersistInetAddress(data: ByteArray): InetAddress = InetAddress.getByAddress(data)
|
||||
}
|
||||
|
||||
@@ -38,9 +38,13 @@ data class TrafficRecord(
|
||||
*/
|
||||
val previousId: Long? = null) {
|
||||
@androidx.room.Dao
|
||||
interface Dao {
|
||||
abstract class Dao {
|
||||
@Insert
|
||||
fun insertInternal(value: TrafficRecord): Long
|
||||
protected abstract fun insertInternal(value: TrafficRecord): Long
|
||||
fun insert(value: TrafficRecord) {
|
||||
check(value.id == null)
|
||||
value.id = insertInternal(value)
|
||||
}
|
||||
|
||||
@Query("""
|
||||
SELECT MIN(TrafficRecord.timestamp) AS timestamp,
|
||||
@@ -52,15 +56,10 @@ data class TrafficRecord(
|
||||
FROM TrafficRecord LEFT JOIN TrafficRecord AS Next ON TrafficRecord.id = Next.previousId
|
||||
/* We only want to find the last record for each chain so that we don't double count */
|
||||
WHERE TrafficRecord.mac = :mac AND Next.id IS NULL""")
|
||||
fun queryStats(mac: Long): ClientStats
|
||||
abstract fun queryStats(mac: Long): ClientStats
|
||||
}
|
||||
}
|
||||
|
||||
fun TrafficRecord.Dao.insert(value: TrafficRecord) {
|
||||
check(value.id == null)
|
||||
value.id = insertInternal(value)
|
||||
}
|
||||
|
||||
data class ClientStats(
|
||||
val timestamp: Long = 0,
|
||||
val count: Long = 0,
|
||||
|
||||
Reference in New Issue
Block a user