diff --git a/app/src/main/java/com/sopt/now/compose/ui/signup/SignupViewModel.kt b/app/src/main/java/com/sopt/now/compose/ui/signup/SignupViewModel.kt index d050e5d..ef39322 100644 --- a/app/src/main/java/com/sopt/now/compose/ui/signup/SignupViewModel.kt +++ b/app/src/main/java/com/sopt/now/compose/ui/signup/SignupViewModel.kt @@ -5,13 +5,14 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.sopt.now.compose.data.remote.response.BaseResponseWithoutDataDto -import com.sopt.now.compose.domain.entity.RequestUserEntity +import com.sopt.now.compose.domain.entity.request.RequestUserEntity import com.sopt.now.compose.domain.repository.AuthRepository -import com.sopt.now.compose.network.request.RequestSignUpDto import com.sopt.now.compose.ui.AuthState import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharedFlow +import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch import retrofit2.Response import javax.inject.Inject @@ -20,26 +21,41 @@ import javax.inject.Inject class SignupViewModel @Inject constructor( private val authRepository: AuthRepository, ) : ViewModel() { + + private val _signUpState = MutableStateFlow(SignUpState()) + val signUpState: StateFlow = _signUpState private val _signupStatus = MutableLiveData() val signupStatus: LiveData = _signupStatus private val _sideEffect = MutableSharedFlow() val sideEffect: SharedFlow = _sideEffect - fun signUp(request: RequestSignUpDto) { + fun updateSignUpState(id: String? = null, password: String? = null, nickname: String? = null, phone: String? = null) { + val currentState = _signUpState.value + _signUpState.value = currentState.copy( + id = id ?: currentState.id, + password = password ?: currentState.password, + nickname = nickname ?: currentState.nickname, + phone = phone ?: currentState.phone + ) + } + + fun signUp() { + val currentState = _signUpState.value + viewModelScope.launch { runCatching { val userEntity = RequestUserEntity( - authenticationId = request.authenticationId, - password = request.password, - nickname = request.nickname, - phone = request.phone + authenticationId = currentState.id, + password = currentState.password, + nickname = currentState.nickname, + phone = currentState.phone ) authRepository.signup(userEntity) }.onSuccess { result -> result.fold( onSuccess = { response -> - handleSuccess(response, request) + handleSuccess(response, currentState) }, onFailure = { _sideEffect.emit(SignupSideEffect.ShowError("서버 에러")) @@ -51,7 +67,7 @@ class SignupViewModel @Inject constructor( } } - private fun handleSuccess(response: Response, request: RequestSignUpDto) { + private fun handleSuccess(response: Response, request: SignUpState) { if (response.isSuccessful) { successResponse(response, request) } else { @@ -59,10 +75,10 @@ class SignupViewModel @Inject constructor( } } - private fun successResponse(response: Response, request: RequestSignUpDto) { + private fun successResponse(response: Response, request: SignUpState) { val memberId = response.headers()["location"] ?: "unknown" authRepository.setUserId(memberId) - authRepository.setId(request.authenticationId) + authRepository.setId(request.id) authRepository.setPassword(request.password) authRepository.setNickname(request.nickname) authRepository.setPhoneNumber(request.phone)