Skip to content

Commit

Permalink
feat/#9: 회원가입 서버 통신 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent 02cbded commit d635e6f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 40 deletions.
6 changes: 6 additions & 0 deletions app/src/main/java/com/sopt/now/test/data/BaseState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.sopt.now.test.data

data class BaseState(
val isSuccess: Boolean,
val message: String
)
68 changes: 28 additions & 40 deletions app/src/main/java/com/sopt/now/test/presentation/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,73 +3,61 @@ package com.sopt.now.test.presentation
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import com.sopt.now.databinding.ActivitySignUpBinding
import com.sopt.now.test.data.UserData
import com.sopt.now.test.data.dto.request.RequestSignUpDto

class SignUpActivity : AppCompatActivity() {

private lateinit var binding: ActivitySignUpBinding
private val binding by lazy { ActivitySignUpBinding.inflate(layoutInflater) }
private val viewModel by viewModels<SignUpViewModel>()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySignUpBinding.inflate(layoutInflater)
setContentView(binding.root)

setupSignUpButton()
initViews()
initObserver()
}

// 회원 가입
private fun setupSignUpButton() {
private fun initViews() {
binding.btnSignUp.setOnClickListener {
with(binding) {
val userId = etSignUpId.text.toString()
val userPw = etSignUpPw.text.toString()
val userName = etSignUpName.text.toString()
val selfDescription = etSignUpDescription.text.toString()

val userData = UserData(userId, userPw, userName, selfDescription)
checkSignUp(userData)
}
viewModel.signUp(getSignUpRequestDto())
}
}

// 회원 가입 가능 여부 체크
private fun checkSignUp(userData: UserData) {
val isValidId = userData.userId.length in MIN_ID_LENGTH..MAX_ID_LENGTH
val isValidPw = userData.userPw.length in MIN_PW_LENGTH..MAX_PW_LENGTH
val isValidName = userData.userName.isBlank() // 공백으로만 이루어진 경우 판단

val message = when {
!isValidId -> "ID는 6~10 글자여야 합니다."
!isValidPw -> "Password는 8~12 글자여야 합니다."
isValidName -> "공백으로만 이루어진 닉네임은 불가합니다."
else -> {
moveToLogin(userData)
"회원가입 성공!"
private fun initObserver() {
viewModel.liveData.observe(this) { response ->
if (response.isSuccess) {
moveToLogin()
}
showToast(response.message)
}
showToast(message)
}

private fun getSignUpRequestDto(): RequestSignUpDto {
val id = binding.etSignUpId.text.toString()
val password = binding.etSignUpPw.text.toString()
val nickname = binding.etSignUpName.text.toString()
val phoneNumber = binding.etSignUpPhone.text.toString()
return RequestSignUpDto(
authenticationId = id,
password = password,
nickname = nickname,
phone = phoneNumber
)
}

// 로그인 페이지로 이동
private fun moveToLogin(userData: UserData) {
private fun moveToLogin() {
Intent(this, LoginActivity::class.java).apply {
putExtra("userData", userData)
setResult(RESULT_OK, this)
startActivity(this)
finish()
}
}

private fun showToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

// 글자 수 제한
companion object {
private const val MIN_ID_LENGTH = 6
private const val MAX_ID_LENGTH = 10
private const val MIN_PW_LENGTH = 8
private const val MAX_PW_LENGTH = 12
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.sopt.now.test.presentation

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.sopt.now.test.data.ServicePool
import com.sopt.now.test.data.BaseState
import com.sopt.now.test.data.dto.request.RequestSignUpDto
import com.sopt.now.test.data.dto.response.ResponseAuthDto
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SignUpViewModel : ViewModel() {
private val authService by lazy { ServicePool.authService }
val liveData = MutableLiveData<BaseState>()

fun signUp(request: RequestSignUpDto) {
authService.signUp(request).enqueue(object : Callback<ResponseAuthDto> {
override fun onResponse(
call: Call<ResponseAuthDto>,
response: Response<ResponseAuthDto>,
) {
if (response.isSuccessful) {
val data: ResponseAuthDto? = response.body()
val userId = response.headers()["location"]
liveData.value = BaseState(
isSuccess = true,
message = "회원가입 성공 유저의 ID는 $userId 입니다."
)
Log.d("SignUp", "data: $data, userId: $userId")
} else {
val error = response.errorBody()?.string()
val gson = Gson()
try {
val errorResponse = gson.fromJson(error, ResponseAuthDto::class.java)
liveData.value = BaseState(
isSuccess = false,
message = "회원가입 실패: ${errorResponse.message}" // 에러 메시지 사용
)
} catch (e: Exception) {
liveData.value = BaseState(
isSuccess = false,
message = "회원가입 실패: 에러 메시지 파싱 실패"
)
}
}
}

override fun onFailure(call: Call<ResponseAuthDto>, t: Throwable) {
liveData.value = BaseState(
isSuccess = false,
message = "서버 에러"
)
}
})
}
}

0 comments on commit d635e6f

Please sign in to comment.