diff --git a/app/src/main/java/com/sopt/now/compose/SignUpActivity.kt b/app/src/main/java/com/sopt/now/compose/presentation/SignUpActivity.kt similarity index 83% rename from app/src/main/java/com/sopt/now/compose/SignUpActivity.kt rename to app/src/main/java/com/sopt/now/compose/presentation/SignUpActivity.kt index 50259db..0cd3a9f 100644 --- a/app/src/main/java/com/sopt/now/compose/SignUpActivity.kt +++ b/app/src/main/java/com/sopt/now/compose/presentation/SignUpActivity.kt @@ -1,4 +1,4 @@ -package com.sopt.now.compose +package com.sopt.now.compose.presentation import android.content.Context import android.content.Intent @@ -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() { @@ -35,7 +40,11 @@ class SignUpActivity : ComponentActivity() { modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background ) { - SignUpCompose() + userPreference = UserPreference(this) + ApiFactory.initializeUserPreference(userPreference) + + val viewModel: SignUpViewModel = remember { SignUpViewModel() } + SignUpCompose(viewModel) } } } @@ -43,7 +52,7 @@ class SignUpActivity : ComponentActivity() { } @Composable -fun SignUpCompose(){ +fun SignUpCompose(viewModel: SignUpViewModel){ val context = LocalContext.current var userId by remember { mutableStateOf("") } var userPw by remember { mutableStateOf("") } @@ -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), @@ -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) } @@ -148,6 +162,6 @@ private fun moveToLogin(context: Context, userId: String, userPw: String, userNa @Composable fun SignUpPreview() { NOWSOPTAndroidTheme { - SignUpCompose() + SignUpCompose(viewModel = SignUpViewModel()) } } \ No newline at end of file diff --git a/app/src/main/java/com/sopt/now/compose/presentation/SignUpViewModel.kt b/app/src/main/java/com/sopt/now/compose/presentation/SignUpViewModel.kt new file mode 100644 index 0000000..6857b92 --- /dev/null +++ b/app/src/main/java/com/sopt/now/compose/presentation/SignUpViewModel.kt @@ -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() + + fun signUp(request: RequestSignUpDto) { + authService.signUp(request).enqueue(object : Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + 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, t: Throwable) { + liveData.value = BaseState( + isSuccess = false, + message = "서버 에러" + ) + } + }) + } +} \ No newline at end of file