-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from rbybound/feature-aos/등록-요청-보내기
Feature(#140, #142, #143): 회원가입 요청 보내기
- Loading branch information
Showing
10 changed files
with
173 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
android/app/src/main/java/com/boostcampwm2023/snappoint/data/repository/LoginRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
package com.boostcampwm2023.snappoint.data.repository | ||
|
||
import com.boostcampwm2023.snappoint.data.remote.model.request.SignupRequest | ||
import com.boostcampwm2023.snappoint.data.remote.model.response.LoginResponse | ||
import com.boostcampwm2023.snappoint.data.remote.model.response.SignupResponse | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface LoginRepository { | ||
|
||
fun postLogin(email: String, password: String): Flow<LoginResponse> | ||
|
||
fun getLogout(): Flow<Unit> | ||
|
||
fun postSignup(email: String, password: String, nickname: String): Flow<SignupResponse> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
android/app/src/main/java/com/boostcampwm2023/snappoint/presentation/signup/SignupEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.boostcampwm2023.snappoint.presentation.signup | ||
|
||
sealed class SignupEvent { | ||
data object Success : SignupEvent() | ||
data class Fail(val messageResId: Int) : SignupEvent() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
package com.boostcampwm2023.snappoint.presentation.signup | ||
|
||
import android.util.Log | ||
import android.util.Patterns | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import com.boostcampwm2023.snappoint.R | ||
import com.boostcampwm2023.snappoint.data.repository.LoginRepository | ||
import com.boostcampwm2023.snappoint.presentation.util.Constants | ||
import com.boostcampwm2023.snappoint.presentation.util.TextVerificationUtil | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharedFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onCompletion | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.update | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class SignupViewModel @Inject constructor() : ViewModel() { | ||
|
||
private val _uiState: MutableStateFlow<SignupUiState> = MutableStateFlow(SignupUiState( | ||
email = "[email protected]", | ||
password = "asdASD123!@#", | ||
passwordConfirm = "asdASD123!@#", | ||
nickname = "nickname", | ||
isInputValid = true | ||
)) | ||
class SignupViewModel @Inject constructor( | ||
private val loginRepository: LoginRepository | ||
) : ViewModel() { | ||
|
||
private val _uiState: MutableStateFlow<SignupUiState> = MutableStateFlow( | ||
SignupUiState( | ||
email = "[email protected]", | ||
password = "asdASD123!@#", | ||
passwordConfirm = "asdASD123!@#", | ||
nickname = "nickname", | ||
isButtonEnabled = true | ||
) | ||
) | ||
val uiState: StateFlow<SignupUiState> = _uiState.asStateFlow() | ||
|
||
private val _event: MutableSharedFlow<SignupEvent> = MutableSharedFlow( | ||
extraBufferCapacity = 1, | ||
onBufferOverflow = BufferOverflow.DROP_OLDEST | ||
) | ||
val event: SharedFlow<SignupEvent> = _event.asSharedFlow() | ||
|
||
fun updateEmail(email: String) { | ||
_uiState.update { | ||
it.copy( | ||
email = email, | ||
emailCode = takeEmailErrorCode(email) | ||
emailErrorResId = getEmailErrorCode(email) | ||
) | ||
} | ||
updateButtonState() | ||
|
@@ -38,7 +59,7 @@ class SignupViewModel @Inject constructor() : ViewModel() { | |
_uiState.update { | ||
it.copy( | ||
password = password, | ||
passwordCode = takePasswordErrorCode(password) | ||
passwordErrorResId = getPasswordErrorCode(password) | ||
) | ||
} | ||
updatePasswordConfirm() | ||
|
@@ -49,7 +70,7 @@ class SignupViewModel @Inject constructor() : ViewModel() { | |
_uiState.update { | ||
it.copy( | ||
passwordConfirm = password, | ||
passwordConfirmCode = takePasswordConfirmErrorCode(password) | ||
passwordConfirmErrorResId = getPasswordConfirmErrorCode(password) | ||
) | ||
} | ||
updateButtonState() | ||
|
@@ -58,7 +79,7 @@ class SignupViewModel @Inject constructor() : ViewModel() { | |
private fun updatePasswordConfirm() { | ||
_uiState.update { | ||
it.copy( | ||
passwordConfirmCode = takePasswordConfirmErrorCode(it.passwordConfirm) | ||
passwordConfirmErrorResId = getPasswordConfirmErrorCode(it.passwordConfirm) | ||
) | ||
} | ||
} | ||
|
@@ -67,7 +88,7 @@ class SignupViewModel @Inject constructor() : ViewModel() { | |
_uiState.update { | ||
it.copy( | ||
nickname = nickname, | ||
nicknameCode = takeNicknameErrorCode(nickname) | ||
nicknameErrorResId = getNicknameErrorCode(nickname) | ||
) | ||
} | ||
updateButtonState() | ||
|
@@ -76,40 +97,83 @@ class SignupViewModel @Inject constructor() : ViewModel() { | |
private fun updateButtonState() { | ||
_uiState.update { | ||
it.copy( | ||
isInputValid = it.emailCode == null && | ||
it.passwordCode == null && | ||
it.passwordConfirmCode == null && | ||
it.nicknameCode == null | ||
isButtonEnabled = it.emailErrorResId == null && | ||
it.passwordErrorResId == null && | ||
it.passwordConfirmErrorResId == null && | ||
it.nicknameErrorResId == null | ||
) | ||
} | ||
} | ||
|
||
private fun takeEmailErrorCode(email: String): Int? { | ||
private fun updateEmailErrorResId(message: String?) { | ||
if (isMessageDuplicationError(message)) { | ||
_uiState.update { | ||
it.copy( | ||
emailErrorResId = R.string.signup_fragment_error_email_duplicate, | ||
isButtonEnabled = false | ||
) | ||
} | ||
} | ||
} | ||
|
||
private fun getEmailErrorCode(email: String): Int? { | ||
return if (TextVerificationUtil.isEmailValid(email)) null else R.string.signup_fragment_error_email_form | ||
} | ||
|
||
private fun takePasswordErrorCode(password: String): Int? { | ||
private fun getPasswordErrorCode(password: String): Int? { | ||
return if (TextVerificationUtil.isPasswordValid(password)) null else R.string.signup_fragment_error_password_length | ||
} | ||
|
||
private fun takePasswordConfirmErrorCode(password: String): Int? { | ||
private fun getPasswordConfirmErrorCode(password: String): Int? { | ||
return if (uiState.value.password == password) null else R.string.signup_fragment_error_password_confirm_mismatch | ||
} | ||
|
||
private fun takeNicknameErrorCode(nickname: String): Int? { | ||
private fun getNicknameErrorCode(nickname: String): Int? { | ||
return if (nickname.length > 1) null else R.string.signup_fragment_error_nickname | ||
} | ||
|
||
fun onSignUpButtonClicked() { | ||
with(uiState.value) { | ||
if (isInputValid.not()) { | ||
if (isButtonEnabled.not()) { | ||
return | ||
} | ||
|
||
val email = email | ||
val password = password | ||
val passwordConfirm = passwordConfirm | ||
val nickname = nickname | ||
loginRepository.postSignup(email, password, nickname) | ||
.onStart { | ||
setProgressBarState(true) | ||
} | ||
.onEach { | ||
_event.emit(SignupEvent.Success) | ||
} | ||
.catch { | ||
updateEmailErrorResId(it.message) | ||
_event.emit( | ||
SignupEvent.Fail(_uiState.value.emailErrorResId ?: R.string.signup_fragment_fail) | ||
) | ||
} | ||
.onCompletion { | ||
setProgressBarState(false) | ||
} | ||
.launchIn(viewModelScope) | ||
} | ||
} | ||
|
||
private fun setProgressBarState(isInProgress: Boolean) { | ||
_uiState.update { | ||
it.copy( | ||
isSignUpInProgress = isInProgress | ||
) | ||
} | ||
} | ||
|
||
private fun getErrorMessage(message: String?): Int? { | ||
return when { | ||
isMessageDuplicationError(message) -> R.string.signup_fragment_fail_duplicate | ||
else -> null | ||
} | ||
} | ||
|
||
private fun isMessageDuplicationError(message: String?): Boolean { | ||
return message == Constants.EMAIL_DUPLICATE_ERROR | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.