add url validation
This commit is contained in:
parent
739b7def10
commit
832ad6a7f1
1 changed files with 26 additions and 3 deletions
|
|
@ -2,8 +2,10 @@ package de.lelehier.keeper.screens
|
||||||
|
|
||||||
import KeeperLargeFontFamily
|
import KeeperLargeFontFamily
|
||||||
import android.print.PrintAttributes.Margins
|
import android.print.PrintAttributes.Margins
|
||||||
|
import android.util.Patterns
|
||||||
import androidx.compose.animation.AnimatedContent
|
import androidx.compose.animation.AnimatedContent
|
||||||
import androidx.compose.animation.AnimatedContentTransitionScope
|
import androidx.compose.animation.AnimatedContentTransitionScope
|
||||||
|
import androidx.compose.animation.AnimatedVisibility
|
||||||
import androidx.compose.animation.core.EaseIn
|
import androidx.compose.animation.core.EaseIn
|
||||||
import androidx.compose.animation.core.tween
|
import androidx.compose.animation.core.tween
|
||||||
import androidx.compose.animation.fadeIn
|
import androidx.compose.animation.fadeIn
|
||||||
|
|
@ -16,6 +18,7 @@ import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.imePadding
|
import androidx.compose.foundation.layout.imePadding
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.text.KeyboardOptions
|
||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.OutlinedTextField
|
import androidx.compose.material3.OutlinedTextField
|
||||||
|
|
@ -31,6 +34,7 @@ import androidx.compose.ui.graphics.Brush
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.text.TextStyle
|
import androidx.compose.ui.text.TextStyle
|
||||||
import androidx.compose.ui.text.font.FontFamily
|
import androidx.compose.ui.text.font.FontFamily
|
||||||
|
import androidx.compose.ui.text.input.KeyboardType
|
||||||
import androidx.compose.ui.text.style.TextAlign
|
import androidx.compose.ui.text.style.TextAlign
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import androidx.compose.ui.unit.sp
|
import androidx.compose.ui.unit.sp
|
||||||
|
|
@ -51,7 +55,7 @@ fun homeScreen(paddingValues: PaddingValues) {
|
||||||
.imePadding(),
|
.imePadding(),
|
||||||
horizontalAlignment = Alignment.CenterHorizontally,
|
horizontalAlignment = Alignment.CenterHorizontally,
|
||||||
verticalArrangement = Arrangement.Center) {
|
verticalArrangement = Arrangement.Center) {
|
||||||
Greeting();
|
Greeting(serverURL);
|
||||||
AnimatedContent(
|
AnimatedContent(
|
||||||
targetState = currentScreen,
|
targetState = currentScreen,
|
||||||
transitionSpec = {
|
transitionSpec = {
|
||||||
|
|
@ -75,7 +79,7 @@ fun homeScreen(paddingValues: PaddingValues) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun Greeting() {
|
fun Greeting(serverURL: String) {
|
||||||
Column (horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(bottom = 24.dp)) {
|
Column (horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.padding(bottom = 24.dp)) {
|
||||||
Text(
|
Text(
|
||||||
text = "Welcome to",
|
text = "Welcome to",
|
||||||
|
|
@ -90,6 +94,7 @@ fun Greeting() {
|
||||||
brush = Brush.linearGradient(listOf(MaterialTheme.colorScheme.onPrimaryContainer, MaterialTheme.colorScheme.onSecondaryContainer))),
|
brush = Brush.linearGradient(listOf(MaterialTheme.colorScheme.onPrimaryContainer, MaterialTheme.colorScheme.onSecondaryContainer))),
|
||||||
textAlign = TextAlign.Center,
|
textAlign = TextAlign.Center,
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,9 +108,27 @@ fun serverDialog(serverURL: String) {
|
||||||
value = serverURL,
|
value = serverURL,
|
||||||
onValueChange = { text ->
|
onValueChange = { text ->
|
||||||
serverURL = text
|
serverURL = text
|
||||||
}
|
},
|
||||||
|
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Uri),
|
||||||
|
supportingText = {
|
||||||
|
AnimatedVisibility(!isValidUrl(serverURL)) {
|
||||||
|
Text(
|
||||||
|
text = "No valid URL",
|
||||||
|
color = MaterialTheme.colorScheme.error
|
||||||
)
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun isValidUrl(url: String): Boolean {
|
||||||
|
return try {
|
||||||
|
// Use Android's Patterns.WEB_URL for robust URL validation
|
||||||
|
// This handles various URL formats, including those without schemes.
|
||||||
|
Patterns.WEB_URL.matcher(url).matches()
|
||||||
|
} catch (e: Exception) {
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue