diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4f4cd357..4f52ec7a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -93,4 +93,7 @@ dependencies { // Serialization implementation(libs.serialization) implementation(libs.kotlin.serilization) + + //DataStore + implementation(libs.data.store) } diff --git a/app/src/main/java/com/everymeal/everymeal_android/di/DataBaseModule.kt b/app/src/main/java/com/everymeal/everymeal_android/di/DataBaseModule.kt new file mode 100644 index 00000000..e0d86dd5 --- /dev/null +++ b/app/src/main/java/com/everymeal/everymeal_android/di/DataBaseModule.kt @@ -0,0 +1,27 @@ +package com.everymeal.everymeal_android.di + +import android.content.Context +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.PreferenceDataStoreFactory +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.preferencesDataStoreFile +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +object DataBaseModule { + + private const val DATASTORE_NAME = "everymeal_datastore" + + @Singleton + @Provides + fun providePreferencesDataStore(@ApplicationContext context: Context): DataStore = + PreferenceDataStoreFactory.create( + produceFile = { context.preferencesDataStoreFile(DATASTORE_NAME) } + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/everymeal/everymeal_android/di/NetworkModule.kt b/app/src/main/java/com/everymeal/everymeal_android/di/NetworkModule.kt index 20b421f3..efaacb05 100644 --- a/app/src/main/java/com/everymeal/everymeal_android/di/NetworkModule.kt +++ b/app/src/main/java/com/everymeal/everymeal_android/di/NetworkModule.kt @@ -53,4 +53,4 @@ object NetworkModule { fun provideAuthApi(retrofit: Retrofit): AuthApi { return retrofit.create(AuthApi::class.java) } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/everymeal/everymeal_android/di/RepositoryModule.kt b/app/src/main/java/com/everymeal/everymeal_android/di/RepositoryModule.kt index 6c98f071..1b437a64 100644 --- a/app/src/main/java/com/everymeal/everymeal_android/di/RepositoryModule.kt +++ b/app/src/main/java/com/everymeal/everymeal_android/di/RepositoryModule.kt @@ -2,10 +2,14 @@ package com.everymeal.everymeal_android.di import com.everymeal.data.datasource.auth.AuthRemoteDataSource import com.everymeal.data.datasource.auth.AuthRemoteRemoteDataSourceImpl +import com.everymeal.data.datasource.local.LocalDataSource +import com.everymeal.data.datasource.local.LocalDataSourceImpl import com.everymeal.data.datasource.onboarding.OnboardingDataSource import com.everymeal.data.datasource.onboarding.OnboardingDataSourceImpl +import com.everymeal.data.repository.local.LocalRepositoryImpl import com.everymeal.data.repository.DefaultAuthRepository import com.everymeal.data.repository.onboarding.OnboardingRepositoryImpl +import com.everymeal.domain.repository.local.LocalRepository import com.everymeal.domain.repository.auth.AuthRepository import com.everymeal.domain.repository.onboarding.OnboardingRepository import dagger.Binds @@ -30,6 +34,18 @@ abstract class RepositoryModule { onboardingDataSourceImpl: OnboardingDataSourceImpl ): OnboardingDataSource + @Singleton + @Binds + abstract fun bindLocalRepository( + localRepositoryImpl: LocalRepositoryImpl + ): LocalRepository + + @Singleton + @Binds + abstract fun bindLocalDataSource( + localDataSourceImpl: LocalDataSourceImpl + ): LocalDataSource + @Singleton @Binds abstract fun bindAuthRemoteDataSource( diff --git a/data/build.gradle.kts b/data/build.gradle.kts index d4c5b0ba..437b388b 100644 --- a/data/build.gradle.kts +++ b/data/build.gradle.kts @@ -41,4 +41,7 @@ dependencies { // Serialization implementation(libs.serialization) implementation(libs.kotlin.serilization) + + //DataStore + implementation(libs.data.store) } \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSource.kt b/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSource.kt new file mode 100644 index 00000000..39fe60a4 --- /dev/null +++ b/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSource.kt @@ -0,0 +1,9 @@ +package com.everymeal.data.datasource.local + +import kotlinx.coroutines.flow.Flow + +interface LocalDataSource { + suspend fun saveUniversity(index : Int, univName : String) + + suspend fun getUniversityIndex() : Flow +} \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSourceImpl.kt b/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSourceImpl.kt new file mode 100644 index 00000000..18765d9f --- /dev/null +++ b/data/src/main/java/com/everymeal/data/datasource/local/LocalDataSourceImpl.kt @@ -0,0 +1,43 @@ +package com.everymeal.data.datasource.local + +import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.Preferences +import androidx.datastore.preferences.core.edit +import androidx.datastore.preferences.core.emptyPreferences +import androidx.datastore.preferences.core.stringPreferencesKey +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.map +import java.io.IOException +import javax.inject.Inject + +object DataStoreKey { + val UNIVERSITY_INDEX = stringPreferencesKey("univ_index") + val UNIVERSITY_NAME = stringPreferencesKey("univ_name") +} + +class LocalDataSourceImpl @Inject constructor( + private val dataStore: DataStore +) : LocalDataSource { + override suspend fun saveUniversity(index: Int, univName: String) { + dataStore.edit { + it[DataStoreKey.UNIVERSITY_INDEX] = index.toString() + it[DataStoreKey.UNIVERSITY_NAME] = univName + } + } + + override suspend fun getUniversityIndex(): Flow { + return dataStore.data + .catch { exception -> + if (exception is IOException) { + exception.printStackTrace() + emit(emptyPreferences()) + } else { + throw exception + } + } + .map { prefs -> + prefs[DataStoreKey.UNIVERSITY_INDEX].orEmpty() + } + } +} \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSource.kt b/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSource.kt index f4978af4..976de013 100644 --- a/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSource.kt +++ b/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSource.kt @@ -1,7 +1,7 @@ package com.everymeal.data.datasource.onboarding -import com.everymeal.data.model.onboarding.UniversityData +import com.everymeal.data.model.onboarding.UniversityResponse interface OnboardingDataSource { - suspend fun getUniversity(): Result> + suspend fun getUniversity(): Result> } \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSourceImpl.kt b/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSourceImpl.kt index 2f447e78..b4f1b700 100644 --- a/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSourceImpl.kt +++ b/data/src/main/java/com/everymeal/data/datasource/onboarding/OnboardingDataSourceImpl.kt @@ -1,6 +1,6 @@ package com.everymeal.data.datasource.onboarding -import com.everymeal.data.model.onboarding.UniversityData +import com.everymeal.data.model.onboarding.UniversityResponse import com.everymeal.data.model.unwrapData import com.everymeal.data.service.onboarding.OnboardingApi import javax.inject.Inject @@ -9,7 +9,7 @@ class OnboardingDataSourceImpl @Inject constructor( private val onboardingApi: OnboardingApi ) : OnboardingDataSource { - override suspend fun getUniversity(): Result> { + override suspend fun getUniversity(): Result> { return runCatching { onboardingApi.getUniversity() }.unwrapData() } } \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt b/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityResponse.kt similarity index 86% rename from data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt rename to data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityResponse.kt index 86e6779e..7a907087 100644 --- a/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityData.kt +++ b/data/src/main/java/com/everymeal/data/model/onboarding/GetUniversityResponse.kt @@ -4,14 +4,14 @@ import com.everymeal.domain.model.onboarding.GetUniversityEntity import kotlinx.serialization.Serializable @Serializable -data class UniversityData( +data class UniversityResponse( val idx: Int, val universityName: String, val campusName: String, val universityShortName: String ) -fun List.toUniversityEntity(): GetUniversityEntity { +fun List.toUniversityEntity(): GetUniversityEntity { val universityDataList = this.map { result -> GetUniversityEntity.UniversityData( idx = result.idx, diff --git a/data/src/main/java/com/everymeal/data/repository/local/LocalRepositoryImpl.kt b/data/src/main/java/com/everymeal/data/repository/local/LocalRepositoryImpl.kt new file mode 100644 index 00000000..6692edf1 --- /dev/null +++ b/data/src/main/java/com/everymeal/data/repository/local/LocalRepositoryImpl.kt @@ -0,0 +1,19 @@ +package com.everymeal.data.repository.local + +import com.everymeal.data.datasource.local.LocalDataSource +import com.everymeal.domain.repository.local.LocalRepository +import kotlinx.coroutines.flow.Flow +import javax.inject.Inject + +class LocalRepositoryImpl @Inject constructor( + private val localDataSource: LocalDataSource +) : LocalRepository { + + override suspend fun saveUniversity(index: Int, univName: String) { + localDataSource.saveUniversity(index, univName) + } + + override suspend fun getUniversityIndex(): Flow { + return localDataSource.getUniversityIndex() + } +} \ No newline at end of file diff --git a/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt b/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt index e76d1a0a..794d1762 100644 --- a/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt +++ b/data/src/main/java/com/everymeal/data/service/onboarding/OnboardingApi.kt @@ -1,11 +1,11 @@ package com.everymeal.data.service.onboarding import com.everymeal.data.model.BaseResponse -import com.everymeal.data.model.onboarding.UniversityData +import com.everymeal.data.model.onboarding.UniversityResponse import retrofit2.http.GET interface OnboardingApi { @GET("/api/v1/universities") - suspend fun getUniversity(): BaseResponse> + suspend fun getUniversity(): BaseResponse> } \ No newline at end of file diff --git a/domain/build.gradle.kts b/domain/build.gradle.kts index 7b184f13..4403f376 100644 --- a/domain/build.gradle.kts +++ b/domain/build.gradle.kts @@ -11,4 +11,7 @@ java { dependencies { // Hilt implementation(libs.hilt.core) + + // Coroutines + implementation(libs.kotlin.coroutines) } diff --git a/domain/src/main/java/com/everymeal/domain/repository/local/LocalRepository.kt b/domain/src/main/java/com/everymeal/domain/repository/local/LocalRepository.kt new file mode 100644 index 00000000..902b6bee --- /dev/null +++ b/domain/src/main/java/com/everymeal/domain/repository/local/LocalRepository.kt @@ -0,0 +1,9 @@ +package com.everymeal.domain.repository.local + +import kotlinx.coroutines.flow.Flow + +interface LocalRepository { + suspend fun saveUniversity(index : Int, univName : String) + + suspend fun getUniversityIndex() : Flow +} \ No newline at end of file diff --git a/domain/src/main/java/com/everymeal/domain/usecase/local/GetUniversityIndexUseCase.kt b/domain/src/main/java/com/everymeal/domain/usecase/local/GetUniversityIndexUseCase.kt new file mode 100644 index 00000000..c196f11a --- /dev/null +++ b/domain/src/main/java/com/everymeal/domain/usecase/local/GetUniversityIndexUseCase.kt @@ -0,0 +1,13 @@ +package com.everymeal.domain.usecase.local + +import com.everymeal.domain.repository.local.LocalRepository +import kotlinx.coroutines.flow.Flow +import javax.inject.Inject + +class GetUniversityIndexUseCase @Inject constructor( + private val localRepository: LocalRepository +) { + suspend operator fun invoke() : Flow { + return localRepository.getUniversityIndex() + } +} \ No newline at end of file diff --git a/domain/src/main/java/com/everymeal/domain/usecase/local/SaveUniversityUseCase.kt b/domain/src/main/java/com/everymeal/domain/usecase/local/SaveUniversityUseCase.kt new file mode 100644 index 00000000..f9addb10 --- /dev/null +++ b/domain/src/main/java/com/everymeal/domain/usecase/local/SaveUniversityUseCase.kt @@ -0,0 +1,16 @@ +package com.everymeal.domain.usecase.local + +import com.everymeal.domain.repository.local.LocalRepository +import com.everymeal.domain.repository.onboarding.OnboardingRepository +import javax.inject.Inject + +class SaveUniversityUseCase @Inject constructor( + private val localRepository: LocalRepository +) { + suspend operator fun invoke( + index : Int, + univName : String + ) { + localRepository.saveUniversity(index, univName) + } +} \ No newline at end of file diff --git a/domain/src/main/java/com/everymeal/domain/usecase/onboarding/GetUniversityUseCase.kt b/domain/src/main/java/com/everymeal/domain/usecase/onboarding/GetUniversityUseCase.kt index 66bed9f5..73c91b88 100644 --- a/domain/src/main/java/com/everymeal/domain/usecase/onboarding/GetUniversityUseCase.kt +++ b/domain/src/main/java/com/everymeal/domain/usecase/onboarding/GetUniversityUseCase.kt @@ -1,10 +1,13 @@ package com.everymeal.domain.usecase.onboarding +import com.everymeal.domain.model.onboarding.GetUniversityEntity import com.everymeal.domain.repository.onboarding.OnboardingRepository import javax.inject.Inject class GetUniversityUseCase @Inject constructor( private val onboardingRepository: OnboardingRepository ) { - suspend operator fun invoke() = onboardingRepository.getUniversity() + suspend operator fun invoke() :Result { + return onboardingRepository.getUniversity() + } } \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1b96bf8c..7bbae4f2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -19,6 +19,9 @@ lottie = "6.1.0" compose-navigation = "2.7.4" accompanist = "0.33.0-alpha" +data-store = "1.0.0" +kotlin-coroutines = "1.7.3" + [libraries] agp = { module = "com.android.tools.build:gradle", version.ref = "agp" } coil-compose = { module = "io.coil-kt:coil-compose", version.ref = "coil-compose" } @@ -69,6 +72,11 @@ compose-lottie = { module = "com.airbnb.android:lottie-compose", version.ref = " #Compose-Navigation compose-navigation = { module = "androidx.navigation:navigation-compose", version.ref = "compose-navigation" } +#data-store +data-store = { module = "androidx.datastore:datastore-preferences", version.ref = "data-store" } + +#kotlin-coroutines +kotlin-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines" } [plugins] androidApplication = { id = "com.android.application", version.ref = "agp" } diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/onboarding/OnboardingScreen.kt b/presentation/src/main/java/com/everymeal/presentation/ui/onboarding/OnboardingScreen.kt index 6ad154e7..ee191fba 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/onboarding/OnboardingScreen.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/onboarding/OnboardingScreen.kt @@ -121,7 +121,7 @@ fun OnboardingScreen( } Spacer(modifier = Modifier.size(30.dp)) EveryMealMainButton( - text = stringResource(id = R.string.next), + text = stringResource(id = R.string.start), onClick = onNavigateToUnivSelect ) } diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectContract.kt b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectContract.kt index a0fc1109..15639cdf 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectContract.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectContract.kt @@ -16,7 +16,10 @@ class UnivSelectContract { val univSelectLoadState: LoadState = LoadState.LOADING, val selectedUniv: String = "", val universities: List = emptyList(), - val networkErrorDialog: Boolean = true + val networkErrorDialog: Boolean = true, + val univIdx: Int = 0, + val univSelectFullName: String = "", + val campusName: String = "" ) : ViewState /* @@ -25,9 +28,16 @@ class UnivSelectContract { */ sealed class UnivSelectEvent : ViewEvent { object InitUnivSelectScreen : UnivSelectEvent() - object SelectButtonClicked : UnivSelectEvent() + data class SelectButtonClicked( + val univIdx: Int, + val univSelectFullName: String, + val campusName: String + ) : UnivSelectEvent() data class SelectedUniv( - val selectedUniv: String + val selectedUniv: String, + val univIdx: Int, + val univSelectFullName: String, + val campusName: String ) : UnivSelectEvent() data class NetworkErrorDialogClicked( val dialogStateChange: Boolean diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectScreen.kt b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectScreen.kt index 52885016..c4f6b04a 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectScreen.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectScreen.kt @@ -120,7 +120,10 @@ fun UnivSelectScreen( index = index ) { viewModel.setEvent(UnivSelectContract.UnivSelectEvent.SelectedUniv( - "${item.universityShortName}+${item.campusName}") + selectedUniv = "${item.universityShortName}+${item.campusName}", + univIdx = item.idx, + univSelectFullName = item.universityName, + campusName = item.campusName) ) } } @@ -161,7 +164,11 @@ fun UnivSelectScreen( text = stringResource(R.string.select), enabled = viewState.selectedUniv.isNotEmpty(), ) { - viewModel.setEvent(UnivSelectContract.UnivSelectEvent.SelectButtonClicked) + viewModel.setEvent(UnivSelectContract.UnivSelectEvent.SelectButtonClicked( + univIdx = viewState.univIdx, + univSelectFullName = viewState.univSelectFullName, + campusName = viewState.campusName + )) } } } diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectViewModel.kt b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectViewModel.kt index f4480720..35ddc2c9 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectViewModel.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/signup/UnivSelectViewModel.kt @@ -1,7 +1,7 @@ package com.everymeal.presentation.ui.signup -import android.util.Log import androidx.lifecycle.viewModelScope +import com.everymeal.domain.usecase.local.SaveUniversityUseCase import com.everymeal.domain.usecase.onboarding.GetUniversityUseCase import com.everymeal.presentation.base.BaseViewModel import com.everymeal.presentation.base.LoadState @@ -9,13 +9,13 @@ import com.everymeal.presentation.ui.signup.UnivSelectContract.UnivSelectEffect import com.everymeal.presentation.ui.signup.UnivSelectContract.UnivSelectEvent import com.everymeal.presentation.ui.signup.UnivSelectContract.UnivSelectState import dagger.hilt.android.lifecycle.HiltViewModel -import kotlinx.coroutines.delay import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel class UnivSelectViewModel @Inject constructor( - private val getUniversityUseCase: GetUniversityUseCase + private val getUniversityUseCase: GetUniversityUseCase, + private val saveUniversityUseCase: SaveUniversityUseCase ) : BaseViewModel( UnivSelectState() ) { @@ -28,13 +28,17 @@ class UnivSelectViewModel @Inject constructor( } is UnivSelectEvent.SelectButtonClicked -> { + saveUniversity(event.univIdx, event.univSelectFullName, event.campusName) sendEffect({ UnivSelectEffect.MoveToMain }) } is UnivSelectEvent.SelectedUniv -> { updateState { copy( - selectedUniv = event.selectedUniv + selectedUniv = event.selectedUniv, + univIdx = event.univIdx, + univSelectFullName = event.univSelectFullName, + campusName = event.campusName ) } } @@ -67,4 +71,10 @@ class UnivSelectViewModel @Inject constructor( } } } + + private fun saveUniversity(index: Int, univName: String, campusName: String) { + viewModelScope.launch { + saveUniversityUseCase(index, univName+campusName) + } + } } \ No newline at end of file diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashActivity.kt b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashActivity.kt index 19724ea5..fa326113 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashActivity.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashActivity.kt @@ -4,14 +4,18 @@ import android.annotation.SuppressLint import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent +import androidx.activity.viewModels +import com.everymeal.presentation.ui.main.MainActivity import com.everymeal.presentation.ui.onboarding.OnboardingActivity import com.everymeal.presentation.ui.theme.EveryMeal_AndroidTheme -import com.everymeal.presentation.ui.signup.UnivSelectActivity import dagger.hilt.android.AndroidEntryPoint @SuppressLint("CustomSplashScreen") @AndroidEntryPoint class SplashActivity : ComponentActivity() { + + private val viewModel: SplashViewModel by viewModels() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -22,8 +26,13 @@ class SplashActivity : ComponentActivity() { setContent { EveryMeal_AndroidTheme { SplashScreen(onFinishSplash = { - OnboardingActivity.startActivity(this) - finish() + if(viewModel.viewState.value.selectedUnitIndex.isNullOrBlank()) { + OnboardingActivity.startActivity(this) + finish() + } else { + MainActivity.startActivity(this) + finish() + } }) } } diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashContract.kt b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashContract.kt new file mode 100644 index 00000000..8a321fdf --- /dev/null +++ b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashContract.kt @@ -0,0 +1,19 @@ +package com.everymeal.presentation.ui.splash + +import com.everymeal.presentation.base.LoadState +import com.everymeal.presentation.base.ViewEvent +import com.everymeal.presentation.base.ViewSideEffect +import com.everymeal.presentation.base.ViewState + +class SplashContract { + data class SplashViewState( + val loginState: LoadState = LoadState.SUCCESS, + val selectedUnitIndex: String? = null, + ) : ViewState + + sealed class SplashSideEffect : ViewSideEffect + + sealed class SplashEvent : ViewEvent { + object InitSplashScreen: SplashEvent() + } +} diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashScreen.kt b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashScreen.kt index 8d001f41..1b22db67 100644 --- a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashScreen.kt +++ b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashScreen.kt @@ -6,11 +6,13 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp +import androidx.hilt.navigation.compose.hiltViewModel import com.airbnb.lottie.compose.LottieAnimation import com.airbnb.lottie.compose.LottieCompositionSpec import com.airbnb.lottie.compose.animateLottieCompositionAsState @@ -20,11 +22,16 @@ import com.everymeal.presentation.ui.theme.EveryMeal_AndroidTheme @SuppressLint("CoroutineCreationDuringComposition") @Composable fun SplashScreen( + viewModel : SplashViewModel = hiltViewModel(), onFinishSplash: () -> Unit, ) { val composition by rememberLottieComposition(LottieCompositionSpec.Asset("everymeal_splash.json")) val progress by animateLottieCompositionAsState(composition) + LaunchedEffect(Unit) { + viewModel.setEvent(SplashContract.SplashEvent.InitSplashScreen) + } + Column( modifier = Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, diff --git a/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashViewModel.kt b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashViewModel.kt new file mode 100644 index 00000000..9735375f --- /dev/null +++ b/presentation/src/main/java/com/everymeal/presentation/ui/splash/SplashViewModel.kt @@ -0,0 +1,40 @@ +package com.everymeal.presentation.ui.splash + +import androidx.lifecycle.viewModelScope +import com.everymeal.domain.usecase.local.GetUniversityIndexUseCase +import com.everymeal.presentation.base.BaseViewModel +import com.everymeal.presentation.base.LoadState +import com.everymeal.presentation.ui.splash.SplashContract.SplashEvent +import com.everymeal.presentation.ui.splash.SplashContract.SplashSideEffect +import com.everymeal.presentation.ui.splash.SplashContract.SplashViewState +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class SplashViewModel @Inject constructor( + private val getUniversityIndexUseCase: GetUniversityIndexUseCase +) : BaseViewModel( + SplashViewState() +) { + + override fun handleEvents(event: SplashEvent) { + when (event) { + is SplashEvent.InitSplashScreen -> { + getUniversityIndex() + } + } + } + + private fun getUniversityIndex() { + viewModelScope.launch { + val univIndex = getUniversityIndexUseCase().first() + updateState { + copy( + selectedUnitIndex = univIndex, + ) + } + } + } +} diff --git a/presentation/src/main/res/values/strings.xml b/presentation/src/main/res/values/strings.xml index cb764820..1722548d 100644 --- a/presentation/src/main/res/values/strings.xml +++ b/presentation/src/main/res/values/strings.xml @@ -102,6 +102,7 @@ 빠른 시일 내에 이용하실 수 있도록\n에브리밀이 준비하고 있어요. + 시작하기 우리 오늘 뭐먹지? 학식에서\n학교 주변 맛집까지 에브리밀에서\n간편하게