Skip to content

Commit

Permalink
[MOD/#19] enqueue로 통신 했던 것을 코루틴 suspend로 대체 - 회원가입 성공
Browse files Browse the repository at this point in the history
  • Loading branch information
kangyuri1114 committed Dec 6, 2023
1 parent fa11e83 commit d00240b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import retrofit2.http.POST
interface AuthService {
// 회원가입
@POST("api/v1/members")
fun signUp(
suspend fun signUp(
@Body request: RequestSignupDto,
): Call<Unit>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import org.sopt.dosopttemplate.R
import org.sopt.dosopttemplate.databinding.ActivitySignupBinding
import org.sopt.dosopttemplate.util.UiState
import org.sopt.dosopttemplate.util.showShortSnackBar
import org.sopt.dosopttemplate.util.showShortToast

Expand Down Expand Up @@ -64,19 +65,27 @@ class SignUpActivity : AppCompatActivity() {

private fun clickSignUpBtn() {
binding.btnSignupSignup.setOnClickListener {
signUpViewModel.signUpUserApi(this)
signUpViewModel.signUpResult.observe(this) { uiState ->
when (uiState) {
is UiState.Success -> {
showShortToast(getString(R.string.signup_success))
val intent = Intent(this, LoginActivity::class.java)
startActivity(intent)
finish()
}

signUpViewModel.signUpResult.observe(this) { signUpSuccessful ->
if (signUpSuccessful) {
showShortToast(getString(R.string.signup_success))
val intent = Intent(this, LoginActivity::class.java)
// intent.putExtra("signUpUser", signUpUser)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent)
} else {
showShortSnackBar(binding.root, getString(R.string.signup_fail))
is UiState.Failure -> {
showShortSnackBar(binding.root, getString(R.string.signup_fail))
}

is UiState.Loading -> {
showShortSnackBar(binding.root, "로딩중")
}
}
}
binding.btnSignupSignup.setOnClickListener {
signUpViewModel.signUpUserApi()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.sopt.dosopttemplate.presentation.auth

import android.content.Context
import android.widget.Toast
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.map
import org.sopt.dosopttemplate.data.User
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import org.sopt.dosopttemplate.data.remote.ServicePool
import org.sopt.dosopttemplate.data.remote.request.RequestSignupDto
import retrofit2.Call
import retrofit2.Response
import org.sopt.dosopttemplate.util.UiState
import java.util.regex.Pattern

class SignUpViewModel : ViewModel() {
private val _signUpResult = MutableLiveData<Boolean>()
val signUpResult: LiveData<Boolean> get() = _signUpResult
private val _signUpResult = MutableLiveData<UiState<Boolean>>()
val signUpResult: LiveData<UiState<Boolean>> get() = _signUpResult

// 사용자가 입력하는 값들
val inputId: MutableLiveData<String> = MutableLiveData()
Expand Down Expand Up @@ -48,39 +46,23 @@ class SignUpViewModel : ViewModel() {
dynamicTextSize.value = newTextSize
}

fun signUpUserApi(context: Context) {
ServicePool.authService.signUp(
RequestSignupDto(
inputId.value!!,
inputPw.value!!,
inputNickname.value!!,
),
)
.enqueue(object : retrofit2.Callback<Unit> {
override fun onResponse(
call: Call<Unit>,
response: Response<Unit>,
) {
if (response.isSuccessful) {
_signUpResult.value = true
fun signUpUserApi() = viewModelScope.launch {
_signUpResult.value = UiState.Loading

User(
inputId.value!!,
inputPw.value!!,
inputNickname.value!!,
)
}
}
runCatching {
ServicePool.authService.signUp(
RequestSignupDto(
inputId.value ?: "",
inputPw.value ?: "",
inputNickname.value ?: "",
),
)
}.onSuccess {
}.onFailure {
_signUpResult.value = UiState.Failure(it.message.toString())
}

override fun onFailure(call: Call<Unit>, t: Throwable) {
Toast.makeText(
context,
"ㅜ ㅜ 서버 에러 발생 ㅜ ㅜ",
Toast.LENGTH_SHORT,
).show()
_signUpResult.value = false
}
})
_signUpResult.value = UiState.Success(true)
}

companion object {
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/org/sopt/dosopttemplate/util/UiState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package org.sopt.dosopttemplate.util
sealed interface UiState<out T> {
object Loading : UiState<Nothing>

data class Success<T>(
val data: T,
data class Success<out T>(
val data: T? = null,
) : UiState<T>

data class Failure(
Expand Down

0 comments on commit d00240b

Please sign in to comment.