-
-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DataStore datasource for storing api tokens
- Loading branch information
Showing
6 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/src/main/java/dev/chungjungsoo/gptmobile/data/datastore/TokenDataSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
50 changes: 50 additions & 0 deletions
50
app/src/main/java/dev/chungjungsoo/gptmobile/data/datastore/TokenDataSourceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/dev/chungjungsoo/gptmobile/data/dto/ApiType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/main/java/dev/chungjungsoo/gptmobile/di/DataSourceModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters