Skip to content

Commit

Permalink
add scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
avan1235 committed Feb 19, 2024
1 parent be3f5c0 commit a5f5d81
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 65 deletions.
68 changes: 39 additions & 29 deletions composeApp/src/commonMain/kotlin/in/procyk/shin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<MutableState<String?>> { 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<MutableState<String?>> { 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)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
}
Expand Down

0 comments on commit a5f5d81

Please sign in to comment.