diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/MainScreen.kt b/app/src/main/java/com/sadellie/unitto/screens/main/MainScreen.kt index 085d02c5..3ce3cc98 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/MainScreen.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/MainScreen.kt @@ -147,8 +147,6 @@ private fun PortraitMainScreenContent( addDigit = processInput, deleteDigit = deleteDigit, clearInput = clearInput, - deleteButtonEnabled = mainScreenUIState.deleteButtonEnabled, - dotButtonEnabled = mainScreenUIState.dotButtonEnabled, ) } } else { @@ -182,7 +180,6 @@ private fun PortraitMainScreenContent( addDigit = processInput, deleteDigit = deleteDigit, clearInput = clearInput, - deleteButtonEnabled = mainScreenUIState.deleteButtonEnabled, ) } } diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/MainScreenUIState.kt b/app/src/main/java/com/sadellie/unitto/screens/main/MainScreenUIState.kt index fd60e671..b32e77ac 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/MainScreenUIState.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/MainScreenUIState.kt @@ -27,9 +27,6 @@ import com.sadellie.unitto.data.KEY_0 * @property resultValue Current output value * @property calculatedValue Currently calculated value. Can be null if not needed (same as input or * expression in input is invalid) - * @property deleteButtonEnabled Delete last symbol from input button state - * @property dotButtonEnabled Add dot to input button state - * @property negateButtonEnabled Switch input between positive and negative button state * @property isLoadingDatabase Whether we are loading data from Database. Need on app launch, once * we are done loading list on Units list can be sorted by usage properly/ * @property isLoadingNetwork Whether we are loading data from network @@ -38,9 +35,6 @@ import com.sadellie.unitto.data.KEY_0 data class MainScreenUIState( var inputValue: String = KEY_0, var resultValue: String = KEY_0, - var deleteButtonEnabled: Boolean = false, - var dotButtonEnabled: Boolean = true, - var negateButtonEnabled: Boolean = false, var isLoadingDatabase: Boolean = true, var isLoadingNetwork: Boolean = false, var showError: Boolean = false, diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt b/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt index 51973162..f80bbf6f 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/MainViewModel.kt @@ -82,8 +82,6 @@ class MainViewModel @Inject constructor( val inputValue: MutableStateFlow = MutableStateFlow(KEY_0) private val latestInputStack: MutableList = mutableListOf(KEY_0) private val _inputDisplayValue: MutableStateFlow = MutableStateFlow(KEY_0) - private val _deleteButtonEnabled: MutableStateFlow = MutableStateFlow(false) - private val _negateButtonEnabled: MutableStateFlow = MutableStateFlow(false) private val _isLoadingDatabase: MutableStateFlow = MutableStateFlow(true) private val _isLoadingNetwork: MutableStateFlow = MutableStateFlow(false) private val _showError: MutableStateFlow = MutableStateFlow(false) @@ -100,9 +98,6 @@ class MainViewModel @Inject constructor( return@combine MainScreenUIState( inputValue = inputDisplayValue, resultValue = convertValue(), - deleteButtonEnabled = inputValue.value != KEY_0, - dotButtonEnabled = canEnterDot(), - negateButtonEnabled = _negateButtonEnabled.value, isLoadingDatabase = isLoadingDatabase, isLoadingNetwork = isLoadingNetwork, showError = showError, @@ -189,8 +184,6 @@ class MainViewModel @Inject constructor( // First we change unit unitFrom = clickedUnit - // Now we check for negate button - _negateButtonEnabled.update { clickedUnit.group.canNegate } // Now we change to positive if the group we switched to supports negate if (!clickedUnit.group.canNegate) { inputValue.update { inputValue.value.removePrefix(KEY_MINUS) } @@ -307,7 +300,6 @@ class MainViewModel @Inject constructor( val lastTwoSymbols = latestInputStack.takeLast(2) val lastSymbol: String = lastTwoSymbols.getOrNull(1) ?: lastTwoSymbols[0] val lastSecondSymbol: String? = lastTwoSymbols.getOrNull(0) - _deleteButtonEnabled.update { true } when (symbolToAdd) { KEY_PLUS, KEY_DIVIDE, KEY_MULTIPLY, KEY_EXPONENT -> { @@ -368,7 +360,9 @@ class MainViewModel @Inject constructor( } } KEY_DOT -> { - setInputSymbols(symbolToAdd) + if (canEnterDot()) { + setInputSymbols(symbolToAdd) + } } KEY_LEFT_BRACKET, KEY_RIGHT_BRACKET -> { when { @@ -411,6 +405,9 @@ class MainViewModel @Inject constructor( * Deletes last symbol from input and handles buttons state (enabled/disabled) */ fun deleteDigit() { + // Default input, don't delete + if (inputValue.value == KEY_0) return + val lastSymbol = latestInputStack.removeLast() // We will need to delete last symbol from both values @@ -443,6 +440,7 @@ class MainViewModel @Inject constructor( else -> { inputValue.update { symbol } _inputDisplayValue.update { displaySymbol } + latestInputStack.clear() latestInputStack.add(symbol) } } @@ -452,7 +450,6 @@ class MainViewModel @Inject constructor( * Clears input value and sets it to default (ZERO) */ fun clearInput() { - _deleteButtonEnabled.update { false } setInputSymbols(KEY_0, false) } @@ -497,7 +494,6 @@ class MainViewModel @Inject constructor( allUnitsRepository.loadFromDatabase(mContext, allBasedUnits) // User is free to convert values and units on units screen can be sorted properly - _negateButtonEnabled.update { unitFrom.group.canNegate } _isLoadingDatabase.update { false } updateCurrenciesBasicUnits() } diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/components/Keyboard.kt b/app/src/main/java/com/sadellie/unitto/screens/main/components/Keyboard.kt index 16f85105..67788c43 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/components/Keyboard.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/components/Keyboard.kt @@ -57,8 +57,6 @@ import com.sadellie.unitto.screens.Formatter * @param addDigit Function that is called when clicking number and dot buttons * @param deleteDigit Function that is called when clicking delete "<" button * @param clearInput Function that is called when clicking clear "AC" button - * @param deleteButtonEnabled Current state of delete "<" button - * @param dotButtonEnabled Current state of clear "AC" button */ @Composable fun Keyboard( @@ -66,8 +64,6 @@ fun Keyboard( addDigit: (String) -> Unit = {}, deleteDigit: () -> Unit = {}, clearInput: () -> Unit = {}, - deleteButtonEnabled: Boolean = false, - dotButtonEnabled: Boolean = true ) { Row( modifier = modifier.fillMaxSize() @@ -91,14 +87,14 @@ fun Keyboard( KeyboardButton(bModifier, KEY_8, onClick = addDigit) KeyboardButton(bModifier, KEY_5, onClick = addDigit) KeyboardButton(bModifier, KEY_2, onClick = addDigit) - KeyboardButton(bModifier, Formatter.fractional, enabled = dotButtonEnabled) { addDigit(KEY_DOT) } + KeyboardButton(bModifier, Formatter.fractional) { addDigit(KEY_DOT) } } Column(cModifier) { KeyboardButton(bModifier, KEY_EXPONENT, isPrimary = false, onClick = { addDigit(KEY_EXPONENT) }) KeyboardButton(bModifier, KEY_9, onClick = addDigit) KeyboardButton(bModifier, KEY_6, onClick = addDigit) KeyboardButton(bModifier, KEY_3, onClick = addDigit) - KeyboardButton(bModifier, KEY_CLEAR, enabled = deleteButtonEnabled, onLongClick = clearInput) { deleteDigit() } + KeyboardButton(bModifier, KEY_CLEAR, onLongClick = clearInput) { deleteDigit() } } Column(cModifier) { KeyboardButton(bModifier, KEY_SQRT, isPrimary = false, onClick = { addDigit(KEY_SQRT) }) diff --git a/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt b/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt index 9b012033..5a240905 100644 --- a/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt +++ b/app/src/main/java/com/sadellie/unitto/screens/main/components/KeyboardButton.kt @@ -41,7 +41,6 @@ import com.sadellie.unitto.ui.theme.NumbersTextStyleTitleLarge * * @param modifier Modifier that is applied to a [Button] component. * @param digit Symbol to show on button. - * @param enabled Current state of this button. * @param isPrimary If true will use `inverseOnSurface` color, else `secondaryContainer`. * @param onLongClick Action to perform when holding this button. * @param onClick Action to perform when clicking this button. @@ -50,7 +49,6 @@ import com.sadellie.unitto.ui.theme.NumbersTextStyleTitleLarge fun KeyboardButton( modifier: Modifier = Modifier, digit: String, - enabled: Boolean = true, isPrimary: Boolean = true, onLongClick: () -> Unit = {}, onClick: (String) -> Unit = {} @@ -67,12 +65,11 @@ fun KeyboardButton( interactionSource = interactionSource, shape = RoundedCornerShape(cornerRadius), colors = ButtonDefaults.buttonColors( - containerColor = if (isPrimary) MaterialTheme.colorScheme.inverseOnSurface else MaterialTheme.colorScheme.secondaryContainer, + containerColor = if (isPrimary) MaterialTheme.colorScheme.surfaceVariant else MaterialTheme.colorScheme.secondaryContainer, contentColor = MaterialTheme.colorScheme.onSecondaryContainer, disabledContentColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f) ), onClick = { onClick(digit) }, - enabled = enabled, contentPadding = PaddingValues(0.dp) ) { Text( diff --git a/app/src/test/java/com/sadellie/unitto/screens/MainViewModelTest.kt b/app/src/test/java/com/sadellie/unitto/screens/MainViewModelTest.kt index 8352217f..c26bba0d 100644 --- a/app/src/test/java/com/sadellie/unitto/screens/MainViewModelTest.kt +++ b/app/src/test/java/com/sadellie/unitto/screens/MainViewModelTest.kt @@ -244,33 +244,73 @@ class MainViewModelTest { } private fun `test dot`() { - // 0 | . | 0. + // 0 | ... | 0. + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) viewModel.processInput(KEY_DOT) assertEquals("0.", viewModel.inputValue.value) assertEquals("0.", viewModel.mainFlow.value.inputValue) viewModel.clearInput() - // 1 | . | 1. + // 1 | ... | 1. viewModel.processInput(KEY_1) viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) assertEquals("1.", viewModel.inputValue.value) assertEquals("1.", viewModel.mainFlow.value.inputValue) viewModel.clearInput() - // 1+ | . | 1+. + // 1+ | ... | 1+. viewModel.processInput(KEY_1) viewModel.processInput(KEY_PLUS) viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) assertEquals("1+.", viewModel.inputValue.value) assertEquals("1+.", viewModel.mainFlow.value.inputValue) viewModel.clearInput() - // √ | . | √. + // √ | ... | √. viewModel.processInput(KEY_SQRT) viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) assertEquals("√.", viewModel.inputValue.value) assertEquals("√.", viewModel.mainFlow.value.inputValue) viewModel.clearInput() + + // √21 | ... | √21. + viewModel.processInput(KEY_SQRT) + viewModel.processInput(KEY_2) + viewModel.processInput(KEY_1) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + assertEquals("√21.", viewModel.inputValue.value) + assertEquals("√21.", viewModel.mainFlow.value.inputValue) + viewModel.clearInput() + + // √21+1.01-.23 | ... | √21+1.01-.23 + viewModel.processInput(KEY_SQRT) + viewModel.processInput(KEY_2) + viewModel.processInput(KEY_1) + viewModel.processInput(KEY_PLUS) + viewModel.processInput(KEY_1) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_0) + viewModel.processInput(KEY_1) + viewModel.processInput(KEY_MINUS) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_2) + viewModel.processInput(KEY_3) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + viewModel.processInput(KEY_DOT) + assertEquals("√21+1.01-.23", viewModel.inputValue.value) + assertEquals("√21+1.01–.23", viewModel.mainFlow.value.inputValue) + viewModel.clearInput() + } private fun `test minus`() { @@ -399,6 +439,9 @@ class MainViewModelTest { } viewModel.clearInput() + // This should not delete default input (0) + viewModel.deleteDigit() + // Now we check that we can delete multiple values viewModel.processInput(KEY_3) viewModel.processInput(KEY_SQRT) @@ -409,5 +452,4 @@ class MainViewModelTest { Dispatchers.resetMain() } - }