Skip to content

Commit

Permalink
Feature #38: SignupViewModel 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
0se0 committed Jun 26, 2024
1 parent 857767f commit 60f89cc
Showing 1 changed file with 32 additions and 11 deletions.
43 changes: 32 additions & 11 deletions app/src/main/java/com/sopt/now/compose/ui/signup/SignupViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sopt.now.compose.data.User
import com.sopt.now.compose.data.UserRepository
import com.sopt.now.compose.data.remote.response.BaseResponseWithoutDataDto
import com.sopt.now.compose.domain.entity.RequestUserEntity
import com.sopt.now.compose.domain.repository.AuthRepository
import com.sopt.now.compose.model.User
import com.sopt.now.compose.network.request.RequestSignUpDto
import com.sopt.now.compose.network.reponse.ResponseDto
import com.sopt.now.compose.ui.AuthState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
Expand All @@ -16,17 +17,33 @@ import javax.inject.Inject

@HiltViewModel
class SignupViewModel @Inject constructor(
private val userRepository: UserRepository,
private val authRepository: AuthRepository,
) : ViewModel() {
private val _signupStatus = MutableLiveData<AuthState>()
val signupStatus: LiveData<AuthState> = _signupStatus

fun signUp(request: RequestSignUpDto) {
viewModelScope.launch {
runCatching {
userRepository.signUp(request)
}.onSuccess { response ->
handleSuccess(response, request)
val userEntity = RequestUserEntity(
authenticationId = request.authenticationId,
password = request.password,
nickname = request.nickname,
phone = request.phone
)
authRepository.signup(userEntity)
}.onSuccess { result ->
result.fold(
onSuccess = { response ->
handleSuccess(response, request)
},
onFailure = {
_signupStatus.value = AuthState(
isSuccess = false,
message = "서버 에러"
)
}
)
}.onFailure {
_signupStatus.value = AuthState(
isSuccess = false,
Expand All @@ -36,25 +53,29 @@ class SignupViewModel @Inject constructor(
}
}

private fun handleSuccess(response: Response<ResponseDto>, request: RequestSignUpDto) {
private fun handleSuccess(response: Response<BaseResponseWithoutDataDto>, request: RequestSignUpDto) {
if (response.isSuccessful) {
successResponse(response, request)
} else {
failResponse(response)
}
}

private fun successResponse(response: Response<ResponseDto>, request: RequestSignUpDto) {
private fun successResponse(response: Response<BaseResponseWithoutDataDto>, request: RequestSignUpDto) {
val memberId = response.headers()["location"] ?: "unknown"
val user = User(request.authenticationId, request.password, request.nickname, request.phone)
userRepository.saveUserData(user)
authRepository.setUserId(memberId)
authRepository.setId(request.authenticationId)
authRepository.setPassword(request.password)
authRepository.setNickname(request.nickname)
authRepository.setPhoneNumber(request.phone)
_signupStatus.value = AuthState(
isSuccess = true,
message = "회원가입 성공 유저의 ID는 $memberId 입니다."
)
}

private fun failResponse(response: Response<ResponseDto>) {
private fun failResponse(response: Response<BaseResponseWithoutDataDto>) {
val error = response.message()
_signupStatus.value = AuthState(
isSuccess = false,
Expand Down

0 comments on commit 60f89cc

Please sign in to comment.