Skip to content

Commit

Permalink
feat/#8: SignUpViewModel 취미 update, 회원가입, 네트워크 처리, intent 처리 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyein9892 committed Nov 15, 2024
1 parent f6d3368 commit b5682fb
Showing 1 changed file with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,33 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.sopt.and.domain.exception.NetworkError
import org.sopt.and.domain.exception.onError
import org.sopt.and.domain.exception.onSuccess
import org.sopt.and.domain.model.User
import org.sopt.and.domain.repository.UserRepository
import org.sopt.and.presentation.R
import org.sopt.and.presentation.delegate.NetworkDelegate
import org.sopt.and.presentation.sign.signup.intent.SignUpSideEffect
import org.sopt.and.presentation.sign.signup.model.SignUpState
import javax.inject.Inject

@HiltViewModel
class SignUpViewModel @Inject constructor(

private val userRepository: UserRepository
): ViewModel() {

@Inject
lateinit var networkDelegate: NetworkDelegate

private var _state = MutableStateFlow(SignUpState())
val state = _state.asStateFlow()

private var _intent = MutableSharedFlow<SignUpSideEffect>()
val intent = _intent.asSharedFlow()

val networkState get() = networkDelegate.networkState

fun updateId(id: String) = _state.update {
it.copy(id = id)
}
Expand All @@ -33,29 +44,32 @@ class SignUpViewModel @Inject constructor(
it.copy(password = password)
}

fun onSignUpButtonClick() = viewModelScope.launch {
val isValidateId = state.value.id.matches(emailPattern)
val isValidatePassword = state.value.password.matches(passwordPattern)
when{
isValidateId && isValidatePassword -> {
_intent.emit(SignUpSideEffect.SnackBar(R.string.signup_success_text))
_intent.emit(SignUpSideEffect.SignUp)
}
!isValidateId -> {
_intent.emit(SignUpSideEffect.SnackBar(R.string.signup_failure_email_text))
}
!isValidatePassword -> {
_intent.emit(SignUpSideEffect.SnackBar(R.string.signup_failure_password_text))
}
else -> {
_intent.emit(SignUpSideEffect.SnackBar(R.string.signup_failure_text))
}
fun updateHobby(hobby: String) = _state.update {
it.copy(hobby = hobby)
}

fun signUp() = viewModelScope.launch {
val currentState = _state.value
val user = User(
username = currentState.id,
password = currentState.password,
hobby = currentState.hobby
)
userRepository.signUp(user).onSuccess {
networkDelegate.handleNetworkSuccess()
}.onError {
networkDelegate.handleSignUpError(it)
}
}

companion object {
val emailPattern = "^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$".toRegex()
val passwordPattern = "^(?=.*[a-zA-Z])(?=.*\\d)(?=.*[@\$!%*?&])[A-Za-z\\d@\$!%*?&]{8,20}$".toRegex()
suspend fun handleSignUpIntentError(message: String) {
_intent.emit(SignUpSideEffect.SnackBarText(message))
}

suspend fun handleSignUpIntentSuccess() {
_intent.emit(SignUpSideEffect.SnackBar(R.string.signup_success_text))
_intent.emit(SignUpSideEffect.SignUp)
}

}

0 comments on commit b5682fb

Please sign in to comment.