ref: async and thread stuff
This commit is contained in:
@@ -9,6 +9,8 @@ import io.ktor.client.request.bearerAuth
|
|||||||
import io.ktor.client.request.get
|
import io.ktor.client.request.get
|
||||||
import io.ktor.client.statement.bodyAsText
|
import io.ktor.client.statement.bodyAsText
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.coroutineScope
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import ru.risdeveau.pixeldragon.baseUrl
|
import ru.risdeveau.pixeldragon.baseUrl
|
||||||
import ru.risdeveau.pixeldragon.client
|
import ru.risdeveau.pixeldragon.client
|
||||||
@@ -58,22 +60,27 @@ suspend fun getRoom(rid: String, joined: Boolean? = null): Room {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val name = getState(rid, "m.room.name", "name")
|
return coroutineScope {
|
||||||
val type = getState(rid, "m.room.create", "type") ?: "m.room"
|
val name = async { getState(rid, "m.room.name", "name") }
|
||||||
val creator = getState(rid, "m.room.create", "creator")
|
val type = async { getState(rid, "m.room.create", "type") ?: "m.room" }
|
||||||
val avatar = getState(rid, "m.room.avatar", "url")
|
val creator = async { getState(rid, "m.room.create", "creator") }
|
||||||
return Room(
|
val avatar = async { getState(rid, "m.room.avatar", "url") }
|
||||||
|
val joined = async { joined ?: isJoined(rid) }
|
||||||
|
val direct = async { if (directWith.isNotEmpty()) User.getById(directWith) else null }
|
||||||
|
|
||||||
|
Room(
|
||||||
rid,
|
rid,
|
||||||
name,
|
name.await(),
|
||||||
type,
|
type.await(),
|
||||||
creator,
|
creator.await(),
|
||||||
null,
|
null,
|
||||||
avatar,
|
avatar.await(),
|
||||||
null,
|
null,
|
||||||
joined ?: isJoined(rid),
|
joined.await(),
|
||||||
if (directWith.isNotEmpty()) User.getById(directWith) else null
|
direct.await()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun getState(rid: String, state: String, key: String): String? {
|
private suspend fun getState(rid: String, state: String, key: String): String? {
|
||||||
val r = client.get("$baseUrl/rooms/$rid/state/$state") { bearerAuth(token) }
|
val r = client.get("$baseUrl/rooms/$rid/state/$state") { bearerAuth(token) }
|
||||||
|
|||||||
@@ -5,6 +5,9 @@
|
|||||||
|
|
||||||
package ru.risdeveau.pixeldragon.repo
|
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.getJoinedRooms
|
||||||
import ru.risdeveau.pixeldragon.api.getRoom
|
import ru.risdeveau.pixeldragon.api.getRoom
|
||||||
import ru.risdeveau.pixeldragon.db.cacheDb
|
import ru.risdeveau.pixeldragon.db.cacheDb
|
||||||
@@ -32,8 +35,10 @@ class Room (
|
|||||||
(!cached or (cachedRoom == null) or (cachedRoom?.isExpired() == true))
|
(!cached or (cachedRoom == null) or (cachedRoom?.isExpired() == true))
|
||||||
) {
|
) {
|
||||||
val room = getRoom(id)
|
val room = getRoom(id)
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
val cacheRoom = room.toEntity()
|
val cacheRoom = room.toEntity()
|
||||||
cacheDb.roomDoa().insert(cacheRoom)
|
cacheDb.roomDoa().insert(cacheRoom)
|
||||||
|
}
|
||||||
return room
|
return room
|
||||||
}
|
}
|
||||||
return cachedRoom!!.toDomain()
|
return cachedRoom!!.toDomain()
|
||||||
@@ -41,8 +46,15 @@ class Room (
|
|||||||
|
|
||||||
suspend fun getJoined(cached: Boolean = true): List<Room> {
|
suspend fun getJoined(cached: Boolean = true): List<Room> {
|
||||||
val cacheJoined = cacheDb.roomDoa().getAllJoined()
|
val cacheJoined = cacheDb.roomDoa().getAllJoined()
|
||||||
if (!isConnected() and cacheJoined.isEmpty()) {
|
if (isConnected() and
|
||||||
return getJoinedRooms()
|
(!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() }
|
return List(cacheJoined.size) { i -> cacheJoined[i].toDomain() }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@
|
|||||||
package ru.risdeveau.pixeldragon.repo
|
package ru.risdeveau.pixeldragon.repo
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import ru.risdeveau.pixeldragon.api.getUserProfile
|
import ru.risdeveau.pixeldragon.api.getUserProfile
|
||||||
import ru.risdeveau.pixeldragon.db.cacheDb
|
import ru.risdeveau.pixeldragon.db.cacheDb
|
||||||
@@ -23,7 +26,7 @@ class User (
|
|||||||
companion object {
|
companion object {
|
||||||
suspend fun getById(id: String, cached: Boolean = true): User? {
|
suspend fun getById(id: String, cached: Boolean = true): User? {
|
||||||
val cachedUser = cacheDb.userDoa().getById(id)
|
val cachedUser = cacheDb.userDoa().getById(id)
|
||||||
if (!isConnected() and
|
if (isConnected() and
|
||||||
(!cached or (cachedUser == null) or (cachedUser?.isExpired() == true))
|
(!cached or (cachedUser == null) or (cachedUser?.isExpired() == true))
|
||||||
) {
|
) {
|
||||||
val userProfile = getUserProfile(id)
|
val userProfile = getUserProfile(id)
|
||||||
@@ -37,7 +40,9 @@ class User (
|
|||||||
userProfile.avatarUrl,
|
userProfile.avatarUrl,
|
||||||
userProfile.other
|
userProfile.other
|
||||||
)
|
)
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
cacheDb.userDoa().insert(user.toEntity())
|
cacheDb.userDoa().insert(user.toEntity())
|
||||||
|
}
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
return cachedUser!!.toDomain()
|
return cachedUser!!.toDomain()
|
||||||
|
|||||||
Reference in New Issue
Block a user