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.core)
implementation(libs.ktor.client.cio) implementation(libs.ktor.client.cio)
implementation(libs.ktor.client.logging) implementation(libs.ktor.client.logging)
implementation(libs.ktor.client.android)
implementation(libs.coil.compose) implementation(libs.coil.compose)
implementation(libs.androidx.datastore.preferences) 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.client.statement.bodyAsText
import io.ktor.http.parameters import io.ktor.http.parameters
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.json.JSONArray import org.json.JSONArray
import org.json.JSONObject import org.json.JSONObject
import ru.sweetbread.unn.db.cacheScheduleItems import ru.sweetbread.unn.db.cacheScheduleItems
@ -147,8 +148,10 @@ suspend fun auth(
if (r.status.value == 302) { if (r.status.value == 302) {
PHPSESSID = PHPSESSID =
"""PHPSESSID=([\w\d]+)""".toRegex().find(r.headers["Set-Cookie"]!!)!!.groupValues[1] """PHPSESSID=([\w\d]+)""".toRegex().find(r.headers["Set-Cookie"]!!)!!.groupValues[1]
GlobalScope.launch(Dispatchers.IO) {
getMyself(login) getMyself(login)
getCSRF() getCSRF()
}
return true return true
} }
return false return false
@ -158,6 +161,7 @@ suspend fun auth(
* Save info about current [User] in memory * Save info about current [User] in memory
*/ */
private suspend fun getMyself(login: String) { private suspend fun getMyself(login: String) {
GlobalScope.launch(Dispatchers.IO) {
val studentinfo = JSONObject(client.get("$ruzapiURL/studentinfo") { val studentinfo = JSONObject(client.get("$ruzapiURL/studentinfo") {
parameter("uns", login.substring(1)) parameter("uns", login.substring(1))
}.bodyAsText()) }.bodyAsText())
@ -185,13 +189,14 @@ private suspend fun getMyself(login: String) {
DateTimeFormatter.ofPattern("yyyy-MM-dd") DateTimeFormatter.ofPattern("yyyy-MM-dd")
), ),
avatar = user.getJSONObject("photo").let { avatar = user.getJSONObject("photo").let {
AvatarSet( ImageSet(
it.getString("orig"), it.getString("orig"),
it.getString("thumbnail"), it.getString("thumbnail"),
it.getString("small"), it.getString("small"),
) )
} }
) )
}
} }
suspend fun getScheduleDay( suspend fun getScheduleDay(
@ -384,4 +389,3 @@ suspend fun getUser(id: Int): User {
fun getComments(id: Int) { fun getComments(id: Int) {
}

View File

@ -27,6 +27,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import androidx.room.Room import androidx.room.Room
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.plugins.HttpRequestRetry import io.ktor.client.plugins.HttpRequestRetry
import io.ktor.client.plugins.HttpTimeout import io.ktor.client.plugins.HttpTimeout
import io.ktor.client.plugins.cache.HttpCache 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 ru.sweetbread.unn.ui.theme.UNNTheme
import splitties.toast.toast import splitties.toast.toast
val client = HttpClient { val client = HttpClient(Android) {
install(HttpCache) install(HttpCache)
install(Logging) { install(Logging) {
logger = object : Logger { logger = object : Logger {

View File

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