Skip to content

Commit

Permalink
Mod #38: StateFlow 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
0se0 committed Jun 27, 2024
1 parent 69893fd commit a1d0874
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 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 @@ -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
Expand All @@ -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> = _signUpState
private val _signupStatus = MutableLiveData<AuthState>()
val signupStatus: LiveData<AuthState> = _signupStatus

private val _sideEffect = MutableSharedFlow<SignupSideEffect>()
val sideEffect: SharedFlow<SignupSideEffect> = _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("서버 에러"))
Expand All @@ -51,18 +67,18 @@ class SignupViewModel @Inject constructor(
}
}

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

private fun successResponse(response: Response<BaseResponseWithoutDataDto>, request: RequestSignUpDto) {
private fun successResponse(response: Response<BaseResponseWithoutDataDto>, 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)
Expand Down

0 comments on commit a1d0874

Please sign in to comment.