From 1f21cfbae0f6755afbe1f1045265b54451f2782a Mon Sep 17 00:00:00 2001 From: Sad Ellie Date: Tue, 12 Sep 2023 17:44:51 +0300 Subject: [PATCH] Option to disable partial history view kinda closes #75 --- core/base/src/main/res/values/strings.xml | 4 + .../unitto/data/userprefs/UserPreferences.kt | 19 ++- .../feature/calculator/CalculatorScreen.kt | 20 ++- .../feature/calculator/CalculatorUIState.kt | 1 + .../feature/calculator/CalculatorViewModel.kt | 1 + .../unitto/feature/calculator/DragState.kt | 23 +++- .../unitto/feature/settings/AboutScreen.kt | 2 +- .../settings/CalculatorSettingsScreen.kt | 61 +++++++++ .../settings/ConverterSettingsScreen.kt | 121 ++++++++++++++++++ .../unitto/feature/settings/SettingsScreen.kt | 67 +++------- .../feature/settings/SettingsViewModel.kt | 9 ++ .../settings/formatting/FormattingScreen.kt | 3 +- .../settings/navigation/SettingsNavigation.kt | 36 +++++- .../feature/settings/themes/ThemesScreen.kt | 3 +- .../settings/unitgroups/UnitGroupsScreen.kt | 3 +- 15 files changed, 301 insertions(+), 72 deletions(-) create mode 100644 feature/settings/src/main/java/com/sadellie/unitto/feature/settings/CalculatorSettingsScreen.kt create mode 100644 feature/settings/src/main/java/com/sadellie/unitto/feature/settings/ConverterSettingsScreen.kt diff --git a/core/base/src/main/res/values/strings.xml b/core/base/src/main/res/values/strings.xml index 690b9ba7..772cc136 100644 --- a/core/base/src/main/res/values/strings.xml +++ b/core/base/src/main/res/values/strings.xml @@ -1414,4 +1414,8 @@ Used in this dialog window. Should be short --> gal/100 mi (US) Gallon per 100 mile (UK) gal/100 mi (UK) + Small history view + Peek latest entrance in history + History view + Unit groups, sorting, formatting \ No newline at end of file diff --git a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/UserPreferences.kt b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/UserPreferences.kt index f3b8984c..58f7fe43 100644 --- a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/UserPreferences.kt +++ b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/UserPreferences.kt @@ -85,6 +85,7 @@ data class UserPreferences( val unitConverterSorting: UnitsListSorting = UnitsListSorting.USAGE, val middleZero: Boolean = false, val systemFont: Boolean = false, + val partialHistoryView: Boolean = true, ) data class UIPreferences( @@ -112,6 +113,7 @@ data class MainPreferences( val unitConverterSorting: UnitsListSorting = UnitsListSorting.USAGE, val middleZero: Boolean = false, val enableToolsExperiment: Boolean = false, + val partialHistoryView: Boolean = true, ) /** @@ -142,6 +144,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS val UNIT_CONVERTER_SORTING = stringPreferencesKey("UNIT_CONVERTER_SORTING_PREF_KEY") val MIDDLE_ZERO = booleanPreferencesKey("MIDDLE_ZERO_PREF_KEY") val SYSTEM_FONT = booleanPreferencesKey("SYSTEM_FONT_PREF_KEY") + val PARTIAL_HISTORY_VIEW = booleanPreferencesKey("PARTIAL_HISTORY_VIEW_PREF_KEY") } val uiPreferencesFlow: Flow = dataStore.data @@ -210,6 +213,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS val unitConverterSorting: UnitsListSorting = preferences[PrefsKeys.UNIT_CONVERTER_SORTING]?.let { UnitsListSorting.valueOf(it) } ?: UnitsListSorting.USAGE val middleZero: Boolean = preferences[PrefsKeys.MIDDLE_ZERO] ?: false val enableToolsExperiment: Boolean = preferences[PrefsKeys.ENABLE_TOOLS_EXPERIMENT] ?: false + val partialHistoryView: Boolean = preferences[PrefsKeys.PARTIAL_HISTORY_VIEW] ?: false MainPreferences( digitsPrecision = digitsPrecision, @@ -224,7 +228,8 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS unitConverterFormatTime = unitConverterFormatTime, unitConverterSorting = unitConverterSorting, middleZero = middleZero, - enableToolsExperiment = enableToolsExperiment + enableToolsExperiment = enableToolsExperiment, + partialHistoryView = partialHistoryView ) } @@ -252,6 +257,7 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS unitConverterSorting = main.unitConverterSorting, middleZero = main.middleZero, systemFont = ui.systemFont, + partialHistoryView = main.partialHistoryView ) } @@ -454,4 +460,15 @@ class UserPreferencesRepository @Inject constructor(private val dataStore: DataS preferences[PrefsKeys.SYSTEM_FONT] = enabled } } + + /** + * Update partial history view preference. + * + * @param enabled When true will enable partial history view. + */ + suspend fun updatePartialHistoryView(enabled: Boolean) { + dataStore.edit { preferences -> + preferences[PrefsKeys.PARTIAL_HISTORY_VIEW] = enabled + } + } } diff --git a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt index 4fcc1ba9..5f6bb70b 100644 --- a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt +++ b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorScreen.kt @@ -23,6 +23,7 @@ import androidx.compose.animation.Crossfade import androidx.compose.foundation.background import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.anchoredDraggable +import androidx.compose.foundation.gestures.snapTo import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints @@ -48,6 +49,7 @@ import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -72,6 +74,7 @@ import com.sadellie.unitto.data.model.HistoryItem import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboard import com.sadellie.unitto.feature.calculator.components.CalculatorKeyboardLoading import com.sadellie.unitto.feature.calculator.components.HistoryList +import kotlinx.coroutines.launch import java.text.SimpleDateFormat import java.util.Locale @@ -149,19 +152,28 @@ private fun CalculatorScreen( var historyItemHeight by remember { mutableStateOf(0.dp) } val textBoxHeight = maxHeight * 0.25f var dragStateCurrentValue by rememberSaveable { mutableStateOf(DragState.CLOSED) } + val corScope = rememberCoroutineScope() val dragState = rememberDragState( historyItem = historyItemHeight, max = maxHeight - textBoxHeight, - initialValue = dragStateCurrentValue + initialValue = dragStateCurrentValue, + enablePartialView = uiState.partialHistoryView ) - val dragDp by remember(dragState.requireOffset()) { + val dragDp by remember(dragState) { derivedStateOf { focusManager.clearFocus(true) - with(density) { dragState.requireOffset().toDp() } + with(density) { + try { + dragState.requireOffset().toDp() + } catch (e: IllegalStateException) { + corScope.launch { dragState.snapTo(DragState.CLOSED) } + 0.dp + } + } } } - val keyboardHeight by remember(dragState.requireOffset()) { + val keyboardHeight by remember(dragState) { derivedStateOf { if (dragDp > historyItemHeight) { maxHeight - textBoxHeight - historyItemHeight diff --git a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorUIState.kt b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorUIState.kt index 51a3e4ba..7c108272 100644 --- a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorUIState.kt +++ b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorUIState.kt @@ -35,6 +35,7 @@ internal sealed class CalculatorUIState { val allowVibration: Boolean = false, val formatterSymbols: FormatterSymbols = FormatterSymbols.Spaces, val middleZero: Boolean = false, + val partialHistoryView: Boolean = true, ) : CalculatorUIState() } diff --git a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorViewModel.kt b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorViewModel.kt index c770a1a6..7be04e32 100644 --- a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorViewModel.kt +++ b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/CalculatorViewModel.kt @@ -77,6 +77,7 @@ internal class CalculatorViewModel @Inject constructor( allowVibration = userPrefs.enableVibrations, formatterSymbols = AllFormatterSymbols.getById(userPrefs.separator), middleZero = userPrefs.middleZero, + partialHistoryView = userPrefs.partialHistoryView, ) }.stateIn( viewModelScope, SharingStarted.WhileSubscribed(5000L), CalculatorUIState.Loading diff --git a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/DragState.kt b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/DragState.kt index e668b9bc..5b7f9a99 100644 --- a/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/DragState.kt +++ b/feature/calculator/src/main/java/com/sadellie/unitto/feature/calculator/DragState.kt @@ -26,25 +26,34 @@ import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.unit.Dp -internal enum class DragState { CLOSED, HALF, OPEN } +internal enum class DragState { CLOSED, SMALL, OPEN } @Composable internal fun rememberDragState( initialValue: DragState = DragState.CLOSED, historyItem: Dp, max: Dp, + enablePartialView: Boolean, ): AnchoredDraggableState { val historyItemHeight = with(LocalDensity.current) { historyItem.toPx() } val maxHeight = with(LocalDensity.current) { max.toPx() } + val anchors: DraggableAnchors = if (enablePartialView) { + DraggableAnchors { + DragState.CLOSED at 0f + DragState.SMALL at historyItemHeight + DragState.OPEN at maxHeight + } + } else { + DraggableAnchors { + DragState.CLOSED at 0f + DragState.OPEN at maxHeight + } + } - return remember(key1 = historyItem) { + return remember(historyItem, enablePartialView) { AnchoredDraggableState( initialValue = initialValue, - anchors = DraggableAnchors { - DragState.CLOSED at 0f - DragState.HALF at historyItemHeight - DragState.OPEN at maxHeight - }, + anchors = anchors, positionalThreshold = { 0f }, velocityThreshold = { 0f }, animationSpec = tween() diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/AboutScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/AboutScreen.kt index 2e91d3eb..f8af1e5b 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/AboutScreen.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/AboutScreen.kt @@ -54,9 +54,9 @@ import com.sadellie.unitto.core.ui.openLink @Composable internal fun AboutScreen( + viewModel: SettingsViewModel = hiltViewModel(), navigateUpAction: () -> Unit, navigateToThirdParty: () -> Unit, - viewModel: SettingsViewModel = hiltViewModel() ) { val mContext = LocalContext.current val userPrefs = viewModel.userPrefs.collectAsStateWithLifecycle() diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/CalculatorSettingsScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/CalculatorSettingsScreen.kt new file mode 100644 index 00000000..73dc57fa --- /dev/null +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/CalculatorSettingsScreen.kt @@ -0,0 +1,61 @@ +/* + * 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 . + */ + +package com.sadellie.unitto.feature.settings + +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Timer +import androidx.compose.material3.Icon +import androidx.compose.runtime.Composable +import androidx.compose.ui.res.stringResource +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import com.sadellie.unitto.core.base.R +import com.sadellie.unitto.core.ui.common.NavigateUpButton +import com.sadellie.unitto.core.ui.common.UnittoListItem +import com.sadellie.unitto.core.ui.common.UnittoScreenWithLargeTopBar + +@Composable +internal fun CalculatorSettingsScreen( + viewModel: SettingsViewModel, + navigateUpAction: () -> Unit, +) { + val userPrefs = viewModel.userPrefs.collectAsStateWithLifecycle() + + UnittoScreenWithLargeTopBar( + title = stringResource(R.string.calculator), + navigationIcon = { NavigateUpButton(navigateUpAction) } + ) { padding -> + LazyColumn(contentPadding = padding) { + item { + UnittoListItem( + label = stringResource(R.string.partial_history_view_setting), + leadingContent = { + Icon( + Icons.Default.Timer, + stringResource(R.string.partial_history_view_setting) + ) + }, + supportContent = stringResource(R.string.partial_history_view_setting_support), + switchState = userPrefs.value.partialHistoryView, + onSwitchChange = viewModel::updatePartialHistoryView + ) + } + } + } +} diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/ConverterSettingsScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/ConverterSettingsScreen.kt new file mode 100644 index 00000000..4bb5c77a --- /dev/null +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/ConverterSettingsScreen.kt @@ -0,0 +1,121 @@ +/* + * 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 . + */ + +package com.sadellie.unitto.feature.settings + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Rule +import androidx.compose.material.icons.filled.Sort +import androidx.compose.material.icons.filled.Timer +import androidx.compose.material3.Icon +import androidx.compose.material3.ListItem +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.lifecycle.compose.collectAsStateWithLifecycle +import com.sadellie.unitto.core.base.R +import com.sadellie.unitto.core.ui.common.NavigateUpButton +import com.sadellie.unitto.core.ui.common.UnittoListItem +import com.sadellie.unitto.core.ui.common.UnittoScreenWithLargeTopBar +import com.sadellie.unitto.data.model.UnitsListSorting +import com.sadellie.unitto.feature.settings.components.AlertDialogWithList + +@Composable +internal fun ConverterSettingsScreen( + viewModel: SettingsViewModel, + navigateUpAction: () -> Unit, + navigateToUnitsGroup: () -> Unit +) { + val userPrefs = viewModel.userPrefs.collectAsStateWithLifecycle() + var showDialog: Boolean by rememberSaveable { mutableStateOf(false) } + + UnittoScreenWithLargeTopBar( + title = stringResource(R.string.unit_converter), + navigationIcon = { NavigateUpButton(navigateUpAction) } + ) { padding -> + LazyColumn(contentPadding = padding) { + // UNIT GROUPS + item { + ListItem( + leadingContent = { + Icon( + Icons.Default.Rule, + stringResource(R.string.disable_unit_group_description), + ) + }, + headlineContent = { Text(stringResource(R.string.unit_groups_setting)) }, + supportingContent = { Text(stringResource(R.string.unit_groups_support)) }, + modifier = Modifier.clickable { navigateToUnitsGroup() } + ) + } + + // UNITS LIST SORTING + item { + ListItem( + leadingContent = { + Icon( + Icons.Default.Sort, + stringResource(R.string.units_sorting) + ) + }, + headlineContent = { Text(stringResource(R.string.units_sorting)) }, + supportingContent = { Text(stringResource(R.string.units_sorting_support)) }, + modifier = Modifier.clickable { showDialog = true } + ) + } + + // FORMAT TIME + item { + UnittoListItem( + label = stringResource(R.string.format_time), + leadingContent = { + Icon( + Icons.Default.Timer, + stringResource(R.string.format_time) + ) + }, + supportContent = stringResource(R.string.format_time_support), + switchState = userPrefs.value.unitConverterFormatTime, + onSwitchChange = viewModel::updateUnitConverterFormatTime + ) + } + } + } + + if (showDialog) { + AlertDialogWithList( + title = stringResource(R.string.units_sorting), + listItems = mapOf( + UnitsListSorting.USAGE to R.string.sort_by_usage, + UnitsListSorting.ALPHABETICAL to R.string.sort_by_alphabetical, + UnitsListSorting.SCALE_DESC to R.string.sort_by_scale_desc, + UnitsListSorting.SCALE_ASC to R.string.sort_by_scale_asc, + ), + selectedItemIndex = userPrefs.value.unitConverterSorting, + selectAction = viewModel::updateUnitConverterSorting, + dismissAction = { showDialog = false } + ) + } +} diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsScreen.kt index 4bf7e8f4..532468b6 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsScreen.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsScreen.kt @@ -22,15 +22,14 @@ import androidx.appcompat.app.AppCompatDelegate import androidx.compose.foundation.clickable import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.Calculate import androidx.compose.material.icons.filled.ExposureZero import androidx.compose.material.icons.filled.Home import androidx.compose.material.icons.filled.Info import androidx.compose.material.icons.filled.Language import androidx.compose.material.icons.filled.Palette import androidx.compose.material.icons.filled.RateReview -import androidx.compose.material.icons.filled.Rule -import androidx.compose.material.icons.filled.Sort -import androidx.compose.material.icons.filled.Timer +import androidx.compose.material.icons.filled.SwapHoriz import androidx.compose.material.icons.filled.Vibration import androidx.compose.material.icons.filled._123 import androidx.compose.material3.Icon @@ -55,12 +54,12 @@ import com.sadellie.unitto.core.ui.common.NavigateUpButton import com.sadellie.unitto.core.ui.common.UnittoListItem import com.sadellie.unitto.core.ui.common.UnittoScreenWithLargeTopBar import com.sadellie.unitto.core.ui.openLink -import com.sadellie.unitto.data.model.UnitsListSorting import com.sadellie.unitto.feature.settings.components.AlertDialogWithList import com.sadellie.unitto.feature.settings.navigation.aboutRoute +import com.sadellie.unitto.feature.settings.navigation.calculatorSettingsRoute +import com.sadellie.unitto.feature.settings.navigation.converterSettingsRoute import com.sadellie.unitto.feature.settings.navigation.formattingRoute import com.sadellie.unitto.feature.settings.navigation.themesRoute -import com.sadellie.unitto.feature.settings.navigation.unitsGroupRoute @Composable internal fun SettingsScreen( @@ -125,52 +124,31 @@ internal fun SettingsScreen( ) } - // UNIT CONVERTER GROUP - item { Header(stringResource(R.string.unit_converter)) } - - // UNIT GROUPS item { ListItem( leadingContent = { Icon( - Icons.Default.Rule, - stringResource(R.string.disable_unit_group_description), + Icons.Default.Calculate, + stringResource(R.string.calculator), ) }, - headlineContent = { Text(stringResource(R.string.unit_groups_setting)) }, - supportingContent = { Text(stringResource(R.string.unit_groups_support)) }, - modifier = Modifier.clickable { navControllerAction(unitsGroupRoute) } + headlineContent = { Text(stringResource(R.string.calculator)) }, + supportingContent = { Text(stringResource(R.string.calculator_settings_support)) }, + modifier = Modifier.clickable { navControllerAction(calculatorSettingsRoute) } ) } - // UNITS LIST SORTING item { ListItem( leadingContent = { Icon( - Icons.Default.Sort, - stringResource(R.string.units_sorting) + Icons.Default.SwapHoriz, + stringResource(R.string.unit_converter), ) }, - headlineContent = { Text(stringResource(R.string.units_sorting)) }, - supportingContent = { Text(stringResource(R.string.units_sorting_support)) }, - modifier = Modifier.clickable { dialogState = DialogState.UNIT_LIST_SORTING } - ) - } - - // FORMAT TIME - item { - UnittoListItem( - label = stringResource(R.string.format_time), - leadingContent = { - Icon( - Icons.Default.Timer, - stringResource(R.string.format_time) - ) - }, - supportContent = stringResource(R.string.format_time_support), - switchState = userPrefs.value.unitConverterFormatTime, - onSwitchChange = viewModel::updateUnitConverterFormatTime + headlineContent = { Text(stringResource(R.string.unit_converter)) }, + supportingContent = { Text(stringResource(R.string.converter_settings_support)) }, + modifier = Modifier.clickable { navControllerAction(converterSettingsRoute) } ) } @@ -276,21 +254,6 @@ internal fun SettingsScreen( ) } - DialogState.UNIT_LIST_SORTING -> { - AlertDialogWithList( - title = stringResource(R.string.units_sorting), - listItems = mapOf( - UnitsListSorting.USAGE to R.string.sort_by_usage, - UnitsListSorting.ALPHABETICAL to R.string.sort_by_alphabetical, - UnitsListSorting.SCALE_DESC to R.string.sort_by_scale_desc, - UnitsListSorting.SCALE_ASC to R.string.sort_by_scale_asc, - ), - selectedItemIndex = userPrefs.value.unitConverterSorting, - selectAction = viewModel::updateUnitConverterSorting, - dismissAction = { resetDialog() } - ) - } - DialogState.LANGUAGE -> { AlertDialogWithList( title = stringResource(R.string.language_setting), @@ -322,5 +285,5 @@ internal fun SettingsScreen( * All possible states for alert dialog that opens when user clicks on settings. */ private enum class DialogState { - NONE, START_SCREEN, UNIT_LIST_SORTING, LANGUAGE + NONE, START_SCREEN, LANGUAGE } diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsViewModel.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsViewModel.kt index a9e7720d..71a17048 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsViewModel.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/SettingsViewModel.kt @@ -93,4 +93,13 @@ class SettingsViewModel @Inject constructor( userPrefsRepository.updateUnitConverterSorting(sorting) } } + + /** + * @see UserPreferencesRepository.updatePartialHistoryView + */ + fun updatePartialHistoryView(enabled: Boolean) { + viewModelScope.launch { + userPrefsRepository.updatePartialHistoryView(enabled) + } + } } diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/formatting/FormattingScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/formatting/FormattingScreen.kt index a9929b51..ef779937 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/formatting/FormattingScreen.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/formatting/FormattingScreen.kt @@ -49,7 +49,6 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.sadellie.unitto.core.base.MAX_PRECISION import com.sadellie.unitto.core.base.OutputFormat @@ -67,7 +66,7 @@ import kotlin.math.roundToInt @Composable fun FormattingRoute( - viewModel: FormattingViewModel = hiltViewModel(), + viewModel: FormattingViewModel, navigateUpAction: () -> Unit, ) { val uiState = viewModel.uiState.collectAsStateWithLifecycle() diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/navigation/SettingsNavigation.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/navigation/SettingsNavigation.kt index 89e92de4..9e92e729 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/navigation/SettingsNavigation.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/navigation/SettingsNavigation.kt @@ -18,6 +18,8 @@ package com.sadellie.unitto.feature.settings.navigation +import androidx.compose.runtime.remember +import androidx.hilt.navigation.compose.hiltViewModel import androidx.navigation.NavController import androidx.navigation.NavGraphBuilder import androidx.navigation.NavHostController @@ -26,6 +28,8 @@ import androidx.navigation.compose.navigation import androidx.navigation.navDeepLink import com.sadellie.unitto.core.base.TopLevelDestinations import com.sadellie.unitto.feature.settings.AboutScreen +import com.sadellie.unitto.feature.settings.CalculatorSettingsScreen +import com.sadellie.unitto.feature.settings.ConverterSettingsScreen import com.sadellie.unitto.feature.settings.SettingsScreen import com.sadellie.unitto.feature.settings.ThirdPartyLicensesScreen import com.sadellie.unitto.feature.settings.formatting.FormattingRoute @@ -40,6 +44,8 @@ internal const val unitsGroupRoute = "units_group_route" internal const val thirdPartyRoute = "third_party_route" internal const val aboutRoute = "about_route" internal const val formattingRoute = "formatting_route" +internal const val calculatorSettingsRoute = "calculator_settings_route" +internal const val converterSettingsRoute = "converter_settings_route" fun NavController.navigateToSettings() { navigate(TopLevelDestinations.Settings.start) @@ -61,7 +67,10 @@ fun NavGraphBuilder.settingGraph( ) ) { composable(start) { + val parent = remember(it) { navController.getBackStackEntry(graph) } + SettingsScreen( + viewModel = hiltViewModel(parent), menuButtonClick = navController::navigateUp, navControllerAction = navController::navigate ) @@ -69,6 +78,7 @@ fun NavGraphBuilder.settingGraph( composable(themesRoute) { ThemesRoute( + viewModel = hiltViewModel(), navigateUpAction = navController::navigateUp, themmoController = themmoController, ) @@ -81,22 +91,46 @@ fun NavGraphBuilder.settingGraph( } composable(aboutRoute) { + val parent = remember(it) { navController.getBackStackEntry(graph) } + AboutScreen( + viewModel = hiltViewModel(parent), navigateUpAction = navController::navigateUp, - navigateToThirdParty = { navController.navigate(thirdPartyRoute) }, + navigateToThirdParty = { navController.navigate(thirdPartyRoute) } ) } composable(unitsGroupRoute) { UnitGroupsScreen( + viewModel = hiltViewModel(), navigateUpAction = navController::navigateUp, ) } composable(formattingRoute) { FormattingRoute( + viewModel = hiltViewModel(), navigateUpAction = navController::navigateUp ) } + + composable(calculatorSettingsRoute) { + val parent = remember(it) { navController.getBackStackEntry(graph) } + + CalculatorSettingsScreen( + viewModel = hiltViewModel(parent), + navigateUpAction = navController::navigateUp, + ) + } + + composable(converterSettingsRoute) { + val parent = remember(it) { navController.getBackStackEntry(graph) } + + ConverterSettingsScreen( + viewModel = hiltViewModel(parent), + navigateUpAction = navController::navigateUp, + navigateToUnitsGroup = { navController.navigate(unitsGroupRoute) } + ) + } } } diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/themes/ThemesScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/themes/ThemesScreen.kt index cc887eb5..efcbe2f1 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/themes/ThemesScreen.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/themes/ThemesScreen.kt @@ -48,7 +48,6 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.sadellie.unitto.core.base.R import com.sadellie.unitto.core.ui.common.Header @@ -81,9 +80,9 @@ private val colorSchemes: List by lazy { @Composable internal fun ThemesRoute( + viewModel: ThemesViewModel, navigateUpAction: () -> Unit = {}, themmoController: ThemmoController, - viewModel: ThemesViewModel = hiltViewModel() ) { val systemFont = viewModel.systemFont.collectAsStateWithLifecycle() diff --git a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/unitgroups/UnitGroupsScreen.kt b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/unitgroups/UnitGroupsScreen.kt index 6956c7ed..ac1c10f8 100644 --- a/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/unitgroups/UnitGroupsScreen.kt +++ b/feature/settings/src/main/java/com/sadellie/unitto/feature/settings/unitgroups/UnitGroupsScreen.kt @@ -47,7 +47,6 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp -import androidx.hilt.navigation.compose.hiltViewModel import com.sadellie.unitto.core.base.R import com.sadellie.unitto.core.ui.common.Header import com.sadellie.unitto.core.ui.common.NavigateUpButton @@ -60,7 +59,7 @@ import org.burnoutcrew.reorderable.reorderable @Composable internal fun UnitGroupsScreen( - viewModel: UnitGroupsViewModel = hiltViewModel(), + viewModel: UnitGroupsViewModel, navigateUpAction: () -> Unit ) { UnittoScreenWithLargeTopBar(