Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 11, 2025
1 parent bdef792 commit 8da932e
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,13 @@ 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.fillMaxWidth
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.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
Expand All @@ -50,18 +47,14 @@ 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.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.github.keelar.exprk.Expressions
import ru.tech.imageresizershrinker.core.domain.image.model.ImageFormat
import ru.tech.imageresizershrinker.core.domain.image.model.ImageInfo
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.utils.provider.rememberLocalEssentials
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedAlertDialog
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.CalculatorDialog
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 Down Expand Up @@ -207,68 +200,10 @@ internal fun ResizeImageFieldImpl(
interactionSource = interactionSource
)

var calculatorExpression by rememberSaveable(value) {
mutableStateOf(value)
}
val essentials = rememberLocalEssentials()
EnhancedAlertDialog(
CalculatorDialog(
visible = showCalculator,
onDismissRequest = { showCalculator = false },
confirmButton = {
EnhancedButton(
onClick = {
runCatching {
Expressions().eval(calculatorExpression)
}.onFailure {
essentials.showFailureToast(it)
}.onSuccess {
onValueChange(it.toInt().toString())
showCalculator = false
}
}
) {
Text(stringResource(R.string.apply))
}
},
title = {
Text(
text = stringResource(R.string.calculate)
)
},
icon = {
Icon(
imageVector = Icons.Outlined.Calculate,
contentDescription = null
)
},
dismissButton = {
EnhancedButton(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
onClick = {
showCalculator = false
}
) {
Text(stringResource(R.string.close))
}
},
text = {
OutlinedTextField(
shape = RoundedCornerShape(16.dp),
value = calculatorExpression,
textStyle = MaterialTheme.typography.titleMedium.copy(textAlign = TextAlign.Center),
maxLines = 1,
placeholder = {
Text(
text = "(5+5)*10",
style = MaterialTheme.typography.titleMedium.copy(textAlign = TextAlign.Center),
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.outline
)
},
onValueChange = { expr ->
calculatorExpression = expr.replace(",", ".").filter { !it.isWhitespace() }
}
)
}
onDismiss = { showCalculator = false },
initialValue = value.toBigDecimalOrNull(),
onValueChange = { onValueChange(it.toInt().toString()) }
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2025 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/

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

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Calculate
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.github.keelar.exprk.Expressions
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberLocalEssentials
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedAlertDialog
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton
import java.math.BigDecimal

@Composable
fun CalculatorDialog(
visible: Boolean,
onDismiss: () -> Unit,
initialValue: BigDecimal?,
onValueChange: (BigDecimal) -> Unit
) {
var calculatorExpression by rememberSaveable(initialValue, visible) {
mutableStateOf(initialValue?.toString()?.replace(",", ".") ?: "")
}
val essentials = rememberLocalEssentials()
EnhancedAlertDialog(
visible = visible,
onDismissRequest = onDismiss,
confirmButton = {
EnhancedButton(
onClick = {
runCatching {
Expressions().eval(calculatorExpression)
}.onFailure {
essentials.showFailureToast(it)
}.onSuccess {
onValueChange(it)
onDismiss()
}
}
) {
Text(stringResource(R.string.apply))
}
},
title = {
Text(
text = stringResource(R.string.calculate)
)
},
icon = {
Icon(
imageVector = Icons.Outlined.Calculate,
contentDescription = null
)
},
dismissButton = {
EnhancedButton(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
onClick = onDismiss
) {
Text(stringResource(R.string.close))
}
},
text = {
OutlinedTextField(
shape = RoundedCornerShape(16.dp),
value = calculatorExpression,
textStyle = MaterialTheme.typography.titleMedium.copy(textAlign = TextAlign.Center),
maxLines = 1,
placeholder = {
Text(
text = "(5+5)*10",
style = MaterialTheme.typography.titleMedium.copy(textAlign = TextAlign.Center),
modifier = Modifier.fillMaxWidth(),
color = MaterialTheme.colorScheme.outline
)
},
onValueChange = { expr ->
calculatorExpression = expr.replace(",", ".").filter { !it.isWhitespace() }
}
)
}
)
}

0 comments on commit 8da932e

Please sign in to comment.