From 19fabf4e9cd33944b65e346fdc0eb7fa36a77d5c Mon Sep 17 00:00:00 2001 From: Jung SeokJoon Date: Sat, 27 Jul 2024 19:27:57 +0900 Subject: [PATCH 1/2] add AuthDataSource --- .../mission_mate/core/datastore/DataStore.kt | 4 -- .../datastore/datasource/AuthDataSource.kt | 11 +++++ .../datasource/AuthDataSourceImpl.kt | 44 +++++++++++++++++++ 3 files changed, 55 insertions(+), 4 deletions(-) delete mode 100644 core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/DataStore.kt create mode 100644 core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSource.kt create mode 100644 core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSourceImpl.kt diff --git a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/DataStore.kt b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/DataStore.kt deleted file mode 100644 index f688f9d7..00000000 --- a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/DataStore.kt +++ /dev/null @@ -1,4 +0,0 @@ -package com.goalpanzi.mission_mate.core.datastore - -class DataStore { -} \ No newline at end of file diff --git a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSource.kt b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSource.kt new file mode 100644 index 00000000..0b1054ff --- /dev/null +++ b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSource.kt @@ -0,0 +1,11 @@ +package com.goalpanzi.mission_mate.core.datastore.datasource + +import kotlinx.coroutines.flow.Flow + +interface AuthDataSource { + fun getAccessToken() : Flow + fun getRefreshToken() : Flow + + fun setAccessToken(accessToken : String) : Flow + fun setRefreshToken(refreshToken : String) : Flow +} \ No newline at end of file diff --git a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSourceImpl.kt b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSourceImpl.kt new file mode 100644 index 00000000..ddb789b5 --- /dev/null +++ b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSourceImpl.kt @@ -0,0 +1,44 @@ +package com.goalpanzi.mission_mate.core.datastore.datasource + +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.stringPreferencesKey +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.map +import javax.inject.Inject + +class AuthDataSourceImpl @Inject constructor( + private val dataStore: DataStore +) : AuthDataSource { + object PreferencesKey { + val ACCESS_TOKEN = stringPreferencesKey("ACCESS_TOKEN") + val REFRESH_TOKEN = stringPreferencesKey("REFRESH_TOKEN") + + } + override fun getAccessToken(): Flow = + dataStore.data.map { preferences -> + preferences[PreferencesKey.ACCESS_TOKEN] + } + + override fun getRefreshToken(): Flow = + dataStore.data.map { preferences -> + preferences[PreferencesKey.REFRESH_TOKEN] + } + + override fun setAccessToken(accessToken: String): Flow = flow { + dataStore.edit { preferences -> + preferences[PreferencesKey.ACCESS_TOKEN] = accessToken + } + emit(Unit) + } + + override fun setRefreshToken(refreshToken: String): Flow = flow { + dataStore.edit { preferences -> + preferences[PreferencesKey.REFRESH_TOKEN] = refreshToken + } + emit(Unit) + } + +} \ No newline at end of file From 63a41a8f814b9cee9ef916686cd3b5a6d77d360c Mon Sep 17 00:00:00 2001 From: Jung SeokJoon Date: Sat, 27 Jul 2024 19:28:09 +0900 Subject: [PATCH 2/2] add DataStoreModule & DataSourceModule --- .../core/datastore/di/DataSourceModule.kt | 18 ++++++++++++ .../core/datastore/di/DataStoreModule.kt | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataSourceModule.kt create mode 100644 core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataStoreModule.kt diff --git a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataSourceModule.kt b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataSourceModule.kt new file mode 100644 index 00000000..9b2b5f66 --- /dev/null +++ b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataSourceModule.kt @@ -0,0 +1,18 @@ +package com.goalpanzi.mission_mate.core.datastore.di + +import com.goalpanzi.mission_mate.core.datastore.datasource.AuthDataSource +import com.goalpanzi.mission_mate.core.datastore.datasource.AuthDataSourceImpl +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent + +@Module +@InstallIn(SingletonComponent::class) +abstract class DataSourceModule { + + @Binds + abstract fun bindAuthDataSource( + authDataSource: AuthDataSourceImpl + ): AuthDataSource +} \ No newline at end of file diff --git a/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataStoreModule.kt b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataStoreModule.kt new file mode 100644 index 00000000..c879f392 --- /dev/null +++ b/core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/DataStoreModule.kt @@ -0,0 +1,29 @@ +package com.goalpanzi.mission_mate.core.datastore.di + +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 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 AUTH_PREFERENCES = "auth_preferences" + +@InstallIn(SingletonComponent::class) +@Module +object DataStoreModule { + @Singleton + @Provides + fun provideAuthPreferencesDataStore( + @ApplicationContext context: Context + ): DataStore { + return PreferenceDataStoreFactory.create( + produceFile = { context.preferencesDataStoreFile(AUTH_PREFERENCES) } + ) + } +} \ No newline at end of file