Skip to content

Commit

Permalink
feat: 첫 실행 datastore 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
easyhz committed Aug 12, 2024
1 parent be9dbe9 commit 0365749
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.easyhz.noffice.core.datastore.datasource.user

interface UserLocalDataSource {
suspend fun getFirstRun(): Result<Boolean>
suspend fun updateFirstRun(newValue: Boolean)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.easyhz.noffice.core.datastore.datasource.user

import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import com.easyhz.noffice.core.common.di.Dispatcher
import com.easyhz.noffice.core.common.di.NofficeDispatchers
import com.easyhz.noffice.core.datastore.di.UserDataStore
import com.easyhz.noffice.core.datastore.util.UserKey
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withContext
import javax.inject.Inject

class UserLocalDataSourceImpl @Inject constructor(
@Dispatcher(NofficeDispatchers.IO) private val dispatcher: CoroutineDispatcher,
@UserDataStore private val dataStore: DataStore<Preferences>
) : UserLocalDataSource {
private val isFirstRun = booleanPreferencesKey(UserKey.IS_FIRST_RUN.key)

override suspend fun getFirstRun(): Result<Boolean> = withContext(dispatcher) {
runCatching {
val preferences = dataStore.data.first()
return@runCatching preferences[isFirstRun]
?: throw generateNullException(UserKey.IS_FIRST_RUN)
}
}

override suspend fun updateFirstRun(newValue: Boolean): Unit = withContext(dispatcher) {
dataStore.edit { preferences ->
preferences[isFirstRun] = newValue
}
}

private fun generateNullException(userKey: UserKey): Exception {
return Exception("${userKey.key} is null")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.easyhz.noffice.core.datastore.di

import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSource
import com.easyhz.noffice.core.datastore.datasource.auth.AuthLocalDataSourceImpl
import com.easyhz.noffice.core.datastore.datasource.user.UserLocalDataSource
import com.easyhz.noffice.core.datastore.datasource.user.UserLocalDataSourceImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -14,4 +16,9 @@ interface DataSourceModule {
fun bindAuthLocalDataSource(
authLocalDataSourceImpl: AuthLocalDataSourceImpl
): AuthLocalDataSource

@Binds
fun bindUserLocalDataSource(
userLocalDataSourceImpl: UserLocalDataSourceImpl
): UserLocalDataSource
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import com.easyhz.noffice.core.datastore.util.authDataStore
import com.easyhz.noffice.core.datastore.util.userDataStore
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand All @@ -16,11 +17,20 @@ import javax.inject.Singleton
abstract class DataStoreModule {
companion object {

@AuthDataStore
@Singleton
@Provides
fun provideUserDataStorePreferences(
fun provideAuthDataStorePreferences(
@ApplicationContext context: Context
): DataStore<Preferences> =
context.authDataStore

@UserDataStore
@Singleton
@Provides
fun provideUserDataStorePreferences(
@ApplicationContext context: Context
): DataStore<Preferences> =
context.userDataStore
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ import androidx.datastore.preferences.preferencesDataStore

val Context.authDataStore: DataStore<Preferences> by preferencesDataStore(
name = "noffice-auth"
)

val Context.userDataStore: DataStore<Preferences> by preferencesDataStore(
name = "noffice-user"
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.easyhz.noffice.core.datastore.util

import androidx.compose.runtime.key

internal enum class AuthKey(
val key: String
) {
Expand All @@ -10,4 +12,12 @@ internal enum class AuthKey(
), AUTH_PROVIDER(
key = "PROVIDER"
)
}

internal enum class UserKey(
val key: String
) {
IS_FIRST_RUN(
key = "IS_FIRST_RUN"
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.easyhz.noffice.core.model.splash

enum class EnterScreenType {
ONBOARDING, LOGIN, HOME
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.easyhz.noffice.data.member.di.repository

import com.easyhz.noffice.data.member.repository.user.UserRepository
import com.easyhz.noffice.data.member.repository.user.UserRepositoryImpl
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Module
@InstallIn(SingletonComponent::class)
interface UserRepositoryModule {
@Binds
fun bindUserRepository(
userRepositoryImpl: UserRepositoryImpl
): UserRepository
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.easyhz.noffice.data.member.repository.user

interface UserRepository {
suspend fun getIsFirstRun(): Result<Boolean>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.easyhz.noffice.data.member.repository.user

import com.easyhz.noffice.core.datastore.datasource.user.UserLocalDataSource
import javax.inject.Inject

class UserRepositoryImpl @Inject constructor(
private val userLocalDataSource: UserLocalDataSource
): UserRepository {
override suspend fun getIsFirstRun(): Result<Boolean> {
return userLocalDataSource.getFirstRun()
}
}

0 comments on commit 0365749

Please sign in to comment.