Forgor how sets work 💀

This commit is contained in:
Sad Ellie 2024-02-08 23:12:04 +03:00
parent 3c10fb9b0c
commit 3d2a63fe95
3 changed files with 11 additions and 27 deletions

View File

@ -105,23 +105,7 @@ internal fun Preferences.getUnitConverterSorting(): UnitsListSorting {
?.let { UnitsListSorting.valueOf(it) } ?: UnitsListSorting.USAGE
}
// TODO Remove when 80% users are on sets
internal fun Preferences.getShownUnitGroups(): List<UnitGroup> {
// Uncomment once legacy is gone
// return this[PrefsKeys.ENABLED_UNIT_GROUPS]
// ?.letTryOrNull { stringSet: Set<String> ->
// stringSet.map { UnitGroup.valueOf(it) }
// } ?: UnitGroup.entries
// Try new method
val unitGroups: List<UnitGroup>? = this[PrefsKeys.ENABLED_UNIT_GROUPS]
?.letTryOrNull { stringSet: Set<String> ->
stringSet.map { UnitGroup.valueOf(it) }
}
if (!unitGroups.isNullOrEmpty()) return unitGroups
// Failed to get from sets, try old method
return this[PrefsKeys.SHOWN_UNIT_GROUPS]
?.letTryOrNull { list ->
list
@ -149,6 +133,8 @@ internal fun Preferences.getAcButton(): Boolean {
return this[PrefsKeys.AC_BUTTON] ?: true
}
internal fun List<UnitGroup>.packToString(): String = this.joinToString(",")
private inline fun <T, R> T.letTryOrNull(block: (T) -> R): R? = try {
this?.let(block)
} catch (e: Exception) {

View File

@ -22,7 +22,6 @@ import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
object PrefsKeys {
// COMMON
@ -53,7 +52,6 @@ object PrefsKeys {
val LATEST_LEFT_SIDE = stringPreferencesKey("LATEST_LEFT_SIDE_PREF_KEY")
val LATEST_RIGHT_SIDE = stringPreferencesKey("LATEST_RIGHT_SIDE_PREF_KEY")
val SHOWN_UNIT_GROUPS = stringPreferencesKey("SHOWN_UNIT_GROUPS_PREF_KEY")
val ENABLED_UNIT_GROUPS = stringSetPreferencesKey("ENABLED_UNIT_GROUPS_PREF_KEY")
val UNIT_CONVERTER_FAVORITES_ONLY = booleanPreferencesKey("UNIT_CONVERTER_FAVORITES_ONLY_PREF_KEY")
val UNIT_CONVERTER_FORMAT_TIME = booleanPreferencesKey("UNIT_CONVERTER_FORMAT_TIME_PREF_KEY")
val UNIT_CONVERTER_SORTING = stringPreferencesKey("UNIT_CONVERTER_SORTING_PREF_KEY")

View File

@ -221,25 +221,25 @@ class UserPreferencesRepositoryImpl @Inject constructor(
override suspend fun updateShownUnitGroups(shownUnitGroups: List<UnitGroup>) {
dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = shownUnitGroups
.map { it.name }
.toSet()
preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = shownUnitGroups.packToString()
}
}
override suspend fun addShownUnitGroup(unitGroup: UnitGroup) {
dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = preferences[PrefsKeys.ENABLED_UNIT_GROUPS]
?.plus(unitGroup.name)
?: emptySet()
preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = preferences
.getShownUnitGroups()
.plus(unitGroup)
.packToString()
}
}
override suspend fun removeShownUnitGroup(unitGroup: UnitGroup) {
dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = preferences[PrefsKeys.ENABLED_UNIT_GROUPS]
?.minus(unitGroup.name)
?: emptySet()
preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = preferences
.getShownUnitGroups()
.minus(unitGroup)
.packToString()
}
}