Skip to content

Commit

Permalink
Use DataStore instead of kprefs
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Mar 30, 2024
1 parent 479555e commit 2ec2e29
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
2 changes: 0 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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" }
Expand Down
1 change: 0 additions & 1 deletion wearApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Preferences>
) {

val showOnlyBookmarkedSessions: MutableStateFlow<Boolean> by localPrefs.BooleanFlow(false)
companion object {
private const val PREF_SHOW_ONLY_BOOKMARK_SESSIONS = "show_only_bookmark_sessions"
}

val showOnlyBookmarkedSessions: Flow<Boolean> = 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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 2ec2e29

Please sign in to comment.