perf: Make network calls async

This commit is contained in:
sweetbread 2024-03-29 18:05:13 +03:00 committed by Sweetbread
parent 455a3cae4c
commit 3138afb0de
4 changed files with 47 additions and 41 deletions

View File

@ -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)

View File

@ -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) {
}

View File

@ -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 {

View File

@ -10,8 +10,7 @@ kotlin = "1.9.0"
coreKtx = "1.12.0"
junitVersion = "1.1.5"
espressoCore = "3.5.1"
ktorClientCio = "2.3.9"
ktorClientCore = "2.3.9"
ktor = "2.3.9"
ktorClientLogging = "2.3.9"
lifecycleRuntimeKtx = "2.7.0"
activityCompose = "1.8.2"
@ -54,8 +53,9 @@ androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-man
androidx-ui-test-junit4 = { group = "androidx.compose.ui", name = "ui-test-junit4" }
androidx-material3 = { group = "androidx.compose.material3", name = "material3" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "appcompat" }
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktorClientCio" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktorClientCore" }
ktor-client-android = { module = "io.ktor:ktor-client-android", version.ref = "ktor" }
ktor-client-cio = { module = "io.ktor:ktor-client-cio", version.ref = "ktor" }
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-logging = { module = "io.ktor:ktor-client-logging", version.ref = "ktorClientLogging" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "annotation" }