Added dependency injection for DataStore

This commit is contained in:
Sad Ellie 2022-06-02 21:31:46 +03:00
parent 941e260a15
commit 2adf756bff
2 changed files with 42 additions and 12 deletions

View File

@ -0,0 +1,41 @@
package com.sadellie.unitto.data.preferences
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.preferencesDataStoreFile
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
// DON'T TOUCH!!!
private const val USER_PREFERENCES = "settings"
/**
* This module is for DataStore dependency injection
*/
@InstallIn(SingletonComponent::class)
@Module
class DataStoreModule {
/**
* Tells Hilt to use this method to get [DataStore]
*
* @param appContext
* @return Singleton of [DataStore]
*/
@Singleton
@Provides
fun provideUserPreferencesDataStore(@ApplicationContext appContext: Context): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler { emptyPreferences() },
produceFile = { appContext.preferencesDataStoreFile(USER_PREFERENCES) }
)
}
}

View File

@ -1,21 +1,14 @@
package com.sadellie.unitto.data.preferences package com.sadellie.unitto.data.preferences
import android.content.Context
import androidx.datastore.core.DataStore import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.* import androidx.datastore.preferences.core.*
import androidx.datastore.preferences.preferencesDataStore
import com.sadellie.unitto.data.units.AbstractUnit import com.sadellie.unitto.data.units.AbstractUnit
import com.sadellie.unitto.data.units.MyUnitIDS import com.sadellie.unitto.data.units.MyUnitIDS
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import javax.inject.Inject import javax.inject.Inject
// It's at the top level to make DataStore singleton
// DON'T TOUCH STRINGS!!!
private val Context.settingsDataStore by preferencesDataStore("settings")
/** /**
* Represents user preferences that are user across the app * Represents user preferences that are user across the app
* *
@ -39,12 +32,8 @@ data class UserPreferences(
/** /**
* Repository that works with DataStore * Repository that works with DataStore
*
* @property context
*/ */
class UserPreferencesRepository @Inject constructor(@ApplicationContext private val context: Context) { class UserPreferencesRepository @Inject constructor(private val dataStore: DataStore<Preferences>) {
private val dataStore: DataStore<Preferences> = context.settingsDataStore
/** /**
* Keys for DataStore * Keys for DataStore