Save Angle mode

This commit is contained in:
Sad Ellie 2023-03-27 14:49:55 +03:00
parent 2122aef790
commit f7cfd65639
6 changed files with 47 additions and 59 deletions

View File

@ -54,6 +54,7 @@ import javax.inject.Inject
* @property shownUnitGroups [UnitGroup]s that user wants to see. Excludes other [UnitGroup]s,
* @property enableVibrations When true will use haptic feedback in app.
* @property enableToolsExperiment When true will enable experimental Tools screen.
* @property radianMode AngleMode in mxParser. When true - Radian, when False - Degree.
*/
data class UserPreferences(
val themingMode: ThemingMode? = null,
@ -67,7 +68,8 @@ data class UserPreferences(
val shownUnitGroups: List<UnitGroup> = ALL_UNIT_GROUPS,
val enableVibrations: Boolean = true,
val enableToolsExperiment: Boolean = false,
val startingScreen: String = TopLevelDestinations.Converter.route
val startingScreen: String = TopLevelDestinations.Converter.route,
val radianMode: Boolean = true
)
/**
@ -90,6 +92,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
val ENABLE_VIBRATIONS = booleanPreferencesKey("ENABLE_VIBRATIONS_PREF_KEY")
val ENABLE_TOOLS_EXPERIMENT = booleanPreferencesKey("ENABLE_TOOLS_EXPERIMENT_PREF_KEY")
val STARTING_SCREEN = stringPreferencesKey("STARTING_SCREEN_PREF_KEY")
val RADIAN_MODE = booleanPreferencesKey("RADIAN_MODE_PREF_KEY")
}
val userPreferencesFlow: Flow<UserPreferences> = dataStore.data
@ -134,6 +137,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
val enableVibrations: Boolean = preferences[PrefsKeys.ENABLE_VIBRATIONS] ?: true
val enableToolsExperiment: Boolean = preferences[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] ?: false
val startingScreen: String = preferences[PrefsKeys.STARTING_SCREEN] ?: TopLevelDestinations.Converter.route
val radianMode: Boolean = preferences[PrefsKeys.RADIAN_MODE] ?: true
UserPreferences(
themingMode = themingMode,
@ -147,7 +151,8 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
shownUnitGroups = shownUnitGroups,
enableVibrations = enableVibrations,
enableToolsExperiment = enableToolsExperiment,
startingScreen = startingScreen
startingScreen = startingScreen,
radianMode = radianMode
)
}
@ -269,4 +274,15 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS
preferences[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] = enabled
}
}
/**
* Update angle mode for calculator.
*
* @param radianMode When true - Radian, when False - Degree.
*/
suspend fun updateRadianMode(radianMode: Boolean) {
dataStore.edit { preferences ->
preferences[PrefsKeys.RADIAN_MODE] = radianMode
}
}
}

View File

