wip
This commit is contained in:
@@ -2,8 +2,10 @@ package com.mycrib.android.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.mycrib.shared.models.AuthResponse
|
||||
import com.mycrib.shared.models.LoginRequest
|
||||
import com.mycrib.shared.models.RegisterRequest
|
||||
import com.mycrib.shared.models.Residence
|
||||
import com.mycrib.shared.network.ApiResult
|
||||
import com.mycrib.shared.network.AuthApi
|
||||
import com.mycrib.storage.TokenStorage
|
||||
@@ -17,6 +19,9 @@ class AuthViewModel : ViewModel() {
|
||||
private val _loginState = MutableStateFlow<ApiResult<String>>(ApiResult.Loading)
|
||||
val loginState: StateFlow<ApiResult<String>> = _loginState
|
||||
|
||||
private val _registerState = MutableStateFlow<ApiResult<AuthResponse>>(ApiResult.Loading)
|
||||
val registerState: StateFlow<ApiResult<AuthResponse>> = _registerState
|
||||
|
||||
fun login(username: String, password: String) {
|
||||
viewModelScope.launch {
|
||||
_loginState.value = ApiResult.Loading
|
||||
@@ -35,7 +40,7 @@ class AuthViewModel : ViewModel() {
|
||||
|
||||
fun register(username: String, email: String, password: String) {
|
||||
viewModelScope.launch {
|
||||
_loginState.value = ApiResult.Loading
|
||||
_registerState.value = ApiResult.Loading
|
||||
val result = authApi.register(
|
||||
RegisterRequest(
|
||||
username = username,
|
||||
@@ -43,11 +48,11 @@ class AuthViewModel : ViewModel() {
|
||||
password = password
|
||||
)
|
||||
)
|
||||
_loginState.value = when (result) {
|
||||
_registerState.value = when (result) {
|
||||
is ApiResult.Success -> {
|
||||
// Store token for future API calls
|
||||
TokenStorage.saveToken(result.data.token)
|
||||
ApiResult.Success(result.data.token)
|
||||
ApiResult.Success(result.data)
|
||||
}
|
||||
is ApiResult.Error -> result
|
||||
else -> ApiResult.Error("Unknown error")
|
||||
@@ -55,6 +60,10 @@ class AuthViewModel : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun resetRegisterState() {
|
||||
_registerState.value = ApiResult.Loading
|
||||
}
|
||||
|
||||
fun logout() {
|
||||
viewModelScope.launch {
|
||||
val token = TokenStorage.getToken()
|
||||
|
||||
Reference in New Issue
Block a user