🐛 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.material)
implementation(libs.androidx.activity) implementation(libs.androidx.activity)
implementation(libs.ktor.client.core) 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) testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules" 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.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import dev.svitan.antifed.ui.theme.AntiFedTheme 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 import kotlinx.coroutines.delay
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@ -45,8 +49,9 @@ class AuthActivity : ComponentActivity() {
AntiFedTheme { AntiFedTheme {
var serverUrl by remember { mutableStateOf("") } var serverUrl by remember { mutableStateOf("") }
var checkingUrl by remember { mutableStateOf(false) } var checkingUrl by remember { mutableStateOf(false) }
var isServerUrlOk by remember { mutableStateOf(false) }
fun isServerUrlOkay(): Boolean { fun serverUrlMatches(): Boolean {
var re = Regex( var re = Regex(
"(?:http[s]?:\\/\\/.)?(?:www\\.)?[-a-zA-Z0-9@%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b(?:[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]*)", "(?:http[s]?:\\/\\/.)?(?:www\\.)?[-a-zA-Z0-9@%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b(?:[-a-zA-Z0-9@:%_\\+.~#?&\\/\\/=]*)",
RegexOption.DOT_MATCHES_ALL RegexOption.DOT_MATCHES_ALL
@ -55,15 +60,23 @@ class AuthActivity : ComponentActivity() {
} }
LaunchedEffect(serverUrl) { LaunchedEffect(serverUrl) {
if (serverUrl.isBlank()) return@LaunchedEffect if (serverUrl.isBlank()) {
if (!isServerUrlOkay()) return@LaunchedEffect isServerUrlOk = false
return@LaunchedEffect
}
if (!serverUrlMatches()) {
isServerUrlOk = false
return@LaunchedEffect
}
checkingUrl = true
delay(1000) delay(1000)
// val client = HttpClient(CIO) checkingUrl = true
// val response = client.get(serverUrl)
// println(response.status) val client = HttpClient(Android)
val response = client.get(serverUrl)
checkingUrl = false checkingUrl = false
isServerUrlOk = response.status == HttpStatusCode.OK
} }
Scaffold( Scaffold(
@ -103,7 +116,7 @@ class AuthActivity : ComponentActivity() {
onValueChange = { serverUrl = it }, onValueChange = { serverUrl = it },
label = { Text(stringResource(R.string.server_url)) }, label = { Text(stringResource(R.string.server_url)) },
singleLine = true, singleLine = true,
isError = !isServerUrlOkay(), isError = !isServerUrlOk,
trailingIcon = { trailingIcon = {
if (checkingUrl) if (checkingUrl)
CircularProgressIndicator( CircularProgressIndicator(

View File

@ -13,6 +13,9 @@ material = "1.10.0"
activity = "1.10.1" activity = "1.10.1"
ktorClientCore = "3.1.3" ktorClientCore = "3.1.3"
ktorClientCio = "3.1.3" ktorClientCio = "3.1.3"
ktorClientAndroid = "3.1.3"
ktorClientContentNegotiation = "3.1.3"
ktorSerializationKotlinxJson = "3.1.3"
[libraries] [libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version = "1.16.0" } androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version = "1.16.0" }
@ -26,7 +29,7 @@ androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" } androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" } androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" } androidx-ui-tooling-preview = { group = "androidx.compose.ui", name = "ui-tooling-preview" }
androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest" } androidx-ui-test-manifest = { group = "androidx.compose.ui", name = "ui-test-manifest", version = "1.8.1" }
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" }
@ -34,6 +37,9 @@ material = { group = "com.google.android.material", name = "material", version =
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" } androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktorClientCore" } ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktorClientCore" }
ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktorClientCio" } ktor-client-cio = { group = "io.ktor", name = "ktor-client-cio", version.ref = "ktorClientCio" }
ktor-client-android = { group = "io.ktor", name = "ktor-client-android", version.ref = "ktorClientAndroid" }
ktor-client-content-negotiation = { group = "io.ktor", name = "ktor-client-content-negotiation", version.ref = "ktorClientContentNegotiation" }
ktor-serialization-kotlinx-json = { group = "io.ktor", name = "ktor-serialization-kotlinx-json", version.ref = "ktorSerializationKotlinxJson" }
[plugins] [plugins]
android-application = { id = "com.android.application", version.ref = "agp" } android-application = { id = "com.android.application", version.ref = "agp" }