Skip to content

Commit

Permalink
Feat: 졸업 예정일 api 연결 중... #5
Browse files Browse the repository at this point in the history
  • Loading branch information
dkyuuum committed Feb 6, 2024
1 parent 03bb4bf commit 66abde6
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package umc.com.mobile.project.ui.board
import android.content.Context
import android.os.Bundle
import android.text.Editable
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -56,6 +57,7 @@ class GradDateFragment : Fragment() {
navigateBack() // 뒤로 가기 버튼 클릭 시
initTodayDate() // 오늘 날짜 설정
saveCheeringMemo()
viewModel.getDateInfo()

return binding.root
}
Expand All @@ -80,7 +82,7 @@ class GradDateFragment : Fragment() {

private fun saveCheeringMemo() {
binding.btnSave.setOnClickListener {
viewModel.updateMemo(binding.tvGradDateMemo.text.toString())
// viewModel.updateDateInfo(binding.tvGradDateDate.text.toString())

Toast.makeText(context, "저장되었습니다.", Toast.LENGTH_LONG).show()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,124 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import umc.com.mobile.project.data.model.home.GradDateResponse
import umc.com.mobile.project.data.model.home.UpdateGradDateRequest
import umc.com.mobile.project.data.model.home.UpdateGradDateResponse
import umc.com.mobile.project.data.network.ApiClient
import umc.com.mobile.project.data.network.api.HomeApi

class GradDateViewModel : ViewModel() {
val selectedDate = MutableLiveData<String>()
val cheeringMemo = MutableLiveData<String>()
val selectedDate = MutableLiveData<String>()

private val dateInfoApiService = ApiClient.createService<HomeApi>()

private val _dateResponse: MutableLiveData<GradDateResponse?> = MutableLiveData()
val dateResponse: MutableLiveData<GradDateResponse?>
get() = _dateResponse

private val _updateDateResponse: MutableLiveData<UpdateGradDateResponse?> = MutableLiveData()
val updateDateResponse: MutableLiveData<UpdateGradDateResponse?>
get() = _updateDateResponse


private val _error: MutableLiveData<String> = MutableLiveData()
val error: LiveData<String>
get() = _error

private val _dday: MutableLiveData<Int> = MutableLiveData()
val dday: LiveData<Int>
get() = _dday

private val _cheeringMessage: MutableLiveData<String> = MutableLiveData()
val cheeringMessage: LiveData<String>
get() = _cheeringMessage

fun updateMemo(input: String) {
cheeringMemo.value = input
_cheeringMessage.postValue(input)
}

val isFilledMemo: LiveData<Boolean> = MediatorLiveData<Boolean>().apply {
addSource(cheeringMemo) { value = isFieldFilled() }
addSource(cheeringMessage) { value = isFieldFilled() }
}

private fun isFieldFilled(): Boolean {
return !cheeringMemo.value.isNullOrEmpty()
return !cheeringMessage.value.isNullOrEmpty()
}

fun init(value: GradDateResponse) {
_dday.postValue(value.result.dday)
_cheeringMessage.postValue(value.result.message)
}

fun getDateInfo() {
dateInfoApiService.getDateInfo().enqueue(object : Callback<GradDateResponse> {
override fun onResponse(
call: Call<GradDateResponse>,
response: Response<GradDateResponse>
) {
if (response.isSuccessful) {
val dateInfoResponse = response.body()
if (dateInfoResponse != null) {
dateResponse.postValue(dateInfoResponse)
Log.d("gradDate", "${response.body()}")
} else {
_error.postValue("서버 응답이 올바르지 않습니다.")
}
} else {
_error.postValue("사용자 정보를 가져오지 못했습니다.")
try {
throw response.errorBody()?.string()?.let {
RuntimeException(it)
} ?: RuntimeException("Unknown error")
} catch (e: Exception) {
Log.e("gradDate", "API 오류: ${e.message}")
}
}
}

override fun onFailure(call: Call<GradDateResponse>, t: Throwable) {
_error.postValue("네트워크 오류: ${t.message}")
Log.d("gradDate", "${t.message}")
}
})
}

fun updateDateInfo(date: String) {
dateInfoApiService.updateDateInfo(
UpdateGradDateRequest(date, cheeringMessage.value.orEmpty())
).enqueue(object : Callback<UpdateGradDateResponse> {
override fun onResponse(
call: Call<UpdateGradDateResponse>,
response: Response<UpdateGradDateResponse>
) {
if (response.isSuccessful) {
val updateDateInfoResponse = response.body()
if (updateDateInfoResponse != null) {
updateDateResponse.postValue(updateDateInfoResponse)
Log.d("updateGradDate", "${response.body()}")
} else {
_error.postValue("서버 응답이 올바르지 않습니다.")
}
} else {
_error.postValue("사용자 정보를 가져오지 못했습니다.")
try {
throw response.errorBody()?.string()?.let {
RuntimeException(it)
} ?: RuntimeException("Unknown error")
} catch (e: Exception) {
Log.e("updateGradDate", "API 오류: ${e.message}")
}
}
}

override fun onFailure(call: Call<UpdateGradDateResponse>, t: Throwable) {
_error.postValue("네트워크 오류: ${t.message}")
Log.d("updateGradDate", "${t.message}")
}
})
}

}
2 changes: 0 additions & 2 deletions app/src/main/res/layout/fragment_grad_date.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@
android:layout_marginTop="59dp"
android:layout_marginEnd="81dp"
android:layout_marginBottom="60dp"
android:text="@string/board_grad_date_dday"
android:textAppearance="@style/SemiBoldFont.36"
android:textColor="#7D7D7D"
app:layout_constraintBottom_toBottomOf="@+id/iv_grad_date_card2"
Expand All @@ -131,7 +130,6 @@
android:background="@drawable/bg_solid_white_radius_10"
android:gravity="top"
android:hint="@string/board_grad_date_memo"
android:text="@{vm.cheeringMemo}"
android:inputType="text"
android:paddingStart="12dp"
android:paddingTop="17dp"
Expand Down

0 comments on commit 66abde6

Please sign in to comment.