From ff6a4531dfb311b2bdebdf9d7b2f13c0753e845d Mon Sep 17 00:00:00 2001 From: flash159483 Date: Wed, 17 Jul 2024 20:33:40 +0900 Subject: [PATCH] =?UTF-8?q?[FEAT]#30:=20DataStore=20=EC=A0=81=EC=9A=A9?= =?UTF-8?q?=ED=95=B4=EC=9A=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/{ => request}/KakaoAuthToken.kt | 3 +- .../model/auth/{ => response}/School.kt | 2 +- data/build.gradle.kts | 1 + .../com/bff/wespot/data/di/DataModule.kt | 8 +++ .../bff/wespot/data/local/WeSpotDataStore.kt | 10 +++ .../wespot/data/local/WeSpotDataStoreImpl.kt | 70 +++++++++++++++++++ domain/build.gradle.kts | 3 +- 7 files changed, 94 insertions(+), 3 deletions(-) rename core/model/src/main/kotlin/com/bff/wespot/model/auth/{ => request}/KakaoAuthToken.kt (55%) rename core/model/src/main/kotlin/com/bff/wespot/model/auth/{ => response}/School.kt (71%) create mode 100644 data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStore.kt create mode 100644 data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStoreImpl.kt diff --git a/core/model/src/main/kotlin/com/bff/wespot/model/auth/KakaoAuthToken.kt b/core/model/src/main/kotlin/com/bff/wespot/model/auth/request/KakaoAuthToken.kt similarity index 55% rename from core/model/src/main/kotlin/com/bff/wespot/model/auth/KakaoAuthToken.kt rename to core/model/src/main/kotlin/com/bff/wespot/model/auth/request/KakaoAuthToken.kt index ae3d4f95..99d2abc0 100644 --- a/core/model/src/main/kotlin/com/bff/wespot/model/auth/KakaoAuthToken.kt +++ b/core/model/src/main/kotlin/com/bff/wespot/model/auth/request/KakaoAuthToken.kt @@ -1,6 +1,7 @@ -package com.bff.wespot.model.auth +package com.bff.wespot.model.auth.request data class KakaoAuthToken( val accessToken: String, val idToken: String?, + val socialType: String ) diff --git a/core/model/src/main/kotlin/com/bff/wespot/model/auth/School.kt b/core/model/src/main/kotlin/com/bff/wespot/model/auth/response/School.kt similarity index 71% rename from core/model/src/main/kotlin/com/bff/wespot/model/auth/School.kt rename to core/model/src/main/kotlin/com/bff/wespot/model/auth/response/School.kt index 8b7decf9..de8ee350 100644 --- a/core/model/src/main/kotlin/com/bff/wespot/model/auth/School.kt +++ b/core/model/src/main/kotlin/com/bff/wespot/model/auth/response/School.kt @@ -1,4 +1,4 @@ -package com.bff.wespot.model.auth +package com.bff.wespot.model.auth.response data class School( val id: Int, diff --git a/data/build.gradle.kts b/data/build.gradle.kts index b18b247b..24806748 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -13,4 +13,5 @@ dependencies { implementation(project(":core:model")) implementation(libs.junit) implementation(libs.kakao.sdk) + implementation(libs.datastore) } diff --git a/data/src/main/kotlin/com/bff/wespot/data/di/DataModule.kt b/data/src/main/kotlin/com/bff/wespot/data/di/DataModule.kt index 47142c14..d20f7032 100644 --- a/data/src/main/kotlin/com/bff/wespot/data/di/DataModule.kt +++ b/data/src/main/kotlin/com/bff/wespot/data/di/DataModule.kt @@ -1,5 +1,7 @@ package com.bff.wespot.data.di +import com.bff.wespot.data.local.WeSpotDataStore +import com.bff.wespot.data.local.WeSpotDataStoreImpl import com.bff.wespot.data.repository.auth.KakaoLoginManagerImpl import com.bff.wespot.domain.repository.message.MessageRepository import com.bff.wespot.data.repository.message.MessageRepositoryImpl @@ -24,4 +26,10 @@ abstract class DataModule { abstract fun bindsKakaoLoginManager( kakaoLoginManagerImpl: KakaoLoginManagerImpl ): KakaoLoginManager + + @Binds + @Singleton + abstract fun bindsWeSpotDataStore( + weSpotDataStoreImpl: WeSpotDataStoreImpl + ): WeSpotDataStore } diff --git a/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStore.kt b/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStore.kt new file mode 100644 index 00000000..1b82f9b4 --- /dev/null +++ b/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStore.kt @@ -0,0 +1,10 @@ +package com.bff.wespot.data.local + +import kotlinx.coroutines.flow.Flow + +interface WeSpotDataStore { + suspend fun saveString(key: String, value: String) + fun getString(key: String): Flow + suspend fun saveBoolean(key: String, value: Boolean) + fun getBoolean(key: String): Flow +} \ No newline at end of file diff --git a/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStoreImpl.kt b/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStoreImpl.kt new file mode 100644 index 00000000..544e886c --- /dev/null +++ b/data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStoreImpl.kt @@ -0,0 +1,70 @@ +package com.bff.wespot.data.local + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.core.IOException +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.booleanPreferencesKey +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.emptyPreferences +import androidx.datastore.preferences.core.stringPreferencesKey +import androidx.datastore.preferences.preferencesDataStore +import dagger.hilt.android.qualifiers.ApplicationContext +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.distinctUntilChanged +import kotlinx.coroutines.flow.map +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class WeSpotDataStoreImpl @Inject constructor( + @ApplicationContext private val context: Context +) : WeSpotDataStore { + private val Context.dataStore: DataStore by preferencesDataStore(name = "wespot") + private val datastore = context.dataStore + + override suspend fun saveString( + key: String, + value: String + ) { + datastore.edit { preferences -> + preferences[stringPreferencesKey(key)] = value + } + } + + override suspend fun saveBoolean( + key: String, + value: Boolean + ) { + datastore.edit { preferences -> + preferences[booleanPreferencesKey(key)] = value + } + } + + override fun getString(key: String): Flow = + datastore.data.distinctUntilChanged() + .catch {e -> + if(e is IOException) { + emit(emptyPreferences()) + } else { + throw e + } + } + .map { + it[stringPreferencesKey(key)] ?: "" + } + + override fun getBoolean(key: String): Flow = + datastore.data.distinctUntilChanged() + .catch {e -> + if(e is IOException) { + emit(emptyPreferences()) + } else { + throw e + } + } + .map { + it[booleanPreferencesKey(key)] ?: false + } +} \ No newline at end of file diff --git a/domain/build.gradle.kts b/domain/build.gradle.kts index c469765f..f0841e34 100644 --- a/domain/build.gradle.kts +++ b/domain/build.gradle.kts @@ -7,6 +7,7 @@ dependencies { } dependencies { - implementation(libs.java.inject) implementation(project(":core:model")) + + implementation(libs.java.inject) }