Skip to content

Commit

Permalink
#3 add vm
Browse files Browse the repository at this point in the history
  • Loading branch information
nastix123 committed Jul 21, 2024
1 parent d40ca0d commit 646478a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
2 changes: 2 additions & 0 deletions android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -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>(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
}
1 change: 1 addition & 0 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down

0 comments on commit 646478a

Please sign in to comment.