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 ?.let { UnitsListSorting.valueOf(it) } ?: UnitsListSorting.USAGE
} }
// TODO Remove when 80% users are on sets
internal fun Preferences.getShownUnitGroups(): List<UnitGroup> { 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] return this[PrefsKeys.SHOWN_UNIT_GROUPS]
?.letTryOrNull { list -> ?.letTryOrNull { list ->
list list
@ -149,6 +133,8 @@ internal fun Preferences.getAcButton(): Boolean {
return this[PrefsKeys.AC_BUTTON] ?: true 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 { private inline fun <T, R> T.letTryOrNull(block: (T) -> R): R? = try {
this?.let(block) this?.let(block)
} catch (e: Exception) { } 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.intPreferencesKey
import androidx.datastore.preferences.core.longPreferencesKey import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.core.stringSetPreferencesKey
object PrefsKeys { object PrefsKeys {
// COMMON // COMMON
@ -53,7 +52,6 @@ object PrefsKeys {
val LATEST_LEFT_SIDE = stringPreferencesKey("LATEST_LEFT_SIDE_PREF_KEY") val LATEST_LEFT_SIDE = stringPreferencesKey("LATEST_LEFT_SIDE_PREF_KEY")
val LATEST_RIGHT_SIDE = stringPreferencesKey("LATEST_RIGHT_SIDE_PREF_KEY") val LATEST_RIGHT_SIDE = stringPreferencesKey("LATEST_RIGHT_SIDE_PREF_KEY")
val SHOWN_UNIT_GROUPS = stringPreferencesKey("SHOWN_UNIT_GROUPS_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_FAVORITES_ONLY = booleanPreferencesKey("UNIT_CONVERTER_FAVORITES_ONLY_PREF_KEY")
val UNIT_CONVERTER_FORMAT_TIME = booleanPreferencesKey("UNIT_CONVERTER_FORMAT_TIME_PREF_KEY") val UNIT_CONVERTER_FORMAT_TIME = booleanPreferencesKey("UNIT_CONVERTER_FORMAT_TIME_PREF_KEY")
val UNIT_CONVERTER_SORTING = stringPreferencesKey("UNIT_CONVERTER_SORTING_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>) { override suspend fun updateShownUnitGroups(shownUnitGroups: List<UnitGroup>) {
dataStore.edit { preferences -> dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = shownUnitGroups preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = shownUnitGroups.packToString()
.map { it.name }
.toSet()
} }
} }
override suspend fun addShownUnitGroup(unitGroup: UnitGroup) { override suspend fun addShownUnitGroup(unitGroup: UnitGroup) {
dataStore.edit { preferences -> dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = preferences[PrefsKeys.ENABLED_UNIT_GROUPS] preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = preferences
?.plus(unitGroup.name) .getShownUnitGroups()
?: emptySet() .plus(unitGroup)
.packToString()
} }
} }
override suspend fun removeShownUnitGroup(unitGroup: UnitGroup) { override suspend fun removeShownUnitGroup(unitGroup: UnitGroup) {
dataStore.edit { preferences -> dataStore.edit { preferences ->
preferences[PrefsKeys.ENABLED_UNIT_GROUPS] = preferences[PrefsKeys.ENABLED_UNIT_GROUPS] preferences[PrefsKeys.SHOWN_UNIT_GROUPS] = preferences
?.minus(unitGroup.name) .getShownUnitGroups()
?: emptySet() .minus(unitGroup)
.packToString()
} }
} }