-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
android/app/src/main/java/by/eapp/musicroom/screens/AuthorizationViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters