mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-19 16:55:26 +02:00
Added dependency injection for DataStore
This commit is contained in:
parent
941e260a15
commit
2adf756bff
@ -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) }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user