ref: async and thread stuff
This commit is contained in:
@@ -5,6 +5,9 @@
|
||||
|
||||
package ru.risdeveau.pixeldragon.repo
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import ru.risdeveau.pixeldragon.api.getJoinedRooms
|
||||
import ru.risdeveau.pixeldragon.api.getRoom
|
||||
import ru.risdeveau.pixeldragon.db.cacheDb
|
||||
@@ -32,8 +35,10 @@ class Room (
|
||||
(!cached or (cachedRoom == null) or (cachedRoom?.isExpired() == true))
|
||||
) {
|
||||
val room = getRoom(id)
|
||||
val cacheRoom = room.toEntity()
|
||||
cacheDb.roomDoa().insert(cacheRoom)
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val cacheRoom = room.toEntity()
|
||||
cacheDb.roomDoa().insert(cacheRoom)
|
||||
}
|
||||
return room
|
||||
}
|
||||
return cachedRoom!!.toDomain()
|
||||
@@ -41,8 +46,15 @@ class Room (
|
||||
|
||||
suspend fun getJoined(cached: Boolean = true): List<Room> {
|
||||
val cacheJoined = cacheDb.roomDoa().getAllJoined()
|
||||
if (!isConnected() and cacheJoined.isEmpty()) {
|
||||
return getJoinedRooms()
|
||||
if (isConnected() and
|
||||
(!cached or cacheJoined.isEmpty() or (cacheJoined.any { it.isExpired() }))
|
||||
) {
|
||||
val rooms = getJoinedRooms()
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
val roomsDb = List(rooms.size) { i -> rooms[i].toEntity() }
|
||||
cacheDb.roomDoa().insert(*roomsDb.toTypedArray())
|
||||
}
|
||||
return rooms
|
||||
}
|
||||
return List(cacheJoined.size) { i -> cacheJoined[i].toDomain() }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user