Skip to content

Commit

Permalink
[FEAT] #8-server post response
Browse files Browse the repository at this point in the history
  • Loading branch information
librarywon committed May 26, 2023
1 parent da8e6f3 commit 96c3dd4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 5 deletions.
44 changes: 44 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/profile/ProfileDto.kt
Original file line number Diff line number Diff line change
@@ -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
)
}
15 changes: 15 additions & 0 deletions app/src/main/java/com/sopt/carrot/data/profile/ProfileService.kt
Original file line number Diff line number Diff line change
@@ -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<ResponseProfileDto>
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class ProfileActivity : AppCompatActivity() {
val dialog = CustomPopupDialog(this@ProfileActivity)
dialog.show()
}
btnProfileAgreeAndApply.setOnClickListener {
viewModel?.applyToServer()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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() {

Expand Down Expand Up @@ -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() }
}
Expand All @@ -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<ResponseProfileDto> {
override fun onResponse(
call: Call<ResponseProfileDto>,
response: Response<ResponseProfileDto>,
) {
if (response.isSuccessful) {
Log.d("통신 성공", response.body()?.data.toString())
} else {
Log.d("통신 실패", response.errorBody()?.string() ?: "Unknown error")
}
}

override fun onFailure(call: Call<ResponseProfileDto>, t: Throwable) {
Log.d("통신 실패", t.localizedMessage ?: "Unknown error")
}
})
}
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 96c3dd4

Please sign in to comment.