Skip to content

Commit

Permalink
feat/#11: 회원가입 서버 통신
Browse files Browse the repository at this point in the history
  • Loading branch information
youjin09222 committed May 3, 2024
1 parent f51e011 commit 47c12ff
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sopt.now.compose
package com.sopt.now.compose.presentation

import android.content.Context
import android.content.Intent
Expand All @@ -24,6 +24,11 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.sopt.now.compose.R
import com.sopt.now.compose.data.ApiFactory
import com.sopt.now.compose.data.ApiFactory.userPreference
import com.sopt.now.compose.data.UserPreference
import com.sopt.now.compose.data.dto.request.RequestSignUpDto
import com.sopt.now.compose.ui.theme.NOWSOPTAndroidTheme

class SignUpActivity : ComponentActivity() {
Expand All @@ -35,15 +40,19 @@ class SignUpActivity : ComponentActivity() {
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
SignUpCompose()
userPreference = UserPreference(this)
ApiFactory.initializeUserPreference(userPreference)

val viewModel: SignUpViewModel = remember { SignUpViewModel() }
SignUpCompose(viewModel)
}
}
}
}
}

@Composable
fun SignUpCompose(){
fun SignUpCompose(viewModel: SignUpViewModel){
val context = LocalContext.current
var userId by remember { mutableStateOf("") }
var userPw by remember { mutableStateOf("") }
Expand Down Expand Up @@ -85,7 +94,7 @@ fun SignUpCompose(){
Spacer(modifier = Modifier.weight(2f))
Button(
onClick = {
checkSignUp(context, userId, userPw, userName, userDescription)
checkSignUp(viewModel, context, userId, userPw, userName, userDescription)
},
colors = ButtonDefaults.buttonColors(containerColor = Color.Black),
shape = RoundedCornerShape(50.dp),
Expand Down Expand Up @@ -116,16 +125,21 @@ fun TextFieldWithLabel(label: String, value: String, hint: String, onValueChange
}

// 회원 가입 가능 여부 체크
fun checkSignUp(context: Context, userId: String, userPw: String, userName: String, userDescription: String) {
val isValidId = userId.length in 6..10
fun checkSignUp(
viewModel: SignUpViewModel,
context: Context,
userId: String,
userPw: String,
userName: String,
userDescription: String) {
val isValidPw = userPw.length in 8..12
val isValidName = userName.trim().isEmpty() // 공백으로만 이루어진 경우 판단

val message = when {
!isValidId -> context.getString(R.string.error_invalid_id)
!isValidPw -> context.getString(R.string.error_invalid_pw)
isValidName -> context.getString(R.string.error_empty_name)
else -> {
viewModel.signUp(RequestSignUpDto(userId, userPw, userName, userDescription))
moveToLogin(context, userId, userPw, userName, userDescription)
context.getString(R.string.signup_success)
}
Expand All @@ -148,6 +162,6 @@ private fun moveToLogin(context: Context, userId: String, userPw: String, userNa
@Composable
fun SignUpPreview() {
NOWSOPTAndroidTheme {
SignUpCompose()
SignUpCompose(viewModel = SignUpViewModel())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.sopt.now.compose.presentation

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.google.gson.Gson
import com.sopt.now.compose.data.BaseState
import com.sopt.now.compose.data.ServicePool
import com.sopt.now.compose.data.dto.request.RequestSignUpDto
import com.sopt.now.compose.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 47c12ff

Please sign in to comment.