Skip to content

Commit

Permalink
Start work on dimension calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 10, 2025
1 parent 4ca608c commit ba88a7e
Showing 1 changed file with 149 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,34 @@

package ru.tech.imageresizershrinker.core.ui.widget.controls

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Calculate
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
Expand All @@ -37,6 +54,8 @@ import ru.tech.imageresizershrinker.core.domain.model.IntegerSize
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.ui.utils.helper.ImageUtils
import ru.tech.imageresizershrinker.core.ui.utils.helper.ImageUtils.restrict
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedAlertDialog
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.modifier.container
import ru.tech.imageresizershrinker.core.ui.widget.text.RoundedTextField

Expand All @@ -55,76 +74,77 @@ fun ResizeImageField(
.animateContentSize()
) {
Row {
RoundedTextField(
value = imageInfo.width.takeIf { it != 0 }
.let { it ?: "" }
.toString(),
onValueChange = { value ->
val maxValue = if (imageInfo.height != 0) {
(ImageUtils.Dimens.MAX_IMAGE_SIZE / imageInfo.height).coerceAtMost(32768)
} else 32768
val widthField: @Composable RowScope.() -> Unit = {
ResizeImageFieldImpl(
value = imageInfo.width.takeIf { it > 0 }
.let { it ?: "" }
.toString(),
onValueChange = { value ->
val maxValue = if (imageInfo.height > 0) {
(ImageUtils.Dimens.MAX_IMAGE_SIZE / imageInfo.height).coerceAtMost(32768)
} else 32768

onWidthChange(
value
.restrict(maxValue)
.toIntOrNull() ?: 0
)
},
shape = RoundedCornerShape(
topStart = 12.dp,
topEnd = 6.dp,
bottomStart = 12.dp,
bottomEnd = 6.dp
),
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
),
label = {
Text(
stringResource(
R.string.width,
originalSize?.width?.toString() ?: ""
onWidthChange(
value
.restrict(maxValue)
.toIntOrNull() ?: 0
)
)
},
modifier = Modifier.weight(1f)
)
Spacer(modifier = Modifier.width(4.dp))
RoundedTextField(
value = imageInfo.height.takeIf { it != 0 }
.let { it ?: "" }
.toString(),
onValueChange = { value ->
val maxValue = if (imageInfo.width != 0) {
(ImageUtils.Dimens.MAX_IMAGE_SIZE / imageInfo.width).coerceAtMost(32768)
} else 32768
},
shape = RoundedCornerShape(
topStart = 12.dp,
topEnd = 6.dp,
bottomStart = 12.dp,
bottomEnd = 6.dp
),
label = {
Text(
stringResource(
R.string.width,
originalSize?.width?.toString() ?: ""
)
)
},
modifier = Modifier.weight(1f)
)
}
val heightField: @Composable RowScope.() -> Unit = {
ResizeImageFieldImpl(
value = imageInfo.height.takeIf { it > 0 }
.let { it ?: "" }
.toString(),
onValueChange = { value ->
val maxValue = if (imageInfo.width > 0) {
(ImageUtils.Dimens.MAX_IMAGE_SIZE / imageInfo.width).coerceAtMost(32768)
} else 32768

onHeightChange(
value
.restrict(maxValue)
.toIntOrNull() ?: 0
)
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
),
shape = RoundedCornerShape(
topEnd = 12.dp,
topStart = 6.dp,
bottomEnd = 12.dp,
bottomStart = 6.dp
),
label = {
Text(
stringResource(
R.string.height,
originalSize?.height?.toString()
?: ""
onHeightChange(
value
.restrict(maxValue)
.toIntOrNull() ?: 0
)
)
},
modifier = Modifier.weight(1f)
)
},
shape = RoundedCornerShape(
topEnd = 12.dp,
topStart = 6.dp,
bottomEnd = 12.dp,
bottomStart = 6.dp
),
label = {
Text(
stringResource(
R.string.height,
originalSize?.height?.toString()
?: ""
)
)
},
modifier = Modifier.weight(1f)
)
}

widthField()
Spacer(modifier = Modifier.width(4.dp))
heightField()
}
IcoSizeWarning(
visible = imageInfo.run {
Expand All @@ -133,4 +153,67 @@ fun ResizeImageField(
)
OOMWarning(visible = showWarning)
}
}

@Composable
internal fun ResizeImageFieldImpl(
value: String,
onValueChange: (String) -> Unit,
label: @Composable () -> Unit,
shape: Shape,
modifier: Modifier
) {
val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState()

var showCalculator by rememberSaveable {
mutableStateOf(false)
}

RoundedTextField(
value = value,
onValueChange = onValueChange,
shape = shape,
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Number
),
label = label,
endIcon = {
AnimatedVisibility(
visible = isFocused,
enter = scaleIn() + fadeIn(),
exit = scaleOut() + fadeOut()
) {
EnhancedIconButton(
onClick = {
showCalculator = true
}
) {
Icon(
imageVector = Icons.Outlined.Calculate,
contentDescription = null
)
}
}
},
modifier = modifier,
interactionSource = interactionSource
)

EnhancedAlertDialog(
visible = showCalculator,
onDismissRequest = { showCalculator = false },
confirmButton = {

},
title = {

},
icon = {

},
dismissButton = {

}
)
}

0 comments on commit ba88a7e

Please sign in to comment.