Skip to content

Commit 68a3ae3

Browse files
committed
feat: remember previously used unit per unit converter category
1 parent 3e38a16 commit 68a3ae3

5 files changed

Lines changed: 55 additions & 15 deletions

File tree

app/src/main/java/net/youapps/calcyou/persistence/FavoriteUnitsRepository.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import net.you_apps.calcyou.Unit
88
class FavoriteUnitsRepository(val context: Context) {
99
val dataStore get() = context.favoriteUnitsDataStore
1010

11-
val unitsFlow = dataStore.data.map { it.categoriesList }
11+
val unitConverterCategoriesFlow = dataStore.data.map { it.categoriesList }
1212

1313
suspend fun addFavoriteUnit(categoryKey: String, unitKey: String) {
1414
dataStore.updateData { data ->
@@ -52,4 +52,34 @@ class FavoriteUnitsRepository(val context: Context) {
5252
.build()
5353
}
5454
}
55+
56+
suspend fun setSelectedUnit(categoryKey: String, unitKey: String?) {
57+
dataStore.updateData { data ->
58+
var builder = data.toBuilder()
59+
60+
// although it's not currently needed, it's possible to clear
61+
// the selected unit to reset it to default
62+
val unit = unitKey?.let { Unit.newBuilder().setKey(it).build() }
63+
64+
// add category if it doesn't exist yet
65+
if (data.categoriesList.none { it.key == categoryKey }) {
66+
builder = builder.addCategories(
67+
FavoriteUnitCategory.newBuilder()
68+
.setKey(categoryKey)
69+
.setSelectedUnit(unit)
70+
.build()
71+
)
72+
} else {
73+
val categoryIndex = data.categoriesList.indexOfFirst { category ->
74+
category.key == categoryKey
75+
}
76+
val updatedCategory = data.categoriesList[categoryIndex].toBuilder()
77+
updatedCategory.setSelectedUnit(unit)
78+
79+
builder.setCategories(categoryIndex, updatedCategory)
80+
}
81+
82+
builder.build()
83+
}
84+
}
5585
}

app/src/main/java/net/youapps/calcyou/ui/screens/ConverterScreen.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import androidx.compose.material3.TextField
2828
import androidx.compose.material3.TextFieldDefaults
2929
import androidx.compose.runtime.Composable
3030
import androidx.compose.runtime.LaunchedEffect
31+
import androidx.compose.runtime.collectAsState
3132
import androidx.compose.runtime.getValue
3233
import androidx.compose.runtime.mutableStateOf
3334
import androidx.compose.runtime.remember
@@ -80,7 +81,11 @@ inline fun <reified T> ConverterScreen(
8081
verticalArrangement = Arrangement.spacedBy(16.dp)
8182
) {
8283
var expanded by remember { mutableStateOf(false) }
83-
var selectedUnit by remember { mutableStateOf(converter.units.first()) }
84+
val selectedUnitKey by converterModel.selectedUnit.collectAsState(null)
85+
val selectedUnit = remember(selectedUnitKey) {
86+
converter.units.firstOrNull { converterModel.unitKey(it) == selectedUnitKey?.key }
87+
?: converter.units.first()
88+
}
8489
var converted: List<Pair<ConverterUnit<T>, T>> by remember { mutableStateOf(listOf()) }
8590
var textFieldValue by remember { mutableStateOf("") }
8691

@@ -153,9 +158,6 @@ inline fun <reified T> ConverterScreen(
153158
converterModel,
154159
converter.units,
155160
selectedUnit,
156-
{ unit ->
157-
selectedUnit = unit
158-
},
159161
onDismissRequest = {
160162
showUnitPickerScreen = false
161163
}

app/src/main/java/net/youapps/calcyou/ui/screens/ConverterUnitPickerScreen.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ fun <T> ConverterUnitPickerScreen(
4747
converterViewModel: UnitConverterViewModel,
4848
availableUnits: List<ConverterUnit<T>>,
4949
selectedUnit: ConverterUnit<T>,
50-
onUnitSelected: (ConverterUnit<T>) -> Unit,
5150
onDismissRequest: () -> Unit
5251
) {
5352
val context = LocalContext.current
@@ -64,6 +63,11 @@ fun <T> ConverterUnitPickerScreen(
6463

6564
val scrollBehavior = SearchBarDefaults.enterAlwaysSearchBarScrollBehavior()
6665

66+
fun setSelectedUnit(unit: ConverterUnit<T>) {
67+
converterViewModel.saveSelectedUnitForCategory(unit)
68+
onDismissRequest()
69+
}
70+
6771
FullscreenDialog(onDismissRequest = onDismissRequest) {
6872
Scaffold(
6973
topBar = {
@@ -76,9 +80,7 @@ fun <T> ConverterUnitPickerScreen(
7680
SearchBarDefaults.InputField(
7781
query = query,
7882
onQueryChange = { query = it },
79-
onSearch = {
80-
81-
},
83+
onSearch = { /* do nothing, search is automatically triggered by onQueryChange */ },
8284
expanded = true,
8385
onExpandedChange = {},
8486
placeholder = { Text(stringResource(R.string.search)) },
@@ -120,8 +122,7 @@ fun <T> ConverterUnitPickerScreen(
120122
isSelected = unit == selectedUnit,
121123
isFavorite = true,
122124
onClicked = {
123-
onUnitSelected(unit)
124-
onDismissRequest()
125+
setSelectedUnit(unit)
125126
},
126127
toggleIsFavorited = {
127128
converterViewModel.removeFavoriteUnit(unit)
@@ -139,8 +140,7 @@ fun <T> ConverterUnitPickerScreen(
139140
isSelected = unit == selectedUnit,
140141
isFavorite = false,
141142
onClicked = {
142-
onUnitSelected(unit)
143-
onDismissRequest()
143+
setSelectedUnit(unit)
144144
},
145145
toggleIsFavorited = {
146146
converterViewModel.addFavoriteUnit(unit)

app/src/main/java/net/youapps/calcyou/viewmodels/UnitConverterViewModel.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ class UnitConverterViewModel(application: Application): AndroidViewModel(applica
1919
private val favoriteUnitsRepository = FavoriteUnitsRepository(application)
2020

2121
val currentCategoryKey = MutableStateFlow<String?>(null)
22-
val favoriteUnits = combine(favoriteUnitsRepository.unitsFlow, currentCategoryKey) { favoriteUnits, category ->
23-
favoriteUnits.firstOrNull { it.key == category }?.favoritesList.orEmpty()
22+
val favoriteUnits = combine(favoriteUnitsRepository.unitConverterCategoriesFlow, currentCategoryKey) { categories, category ->
23+
categories.firstOrNull { it.key == category }?.favoritesList.orEmpty()
24+
}
25+
val selectedUnit = combine(favoriteUnitsRepository.unitConverterCategoriesFlow, currentCategoryKey) { categories, category ->
26+
categories.firstOrNull { it.key == category }?.selectedUnit
2427
}
2528

2629
fun <T> unitKey(unit: ConverterUnit<T>): String {
@@ -38,6 +41,10 @@ class UnitConverterViewModel(application: Application): AndroidViewModel(applica
3841
favoriteUnitsRepository.removeFavoriteUnit(currentCategoryKey.value!!, unitKey(unit))
3942
}
4043

44+
fun <T> saveSelectedUnitForCategory(unit: ConverterUnit<T>) = viewModelScope.launch {
45+
favoriteUnitsRepository.setSelectedUnit(currentCategoryKey.value!!, unitKey(unit))
46+
}
47+
4148
companion object {
4249
val Factory = viewModelFactory {
4350
initializer {

app/src/main/proto/favorite_units.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ message Unit {
1010
message FavoriteUnitCategory {
1111
string key = 1;
1212
repeated Unit favorites = 2;
13+
optional Unit selected_unit = 3;
1314
}
1415

1516
message FavoriteUnits {

0 commit comments

Comments
 (0)