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 package gq.kirmanak.mealient.data
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import androidx.room.Room import androidx.room.Room
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
@@ -16,5 +18,10 @@ interface AppModule {
fun createDb(@ApplicationContext context: Context): AppDb { fun createDb(@ApplicationContext context: Context): AppDb {
return Room.databaseBuilder(context, AppDb::class.java, "app.db").build() 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 package gq.kirmanak.mealient.data.auth.impl
import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import dagger.hilt.android.qualifiers.ApplicationContext
import gq.kirmanak.mealient.data.auth.AuthStorage import gq.kirmanak.mealient.data.auth.AuthStorage
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -20,10 +17,9 @@ private const val TOKEN_KEY = "AUTH_TOKEN"
private const val BASE_URL_KEY = "BASE_URL" private const val BASE_URL_KEY = "BASE_URL"
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
class AuthStorageImpl @Inject constructor(@ApplicationContext private val context: Context) : class AuthStorageImpl @Inject constructor(
AuthStorage {
private val sharedPreferences: SharedPreferences private val sharedPreferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(context) ) : AuthStorage {
override fun storeAuthData(token: String, baseUrl: String) { override fun storeAuthData(token: String, baseUrl: String) {
Timber.v("storeAuthData() called with: token = $token, baseUrl = $baseUrl") Timber.v("storeAuthData() called with: token = $token, baseUrl = $baseUrl")