ref: reformat caching data in Rooms

This commit is contained in:
2025-11-04 20:30:22 +03:00
parent 05fbfbac07
commit ebbc9a4b1f
9 changed files with 246 additions and 87 deletions
+43
View File
@@ -0,0 +1,43 @@
/*
* Created by sweetbread
* Copyright (c) 2025. All rights reserved.
*/
package ru.risdeveau.pixeldragon.repo
import android.util.Log
import org.json.JSONObject
import ru.risdeveau.pixeldragon.api.getUserProfile
import ru.risdeveau.pixeldragon.db.cacheDb
import ru.risdeveau.pixeldragon.db.isExpired
import ru.risdeveau.pixeldragon.db.toDomain
import ru.risdeveau.pixeldragon.db.toEntity
class User (
val id: String,
val name: String?,
val avatarUrl: String?,
val attrs: JSONObject
) {
companion object {
suspend fun getById(id: String): User? {
val cachedUser = cacheDb.userDoa().getById(id)
if ((cachedUser == null) or (cachedUser?.isExpired() == true)) {
val userProfile = getUserProfile(id)
if (userProfile == null) {
Log.i("User.getById", "User $id not found")
return null
}
val user = User(
id,
userProfile.displayName,
userProfile.avatarUrl,
userProfile.other
)
cacheDb.userDoa().insert(user.toEntity())
return user
}
return cachedUser!!.toDomain()
}
}
}