Skip to content

Commit 29f08a1

Browse files
committed
[BOOK-183] feat: 온보딩 DataStore 환경 구축
1 parent eec1a0e commit 29f08a1

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.ninecraft.booket.core.datastore.api.datasource
2+
3+
import kotlinx.coroutines.flow.Flow
4+
5+
interface OnboardingDataSource {
6+
val isOnboardingCompleted: Flow<Boolean>
7+
suspend fun setOnboardingCompleted(isCompleted: Boolean)
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.ninecraft.booket.core.datastore.impl.datasource
2+
3+
import androidx.datastore.core.DataStore
4+
import androidx.datastore.preferences.core.Preferences
5+
import androidx.datastore.preferences.core.booleanPreferencesKey
6+
import androidx.datastore.preferences.core.edit
7+
import com.ninecraft.booket.core.datastore.api.datasource.OnboardingDataSource
8+
import com.ninecraft.booket.core.datastore.impl.di.OnboardingDataStore
9+
import com.ninecraft.booket.core.datastore.impl.util.handleIOException
10+
import kotlinx.coroutines.flow.Flow
11+
import kotlinx.coroutines.flow.map
12+
import javax.inject.Inject
13+
14+
class DefaultOnboardingDataSource @Inject constructor(
15+
@OnboardingDataStore private val dataStore: DataStore<Preferences>,
16+
) : OnboardingDataSource {
17+
override val isOnboardingCompleted: Flow<Boolean> = dataStore.data
18+
.handleIOException()
19+
.map { prefs ->
20+
prefs[IS_ONBOARDING_COMPLETED] ?: false
21+
}
22+
23+
override suspend fun setOnboardingCompleted(isCompleted: Boolean) {
24+
dataStore.edit { prefs ->
25+
prefs[IS_ONBOARDING_COMPLETED] = isCompleted
26+
}
27+
}
28+
29+
companion object {
30+
private val IS_ONBOARDING_COMPLETED = booleanPreferencesKey("IS_ONBOARDING_COMPLETED")
31+
}
32+
}

core/datastore/impl/src/main/kotlin/com/ninecraft/booket/core/datastore/impl/di/DataStoreModule.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import android.content.Context
44
import androidx.datastore.core.DataStore
55
import androidx.datastore.preferences.core.Preferences
66
import androidx.datastore.preferences.preferencesDataStore
7+
import com.ninecraft.booket.core.datastore.api.datasource.OnboardingDataSource
78
import com.ninecraft.booket.core.datastore.api.datasource.RecentSearchDataSource
89
import com.ninecraft.booket.core.datastore.api.datasource.TokenDataSource
10+
import com.ninecraft.booket.core.datastore.impl.datasource.DefaultOnboardingDataSource
911
import com.ninecraft.booket.core.datastore.impl.datasource.DefaultRecentSearchDataSource
1012
import com.ninecraft.booket.core.datastore.impl.datasource.DefaultTokenDataSource
1113
import dagger.Binds
@@ -21,9 +23,11 @@ import javax.inject.Singleton
2123
object DataStoreModule {
2224
private const val TOKEN_DATASTORE_NAME = "TOKENS_DATASTORE"
2325
private const val RECENT_SEARCH_DATASTORE_NAME = "RECENT_SEARCH_DATASTORE"
26+
private const val ONBOARDING_DATASTORE_NAME = "ONBOARDING_DATASTORE"
2427

2528
private val Context.tokenDataStore by preferencesDataStore(name = TOKEN_DATASTORE_NAME)
2629
private val Context.recentSearchDataStore by preferencesDataStore(name = RECENT_SEARCH_DATASTORE_NAME)
30+
private val Context.onboardingDataStore by preferencesDataStore(name = ONBOARDING_DATASTORE_NAME)
2731

2832
@TokenDataStore
2933
@Provides
@@ -38,6 +42,13 @@ object DataStoreModule {
3842
fun provideRecentSearchDataStore(
3943
@ApplicationContext context: Context,
4044
): DataStore<Preferences> = context.recentSearchDataStore
45+
46+
@OnboardingDataStore
47+
@Provides
48+
@Singleton
49+
fun provideOnboardingDataStore(
50+
@ApplicationContext context: Context,
51+
): DataStore<Preferences> = context.onboardingDataStore
4152
}
4253

4354
@Module
@@ -55,4 +66,10 @@ abstract class DataStoreBindModule {
5566
abstract fun bindRecentSearchDataSource(
5667
defaultRecentSearchDataSource: DefaultRecentSearchDataSource,
5768
): RecentSearchDataSource
69+
70+
@Binds
71+
@Singleton
72+
abstract fun bindOnboardingDataSource(
73+
defaultOnboardingDataSource: DefaultOnboardingDataSource,
74+
): OnboardingDataSource
5875
}

core/datastore/impl/src/main/kotlin/com/ninecraft/booket/core/datastore/impl/di/DataStoreQualifier.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ annotation class TokenDataStore
99
@Qualifier
1010
@Retention(AnnotationRetention.BINARY)
1111
annotation class RecentSearchDataStore
12+
13+
@Qualifier
14+
@Retention(AnnotationRetention.BINARY)
15+
annotation class OnboardingDataStore

0 commit comments

Comments
 (0)