feat(android): set up DataStore

This commit is contained in:
Harsh Shandilya 2023-03-12 21:10:06 +05:30
parent 9cbc754574
commit 690052b48f
No known key found for this signature in database
5 changed files with 54 additions and 0 deletions

View file

@ -59,6 +59,7 @@ dependencies {
implementation(libs.androidx.compose.material)
implementation(libs.androidx.compose.material3)
implementation(libs.androidx.core.splashscreen)
implementation(libs.androidx.datastore.preferences)
implementation(libs.androidx.lifecycle.compose)
implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.paging.compose)

View file

@ -0,0 +1,36 @@
package dev.msfjarvis.claw.android.injection
import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.PreferenceDataStoreFactory
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.preferencesDataStoreFile
import com.deliveryhero.whetstone.ForScope
import com.deliveryhero.whetstone.app.ApplicationScope
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import dev.msfjarvis.claw.util.coroutines.DispatcherProvider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
@Module
@ContributesTo(ApplicationScope::class)
interface DataStoreModule {
@Singleton
@Provides
fun provideLoginDatastore(
@ForScope(ApplicationScope::class) context: Context,
dispatcherProvider: DispatcherProvider,
): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
corruptionHandler = null,
migrations = emptyList(),
scope = CoroutineScope(dispatcherProvider.io() + SupervisorJob()),
) {
context.preferencesDataStoreFile("login")
}
}
}

View file

@ -0,0 +1,15 @@
package dev.msfjarvis.claw.android.settings
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.stringPreferencesKey
import javax.inject.Inject
class LoginSettings @Inject constructor(
val dataStore: DataStore<Preferences>,
) {
companion object {
val CSRF_TOKEN = stringPreferencesKey("csrf_token")
val LOGIN_COOKIE = stringPreferencesKey("login_cookie")
}
}

View file

@ -35,6 +35,7 @@ androidx-compose-ui-text = { module = "androidx.compose.ui:ui-text" }
androidx-compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
androidx-compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview" }
androidx-core-splashscreen = "androidx.core:core-splashscreen:1.0.0"
androidx-datastore-preferences = "androidx.datastore:datastore-preferences:1.0.0"
androidx-lifecycle-compose = "androidx.lifecycle:lifecycle-viewmodel-compose:2.6.0"
androidx-navigation-compose = "androidx.navigation:navigation-compose:2.6.0-alpha07"
androidx-paging-compose = "androidx.paging:paging-compose:1.0.0-alpha18"

View file

@ -70,6 +70,7 @@ dependencyResolutionManagement {
includeGroup("androidx.cursoradapter")
includeGroup("androidx.customview")
includeGroup("androidx.databinding")
includeGroup("androidx.datastore")
includeGroup("androidx.drawerlayout")
includeGroup("androidx.emoji2")
includeGroup("androidx.exifinterface")