diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 2f70059c..bce5858a 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -28,7 +28,6 @@ androidx-wear-compose = "1.3.0" playServicesWearable = "18.1.0" compose-ui-tooling = "1.3.0" horologist = "0.6.5" -kprefs = "1.7.2" [libraries] androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "androidxActivity" } @@ -105,7 +104,6 @@ compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" androidx-compose-ui-tooling = { module = "androidx.wear.compose:compose-ui-tooling", version.ref = "compose-ui-tooling" } wear-compose-navigation = { module = "androidx.wear.compose:compose-navigation", version.ref = "androidx-wear-compose" } desugar-jdk-libs = "com.android.tools:desugar_jdk_libs:2.0.4" -kprefs = { module = "org.jraf:kprefs", version.ref = "kprefs" } [plugins] android-application = { id = "com.android.application", version.ref = "android-gradle-plugin" } diff --git a/wearApp/build.gradle.kts b/wearApp/build.gradle.kts index a79ec65d..754f2e7c 100644 --- a/wearApp/build.gradle.kts +++ b/wearApp/build.gradle.kts @@ -63,7 +63,6 @@ dependencies { implementation(libs.wear.compose.navigation) implementation(platform(libs.firebase.bom)) implementation(libs.firebase.auth.ktx) - implementation(libs.kprefs) coreLibraryDesugaring(libs.desugar.jdk.libs) debugImplementation(libs.compose.ui.tooling) diff --git a/wearApp/src/main/java/fr/paug/androidmakers/wear/data/LocalPreferencesRepository.kt b/wearApp/src/main/java/fr/paug/androidmakers/wear/data/LocalPreferencesRepository.kt index 9b0b21ab..94ed49cf 100644 --- a/wearApp/src/main/java/fr/paug/androidmakers/wear/data/LocalPreferencesRepository.kt +++ b/wearApp/src/main/java/fr/paug/androidmakers/wear/data/LocalPreferencesRepository.kt @@ -1,11 +1,27 @@ package fr.paug.androidmakers.wear.data -import android.content.Context -import kotlinx.coroutines.flow.MutableStateFlow -import org.jraf.android.kprefs.Prefs +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.map -class LocalPreferencesRepository(applicationContext: Context) { - private val localPrefs = Prefs(applicationContext) +class LocalPreferencesRepository( + private val dataStore: DataStore +) { - val showOnlyBookmarkedSessions: MutableStateFlow by localPrefs.BooleanFlow(false) + companion object { + private const val PREF_SHOW_ONLY_BOOKMARK_SESSIONS = "show_only_bookmark_sessions" + } + + val showOnlyBookmarkedSessions: Flow = dataStore.data.map { prefs -> + prefs[booleanPreferencesKey(PREF_SHOW_ONLY_BOOKMARK_SESSIONS)] ?: false + } + + suspend fun setShowOnlyBookmarkedSessions(showOnlyBookmarkedSessions: Boolean) { + dataStore.edit { prefs -> + prefs[booleanPreferencesKey(PREF_SHOW_ONLY_BOOKMARK_SESSIONS)] = showOnlyBookmarkedSessions + } + } } diff --git a/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsScreen.kt b/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsScreen.kt index 09c3bd24..a8bcc679 100644 --- a/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsScreen.kt +++ b/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsScreen.kt @@ -67,7 +67,9 @@ fun SettingsScreen( } item { - val showOnlyBookmarkedSessions: Boolean by viewModel.showOnlyBookmarkedSessions.collectAsStateWithLifecycle() + val showOnlyBookmarkedSessions: Boolean by viewModel.showOnlyBookmarkedSessions.collectAsStateWithLifecycle( + false + ) ToggleChip( modifier = Modifier.fillMaxWidth(), checked = showOnlyBookmarkedSessions, diff --git a/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsViewModel.kt b/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsViewModel.kt index ee2af75c..e676188c 100644 --- a/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsViewModel.kt +++ b/wearApp/src/main/java/fr/paug/androidmakers/wear/ui/settings/SettingsViewModel.kt @@ -2,16 +2,20 @@ package fr.paug.androidmakers.wear.ui.settings import android.app.Application import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.viewModelScope import fr.paug.androidmakers.wear.data.LocalPreferencesRepository +import kotlinx.coroutines.launch class SettingsViewModel( - application: Application, - private val localPreferencesRepository: LocalPreferencesRepository, + application: Application, + private val localPreferencesRepository: LocalPreferencesRepository, ) : AndroidViewModel(application) { val showOnlyBookmarkedSessions = localPreferencesRepository.showOnlyBookmarkedSessions fun setShowOnlyBookmarkedSessions(showOnlyBookmarkedSessions: Boolean) { - localPreferencesRepository.showOnlyBookmarkedSessions.value = showOnlyBookmarkedSessions + viewModelScope.launch { + localPreferencesRepository.setShowOnlyBookmarkedSessions(showOnlyBookmarkedSessions) + } } }