Skip to content

Commit

Permalink
Save calculator keyboard state
Browse files Browse the repository at this point in the history
closes #175
  • Loading branch information
sadellie committed Feb 14, 2024
1 parent b0a04f4 commit 7668533
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,8 @@ interface UserPreferencesRepository {
suspend fun updatePartialHistoryView(enabled: Boolean)

suspend fun updateAcButton(enabled: Boolean)

suspend fun updateAdditionalButtons(enabled: Boolean)

suspend fun updateInverseMode(enabled: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface CalculatorPreferences {
val formatterSymbols: FormatterSymbols
val middleZero: Boolean
val acButton: Boolean
val additionalButtons: Boolean
val inverseMode: Boolean
val partialHistoryView: Boolean
val precision: Int
val outputFormat: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ internal fun Preferences.getAcButton(): Boolean {
return this[PrefsKeys.AC_BUTTON] ?: true
}

internal fun Preferences.getAdditionalButtons(): Boolean {
return this[PrefsKeys.ADDITIONAL_BUTTONS] ?: false
}

internal fun Preferences.getInverseMode(): Boolean {
return this[PrefsKeys.INVERSE_MODE] ?: false
}

internal fun List<UnitGroup>.packToString(): String = this.joinToString(",")

private inline fun <T, R> T.letTryOrNull(block: (T) -> R): R? = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ data class CalculatorPreferencesImpl(
override val formatterSymbols: FormatterSymbols,
override val middleZero: Boolean,
override val acButton: Boolean,
override val additionalButtons: Boolean,
override val inverseMode: Boolean,
override val partialHistoryView: Boolean,
override val precision: Int,
override val outputFormat: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ object PrefsKeys {
// CALCULATOR
val RADIAN_MODE = booleanPreferencesKey("RADIAN_MODE_PREF_KEY")
val PARTIAL_HISTORY_VIEW = booleanPreferencesKey("PARTIAL_HISTORY_VIEW_PREF_KEY")
val ADDITIONAL_BUTTONS = booleanPreferencesKey("ADDITIONAL_BUTTONS_PREF_KEY")
val INVERSE_MODE = booleanPreferencesKey("INVERSE_MODE_PREF_KEY")

// UNIT CONVERTER
val LATEST_LEFT_SIDE = stringPreferencesKey("LATEST_LEFT_SIDE_PREF_KEY")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ class UserPreferencesRepositoryImpl @Inject constructor(
precision = preferences.getDigitsPrecision(),
outputFormat = preferences.getOutputFormat(),
acButton = preferences.getAcButton(),
additionalButtons = preferences.getAdditionalButtons(),
inverseMode = preferences.getInverseMode(),
)
}

Expand Down Expand Up @@ -312,4 +314,16 @@ class UserPreferencesRepositoryImpl @Inject constructor(
preferences[PrefsKeys.AC_BUTTON] = enabled
}
}

override suspend fun updateAdditionalButtons(enabled: Boolean) {
dataStore.edit { preferences ->
preferences[PrefsKeys.ADDITIONAL_BUTTONS] = enabled
}
}

override suspend fun updateInverseMode(enabled: Boolean) {
dataStore.edit { preferences ->
preferences[PrefsKeys.INVERSE_MODE] = enabled
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class CalculatorScreenTest {
history = emptyList(),
middleZero = false,
acButton = true,
additionalButtons = false,
inverseMode = false,
partialHistoryView = true
),
openDrawer = {},
Expand All @@ -60,9 +62,12 @@ class CalculatorScreenTest {
onDeleteClick = {},
onClearClick = {},
onEqualClick = {},
onAngleClick = {},
onRadianModeClick = {},
onAdditionalButtonsClick = {},
onInverseModeClick = {},
onClearHistoryClick = {},
) {}
onDeleteHistoryItemClick = {},
)
}

onNodeWithTag("loading").assertDoesNotExist()
Expand All @@ -83,6 +88,8 @@ class CalculatorScreenTest {
history = emptyList(),
middleZero = false,
acButton = true,
additionalButtons = false,
inverseMode = false,
partialHistoryView = true
),
openDrawer = {},
Expand All @@ -92,9 +99,12 @@ class CalculatorScreenTest {
onDeleteClick = {},
onClearClick = {},
onEqualClick = {},
onAngleClick = {},
onRadianModeClick = {},
onAdditionalButtonsClick = {},
onInverseModeClick = {},
onClearHistoryClick = {},
) {}
onDeleteHistoryItemClick = {},
)
}

onNodeWithTag("inputBox")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ internal fun CalculatorRoute(
onDeleteClick = viewModel::deleteTokens,
onClearClick = viewModel::clearInput,
onEqualClick = viewModel::equal,
onAngleClick = viewModel::updateRadianMode,
onRadianModeClick = viewModel::updateRadianMode,
onAdditionalButtonsClick = viewModel::updateAdditionalButtons,
onInverseModeClick = viewModel::updateInverseMode,
onClearHistoryClick = viewModel::clearHistory,
onDeleteHistoryItemClick = viewModel::deleteHistoryItem,
)
Expand All @@ -114,7 +116,9 @@ internal fun Ready(
onDeleteClick: () -> Unit,
onClearClick: () -> Unit,
onEqualClick: () -> Unit,
onAngleClick: (Boolean) -> Unit,
onRadianModeClick: (Boolean) -> Unit,
onAdditionalButtonsClick: (Boolean) -> Unit,
onInverseModeClick: (Boolean) -> Unit,
onClearHistoryClick: () -> Unit,
onDeleteHistoryItemClick: (HistoryItem) -> Unit,
) {
Expand Down Expand Up @@ -248,8 +252,12 @@ internal fun Ready(
onDeleteClick = onDeleteClick,
onClearClick = onClearClick,
onEqualClick = { focusManager.clearFocus(); onEqualClick() },
onAngleClick = onAngleClick,
radianMode = uiState.radianMode,
onRadianModeClick = onRadianModeClick,
additionalButtons = uiState.additionalButtons,
onAdditionalButtonsClick = onAdditionalButtonsClick,
inverseMode = uiState.inverseMode,
onInverseModeClick = onInverseModeClick,
showAcButton = uiState.acButton,
middleZero = uiState.middleZero,
fractional = uiState.formatterSymbols.fractional,
Expand Down Expand Up @@ -329,6 +337,8 @@ private fun PreviewCalculatorScreen() {
history = historyItems,
middleZero = false,
acButton = true,
additionalButtons = false,
inverseMode = false,
partialHistoryView = true
),
openDrawer = {},
Expand All @@ -338,7 +348,10 @@ private fun PreviewCalculatorScreen() {
onDeleteClick = {},
onClearClick = {},
onEqualClick = {},
onAngleClick = {},
onRadianModeClick = {},
onAdditionalButtonsClick = {},
onInverseModeClick = {},
onClearHistoryClick = {},
) {}
onDeleteHistoryItemClick = {},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ internal sealed class CalculatorUIState {
val history: List<HistoryItem>,
val middleZero: Boolean,
val acButton: Boolean,
val additionalButtons: Boolean,
val inverseMode: Boolean,
val partialHistoryView: Boolean,
) : CalculatorUIState()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ internal class CalculatorViewModel @Inject constructor(
history = history,
middleZero = prefs.middleZero,
acButton = prefs.acButton,
additionalButtons = prefs.additionalButtons,
inverseMode = prefs.inverseMode,
partialHistoryView = prefs.partialHistoryView,
)
}
Expand Down Expand Up @@ -158,6 +160,14 @@ internal class CalculatorViewModel @Inject constructor(
userPrefsRepository.updateRadianMode(newValue)
}

fun updateAdditionalButtons(newValue: Boolean) = viewModelScope.launch {
userPrefsRepository.updateAdditionalButtons(newValue)
}

fun updateInverseMode(newValue: Boolean) = viewModelScope.launch {
userPrefsRepository.updateInverseMode(newValue)
}

fun clearHistory() = viewModelScope.launch(Dispatchers.IO) {
calculatorHistoryRepository.clear()
}
Expand Down
Loading

0 comments on commit 7668533

Please sign in to comment.