ref: reformat caching data in Rooms
This commit is contained in:
@@ -1,18 +1,59 @@
|
||||
/*
|
||||
* Created by sweetbread on 22.02.2025, 15:45
|
||||
* Created by sweetbread
|
||||
* Copyright (c) 2025. All rights reserved.
|
||||
* Last modified 21.02.2025, 13:38
|
||||
*/
|
||||
|
||||
package ru.risdeveau.pixeldragon.db
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Delete
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Insert
|
||||
import androidx.room.PrimaryKey
|
||||
import androidx.room.Query
|
||||
import androidx.room.TypeConverters
|
||||
import org.json.JSONObject
|
||||
import ru.risdeveau.pixeldragon.repo.User
|
||||
import java.time.Instant
|
||||
|
||||
@Entity
|
||||
data class User(
|
||||
@PrimaryKey val uid: String,
|
||||
@ColumnInfo(name = "name") val name: String?,
|
||||
@ColumnInfo(name = "avatar") val avatar: String?
|
||||
)
|
||||
@Entity(tableName = "user")
|
||||
@TypeConverters(Converters::class)
|
||||
data class UserDB (
|
||||
@PrimaryKey val id: String,
|
||||
val updatedAt: Instant,
|
||||
val name: String?,
|
||||
val avatar: String?,
|
||||
val attrs: String,
|
||||
)
|
||||
|
||||
fun UserDB.isExpired(cacheDuration: Long = 60 * 60): Boolean {
|
||||
return Instant.now().minusSeconds(cacheDuration) > updatedAt
|
||||
}
|
||||
|
||||
fun UserDB.toDomain(): User = User(
|
||||
id = id,
|
||||
name = name,
|
||||
avatarUrl = avatar,
|
||||
attrs = JSONObject(attrs),
|
||||
)
|
||||
|
||||
fun User.toEntity(): UserDB = UserDB(
|
||||
id = id,
|
||||
updatedAt = Instant.now(),
|
||||
name = name,
|
||||
avatar = avatarUrl,
|
||||
attrs = attrs.toString().trim(),
|
||||
)
|
||||
|
||||
|
||||
@Dao
|
||||
interface UserDao {
|
||||
@Query("SELECT * FROM user WHERE id LIKE :id LIMIT 1")
|
||||
fun getById(id: String): UserDB?
|
||||
|
||||
@Insert
|
||||
fun insert(vararg users: UserDB)
|
||||
|
||||
@Delete
|
||||
fun delete(user: UserDB)
|
||||
}
|
||||
Reference in New Issue
Block a user