@ -1,21 +0,0 @@
/*
* Unitto is a unit converter for Android
* Copyright (c) 2023 Elshan Agaev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.sadellie.unitto.feature.calculator
internal enum class AngleMode { DEG, RAD }

View File

@ -269,7 +269,7 @@ private fun CalculatorScreen(
numPad = {
CalculatorKeyboard(
modifier = Modifier.padding(horizontal = 8.dp, vertical = 4.dp),
angleMode = uiState.angleMode,
radianMode = uiState.radianMode,
allowVibration = uiState.allowVibration,
addSymbol = addSymbol,
clearSymbols = clearSymbols,

View File

@ -24,7 +24,7 @@ import com.sadellie.unitto.data.model.HistoryItem
internal data class CalculatorUIState(
val input: TextFieldValue = TextFieldValue(),
val output: String = "",
val angleMode: AngleMode = AngleMode.RAD,
val radianMode: Boolean = true,
val history: List<HistoryItem> = emptyList(),
val allowVibration: Boolean = false
)

View File

@ -34,7 +34,6 @@ import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
@ -46,7 +45,7 @@ import org.mariuszgromada.math.mxparser.mXparser as MathParser
@HiltViewModel
internal class CalculatorViewModel @Inject constructor(
userPrefsRepository: UserPreferencesRepository,
private val userPrefsRepository: UserPreferencesRepository,
private val calculatorHistoryRepository: CalculatorHistoryRepository,
private val textFieldController: TextFieldController
) : ViewModel() {
@ -58,16 +57,15 @@ internal class CalculatorViewModel @Inject constructor(
)
private val _output: MutableStateFlow<String> = MutableStateFlow("")
private val _angleMode: MutableStateFlow<AngleMode> = MutableStateFlow(AngleMode.RAD)
private val _history = calculatorHistoryRepository.historyFlow
val uiState = combine(
textFieldController.input, _output, _angleMode, _history, _userPrefs
) { input, output, angleMode, history, userPrefs ->
textFieldController.input, _output, _history, _userPrefs
) { input, output, history, userPrefs ->
return@combine CalculatorUIState(
input = input,
output = output,
angleMode = angleMode,
radianMode = userPrefs.radianMode,
history = history,
allowVibration = userPrefs.enableVibrations
)
@ -82,14 +80,8 @@ internal class CalculatorViewModel @Inject constructor(
fun clearSymbols() = textFieldController.clearInput()
fun toggleCalculatorMode() {
_angleMode.update {
if (it == AngleMode.DEG) {
MathParser.setRadiansMode()
AngleMode.RAD
} else {
MathParser.setDegreesMode()
AngleMode.DEG
}
viewModelScope.launch {
userPrefsRepository.updateRadianMode(!_userPrefs.value.radianMode)
}
}
@ -185,7 +177,9 @@ internal class CalculatorViewModel @Inject constructor(
// Observe and invoke calculation without UI lag.
viewModelScope.launch(Dispatchers.Default) {
merge(_userPrefs, textFieldController.input, _angleMode).collectLatest {
combine(_userPrefs, textFieldController.input) { userPrefs, _ ->
if (userPrefs.radianMode) MathParser.setRadiansMode() else MathParser.setDegreesMode()
}.collectLatest {
calculateInput()
}
}

View File

@ -93,12 +93,11 @@ import com.sadellie.unitto.core.ui.common.key.unittoicons.RightBracket
import com.sadellie.unitto.core.ui.common.key.unittoicons.Sin
import com.sadellie.unitto.core.ui.common.key.unittoicons.SquareRootWide
import com.sadellie.unitto.core.ui.common.key.unittoicons.Tan
import com.sadellie.unitto.feature.calculator.AngleMode
@Composable
internal fun CalculatorKeyboard(
modifier: Modifier,
angleMode: AngleMode,
radianMode: Boolean,
allowVibration: Boolean,
addSymbol: (String) -> Unit,
clearSymbols: () -> Unit,
@ -109,7 +108,7 @@ internal fun CalculatorKeyboard(
if (LocalConfiguration.current.orientation == Configuration.ORIENTATION_PORTRAIT) {
PortraitKeyboard(
modifier = modifier,
angleMode = angleMode,
radianMode = radianMode,
allowVibration = allowVibration,
addSymbol = addSymbol,
toggleAngleMode = toggleAngleMode,
@ -120,7 +119,7 @@ internal fun CalculatorKeyboard(
} else {
LandscapeKeyboard(
modifier = modifier,
angleMode = angleMode,
radianMode = radianMode,
allowVibration = allowVibration,
addSymbol = addSymbol,
toggleAngleMode = toggleAngleMode,
@ -134,7 +133,7 @@ internal fun CalculatorKeyboard(
@Composable
private fun PortraitKeyboard(
modifier: Modifier,
angleMode: AngleMode,
radianMode: Boolean,
allowVibration: Boolean,
addSymbol: (String) -> Unit,
toggleAngleMode: () -> Unit,
@ -175,7 +174,7 @@ private fun PortraitKeyboard(
allowVibration = allowVibration,
addSymbol = addSymbol,
showAdditional = showAdditional,
angleMode = angleMode,
radianMode = radianMode,
toggleAngleMode = toggleAngleMode,
toggleInvMode = { invMode = !invMode }
)
@ -185,7 +184,7 @@ private fun PortraitKeyboard(
allowVibration = allowVibration,
addSymbol = addSymbol,
showAdditional = showAdditional,
angleMode = angleMode,
radianMode = radianMode,
toggleAngleMode = toggleAngleMode,
toggleInvMode = { invMode = !invMode }
)
@ -241,7 +240,7 @@ private fun AdditionalButtonsPortrait(
allowVibration: Boolean,
addSymbol: (String) -> Unit,
showAdditional: Boolean,
angleMode: AngleMode,
radianMode: Boolean,
toggleAngleMode: () -> Unit,
toggleInvMode: () -> Unit
) {
@ -255,7 +254,7 @@ private fun AdditionalButtonsPortrait(
AnimatedVisibility(showAdditional) {
Column {
Row(horizontalArrangement = Arrangement.spacedBy(2.dp)) {
KeyboardButtonAdditional(modifier, if (angleMode == AngleMode.DEG) UnittoIcons.Deg else UnittoIcons.Rad, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(modifier, if (radianMode) UnittoIcons.Rad else UnittoIcons.Deg, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(modifier, UnittoIcons.Sin, allowVibration) { addSymbol(Token.sin) }
KeyboardButtonAdditional(modifier, UnittoIcons.Cos, allowVibration) { addSymbol(Token.cos) }
KeyboardButtonAdditional(modifier, UnittoIcons.Tan, allowVibration) { addSymbol(Token.tan) }
@ -277,7 +276,7 @@ private fun AdditionalButtonsPortraitInverse(
allowVibration: Boolean,
addSymbol: (String) -> Unit,
showAdditional: Boolean,
angleMode: AngleMode,
radianMode: Boolean,
toggleAngleMode: () -> Unit,
toggleInvMode: () -> Unit
) {
@ -291,7 +290,7 @@ private fun AdditionalButtonsPortraitInverse(
AnimatedVisibility(showAdditional) {
Column {
Row(horizontalArrangement = Arrangement.spacedBy(2.dp)) {
KeyboardButtonAdditional(modifier, if (angleMode == AngleMode.DEG) UnittoIcons.Deg else UnittoIcons.Rad, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(modifier, if (radianMode) UnittoIcons.Rad else UnittoIcons.Deg, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(modifier, UnittoIcons.ArSin, allowVibration) { addSymbol(Token.arSin) }
KeyboardButtonAdditional(modifier, UnittoIcons.ArCos, allowVibration) { addSymbol(Token.arCos) }
KeyboardButtonAdditional(modifier, UnittoIcons.AcTan, allowVibration) { addSymbol(Token.acTan) }
@ -310,7 +309,7 @@ private fun AdditionalButtonsPortraitInverse(
@Composable
private fun LandscapeKeyboard(
modifier: Modifier,
angleMode: AngleMode,
radianMode: Boolean,
allowVibration: Boolean,
addSymbol: (String) -> Unit,
toggleAngleMode: () -> Unit,
@ -334,7 +333,7 @@ private fun LandscapeKeyboard(
modifier = Modifier.weight(1f),
buttonModifier = buttonModifier,
allowVibration = allowVibration,
angleMode = angleMode,
radianMode = radianMode,
addSymbol = addSymbol,
toggleAngleMode = toggleAngleMode,
toggleInvMode = { invMode = !invMode }
@ -344,7 +343,7 @@ private fun LandscapeKeyboard(
modifier = Modifier.weight(1f),
buttonModifier = buttonModifier,
allowVibration = allowVibration,
angleMode = angleMode,
radianMode = radianMode,
addSymbol = addSymbol,
toggleAngleMode = toggleAngleMode,
toggleInvMode = { invMode = !invMode }
@ -392,13 +391,13 @@ private fun AdditionalButtonsLandscape(
modifier: Modifier,
buttonModifier: Modifier,
allowVibration: Boolean,
angleMode: AngleMode,
radianMode: Boolean,
addSymbol: (String) -> Unit,
toggleAngleMode: () -> Unit,
toggleInvMode: () -> Unit
) {
Column(modifier) {
KeyboardButtonAdditional(buttonModifier, if (angleMode == AngleMode.DEG) UnittoIcons.Deg else UnittoIcons.Rad, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(buttonModifier, if (radianMode) UnittoIcons.Rad else UnittoIcons.Deg, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.Inv, allowVibration) { toggleInvMode() }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.Sin, allowVibration) { addSymbol(Token.sin) }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.E, allowVibration) { addSymbol(Token.e) }
@ -424,13 +423,13 @@ private fun AdditionalButtonsLandscapeInverse(
modifier: Modifier,
buttonModifier: Modifier,
allowVibration: Boolean,
angleMode: AngleMode,
radianMode: Boolean,
addSymbol: (String) -> Unit,
toggleAngleMode: () -> Unit,
toggleInvMode: () -> Unit
) {
Column(modifier) {
KeyboardButtonAdditional(buttonModifier, if (angleMode == AngleMode.DEG) UnittoIcons.Deg else UnittoIcons.Rad, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(buttonModifier, if (radianMode) UnittoIcons.Rad else UnittoIcons.Deg, allowVibration) { toggleAngleMode() }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.Inv, allowVibration) { toggleInvMode() }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.ArSin, allowVibration) { addSymbol(Token.arSin) }
KeyboardButtonAdditional(buttonModifier, UnittoIcons.E, allowVibration) { addSymbol(Token.e) }
@ -456,7 +455,7 @@ private fun AdditionalButtonsLandscapeInverse(
private fun PreviewCalculatorKeyboard() {
CalculatorKeyboard(
modifier = Modifier,
angleMode = AngleMode.DEG,
radianMode = true,
addSymbol = {},
clearSymbols = {},
deleteSymbol = {},