Provide SharedPreferences via Hilt

This commit is contained in:
Kirill Kamakin
2021-11-20 15:36:13 +03:00
parent 06c8144dd3
commit e39b8ae354
2 changed files with 9 additions and 6 deletions

View File

@@ -1,6 +1,8 @@
package gq.kirmanak.mealient.data
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import androidx.room.Room
import dagger.Module
import dagger.Provides
@@ -16,5 +18,10 @@ interface AppModule {
fun createDb(@ApplicationContext context: Context): AppDb {
return Room.databaseBuilder(context, AppDb::class.java, "app.db").build()
}
@Provides
fun createSharedPreferences(@ApplicationContext context: Context): SharedPreferences {
return PreferenceManager.getDefaultSharedPreferences(context)
}
}
}

View File

@@ -1,9 +1,6 @@
package gq.kirmanak.mealient.data.auth.impl
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import dagger.hilt.android.qualifiers.ApplicationContext
import gq.kirmanak.mealient.data.auth.AuthStorage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -20,10 +17,9 @@ private const val TOKEN_KEY = "AUTH_TOKEN"
private const val BASE_URL_KEY = "BASE_URL"
@ExperimentalCoroutinesApi
class AuthStorageImpl @Inject constructor(@ApplicationContext private val context: Context) :
AuthStorage {
class AuthStorageImpl @Inject constructor(
private val sharedPreferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(context)
) : AuthStorage {
override fun storeAuthData(token: String, baseUrl: String) {
Timber.v("storeAuthData() called with: token = $token, baseUrl = $baseUrl")