From 96c3dd4351d006624a10e716185357f0c4e19138 Mon Sep 17 00:00:00 2001 From: librarywon Date: Mon, 22 May 2023 22:33:50 +0900 Subject: [PATCH] [FEAT] #8-server post response --- .../sopt/carrot/data/profile/ProfileDto.kt | 44 +++++++++++++++++ .../carrot/data/profile/ProfileService.kt | 15 ++++++ .../presentation/profile/ProfileActivity.kt | 3 ++ .../presentation/profile/ProfileViewModel.kt | 47 +++++++++++++++++-- app/src/main/res/layout/activity_profile.xml | 2 +- 5 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/com/sopt/carrot/data/profile/ProfileDto.kt create mode 100644 app/src/main/java/com/sopt/carrot/data/profile/ProfileService.kt diff --git a/app/src/main/java/com/sopt/carrot/data/profile/ProfileDto.kt b/app/src/main/java/com/sopt/carrot/data/profile/ProfileDto.kt new file mode 100644 index 0000000..d6229c6 --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/profile/ProfileDto.kt @@ -0,0 +1,44 @@ +package com.sopt.carrot.data.profile + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class RequestProfileDto( + @SerialName("birthYear") + val birthYear: Int, + @SerialName("gender") + val gender: Int, + @SerialName("introduction") + val introduction: String?, + @SerialName("name") + val name: String?, + @SerialName("phoneNumber") + val phoneNumber: String +) + +@Serializable +data class ResponseProfileDto( + @SerialName("data") + val `data`: Data, + @SerialName("message") + val message: String, + @SerialName("status") + val status: Int, + @SerialName("success") + val success: Boolean +) { + @Serializable + data class Data( + @SerialName("birthYear") + val birthYear: Int, + @SerialName("gender") + val gender: Int, + @SerialName("introduction") + val introduction: String, + @SerialName("name") + val name: Any, + @SerialName("phoneNumber") + val phoneNumber: String + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/data/profile/ProfileService.kt b/app/src/main/java/com/sopt/carrot/data/profile/ProfileService.kt new file mode 100644 index 0000000..a94cfc0 --- /dev/null +++ b/app/src/main/java/com/sopt/carrot/data/profile/ProfileService.kt @@ -0,0 +1,15 @@ +package com.sopt.carrot.data.profile + +import retrofit2.Call +import retrofit2.http.Body +import retrofit2.http.Headers +import retrofit2.http.POST + + +interface ProfileService { + @Headers("Authorization: 1") + @POST("/users/profile") + fun apply( + @Body request: RequestProfileDto + ): Call +} \ No newline at end of file diff --git a/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileActivity.kt b/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileActivity.kt index 690270f..0efcb23 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileActivity.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileActivity.kt @@ -95,6 +95,9 @@ class ProfileActivity : AppCompatActivity() { val dialog = CustomPopupDialog(this@ProfileActivity) dialog.show() } + btnProfileAgreeAndApply.setOnClickListener { + viewModel?.applyToServer() + } } } diff --git a/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileViewModel.kt b/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileViewModel.kt index 826258d..9701db2 100644 --- a/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileViewModel.kt +++ b/app/src/main/java/com/sopt/carrot/presentation/profile/ProfileViewModel.kt @@ -1,6 +1,12 @@ package com.sopt.carrot.presentation.profile +import android.util.Log import androidx.lifecycle.* +import com.sopt.carrot.data.ApiPool.profileService +import com.sopt.carrot.data.profile.RequestProfileDto +import com.sopt.carrot.data.profile.ResponseProfileDto +import retrofit2.Call +import retrofit2.Response class ProfileViewModel : ViewModel() { @@ -32,7 +38,7 @@ class ProfileViewModel : ViewModel() { init { _isButtonEnabled.value = false selectedGender.value = 1 - introduction.value ="" + introduction.value = "" _isButtonEnabled.addSource(phoneNumberValidation) { validateForm() } _isButtonEnabled.addSource(birthYearValidation) { validateForm() } } @@ -42,10 +48,43 @@ class ProfileViewModel : ViewModel() { } private fun validateBirthYear(birthYear: String?): Boolean { - return !birthYear.isNullOrBlank() && birthYear!!.length == 4 + return !birthYear.isNullOrBlank() && birthYear.length == 4 } private fun validateForm() { - _isButtonEnabled.value = phoneNumberValidation.value ?: false && birthYearValidation.value ?: false + _isButtonEnabled.value = + phoneNumberValidation.value ?: false && birthYearValidation.value ?: false } -} + + fun applyToServer() { + val birthYearInt = birthYear.value?.toIntOrNull() + val phoneNumberValue = phoneNumber.value + if (birthYearInt != null && validateBirthYear(birthYearInt.toString()) && !phoneNumberValue.isNullOrBlank()) { + // 유효한 값인 경우에만 API 호출 + profileService.apply( + RequestProfileDto( + birthYearInt, + selectedGender.value ?: 0, + introduction.value, + name.value, + phoneNumberValue + ) + ).enqueue(object : retrofit2.Callback { + override fun onResponse( + call: Call, + response: Response, + ) { + if (response.isSuccessful) { + Log.d("통신 성공", response.body()?.data.toString()) + } else { + Log.d("통신 실패", response.errorBody()?.string() ?: "Unknown error") + } + } + + override fun onFailure(call: Call, t: Throwable) { + Log.d("통신 실패", t.localizedMessage ?: "Unknown error") + } + }) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_profile.xml b/app/src/main/res/layout/activity_profile.xml index ca22281..6863cc1 100644 --- a/app/src/main/res/layout/activity_profile.xml +++ b/app/src/main/res/layout/activity_profile.xml @@ -248,7 +248,7 @@ android:layout_marginRight="16dp" android:background="@drawable/radius_gray6_radius_1" android:hint="@string/profile_birthHint" - android:inputType="numberDecimal" + android:inputType="number" android:maxLength="4" android:paddingVertical="13dp" android:paddingLeft="18dp"