perf: Make network calls async
This commit is contained in:
@@ -100,6 +100,7 @@ dependencies {
|
||||
implementation(libs.ktor.client.core)
|
||||
implementation(libs.ktor.client.cio)
|
||||
implementation(libs.ktor.client.logging)
|
||||
implementation(libs.ktor.client.android)
|
||||
implementation(libs.coil.compose)
|
||||
|
||||
implementation(libs.androidx.datastore.preferences)
|
||||
|
||||
@@ -8,7 +8,8 @@ import io.ktor.client.request.parameter
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.parameters
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import ru.sweetbread.unn.db.cacheScheduleItems
|
||||
@@ -147,8 +148,10 @@ suspend fun auth(
|
||||
if (r.status.value == 302) {
|
||||
PHPSESSID =
|
||||
"""PHPSESSID=([\w\d]+)""".toRegex().find(r.headers["Set-Cookie"]!!)!!.groupValues[1]
|
||||
getMyself(login)
|
||||
getCSRF()
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
getMyself(login)
|
||||
getCSRF()
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -158,40 +161,42 @@ suspend fun auth(
|
||||
* Save info about current [User] in memory
|
||||
*/
|
||||
private suspend fun getMyself(login: String) {
|
||||
val studentinfo = JSONObject(client.get("$ruzapiURL/studentinfo") {
|
||||
parameter("uns", login.substring(1))
|
||||
}.bodyAsText())
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
val studentinfo = JSONObject(client.get("$ruzapiURL/studentinfo") {
|
||||
parameter("uns", login.substring(1))
|
||||
}.bodyAsText())
|
||||
|
||||
val user = JSONObject(
|
||||
client.get("$vuzapiURL/user") {
|
||||
header("Cookie", "PHPSESSID=$PHPSESSID")
|
||||
}.bodyAsText()
|
||||
)
|
||||
val user = JSONObject(
|
||||
client.get("$vuzapiURL/user") {
|
||||
header("Cookie", "PHPSESSID=$PHPSESSID")
|
||||
}.bodyAsText()
|
||||
)
|
||||
|
||||
ME = User(
|
||||
unnId = studentinfo.getString("id").toInt(),
|
||||
bitrixId = user.getInt("bitrix_id"),
|
||||
userId = user.getInt("id"),
|
||||
type = when (studentinfo.getString("type")) {
|
||||
"lecturer" -> Type.Lecturer // ig,,,
|
||||
else -> Type.Student
|
||||
},
|
||||
email = user.getString("email"),
|
||||
nameRu = user.getString("fullname"),
|
||||
nameEn = user.getString("fullname_en"),
|
||||
isMale = user.getString("sex") == "M",
|
||||
birthday = LocalDate.parse(
|
||||
user.getString("birthdate"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
),
|
||||
avatar = user.getJSONObject("photo").let {
|
||||
AvatarSet(
|
||||
it.getString("orig"),
|
||||
it.getString("thumbnail"),
|
||||
it.getString("small"),
|
||||
)
|
||||
}
|
||||
)
|
||||
ME = User(
|
||||
unnId = studentinfo.getString("id").toInt(),
|
||||
bitrixId = user.getInt("bitrix_id"),
|
||||
userId = user.getInt("id"),
|
||||
type = when (studentinfo.getString("type")) {
|
||||
"lecturer" -> Type.Lecturer // ig,,,
|
||||
else -> Type.Student
|
||||
},
|
||||
email = user.getString("email"),
|
||||
nameRu = user.getString("fullname"),
|
||||
nameEn = user.getString("fullname_en"),
|
||||
isMale = user.getString("sex") == "M",
|
||||
birthday = LocalDate.parse(
|
||||
user.getString("birthdate"),
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
||||
),
|
||||
avatar = user.getJSONObject("photo").let {
|
||||
ImageSet(
|
||||
it.getString("orig"),
|
||||
it.getString("thumbnail"),
|
||||
it.getString("small"),
|
||||
)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getScheduleDay(
|
||||
@@ -384,4 +389,3 @@ suspend fun getUser(id: Int): User {
|
||||
|
||||
fun getComments(id: Int) {
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import androidx.navigation.compose.composable
|
||||
import androidx.navigation.compose.rememberNavController
|
||||
import androidx.room.Room
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.engine.android.Android
|
||||
import io.ktor.client.plugins.HttpRequestRetry
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.plugins.cache.HttpCache
|
||||
@@ -39,7 +40,7 @@ import ru.sweetbread.unn.ui.composes.Schedule
|
||||
import ru.sweetbread.unn.ui.theme.UNNTheme
|
||||
import splitties.toast.toast
|
||||
|
||||
val client = HttpClient {
|
||||
val client = HttpClient(Android) {
|
||||
install(HttpCache)
|
||||
install(Logging) {
|
||||
logger = object : Logger {
|
||||
|
||||
Reference in New Issue
Block a user