mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-18 16:25:27 +02:00
parent
e06bb76b02
commit
99a91be8ca
@ -94,6 +94,8 @@ Used in this dialog window. Should be short -->
|
||||
<string name="settings_auto">Auto</string>
|
||||
<string name="settings_calculator_support">History view</string>
|
||||
<string name="settings_clear_cache">Clear cache</string>
|
||||
<string name="settings_clear_input">Clear input</string>
|
||||
<string name="settings_clear_input_support">Start new expression when clicking \"=\" and clicking any button</string>
|
||||
<string name="settings_color_scheme">Color scheme</string>
|
||||
<string name="settings_color_theme">Color theme</string>
|
||||
<string name="settings_color_theme_support">Pick a theming mode</string>
|
||||
|
@ -86,4 +86,6 @@ interface UserPreferencesRepository {
|
||||
suspend fun updatePartialHistoryView(enabled: Boolean)
|
||||
|
||||
suspend fun updateAcButton(enabled: Boolean)
|
||||
|
||||
suspend fun updateClearInputAfterEquals(enabled: Boolean)
|
||||
}
|
||||
|
@ -27,4 +27,5 @@ interface CalculatorPreferences {
|
||||
val partialHistoryView: Boolean
|
||||
val precision: Int
|
||||
val outputFormat: Int
|
||||
val clearInputAfterEquals: Boolean
|
||||
}
|
||||
|
@ -43,7 +43,6 @@ data class AppPreferencesImpl(
|
||||
override val systemFont: Boolean,
|
||||
) : AppPreferences
|
||||
|
||||
|
||||
data class GeneralPreferencesImpl(
|
||||
override val enableVibrations: Boolean,
|
||||
) : GeneralPreferences
|
||||
@ -57,6 +56,7 @@ data class CalculatorPreferencesImpl(
|
||||
override val partialHistoryView: Boolean,
|
||||
override val precision: Int,
|
||||
override val outputFormat: Int,
|
||||
override val clearInputAfterEquals: Boolean,
|
||||
) : CalculatorPreferences
|
||||
|
||||
data class ConverterPreferencesImpl(
|
||||
|
@ -47,4 +47,5 @@ internal object PrefsKeys {
|
||||
val SYSTEM_FONT = booleanPreferencesKey("SYSTEM_FONT_PREF_KEY")
|
||||
val PARTIAL_HISTORY_VIEW = booleanPreferencesKey("PARTIAL_HISTORY_VIEW_PREF_KEY")
|
||||
val AC_BUTTON = booleanPreferencesKey("AC_BUTTON_PREF_KEY")
|
||||
val CLEAR_INPUT_AFTER_EQUALS = booleanPreferencesKey("CLEAR_INPUT_AFTER_EQUALS_PREF_KEY")
|
||||
}
|
||||
|
@ -85,6 +85,7 @@ class UserPreferencesRepositoryImpl @Inject constructor(
|
||||
precision = preferences.getDigitsPrecision(),
|
||||
outputFormat = preferences.getOutputFormat(),
|
||||
acButton = preferences.getAcButton(),
|
||||
clearInputAfterEquals = preferences.getClearInputAfterEquals()
|
||||
)
|
||||
}
|
||||
|
||||
@ -280,6 +281,12 @@ class UserPreferencesRepositoryImpl @Inject constructor(
|
||||
preferences[PrefsKeys.AC_BUTTON] = enabled
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun updateClearInputAfterEquals(enabled: Boolean) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[PrefsKeys.CLEAR_INPUT_AFTER_EQUALS] = enabled
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Preferences.getEnableDynamicTheme(): Boolean {
|
||||
@ -376,6 +383,10 @@ private fun Preferences.getAcButton(): Boolean {
|
||||
return this[PrefsKeys.AC_BUTTON] ?: false
|
||||
}
|
||||
|
||||
private fun Preferences.getClearInputAfterEquals(): Boolean {
|
||||
return this[PrefsKeys.CLEAR_INPUT_AFTER_EQUALS] ?: true
|
||||
}
|
||||
|
||||
private inline fun <T, R> T.letTryOrNull(block: (T) -> R): R? = try {
|
||||
this?.let(block)
|
||||
} catch (e: Exception) {
|
||||
|
@ -120,36 +120,39 @@ internal class CalculatorViewModel @Inject constructor(
|
||||
.stateIn(viewModelScope, CalculatorUIState.Loading)
|
||||
|
||||
fun addTokens(tokens: String) = _input.update {
|
||||
val newValue = if (_equalClicked.value) {
|
||||
_equalClicked.update { false }
|
||||
val clearInputAfterEquals = _prefs.value?.clearInputAfterEquals ?: true
|
||||
val newValue = if (_equalClicked.value and clearInputAfterEquals) {
|
||||
TextFieldValue().addTokens(tokens)
|
||||
} else {
|
||||
it.addTokens(tokens)
|
||||
}
|
||||
_equalClicked.update { false }
|
||||
_fractionJob?.cancel()
|
||||
savedStateHandle[_inputKey] = newValue.text
|
||||
newValue
|
||||
}
|
||||
|
||||
fun addBracket() = _input.update {
|
||||
val newValue = if (_equalClicked.value) {
|
||||
_equalClicked.update { false }
|
||||
val clearInputAfterEquals = _prefs.value?.clearInputAfterEquals ?: true
|
||||
val newValue = if (_equalClicked.value and clearInputAfterEquals) {
|
||||
TextFieldValue().addBracket()
|
||||
} else {
|
||||
it.addBracket()
|
||||
}
|
||||
_equalClicked.update { false }
|
||||
_fractionJob?.cancel()
|
||||
savedStateHandle[_inputKey] = newValue.text
|
||||
newValue
|
||||
}
|
||||
|
||||
fun deleteTokens() = _input.update {
|
||||
val newValue = if (_equalClicked.value) {
|
||||
_equalClicked.update { false }
|
||||
val clearInputAfterEquals = _prefs.value?.clearInputAfterEquals ?: true
|
||||
val newValue = if (_equalClicked.value and clearInputAfterEquals) {
|
||||
TextFieldValue().deleteTokens()
|
||||
} else {
|
||||
it.deleteTokens()
|
||||
}
|
||||
_equalClicked.update { false }
|
||||
_fractionJob?.cancel()
|
||||
savedStateHandle[_inputKey] = newValue.text
|
||||
newValue
|
||||
|
@ -20,6 +20,7 @@ package com.sadellie.unitto.feature.settings.calculator
|
||||
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.automirrored.filled.Backspace
|
||||
import androidx.compose.material.icons.filled.Timer
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.res.stringResource
|
||||
@ -47,7 +48,8 @@ internal fun CalculatorSettingsRoute(
|
||||
CalculatorSettingsScreen(
|
||||
prefs = prefs,
|
||||
navigateUpAction = navigateUpAction,
|
||||
updatePartialHistoryView = viewModel::updatePartialHistoryView
|
||||
updatePartialHistoryView = viewModel::updatePartialHistoryView,
|
||||
updateClearInputAfterEquals = viewModel::updateClearInputAfterEquals,
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -58,6 +60,7 @@ private fun CalculatorSettingsScreen(
|
||||
prefs: CalculatorPreferences,
|
||||
navigateUpAction: () -> Unit,
|
||||
updatePartialHistoryView: (Boolean) -> Unit,
|
||||
updateClearInputAfterEquals: (Boolean) -> Unit,
|
||||
) {
|
||||
UnittoScreenWithLargeTopBar(
|
||||
title = stringResource(R.string.calculator_title),
|
||||
@ -73,6 +76,16 @@ private fun CalculatorSettingsScreen(
|
||||
onSwitchChange = updatePartialHistoryView
|
||||
)
|
||||
}
|
||||
|
||||
item("clear input") {
|
||||
UnittoListItem(
|
||||
headlineText = stringResource(R.string.settings_clear_input),
|
||||
icon = Icons.AutoMirrored.Filled.Backspace,
|
||||
supportingText = stringResource(R.string.settings_clear_input_support),
|
||||
switchState = prefs.clearInputAfterEquals,
|
||||
onSwitchChange = updateClearInputAfterEquals
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -90,8 +103,10 @@ private fun PreviewCalculatorSettingsScreen() {
|
||||
precision = 3,
|
||||
outputFormat = OutputFormat.PLAIN,
|
||||
acButton = true,
|
||||
clearInputAfterEquals = true,
|
||||
),
|
||||
navigateUpAction = {},
|
||||
updatePartialHistoryView = {}
|
||||
updatePartialHistoryView = {},
|
||||
updateClearInputAfterEquals = {},
|
||||
)
|
||||
}
|
||||
|
@ -36,4 +36,8 @@ class CalculatorViewModel @Inject constructor(
|
||||
fun updatePartialHistoryView(enabled: Boolean) = viewModelScope.launch {
|
||||
userPrefsRepository.updatePartialHistoryView(enabled)
|
||||
}
|
||||
|
||||
fun updateClearInputAfterEquals(enabled: Boolean) = viewModelScope.launch {
|
||||
userPrefsRepository.updateClearInputAfterEquals(enabled)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user