Skip to content

Commit

Permalink
Merge pull request #178 from Semoban/feature/fix_medical_record_image…
Browse files Browse the repository at this point in the history
…_logic

fix: 진료기록 이미지 처리 로직 변경
  • Loading branch information
iiolo authored Apr 27, 2024
2 parents c1f6652 + 0a4222d commit 3e718a5
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 64 deletions.
6 changes: 2 additions & 4 deletions app/src/main/java/com/project/meongcare/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ class MainActivity : AppCompatActivity() {
fragmentContainerView.findNavController().navigate(R.id.homeFragment)
}
else -> {
showUpdateDialog()
// fragmentContainerView.findNavController().navigate(R.id.medicalRecordFragment)
fragmentContainerView.findNavController().navigate(R.id.medicalRecordFragment)
}
}

Expand All @@ -82,8 +81,7 @@ class MainActivity : AppCompatActivity() {
if (menuItem.itemId == R.id.menuMainBottomNavHome) {
fragmentContainerView.findNavController().navigate(R.id.homeFragment)
} else {
showUpdateDialog()
// fragmentContainerView.findNavController().navigate(R.id.medicalRecordFragment)
fragmentContainerView.findNavController().navigate(R.id.medicalRecordFragment)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.project.meongcare.medicalRecord.model.data.remote

import com.project.meongcare.medicalRecord.model.entities.MedicalRecordDto
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGet
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGetResponse
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordPutDto
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.DELETE
import retrofit2.http.GET
import retrofit2.http.Header
Expand Down Expand Up @@ -36,19 +39,15 @@ interface MedicalRecordApi {
@Header("AccessToken") accessToken: String,
): Response<Int>

@Multipart
@POST("medical-record")
suspend fun addMedicalRecord(
@Header("AccessToken") accessToken: String?,
@Part filePart: MultipartBody.Part?,
@Part("dto") medicalRecordDto: RequestBody,
@Body medicalRecordDto: MedicalRecordDto,
): Response<ResponseBody>

@Multipart
@PUT("medical-record")
suspend fun putMedicalRecord(
@Header("AccessToken") accessToken: String?,
@Part filePart: MultipartBody.Part,
@Part("dto") medicalRecordPutDto: RequestBody,
@Body medicalRecordPutDto: MedicalRecordPutDto,
): Response<ResponseBody>
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.project.meongcare.medicalRecord.model.data.repository

import com.project.meongcare.medicalRecord.model.entities.MedicalRecordDto
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGet
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGetResponse
import com.project.meongcare.medicalRecord.model.entities.RequestMedicalRecord
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordPutDto
import retrofit2.Response

interface MedicalRecordRepository {
Expand All @@ -24,11 +25,11 @@ interface MedicalRecordRepository {

suspend fun addMedicalRecord(
accessToken: String?,
requestMedicalRecord: RequestMedicalRecord,
medicalRecordDto: MedicalRecordDto,
): Int

suspend fun putMedicalRecord(
accessToken: String?,
requestMedicalRecord: RequestMedicalRecord,
medicalRecordPutDto: MedicalRecordPutDto,
): Int
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGet
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordGetResponse
import com.project.meongcare.medicalRecord.model.entities.RequestMedicalRecord
import com.project.meongcare.medicalRecord.model.data.remote.MedicalRecordRetrofitClient
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordDto
import com.project.meongcare.medicalRecord.model.entities.MedicalRecordPutDto
import org.json.JSONObject
import retrofit2.Response
import javax.inject.Inject
Expand Down Expand Up @@ -73,26 +75,24 @@ class MedicalRecordRepositoryImpl

override suspend fun addMedicalRecord(
accessToken: String?,
requestMedicalRecord: RequestMedicalRecord,
medicalRecordDto: MedicalRecordDto,
): Int {
val response =
medicalRecordRetrofitClient.medicalRecordApi.addMedicalRecord(
accessToken,
requestMedicalRecord.file,
requestMedicalRecord.dto,
medicalRecordDto,
)
return response.code()
}

override suspend fun putMedicalRecord(
accessToken: String?,
requestMedicalRecord: RequestMedicalRecord,
medicalRecordPutDto: MedicalRecordPutDto,
): Int {
val response =
medicalRecordRetrofitClient.medicalRecordApi.putMedicalRecord(
accessToken,
requestMedicalRecord.file,
requestMedicalRecord.dto,
medicalRecordPutDto,
)
return response.code()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data class MedicalRecordDto(
val hospitalName: String,
val doctorName: String,
val note: String,
val imageURL: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ data class MedicalRecordGet(
val hospitalName: String,
val doctorName: String,
val note: String,
val imageUrl: String,
val imageUrl: String?,
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data class MedicalRecordPutDto(
val hospitalName: String,
val doctorName: String,
val note: String,
val imageURL: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,26 @@ import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.bumptech.glide.Glide
import com.project.meongcare.MainActivity
import com.project.meongcare.BuildConfig
import com.project.meongcare.R
import com.project.meongcare.aws.util.AWSS3ImageUtils.convertUriToFile
import com.project.meongcare.aws.util.MEDICAL_RECORD_FOLDER_PATH
import com.project.meongcare.aws.util.PARENT_FOLDER_PATH
import com.project.meongcare.aws.viewmodel.AWSS3ViewModel
import com.project.meongcare.databinding.FragmentMedicalRecordAddBinding
import com.project.meongcare.medicalRecord.model.data.local.OnPictureChangedListener
import com.project.meongcare.medicalRecord.model.utils.MedicalRecordUtils
import com.project.meongcare.medicalRecord.view.bottomSheet.MedicalRecordDateBottomSheetDialogFragment
import com.project.meongcare.medicalRecord.view.bottomSheet.MedicalRecordPictureBottomSheetDialogFragment
import com.project.meongcare.medicalRecord.viewmodel.DogViewModel
import com.project.meongcare.medicalRecord.viewmodel.MedicalRecordViewModel
import com.project.meongcare.medicalRecord.viewmodel.UserViewModel
import com.project.meongcare.snackbar.view.CustomSnackBar
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File
import java.time.LocalDate
import java.time.format.DateTimeFormatter

Expand All @@ -38,10 +49,17 @@ class MedicalRecordAddFragment :
OnPictureChangedListener {
private lateinit var binding: FragmentMedicalRecordAddBinding
private lateinit var mainActivity: MainActivity
private lateinit var filePath: String
private lateinit var imageFile: File

private val medicalRecordViewModel: MedicalRecordViewModel by viewModels()
private val awsS3ViewModel: AWSS3ViewModel by viewModels()
private val userViewModel: UserViewModel by viewModels()
private val dogViewModel: DogViewModel by viewModels()

private var addSelectedDate: String = ""
private var accessToken = ""
private var dogId = -1L

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -58,6 +76,8 @@ class MedicalRecordAddFragment :
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)
getAccessToken()
getDogId()
initImg()
initDateBtn()
initHospitalName()
Expand All @@ -66,11 +86,58 @@ class MedicalRecordAddFragment :
initCompleteBtn()
}

private fun getAccessToken() {
userViewModel.accessTokenPreferencesLiveData.observe(viewLifecycleOwner) { accessToken ->
if (accessToken != null) {
this.accessToken = accessToken
}
}
}

private fun getDogId() {
dogViewModel.dogIdPreferencesLiveData.observe(viewLifecycleOwner) { dogId ->
if (dogId != null) {
this.dogId = dogId
}
}
}

private fun getPreSignedURL() {
awsS3ViewModel.getPreSignedUrl(accessToken, filePath)
awsS3ViewModel.preSignedUrl.observe(viewLifecycleOwner) { response ->
if (response != null) {
val requestBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
uploadImage(response.preSignedUrl, requestBody)
}
}
}

private fun uploadImage(
preSignedURL: String,
requestBody: RequestBody,
) {
awsS3ViewModel.uploadImageToS3(preSignedURL, requestBody)
awsS3ViewModel.uploadImageResponse.observe(viewLifecycleOwner) { response ->
if (response == 200) {
val imageURL = BuildConfig.AWS_S3_BASE_URL + filePath
postMedicalRecord(imageURL)
showResultMessage()
}
}
}

private fun initCompleteBtn() {
binding.layoutMedicalrecordaddNoteRecord.buttonFooterone.setOnClickListener {
if (checkMedicalRecordDataNull()) {
postMedicalRecord()
showResultMessage()
if (checkMedicalRecordDataNull() && accessToken.isNotEmpty() && dogId != -1L) {
val uri = medicalRecordViewModel.medicalRecordAddImgUri.value!!
if (uri == Uri.EMPTY) {
postMedicalRecord(null)
showResultMessage()
} else {
imageFile = convertUriToFile(requireContext(), uri)
filePath = "$PARENT_FOLDER_PATH$MEDICAL_RECORD_FOLDER_PATH${imageFile.name}"
getPreSignedURL()
}
}
}
}
Expand All @@ -86,8 +153,7 @@ class MedicalRecordAddFragment :
}
}

private fun postMedicalRecord() {
val uri = medicalRecordViewModel.medicalRecordAddImgUri.value
private fun postMedicalRecord(imageURL: String?) {
val date = addSelectedDate
val time =
String.format(
Expand All @@ -101,11 +167,13 @@ class MedicalRecordAddFragment :
val note = binding.edittextMedicalrecordaddNoteDetail.text.toString()

medicalRecordViewModel.addMedicalRecord(
accessToken,
dogId,
dateTime,
hospitalName,
doctorName,
note,
uri ?: Uri.EMPTY,
imageURL,
)
}

Expand Down
Loading

0 comments on commit 3e718a5

Please sign in to comment.