-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
7 deletions.
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
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
58 changes: 58 additions & 0 deletions
58
core/data/src/main/java/com/yapp/core/data/local/DataStoreModule.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,58 @@ | ||
package com.yapp.core.data.local | ||
|
||
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 tech.thdev.useful.encrypted.data.store.preferences.security.generateUsefulSecurity | ||
import javax.inject.Qualifier | ||
import javax.inject.Singleton | ||
|
||
@InstallIn(SingletonComponent::class) | ||
@Module | ||
internal object DataStoreModule { | ||
|
||
@Singleton | ||
@Provides | ||
fun provideDataStore(@ApplicationContext context: Context): DataStore<Preferences> { | ||
return PreferenceDataStoreFactory.create( | ||
corruptionHandler = ReplaceFileCorruptionHandler( | ||
produceNewData = { emptyPreferences() }, | ||
), | ||
produceFile = { context.preferencesDataStoreFile(DATASTORE_NAME) }, | ||
) | ||
} | ||
|
||
@EncryptedDataStore | ||
@Singleton | ||
@Provides | ||
fun providedEncryptedDataStore(@ApplicationContext context: Context): DataStore<Preferences> { | ||
return PreferenceDataStoreFactory.create( | ||
corruptionHandler = ReplaceFileCorruptionHandler( | ||
produceNewData = { emptyPreferences() }, | ||
), | ||
produceFile = { context.preferencesDataStoreFile(ENCRYPTED_DATASTORE_NAME) }, | ||
) | ||
} | ||
|
||
@Singleton | ||
@Provides | ||
fun provideSecurityPreference( | ||
@EncryptedDataStore dataStore: DataStore<Preferences>, | ||
): SecurityPreferences = dataStore.generateSecurityPreferences(generateUsefulSecurity()) | ||
|
||
private const val DATASTORE_NAME = "yapp-datastore" | ||
private const val ENCRYPTED_DATASTORE_NAME = "yapp-datastore-encrypted" | ||
} | ||
|
||
@Qualifier | ||
@Retention(AnnotationRetention.BINARY) | ||
internal annotation class EncryptedDataStore |
31 changes: 31 additions & 0 deletions
31
core/data/src/main/java/com/yapp/core/data/local/SecurityPreferences.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,31 @@ | ||
package com.yapp.core.data.local | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import tech.thdev.useful.encrypted.data.store.preferences.ksp.annotations.UsefulPreferences | ||
import tech.thdev.useful.encrypted.data.store.preferences.ksp.annotations.value.ClearValues | ||
import tech.thdev.useful.encrypted.data.store.preferences.ksp.annotations.value.GetValue | ||
import tech.thdev.useful.encrypted.data.store.preferences.ksp.annotations.value.SetValue | ||
|
||
@UsefulPreferences | ||
interface SecurityPreferences { | ||
|
||
@GetValue(KEY_ACCESS_TOKEN, defaultValue = "") | ||
fun flowAccessToken(): Flow<String> | ||
|
||
@SetValue(KEY_ACCESS_TOKEN) | ||
suspend fun setAccessToken(value: String) | ||
|
||
@GetValue(KEY_REFRESH_TOKEN, defaultValue = "") | ||
fun flowRefreshToken(): Flow<String> | ||
|
||
@SetValue(KEY_REFRESH_TOKEN) | ||
suspend fun setRefreshToken(value: String) | ||
|
||
@ClearValues | ||
suspend fun clearAll() | ||
|
||
companion object { | ||
private const val KEY_ACCESS_TOKEN = "key-access-token" | ||
private const val KEY_REFRESH_TOKEN = "key-refresh-token" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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