Skip to content

Commit

Permalink
Merge pull request #6 from Nexters/feature/init-datastore
Browse files Browse the repository at this point in the history
Feature/init datastore
  • Loading branch information
eshc123 authored Jul 27, 2024
2 parents 0177fb3 + 63a41a8 commit fd3ee77
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 4 deletions.

This file was deleted.

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>
}
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)
}

}
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
}
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) }
)
}
}

0 comments on commit fd3ee77

Please sign in to comment.