From 646478abc85347678424cb85f0948eb17818f40b Mon Sep 17 00:00:00 2001 From: anastasizzz Date: Sun, 21 Jul 2024 23:36:55 +0300 Subject: [PATCH] #3 add vm --- android/app/build.gradle.kts | 2 + .../screens/AuthorizationViewModel.kt | 81 +++++++++++++++++++ android/gradle/libs.versions.toml | 1 + 3 files changed, 84 insertions(+) diff --git a/android/app/build.gradle.kts b/android/app/build.gradle.kts index 74a9008..919fee6 100644 --- a/android/app/build.gradle.kts +++ b/android/app/build.gradle.kts @@ -92,6 +92,8 @@ dependencies { implementation(libs.accompanist.navigation.animation) implementation(libs.androidx.datastore.preferences) implementation(libs.androidx.datastore.preferences.core) + + implementation(libs.androidx.lifecycle.viewmodel.ktx) } diff --git a/android/app/src/main/java/by/eapp/musicroom/screens/AuthorizationViewModel.kt b/android/app/src/main/java/by/eapp/musicroom/screens/AuthorizationViewModel.kt index 1a31221..fac80ac 100644 --- a/android/app/src/main/java/by/eapp/musicroom/screens/AuthorizationViewModel.kt +++ b/android/app/src/main/java/by/eapp/musicroom/screens/AuthorizationViewModel.kt @@ -1,3 +1,84 @@ package by.eapp.musicroom.screens +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import by.eapp.musicroom.domain.model.LoginData +import by.eapp.musicroom.domain.model.RegistrationData +import by.eapp.musicroom.domain.model.SubmitData +import by.eapp.musicroom.domain.repo.login.AuthorizationService +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.delay +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class AuthorizationViewModel @Inject constructor( + private val auth: AuthorizationService, +) : ViewModel() { + private val _stateUi = MutableStateFlow(LoginScreenState.Init) + val stateUi = _stateUi.asStateFlow() + + fun registerUser(registrationData: RegistrationData) { + _stateUi.value = LoginScreenState.Loading + viewModelScope.launch { + val userId = auth.registerUser(registrationData) + delay(DELAY_TIME) + auth.sendCode(userId) + } + } + + + fun submitCode(submitData: SubmitData) { + _stateUi.value = LoginScreenState.Loading + viewModelScope.launch { + val token = auth.submitCode(submitData) + delay(DELAY_TIME) + _stateUi.value = LoginScreenState.Success + } + } + + fun loginUser(loginData: LoginData) { + _stateUi.value = LoginScreenState.Loading + viewModelScope.launch { + val tokens = auth.loginUser(loginData) + delay(DELAY_TIME) + auth.refreshToken(tokens.refreshToken) + _stateUi.value = LoginScreenState.Success + } + } + + fun dispatch(action: LoginScreenAction) { + _stateUi.value = when (action) { + is LoginScreenAction.StartAuth -> LoginScreenState.Loading + is LoginScreenAction.Success -> LoginScreenState.Success + is LoginScreenAction.Error -> LoginScreenState.Error(action.error) + } + } + + companion object { + const val DELAY_TIME = 500L + } + +} + +sealed interface RegistrationScreenAction { + data object Loading : RegistrationScreenAction + data object Success : RegistrationScreenAction + data class Error(val error: Throwable?) : RegistrationScreenAction +} + +sealed interface LoginScreenAction { + data object StartAuth : LoginScreenAction + data object Success : LoginScreenAction + data class Error(val error: Throwable?) : LoginScreenAction +} + +sealed interface LoginScreenState { + data object Init : LoginScreenState + data object Loading : LoginScreenState + data class Error(val error: Throwable?) : LoginScreenState + data object Success : LoginScreenState +} diff --git a/android/gradle/libs.versions.toml b/android/gradle/libs.versions.toml index 145854b..055b577 100644 --- a/android/gradle/libs.versions.toml +++ b/android/gradle/libs.versions.toml @@ -30,6 +30,7 @@ androidx-datastore-preferences = { module = "androidx.datastore:datastore-prefer androidx-datastore-preferences-core = { module = "androidx.datastore:datastore-preferences-core", version.ref = "datastorePreferencesCore" } androidx-hilt-compiler = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltCompiler" } androidx-hilt-work = { module = "androidx.hilt:hilt-work", version.ref = "hiltCompiler" } +androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleRuntimeKtx" } androidx-material-icons-extended = { module = "androidx.compose.material:material-icons-extended", version.ref = "materialIconsExtended" } androidx-navigation-compose = { module = "androidx.navigation:navigation-compose", version.ref = "navigationCompose" } androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }