Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 이상증상 관련 로그인 정보 연결 및 스낵바 이벤트 구현 #117

Merged
merged 11 commits into from
Jan 19, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ val Context.dogDataStore: DataStore<Preferences> by preferencesDataStore(name =

@HiltAndroidApp
class GlobalApplication : Application() {
lateinit var context: Context

init {
instance = this
}

companion object {
private var instance: GlobalApplication? = null

fun applicationContext(): Context {
return instance!!.applicationContext
}
}

override fun onCreate() {
super.onCreate()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ import retrofit2.http.Query
interface SymptomAPI {
@GET("/symptom/{dogId}")
suspend fun getSymptomList(
@Header("AccessToken") accessToken: String,
@Path("dogId") dogId: Int,
@Header("AccessToken") accessToken: String?,
@Path("dogId") dogId: Long?,
@Query("dateTime") dateTime: String,
): Response<ResultSymptom>

@POST("/symptom")
suspend fun addSymptom(
@Header("AccessToken") accessToken: String,
@Header("AccessToken") accessToken: String?,
@Body requestBody: ToAddSymptom,
): Response<ResponseBody>

@DELETE("/symptom")
suspend fun deleteSymptom(
@Header("AccessToken") accessToken: String,
@Header("AccessToken") accessToken: String?,
@Query("symptomIds") symtomIds: IntArray,
): Response<ResponseBody>

@PATCH("/symptom")
suspend fun patchSymptom(
@Header("AccessToken") accessToken: String,
@Header("AccessToken") accessToken: String?,
@Body requestBody: ToEditSymptom,
): Response<ResponseBody>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,72 +1,83 @@
package com.project.meongcare.symptom.model.data.repository

import com.project.meongcare.MainActivity
import com.project.meongcare.symptom.model.data.remote.SymptomAPI
import com.project.meongcare.symptom.model.data.remote.SymptomRetrofitInstance
import com.project.meongcare.symptom.model.entities.ResultSymptom
import com.project.meongcare.symptom.model.entities.ToAddSymptom
import com.project.meongcare.symptom.model.entities.ToEditSymptom
import okhttp3.ResponseBody
import javax.inject.Inject

class SymptomRepository {
companion object {
private val symptomAPI: SymptomAPI =
SymptomRetrofitInstance.getInstance().create(SymptomAPI::class.java)
}
class SymptomRepository
@Inject
constructor() {
companion object {
private val symptomAPI: SymptomAPI =
SymptomRetrofitInstance.getInstance().create(SymptomAPI::class.java)
}

suspend fun getSymptomByDogId(
accessToken: String?,
dogId: Long?,
dateTime: String,
): Result<ResultSymptom> =
kotlin.runCatching {
val response =
symptomAPI.getSymptomList(
accessToken,
dogId,
dateTime,
)
if (response.isSuccessful) {
response.body()
?: throw RuntimeException("Symptom get API 통신 실패")
} else {
throw RuntimeException("Symptom get API 통신 실패")
}
}

suspend fun getSymptomByDogId(
dogId: Int,
dateTime: String,
): Result<ResultSymptom> =
kotlin.runCatching {
suspend fun addSymptom(
accessToken: String?,
toAddSymptom: ToAddSymptom,
): Int {
val response =
symptomAPI.getSymptomList(
MainActivity.ACCESS_TOKEN,
dogId,
dateTime,
symptomAPI.addSymptom(
accessToken,
toAddSymptom,
)
if (response.isSuccessful) {
response.body()
?: throw RuntimeException("Symptom get API 통신 실패")
} else {
throw RuntimeException("Symptom get API 통신 실패")
}
return response.code()
}

suspend fun addSymptom(toAddSymptom: ToAddSymptom): Int {
val response =
symptomAPI.addSymptom(
MainActivity.ACCESS_TOKEN,
toAddSymptom,
)
return response.code()
}

suspend fun deleteSymptom(symptomIds: IntArray): Int {
val response =
symptomAPI.deleteSymptom(
MainActivity.ACCESS_TOKEN,
symptomIds,
)

return response.code()
}

suspend fun patchSymptom(toEditSymptom: ToEditSymptom): Result<ResponseBody> =
kotlin.runCatching {
suspend fun deleteSymptom(
accessToken: String?,
symptomIds: IntArray,
): Int {
val response =
symptomAPI.patchSymptom(
MainActivity.ACCESS_TOKEN,
toEditSymptom,
symptomAPI.deleteSymptom(
accessToken,
symptomIds,
)
return response.code()
}

if (response.isSuccessful) {
response.body() ?: throw RuntimeException("이상증상 수정 API 통신 에러")
} else {
val errorBody = response.errorBody()?.string()
val errorMessage =
"이상증상 수정 API 통신 에러 코드 ${response.code()}, 전문: $errorBody"
throw RuntimeException(errorMessage)
suspend fun patchSymptom(
accessToken: String?,
toEditSymptom: ToEditSymptom,
): Result<ResponseBody> =
kotlin.runCatching {
val response =
symptomAPI.patchSymptom(
accessToken,
toEditSymptom,
)

if (response.isSuccessful) {
response.body() ?: throw RuntimeException("이상증상 수정 API 통신 에러")
} else {
val errorBody = response.errorBody()?.string()
val errorMessage =
"이상증상 수정 API 통신 에러 코드 ${response.code()}, 전문: $errorBody"
throw RuntimeException(errorMessage)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.project.meongcare.R
import com.project.meongcare.symptom.model.entities.Symptom
import com.project.meongcare.symptom.model.entities.SymptomType
import com.project.meongcare.symptom.view.bottomSheet.SymptomBottomSheetDialogFragment
import java.text.SimpleDateFormat
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
Expand All @@ -26,14 +27,6 @@ class SymptomUtils {
return dateTime.format(outputFormatter)
}

fun convertDateToSimpleTime(localMili: String): String {
val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
val dateTime = LocalDateTime.parse(localMili, inputFormatter)

val outputFormatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault())
return dateTime.format(outputFormatter)
}

fun convertDateToMonthDate(localMili: String): String {
val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")
val dateTime = LocalDateTime.parse(localMili, inputFormatter)
Expand Down Expand Up @@ -65,6 +58,13 @@ class SymptomUtils {
return localDateTime.format(formatter)
}

fun convertDateToSimpleTime(inputDateString: String): String {
val inputFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
val date: Date = inputFormat.parse(inputDateString)
val outputFormat = SimpleDateFormat("HH:mm:ss")
return outputFormat.format(date)
}

fun getSymptomImg(symptomData: Symptom): Int {
return when (symptomData.symptomString) {
SymptomType.WEIGHT_LOSS.symptomName -> R.drawable.all_weighing_machine
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import androidx.navigation.fragment.findNavController
import com.project.meongcare.MainActivity
import com.project.meongcare.R
import com.project.meongcare.databinding.FragmentSymptomAddBinding
import com.project.meongcare.snackbar.view.CustomSnackBar
import com.project.meongcare.symptom.model.data.repository.SymptomRepository
import com.project.meongcare.symptom.model.entities.ToAddSymptom
import com.project.meongcare.symptom.utils.SymptomUtils.Companion.getSymptomName
import com.project.meongcare.symptom.utils.SymptomUtils.Companion.hideKeyboard
import com.project.meongcare.symptom.utils.SymptomUtils.Companion.showCalendarBottomSheet
Expand Down Expand Up @@ -53,8 +53,6 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
val factory = SymptomViewModelFactory(SymptomRepository())
symptomViewModel = ViewModelProvider(this, factory)[SymptomViewModel::class.java]

getBundleOfSymptomSelectData()

symptomViewModel.run {
if (sharedPreferences.getInt("symptomItemImgId", 0) != 0 && sharedPreferences.getString(
"symptomItemTitle",
Expand All @@ -76,9 +74,7 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
setGetItem(it)
}

addSymptomCode.observe(viewLifecycleOwner) {
checkSuccessToAdd(it)
}
checkAddSuccess()
}

fragmentSymptomAddBinding.run {
Expand Down Expand Up @@ -111,6 +107,19 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
return fragmentSymptomAddBinding.root
}

private fun checkAddSuccess() {
symptomViewModel.addSymptomCode.observe(viewLifecycleOwner) {
if (it == 200) {
Log.d("이상증상 추가 코드1", it.toString())
showSuccessSnackbar()
findNavController().popBackStack()
} else {
Log.d("이상증상 추가 코드2", it.toString())
showFailSnackbar()
}
}
}

private fun getItemCustom() {
val editTextItemCustom = fragmentSymptomAddBinding.editTextSymptomAddCustom
editTextItemCustom.setOnEditorActionListener { t, a, k ->
Expand Down Expand Up @@ -160,13 +169,6 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
}
}

private fun checkSuccessToAdd(it: Int?) {
if (it == 200) {
symptomViewModel.clearLiveData()
findNavController().popBackStack()
}
}

private fun setGetItem(visible: Int?) {
if (visible == View.VISIBLE) {
fragmentSymptomAddBinding.textViewSymptomAddSelectSymptom.run {
Expand All @@ -188,15 +190,6 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
}
}

private fun getBundleOfSymptomSelectData() {
if (arguments?.getInt("symptomItemImgId") != null) {
Log.d("번들 확인1", arguments?.getInt("symptomItemImgId").toString())
Log.d("번들 확인2", arguments?.getString("symptomItemTitle").toString())
symptomViewModel.symptomItemImgId.value = arguments?.getInt("symptomItemImgId")
symptomViewModel.symptomItemTitle.value = arguments?.getString("symptomItemTitle")
}
}

private fun setClearEditTextSymptomAddCustom() {
fragmentSymptomAddBinding.editTextSymptomAddCustom.text.clear()
fragmentSymptomAddBinding.editTextSymptomAddCustom.clearFocus()
Expand All @@ -206,7 +199,7 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
val dateTimeString = getDateTimeString()
val addItemName = getAddItemName()
val addItemTitle = symptomViewModel.symptomItemTitle.value
checkNullAndAddData(dateTimeString, addItemName, addItemTitle)
checkNullAndAddData(addItemName, addItemTitle, dateTimeString)
}

private fun getAddItemName() =
Expand Down Expand Up @@ -291,22 +284,37 @@ class SymptomAddFragment : Fragment(), SymptomBottomSheetDialogFragment.OnDateSe
}

private fun checkNullAndAddData(
dateTimeString: String?,
addItemName: String?,
addItemTitle: String?,
dateTimeString: String?,
) {
if (dateTimeString != null && addItemName != null && addItemTitle != null) {
val toAddSymptom = ToAddSymptom(1, addItemName, addItemTitle, dateTimeString)
symptomViewModel.addSymptomData(toAddSymptom)
symptomViewModel.addSymptomData(addItemName, addItemTitle, dateTimeString)
}
}

private fun showSuccessSnackbar() {
CustomSnackBar.make(
activity?.findViewById(android.R.id.content)!!,
R.drawable.snackbar_success_16dp,
"추가가 완료되었습니다",
).show()
}

private fun showFailSnackbar() {
CustomSnackBar.make(
activity?.findViewById(android.R.id.content)!!,
R.drawable.snackbar_error_16dp,
"추가에 실패하였습니다.\n잠시 후 다시 시도해주세요",
).show()
}

override fun onDateSelected(date: LocalDate) {
symptomViewModel.updateSymptomDate(date, isEditSymptom)
}

override fun onDestroy() {
super.onDestroy()
override fun onDestroyView() {
super.onDestroyView()
editor.remove("symptomItemTitle")
editor.remove("symptomItemImgID")
editor.apply()
Expand Down
Loading