Skip to content

Commit

Permalink
Add DataStore datasource for storing api tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Taewan-P committed May 20, 2024
1 parent c04eacc commit 6c89ea0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 1 deletion.
5 changes: 4 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)

// DataStore
implementation(libs.androidx.datastore)

// Dependency Injection
implementation(libs.hilt)
kapt(libs.hilt.compiler)
Expand All @@ -78,4 +81,4 @@ dependencies {

kapt {
correctErrorTypes = true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.chungjungsoo.gptmobile.data.datastore

import dev.chungjungsoo.gptmobile.data.dto.ApiType

interface TokenDataSource {
suspend fun updateStatus(apiType: ApiType, status: Boolean)
suspend fun updateToken(apiType: ApiType, token: String)
suspend fun getStatus(apiType: ApiType): Boolean?
suspend fun getToken(apiType: ApiType): String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.chungjungsoo.gptmobile.data.datastore

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import dev.chungjungsoo.gptmobile.data.dto.ApiType
import javax.inject.Inject
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map

class TokenDataSourceImpl @Inject constructor(
private val dataStore: DataStore<Preferences>
) : TokenDataSource {
private val apiStatusMap = mapOf(
ApiType.OPENAI to booleanPreferencesKey("openai_status"),
ApiType.ANTHROPIC to booleanPreferencesKey("anthropic_status"),
ApiType.GOOGLE to booleanPreferencesKey("google_status")
)
private val apiTokenMap = mapOf(
ApiType.OPENAI to stringPreferencesKey("openai_token"),
ApiType.ANTHROPIC to stringPreferencesKey("anthropic_token"),
ApiType.GOOGLE to stringPreferencesKey("google_token")
)

override suspend fun updateStatus(apiType: ApiType, status: Boolean) {
dataStore.edit { pref ->
pref[apiStatusMap[apiType]!!] = status
}
}

override suspend fun updateToken(apiType: ApiType, token: String) {
dataStore.edit { pref ->
pref[apiTokenMap[apiType]!!] = token
}
}

override suspend fun getStatus(apiType: ApiType): Boolean? {
return dataStore.data.map { pref ->
pref[apiStatusMap[apiType]!!]
}.first()
}

override suspend fun getToken(apiType: ApiType): String? {
return dataStore.data.map { pref ->
pref[apiTokenMap[apiType]!!]
}.first()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.chungjungsoo.gptmobile.data.dto

enum class ApiType {
OPENAI,
ANTHROPIC,
GOOGLE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.chungjungsoo.gptmobile.di

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

private const val TOKEN_PREF_FILE = "token"

@Module
@InstallIn(SingletonComponent::class)
object DataSourceModule {
@Provides
@Singleton
fun provideTokenDataStore(@ApplicationContext applicationContext: Context): DataStore<Preferences> {
return PreferenceDataStoreFactory.create(
corruptionHandler = ReplaceFileCorruptionHandler(
produceNewData = { emptyPreferences() }
),
produceFile = { applicationContext.preferencesDataStoreFile(TOKEN_PREF_FILE) }
)
}
}
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ espressoCore = "3.5.1"
lifecycleRuntimeKtx = "2.8.0"
activityCompose = "1.9.0"
composeBom = "2024.05.00"
datastore = "1.1.1"
hilt = "2.51.1"

[libraries]
Expand All @@ -18,6 +19,7 @@ androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-co
androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }
androidx-activity-compose = { group = "androidx.activity", name = "activity-compose", version.ref = "activityCompose" }
androidx-compose-bom = { group = "androidx.compose", name = "compose-bom", version.ref = "composeBom" }
androidx-datastore = { group = "androidx.datastore", name = "datastore-preferences", version.ref = "datastore" }
androidx-ui = { group = "androidx.compose.ui", name = "ui" }
androidx-ui-graphics = { group = "androidx.compose.ui", name = "ui-graphics" }
androidx-ui-tooling = { group = "androidx.compose.ui", name = "ui-tooling" }
Expand Down

0 comments on commit 6c89ea0

Please sign in to comment.