From a5f5d819345b871e35fe260b06c38b9448ac7040 Mon Sep 17 00:00:00 2001 From: Maciej Procyk Date: Mon, 19 Feb 2024 02:04:23 +0100 Subject: [PATCH] add scroll --- .../commonMain/kotlin/in/procyk/shin/App.kt | 68 ++++++++++-------- .../in/procyk/shin/ui/ShortenRequest.kt | 71 +++++++++---------- 2 files changed, 74 insertions(+), 65 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/in/procyk/shin/App.kt b/composeApp/src/commonMain/kotlin/in/procyk/shin/App.kt index d433f9c..afb523e 100644 --- a/composeApp/src/commonMain/kotlin/in/procyk/shin/App.kt +++ b/composeApp/src/commonMain/kotlin/in/procyk/shin/App.kt @@ -5,7 +5,6 @@ import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Modifier import androidx.compose.ui.input.key.* -import androidx.compose.ui.text.TextStyle import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.dp @@ -29,36 +28,47 @@ fun ShinApp() { val snackbarHostState = remember { SnackbarHostState() } val snackbarHostStateScope = rememberCoroutineScope() Scaffold( - snackbarHost = { SnackbarHost(hostState = snackbarHostState) } + snackbarHost = { SnackbarHost(hostState = snackbarHostState) }, + modifier = Modifier.fillMaxSize() ) { - var shortenedUrl by remember> { mutableStateOf(null) } - val scrollState = rememberScrollState() - Column( - modifier = Modifier - .fillMaxSize() - .onKeyEvent { event -> event.isEscDown.also { if (it) shortenedUrl = null } } - .verticalScroll(scrollState), - verticalArrangement = Arrangement.Center, + BoxWithConstraints( + modifier = Modifier.fillMaxSize(), ) { - Text( - text = "Shin", - fontFamily = FontFamily(Font(Res.font.Mansalva_Regular)), - style = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.primary), - fontSize = 64.sp, - textAlign = TextAlign.Center, - modifier = Modifier.fillMaxWidth(), - ) - Text( - text = "Shorten Your URL with Kotlin", - textAlign = TextAlign.Center, - modifier = Modifier.fillMaxWidth(), - ) - Spacer(Modifier.size(32.dp)) - ShortenRequest( - client = client, - onResponse = { shortenedUrl = it }, - onError = { snackbarHostStateScope.showErrorSnackbarNotification(snackbarHostState, it) }) - ShortenResponse(shortenedUrl) + val isVertical = maxHeight > maxWidth + val maxWidth = maxWidth + + var shortenedUrl by remember> { mutableStateOf(null) } + val scrollState = rememberScrollState() + Column( + modifier = Modifier + .fillMaxSize() + .onKeyEvent { event -> event.isEscDown.also { if (it) shortenedUrl = null } } + .verticalScroll(scrollState), + verticalArrangement = Arrangement.Center, + ) { + Text( + text = "Shin", + fontFamily = FontFamily(Font(Res.font.Mansalva_Regular)), + style = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.primary), + fontSize = 64.sp, + textAlign = TextAlign.Center, + modifier = Modifier.fillMaxWidth(), + ) + Text( + text = "Shorten Your URL with Kotlin", + textAlign = TextAlign.Center, + modifier = Modifier.fillMaxWidth(), + ) + Spacer(Modifier.size(32.dp)) + ShortenRequest( + client = client, + maxWidth = maxWidth, + isVertical = isVertical, + onResponse = { shortenedUrl = it }, + onError = { snackbarHostStateScope.showErrorSnackbarNotification(snackbarHostState, it) }, + ) + ShortenResponse(shortenedUrl) + } } } } diff --git a/composeApp/src/commonMain/kotlin/in/procyk/shin/ui/ShortenRequest.kt b/composeApp/src/commonMain/kotlin/in/procyk/shin/ui/ShortenRequest.kt index b95fc01..aa5645a 100644 --- a/composeApp/src/commonMain/kotlin/in/procyk/shin/ui/ShortenRequest.kt +++ b/composeApp/src/commonMain/kotlin/in/procyk/shin/ui/ShortenRequest.kt @@ -29,46 +29,45 @@ import kotlinx.coroutines.launch @Composable internal fun ShortenRequest( client: HttpClient, + maxWidth: Dp, + isVertical: Boolean, onResponse: (String) -> Unit, onError: (String) -> Unit, space: Dp = 8.dp, ) { - BoxWithConstraints { - val maxWidth = maxWidth - if (maxHeight > maxWidth) { - Column( - modifier = Modifier.fillMaxWidth().padding(16.dp), - verticalArrangement = Arrangement.spacedBy( - space = space, - alignment = Alignment.CenterVertically, - ), - horizontalAlignment = Alignment.CenterHorizontally, - ) { - ShortenRequestElements( - client = client, - onResponse = onResponse, - onError = onError, - fillMaxWidth = true, - maxTextFieldWidth = maxWidth / 2 - ) - } - } else { - Row( - modifier = Modifier.fillMaxWidth(), - horizontalArrangement = Arrangement.spacedBy( - space = space, - alignment = Alignment.CenterHorizontally, - ), - verticalAlignment = Alignment.Bottom, - ) { - ShortenRequestElements( - client = client, - onResponse = onResponse, - onError = onError, - fillMaxWidth = false, - maxTextFieldWidth = maxWidth / 2 - ) - } + if (isVertical) { + Column( + modifier = Modifier.fillMaxWidth().padding(16.dp), + verticalArrangement = Arrangement.spacedBy( + space = space, + alignment = Alignment.CenterVertically, + ), + horizontalAlignment = Alignment.CenterHorizontally, + ) { + ShortenRequestElements( + client = client, + onResponse = onResponse, + onError = onError, + fillMaxWidth = true, + maxTextFieldWidth = maxWidth / 2 + ) + } + } else { + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.spacedBy( + space = space, + alignment = Alignment.CenterHorizontally, + ), + verticalAlignment = Alignment.Bottom, + ) { + ShortenRequestElements( + client = client, + onResponse = onResponse, + onError = onError, + fillMaxWidth = false, + maxTextFieldWidth = maxWidth / 2 + ) } } }