🐛 Fixes ktor fetch not working

This commit is contained in:
Daniel Svitan
2025-05-17 11:15:40 +02:00
parent e4a2fdfd59
commit 3f5886ef4a
4 changed files with 32 additions and 10 deletions

View File

@@ -52,7 +52,9 @@ dependencies {
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.cio)
implementation(libs.ktor.client.android)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"

View File

@@ -34,6 +34,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import dev.svitan.antifed.ui.theme.AntiFedTheme
import io.ktor.client.HttpClient
import io.ktor.client.engine.android.Android
import io.ktor.client.request.get
import io.ktor.http.HttpStatusCode
import kotlinx.coroutines.delay
@OptIn(ExperimentalMaterial3Api::class)
@@ -45,8 +49,9 @@ class AuthActivity : ComponentActivity() {
AntiFedTheme {
var serverUrl by remember { mutableStateOf("") }
var checkingUrl by remember { mutableStateOf(false) }
var isServerUrlOk by remember { mutableStateOf(false) }
fun isServerUrlOkay(): Boolean {
fun serverUrlMatches(): Boolean {
var re = Regex(
"(?:http[s]?:\\/\\/.)?(?:www\\.)?[-a-zA-Z0-9@%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b(?:[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]*)",
RegexOption.DOT_MATCHES_ALL
@@ -55,15 +60,23 @@ class AuthActivity : ComponentActivity() {
}
LaunchedEffect(serverUrl) {
if (serverUrl.isBlank()) return@LaunchedEffect
if (!isServerUrlOkay()) return@LaunchedEffect
if (serverUrl.isBlank()) {
isServerUrlOk = false
return@LaunchedEffect
}
if (!serverUrlMatches()) {
isServerUrlOk = false
return@LaunchedEffect
}
checkingUrl = true
delay(1000)
// val client = HttpClient(CIO)
// val response = client.get(serverUrl)
// println(response.status)
checkingUrl = true
val client = HttpClient(Android)
val response = client.get(serverUrl)
checkingUrl = false
isServerUrlOk = response.status == HttpStatusCode.OK
}
Scaffold(
@@ -103,7 +116,7 @@ class AuthActivity : ComponentActivity() {
onValueChange = { serverUrl = it },
label = { Text(stringResource(R.string.server_url)) },
singleLine = true,
isError = !isServerUrlOkay(),
isError = !isServerUrlOk,
trailingIcon = {
if (checkingUrl)
CircularProgressIndicator(