Skip to content

Commit 2726da5

Browse files
committed
Update DataStoreUtils
1 parent 6c75042 commit 2726da5

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//package com.asemlab.samples.di
2+
//
3+
//import android.content.Context
4+
//import androidx.datastore.core.DataStore
5+
//import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
6+
//import androidx.datastore.preferences.SharedPreferencesMigration
7+
//import androidx.datastore.preferences.core.PreferenceDataStoreFactory
8+
//import androidx.datastore.preferences.core.Preferences
9+
//import androidx.datastore.preferences.core.emptyPreferences
10+
//import androidx.datastore.preferences.preferencesDataStoreFile
11+
//import com.asemlab.screenbrightness.utils.DataStoreUtils
12+
//import dagger.Module
13+
//import dagger.Provides
14+
//import dagger.hilt.InstallIn
15+
//import dagger.hilt.android.qualifiers.ApplicationContext
16+
//import dagger.hilt.components.SingletonComponent
17+
//import kotlinx.coroutines.CoroutineScope
18+
//import kotlinx.coroutines.Dispatchers
19+
//import kotlinx.coroutines.SupervisorJob
20+
//import javax.inject.Singleton
21+
//
22+
//@Module
23+
//@InstallIn(SingletonComponent::class)
24+
//object DataStoreModule {
25+
//
26+
// @Singleton
27+
// @Provides
28+
// fun providePreferencesDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> {
29+
// return PreferenceDataStoreFactory.create(
30+
// corruptionHandler = ReplaceFileCorruptionHandler(
31+
// produceNewData = { emptyPreferences() }
32+
// ),
33+
// // TODO Migrations can be added here if you're migrating from SharedPreferences
34+
// // or changing DataStore schema
35+
// migrations = listOf(SharedPreferencesMigration(appContext, "your_shared_prefs_name")),
36+
// scope = CoroutineScope(Dispatchers.IO + SupervisorJob()), // Application-wide scope
37+
// produceFile = { appContext.preferencesDataStoreFile("user_details") }
38+
// )
39+
// }
40+
//
41+
// @Singleton
42+
// @Provides
43+
// fun provideDataStoreUtils(dataStore: DataStore<Preferences>): DataStoreUtils {
44+
// return DataStoreUtils(dataStore)
45+
// }
46+
//}

app/src/main/java/com/asemlab/samples/utils/DataStoreUtils.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ import kotlinx.coroutines.flow.first
1212
import kotlinx.coroutines.flow.map
1313

1414

15-
object DataStoreUtils {
15+
object DataStoreUtils /*@Inject constructor(
16+
private val dataStore: DataStore<Preferences>
17+
)*/ {
1618

1719
// TODO Init DataStore object
1820
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "user_details")
@@ -23,6 +25,13 @@ object DataStoreUtils {
2325
val GENDER_KEY = booleanPreferencesKey("gender")
2426

2527
// TODO Write to DataStore
28+
29+
// suspend fun <T> putValue(key: Preferences.Key<T>, newValue: T) {
30+
// dataStore.edit { data ->
31+
// data[key] = newValue
32+
// }
33+
// }
34+
2635
suspend fun setUsername(context: Context, username: String) {
2736
context.dataStore.edit { data ->
2837
data[USERNAME_KEY] = username
@@ -42,6 +51,16 @@ object DataStoreUtils {
4251
}
4352

4453
// TODO Read from DataStore
54+
55+
// suspend fun <T> getValue(
56+
// key: Preferences.Key<T>,
57+
// default: T
58+
// ): T {
59+
// return dataStore.data.map {
60+
// it[key] ?: default
61+
// }.first()
62+
// }
63+
4564
suspend fun getUsername(context: Context): String {
4665
return context.dataStore.data.map {
4766
it[USERNAME_KEY] ?: ""

0 commit comments

Comments
 (0)