Skip to content

Commit

Permalink
[composeApp] better
Browse files Browse the repository at this point in the history
• reduced the height of the thumb for the new Material3 Slider, smh
• [Desktop] no need to remember the view model since now it'll get called only once when main starts ⇝ it's no longer inside the application composable
  • Loading branch information
Luki120 committed Nov 28, 2024
1 parent 4ca1aa3 commit 911265a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
15 changes: 12 additions & 3 deletions composeApp/src/commonMain/kotlin/ui/presentation/App.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ui.presentation

import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
Expand All @@ -17,6 +18,7 @@ import ui.composables.AttributedString
import ui.composables.FooterView
import ui.theme.AppTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun App(darkTheme: Boolean, dynamicColor: Boolean, viewModel: AppViewModel) {
Expand All @@ -29,6 +31,8 @@ fun App(darkTheme: Boolean, dynamicColor: Boolean, viewModel: AppViewModel) {
var length by remember { mutableFloatStateOf(20f) }
var textState by remember { mutableStateOf("") }

val interactionSource = remember { MutableInteractionSource() }

LaunchedEffect(Unit) {
textState = viewModel.getRandomString(length = length.toInt())
}
Expand Down Expand Up @@ -62,16 +66,21 @@ fun App(darkTheme: Boolean, dynamicColor: Boolean, viewModel: AppViewModel) {
verticalAlignment = Alignment.CenterVertically
) {
Slider(
colors = SliderDefaults.colors(thumbColor = Color.White),
modifier = Modifier.width(200.dp),
value = length,
valueRange = 10f..25f,
onValueChange = {
isAnimating = true
length = it
textState = viewModel.getRandomString(length = length.toInt())
textState = viewModel.getRandomString(length = length.toInt())
},
thumb = {
SliderDefaults.Thumb(
interactionSource = interactionSource,
modifier = Modifier.height(30.dp)
)
},
onValueChangeFinished = { isAnimating = false }
onValueChangeFinished = { isAnimating = false },
)
Text(
text = "${length.toInt()}",
Expand Down
24 changes: 11 additions & 13 deletions composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.window.Window
Expand All @@ -12,22 +11,21 @@ import di.SettingsProvider.provideSettingsRepository
import ui.presentation.App
import ui.presentation.AppViewModel

fun main() = application {
fun main() {
val viewModel = AppViewModel(
passwordGeneratorRepository = providePasswordGeneratorRepository(),
settingsRepository = provideSettingsRepository()
)
Window(onCloseRequest = ::exitApplication, title = "AuroraKMP") {
Box(
modifier = Modifier
.background(if (isSystemInDarkTheme()) Color.Black else Color.White)
.fillMaxSize()
) {
App(
darkTheme = isSystemInDarkTheme(),
dynamicColor = false,
viewModel = remember { viewModel }
)

application {
Window(onCloseRequest = ::exitApplication, title = "AuroraKMP") {
Box(
modifier = Modifier
.background(if (isSystemInDarkTheme()) Color.Black else Color.White)
.fillMaxSize()
) {
App(darkTheme = isSystemInDarkTheme(), dynamicColor = false, viewModel = viewModel)
}
}
}
}

0 comments on commit 911265a

Please sign in to comment.