Skip to content

Commit

Permalink
[FEAT]#30: DataStore 적용해요
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Jul 17, 2024
1 parent 05ffb5d commit ff6a453
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.bff.wespot.model.auth
package com.bff.wespot.model.auth.response

data class School(
val id: Int,
Expand Down
1 change: 1 addition & 0 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ dependencies {
implementation(project(":core:model"))
implementation(libs.junit)
implementation(libs.kakao.sdk)
implementation(libs.datastore)
}
8 changes: 8 additions & 0 deletions data/src/main/kotlin/com/bff/wespot/data/di/DataModule.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -24,4 +26,10 @@ abstract class DataModule {
abstract fun bindsKakaoLoginManager(
kakaoLoginManagerImpl: KakaoLoginManagerImpl
): KakaoLoginManager

@Binds
@Singleton
abstract fun bindsWeSpotDataStore(
weSpotDataStoreImpl: WeSpotDataStoreImpl
): WeSpotDataStore
}
10 changes: 10 additions & 0 deletions data/src/main/kotlin/com/bff/wespot/data/local/WeSpotDataStore.kt
Original file line number Diff line number Diff line change
@@ -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<String>
suspend fun saveBoolean(key: String, value: Boolean)
fun getBoolean(key: String): Flow<Boolean>
}
Original file line number Diff line number Diff line change
@@ -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<Preferences> 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<String> =
datastore.data.distinctUntilChanged()
.catch {e ->
if(e is IOException) {
emit(emptyPreferences())
} else {
throw e
}
}
.map {
it[stringPreferencesKey(key)] ?: ""
}

override fun getBoolean(key: String): Flow<Boolean> =
datastore.data.distinctUntilChanged()
.catch {e ->
if(e is IOException) {
emit(emptyPreferences())
} else {
throw e
}
}
.map {
it[booleanPreferencesKey(key)] ?: false
}
}
3 changes: 2 additions & 1 deletion domain/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
}

dependencies {
implementation(libs.java.inject)
implementation(project(":core:model"))

implementation(libs.java.inject)
}

0 comments on commit ff6a453

Please sign in to comment.