Skip to content

Commit

Permalink
[fix] : Auth, SignUp Viewmodel 의존성 수동 주입, 서버 통신 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
chanubc committed Jan 2, 2024
1 parent 9cfbe12 commit 8843f98
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
39 changes: 19 additions & 20 deletions app/src/main/java/org/sopt/dosopttemplate/ui/login/AuthViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.data.ApiFactory.ServicePool.authService
import org.sopt.dosopttemplate.data.dto.remote.request.RequestLoginDto
import org.sopt.dosopttemplate.data.repositoryimpl.AuthRepository
import org.sopt.dosopttemplate.ui.model.UserInfo

class AuthViewModel : ViewModel() {
class AuthViewModel(private val authRepository: AuthRepository) : ViewModel() {
private val _loginState = MutableStateFlow<LoginState>(LoginState.Loading)
val loginState: StateFlow<LoginState> = _loginState.asStateFlow()

Expand All @@ -23,26 +23,25 @@ class AuthViewModel : ViewModel() {

fun login(id: String, password: String) {
viewModelScope.launch {
kotlin.runCatching {
authService.postLogin(RequestLoginDto(id, password))
}.onSuccess {
if (it.isSuccessful) {
val response = it.body()
if (response != null) {
_loginState.value = LoginState.Success(response)
Log.d("server", _loginState.value.toString())
UserInfo.updateUserInfo(
id = response.id.toString(),
nickName = response.nickname.toString(),
)
authRepository.postLogin(RequestLoginDto(id, password))
.onSuccess {
if (it.isSuccessful) {
val response = it.body()
if (response != null) {
_loginState.value = LoginState.Success(response)
Log.d("server", _loginState.value.toString())
UserInfo.updateUserInfo(
id = response.id.toString(),
nickName = response.nickname.toString(),
)
}
} else {
_loginState.value = LoginState.Error
Log.d("server", it.code().toString())
}
} else {
_loginState.value = LoginState.Error
Log.d("server", it.code().toString())
}.onFailure {
Log.d("server", it.message.toString())
}
}.onFailure {
Log.d("server", it.message.toString())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.data.ApiFactory.ServicePool.authService
import org.sopt.dosopttemplate.data.dto.remote.request.RequestSignUpDto
import org.sopt.dosopttemplate.data.repositoryimpl.AuthRepository
import org.sopt.dosopttemplate.ui.model.User
import org.sopt.dosopttemplate.utils.UiState

class SignUpViewModel : ViewModel() {
private val _signUpState = MutableStateFlow<SignUpState>(SignUpState.Loading)
val signUpState: StateFlow<SignUpState> = _signUpState.asStateFlow()
class SignUpViewModel(private val authRepository: AuthRepository) : ViewModel() {
private val _signUpState = MutableStateFlow<UiState<User>>(UiState.Loading)
val signUpState: StateFlow<UiState<User>> = _signUpState.asStateFlow()

private val id = MutableLiveData<String>()
private val password = MutableLiveData<String>()
Expand Down Expand Up @@ -103,21 +104,19 @@ class SignUpViewModel : ViewModel() {
_isBtnSelected.value = isIdValid && isPasswordValid && isNickNameValid && isMbtiValid
}

fun signUpServer(userEntity: User) {
fun postSignUp(userEntity: User) {
viewModelScope.launch {
kotlin.runCatching {
authService.postSignUp(
RequestSignUpDto(
userEntity.id,
userEntity.pwd,
userEntity.nickName,
),
)
}.onSuccess {
authRepository.postSignUp(
RequestSignUpDto(
userEntity.id,
userEntity.pwd,
userEntity.nickName,
),
).onSuccess {
if (it.isSuccessful) {
_signUpState.value = SignUpState.Success
_signUpState.value = UiState.Success(userEntity)
} else {
_signUpState.value = SignUpState.Error
_signUpState.value = UiState.Error
}
}.onFailure {
Log.e("SignUpActivity", "Error: ${it.message}")
Expand Down

0 comments on commit 8843f98

Please sign in to comment.