🐛 Fixes ktor fetch not working
This commit is contained in:
@@ -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)
|
||||
|
@@ -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"
|
||||
|
@@ -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(
|
||||
|
Reference in New Issue
Block a user