I have a sharedPreference object, and I want to make it as dependency inject component through the project.
// sharedPreference object
private const val PREF_TAG = "tag"
object MyPreference {
fun getStoredTag(context: Context): String {
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
return prefs.getString(PREF_TAG, "")!!
}
fun setStoredTag(context: Context, query: String) {
PreferenceManager.getDefaultSharedPreferences(context)
.edit()
.putString(PREF_TAG, query)
.apply()
}
}
// How to correctly inject the sharedPreference?
// create a module?
@Module
@InstallIn(SingletonComponent::class)
object PreferenceModule {
@Provides
@Singleton
fun provideSharedPreference(): SharedPreferences {
return MyPreference()
}
}
// or directly inject in viewModel
class LoginViewModel @ViewModelInject constructor(
application: Application,
myPreference: MyPreference
) : AndroidViewModel(application) {
...
}
// or another way?
with Hilt 2.38.1:
SharedPreferencesModule.kt
@Module
@InstallIn(SingletonComponent::class)
class SharedPreferencesModule {
@Singleton
@Provides
fun provideSharedPreference(@ApplicationContext context: Context): SharedPreferences {
return context.getSharedPreferences("preferences_name", Context.MODE_PRIVATE)
}
}
CustomViewModel.kt
@HiltViewModel
class CustomViewModel @Inject constructor(
private val sharedPreferences: SharedPreferences
):ViewModel() {
fun customFunction() {
sharedPreferences.edit().putString("firstStoredString", "this is the content").apply()
val firstStoredString = sharedPreferences.getString("firstStoredString", "")
}
...
This is a bit of an opinion-based answer, but at least I will give you a direction to follow.
You'd usually maintain an instance of SharedPreferences
within your wrapper class. So...
class
instead of an object
declarationHilt
setup, you can directly use hilt annotations for the class, directly injecting Context
into the constructor@Singleton
class MyPreference @Inject constructor(@ApplicationContext context : Context){
val prefs = PreferenceManager.getDefaultSharedPreferences(context)
fun getStoredTag(): String {
return prefs.getString(PREF_TAG, "")!!
}
fun setStoredTag(query: String) {
prefs.edit().putString(PREF_TAG, query).apply()
}
}
Module
, you can simply use the @ViewModelInject
class LoginViewModel @ViewModelInject constructor(
application: Application,
myPreference: MyPreference
) : AndroidViewModel(application) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With