diff --git a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PreferenceExt.kt b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PreferenceExt.kt index 39e5e744..7e222bb3 100644 --- a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PreferenceExt.kt +++ b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PreferenceExt.kt @@ -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 { -// Uncomment once legacy is gone -// return this[PrefsKeys.ENABLED_UNIT_GROUPS] -// ?.letTryOrNull { stringSet: Set -> -// stringSet.map { UnitGroup.valueOf(it) } -// } ?: UnitGroup.entries - - // Try new method - val unitGroups: List? = this[PrefsKeys.ENABLED_UNIT_GROUPS] - ?.letTryOrNull { stringSet: Set -> - 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.packToString(): String = this.joinToString(",") + private inline fun T.letTryOrNull(block: (T) -> R): R? = try { this?.let(block) } catch (e: Exception) { diff --git a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PrefsKeys.kt b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PrefsKeys.kt index 9b70976d..0c65ccce 100644 --- a/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PrefsKeys.kt +++ b/data/userprefs/src/main/java/com/sadellie/unitto/data/userprefs/PrefsKeys.kt @@ -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") 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 56e20694..6d8a97f3 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 @@ -221,25 +221,25 @@ class UserPreferencesRepositoryImpl @Inject constructor( override suspend fun updateShownUnitGroups(shownUnitGroups: List) { 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() } }