diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSource.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSource.kt new file mode 100644 index 00000000..1bbc8db8 --- /dev/null +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSource.kt @@ -0,0 +1,6 @@ +package com.easyhz.noffice.core.datastore.datasource.user + +interface UserLocalDataSource { + suspend fun getFirstRun(): Result + suspend fun updateFirstRun(newValue: Boolean) +} \ No newline at end of file diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSourceImpl.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSourceImpl.kt new file mode 100644 index 00000000..42e35e27 --- /dev/null +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/datasource/user/UserLocalDataSourceImpl.kt @@ -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 +) : UserLocalDataSource { + private val isFirstRun = booleanPreferencesKey(UserKey.IS_FIRST_RUN.key) + + override suspend fun getFirstRun(): Result = 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") + } +} \ No newline at end of file diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataSourceModule.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataSourceModule.kt index ea0579a5..9a3eb633 100644 --- a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataSourceModule.kt +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataSourceModule.kt @@ -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 @@ -14,4 +16,9 @@ interface DataSourceModule { fun bindAuthLocalDataSource( authLocalDataSourceImpl: AuthLocalDataSourceImpl ): AuthLocalDataSource + + @Binds + fun bindUserLocalDataSource( + userLocalDataSourceImpl: UserLocalDataSourceImpl + ): UserLocalDataSource } \ No newline at end of file diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataStoreModule.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataStoreModule.kt index 07872557..930b3b24 100644 --- a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataStoreModule.kt +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/di/DataStoreModule.kt @@ -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 @@ -16,11 +17,20 @@ import javax.inject.Singleton abstract class DataStoreModule { companion object { + @AuthDataStore @Singleton @Provides - fun provideUserDataStorePreferences( + fun provideAuthDataStorePreferences( @ApplicationContext context: Context ): DataStore = context.authDataStore + + @UserDataStore + @Singleton + @Provides + fun provideUserDataStorePreferences( + @ApplicationContext context: Context + ): DataStore = + context.userDataStore } } \ No newline at end of file diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreConfig.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreConfig.kt index 6e1697d3..49252a51 100644 --- a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreConfig.kt +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreConfig.kt @@ -7,4 +7,8 @@ import androidx.datastore.preferences.preferencesDataStore val Context.authDataStore: DataStore by preferencesDataStore( name = "noffice-auth" +) + +val Context.userDataStore: DataStore by preferencesDataStore( + name = "noffice-user" ) \ No newline at end of file diff --git a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreKey.kt b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreKey.kt index 8deb2a28..db797add 100644 --- a/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreKey.kt +++ b/core/datastore/src/main/java/com/easyhz/noffice/core/datastore/util/DataStoreKey.kt @@ -1,5 +1,7 @@ package com.easyhz.noffice.core.datastore.util +import androidx.compose.runtime.key + internal enum class AuthKey( val key: String ) { @@ -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" + ) } \ No newline at end of file diff --git a/core/model/src/main/java/com/easyhz/noffice/core/model/splash/EnterScreenType.kt b/core/model/src/main/java/com/easyhz/noffice/core/model/splash/EnterScreenType.kt new file mode 100644 index 00000000..ef9280b6 --- /dev/null +++ b/core/model/src/main/java/com/easyhz/noffice/core/model/splash/EnterScreenType.kt @@ -0,0 +1,5 @@ +package com.easyhz.noffice.core.model.splash + +enum class EnterScreenType { + ONBOARDING, LOGIN, HOME +} \ No newline at end of file diff --git a/data/member/src/main/java/com/easyhz/noffice/data/member/di/repository/UserRepositoryModule.kt b/data/member/src/main/java/com/easyhz/noffice/data/member/di/repository/UserRepositoryModule.kt new file mode 100644 index 00000000..36eb2ec4 --- /dev/null +++ b/data/member/src/main/java/com/easyhz/noffice/data/member/di/repository/UserRepositoryModule.kt @@ -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 +} \ No newline at end of file diff --git a/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepository.kt b/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepository.kt new file mode 100644 index 00000000..c4e5b6a7 --- /dev/null +++ b/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepository.kt @@ -0,0 +1,5 @@ +package com.easyhz.noffice.data.member.repository.user + +interface UserRepository { + suspend fun getIsFirstRun(): Result +} \ No newline at end of file diff --git a/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepositoryImpl.kt b/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepositoryImpl.kt new file mode 100644 index 00000000..e25bf833 --- /dev/null +++ b/data/member/src/main/java/com/easyhz/noffice/data/member/repository/user/UserRepositoryImpl.kt @@ -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 { + return userLocalDataSource.getFirstRun() + } +} \ No newline at end of file