-
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.
Merge pull request #6 from Nexters/feature/init-datastore
Feature/init datastore
- Loading branch information
Showing
5 changed files
with
102 additions
and
4 deletions.
There are no files selected for viewing
4 changes: 0 additions & 4 deletions
4
core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/DataStore.kt
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
...tore/src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSource.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,11 @@ | ||
package com.goalpanzi.mission_mate.core.datastore.datasource | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AuthDataSource { | ||
fun getAccessToken() : Flow<String?> | ||
fun getRefreshToken() : Flow<String?> | ||
|
||
fun setAccessToken(accessToken : String) : Flow<Unit> | ||
fun setRefreshToken(refreshToken : String) : Flow<Unit> | ||
} |
44 changes: 44 additions & 0 deletions
44
.../src/main/java/com/goalpanzi/mission_mate/core/datastore/datasource/AuthDataSourceImpl.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,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<Preferences> | ||
) : AuthDataSource { | ||
object PreferencesKey { | ||
val ACCESS_TOKEN = stringPreferencesKey("ACCESS_TOKEN") | ||
val REFRESH_TOKEN = stringPreferencesKey("REFRESH_TOKEN") | ||
|
||
} | ||
override fun getAccessToken(): Flow<String?> = | ||
dataStore.data.map { preferences -> | ||
preferences[PreferencesKey.ACCESS_TOKEN] | ||
} | ||
|
||
override fun getRefreshToken(): Flow<String?> = | ||
dataStore.data.map { preferences -> | ||
preferences[PreferencesKey.REFRESH_TOKEN] | ||
} | ||
|
||
override fun setAccessToken(accessToken: String): Flow<Unit> = flow { | ||
dataStore.edit { preferences -> | ||
preferences[PreferencesKey.ACCESS_TOKEN] = accessToken | ||
} | ||
emit(Unit) | ||
} | ||
|
||
override fun setRefreshToken(refreshToken: String): Flow<Unit> = flow { | ||
dataStore.edit { preferences -> | ||
preferences[PreferencesKey.REFRESH_TOKEN] = refreshToken | ||
} | ||
emit(Unit) | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
.../datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/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,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 | ||
} |
29 changes: 29 additions & 0 deletions
29
core/datastore/src/main/java/com/goalpanzi/mission_mate/core/datastore/di/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,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<Preferences> { | ||
return PreferenceDataStoreFactory.create( | ||
produceFile = { context.preferencesDataStoreFile(AUTH_PREFERENCES) } | ||
) | ||
} | ||
} |