feat: check for internet connection before sending API requests
This commit is contained in:
@@ -10,6 +10,7 @@ import androidx.room.Room
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverter
|
||||
import splitties.init.appCtx
|
||||
import splitties.systemservices.connectivityManager
|
||||
import java.time.Instant
|
||||
|
||||
@Database(entities = [RoomDB::class, SpaceToRoom::class, UserDB::class], version = 1)
|
||||
@@ -30,6 +31,8 @@ class Converters {
|
||||
}
|
||||
}
|
||||
|
||||
fun isConnected(): Boolean = connectivityManager.isDefaultNetworkActive
|
||||
|
||||
val cacheDb = Room.databaseBuilder(
|
||||
appCtx,
|
||||
AppDatabase::class.java, "cache"
|
||||
|
||||
@@ -8,6 +8,7 @@ package ru.risdeveau.pixeldragon.repo
|
||||
import ru.risdeveau.pixeldragon.api.getJoinedRooms
|
||||
import ru.risdeveau.pixeldragon.api.getRoom
|
||||
import ru.risdeveau.pixeldragon.db.cacheDb
|
||||
import ru.risdeveau.pixeldragon.db.isConnected
|
||||
import ru.risdeveau.pixeldragon.db.isExpired
|
||||
import ru.risdeveau.pixeldragon.db.toDomain
|
||||
import ru.risdeveau.pixeldragon.db.toEntity
|
||||
@@ -27,7 +28,9 @@ class Room (
|
||||
companion object {
|
||||
suspend fun getById(id: String, cached: Boolean = true): Room {
|
||||
val cachedRoom = cacheDb.roomDoa().getById(id)
|
||||
if ((cachedRoom == null) or (cachedRoom?.isExpired() == true)) {
|
||||
if (!isConnected() and
|
||||
(!cached or (cachedRoom == null) or (cachedRoom?.isExpired() == true))
|
||||
) {
|
||||
val room = getRoom(id)
|
||||
val cacheRoom = room.toEntity()
|
||||
cacheDb.roomDoa().insert(cacheRoom)
|
||||
@@ -38,7 +41,7 @@ class Room (
|
||||
|
||||
suspend fun getJoined(cached: Boolean = true): List<Room> {
|
||||
val cacheJoined = cacheDb.roomDoa().getAllJoined()
|
||||
if (cacheJoined.isEmpty()) {
|
||||
if (!isConnected() and cacheJoined.isEmpty()) {
|
||||
return getJoinedRooms()
|
||||
}
|
||||
return List(cacheJoined.size) { i -> cacheJoined[i].toDomain() }
|
||||
|
||||
@@ -9,6 +9,7 @@ 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.isConnected
|
||||
import ru.risdeveau.pixeldragon.db.isExpired
|
||||
import ru.risdeveau.pixeldragon.db.toDomain
|
||||
import ru.risdeveau.pixeldragon.db.toEntity
|
||||
@@ -20,9 +21,11 @@ class User (
|
||||
val attrs: JSONObject
|
||||
) {
|
||||
companion object {
|
||||
suspend fun getById(id: String): User? {
|
||||
suspend fun getById(id: String, cached: Boolean = true): User? {
|
||||
val cachedUser = cacheDb.userDoa().getById(id)
|
||||
if ((cachedUser == null) or (cachedUser?.isExpired() == true)) {
|
||||
if (!isConnected() and
|
||||
(!cached or (cachedUser == null) or (cachedUser?.isExpired() == true))
|
||||
) {
|
||||
val userProfile = getUserProfile(id)
|
||||
if (userProfile == null) {
|
||||
Log.i("User.getById", "User $id not found")
|
||||
|
||||
Reference in New Issue
Block a user