From 511b6e40a7d46074a5de00b40054877735530600 Mon Sep 17 00:00:00 2001 From: harish Date: Fri, 22 Sep 2023 21:58:38 +0530 Subject: [PATCH] Health Data rendering PDF and Image --- .../ui/main/adapters/HealthDataAdapter.kt | 80 ++++++++++++++++--- .../abha/ui/main/fragment/AbdmPdfViewer.kt | 39 ++++++++- .../fragment/PatientHealthDataFragment.kt | 12 ++- .../res/layout/fragment_img_pdf_layout.xml | 64 +++++++++++++++ app/src/main/res/layout/fragment_pdf.xml | 20 ----- .../main/res/layout/health_section_layout.xml | 2 + app/src/main/res/layout/key_value_layout.xml | 2 + app/src/main/res/values/themes.xml | 5 ++ 8 files changed, 192 insertions(+), 32 deletions(-) create mode 100644 app/src/main/res/layout/fragment_img_pdf_layout.xml delete mode 100644 app/src/main/res/layout/fragment_pdf.xml diff --git a/app/src/main/java/org/commcare/dalvik/abha/ui/main/adapters/HealthDataAdapter.kt b/app/src/main/java/org/commcare/dalvik/abha/ui/main/adapters/HealthDataAdapter.kt index a21735f..63ca9a7 100644 --- a/app/src/main/java/org/commcare/dalvik/abha/ui/main/adapters/HealthDataAdapter.kt +++ b/app/src/main/java/org/commcare/dalvik/abha/ui/main/adapters/HealthDataAdapter.kt @@ -1,8 +1,10 @@ package org.commcare.dalvik.abha.ui.main.adapters +import android.graphics.Paint import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.Toast import androidx.core.content.ContextCompat import androidx.recyclerview.widget.RecyclerView import androidx.transition.AutoTransition @@ -14,8 +16,12 @@ import org.commcare.dalvik.abha.databinding.PatientHealthDataBinding import org.commcare.dalvik.abha.databinding.PatientHealthDataCellBinding import org.commcare.dalvik.domain.model.HealthContentModel import org.commcare.dalvik.domain.model.KeyValueModel +import timber.log.Timber -class HealthDataAdapter(private var dataList: List) : +class HealthDataAdapter( + private var dataList: List, + val callback: (fileData: FileData) -> Any +) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HealthDataViewHolder { @@ -39,35 +45,85 @@ class HealthDataAdapter(private var dataList: List) : fun bindModel(model: HealthContentModel) { binding.model = model binding.sectionHolder.removeAllViews() + val context = binding.root.context model.content.forEach { sectionModel -> val sectionBinding = - HealthDataSectionBinding.inflate(LayoutInflater.from(binding.root.context)) + HealthDataSectionBinding.inflate(LayoutInflater.from(context)) sectionBinding.model = sectionModel + var skipNextEntry = false + sectionModel.entries.forEachIndexed { index, sectionEntry -> + if (skipNextEntry) { + skipNextEntry = false + return@forEachIndexed + } val kvBinding = - KeyValueBinding.inflate(LayoutInflater.from(binding.root.context)) + KeyValueBinding.inflate(LayoutInflater.from(context)) if (index % 2 == 0) { kvBinding.tableRow.setBackgroundColor( ContextCompat.getColor( - binding.root.context, + context, R.color.white ) ) } else { kvBinding.tableRow.setBackgroundColor( ContextCompat.getColor( - binding.root.context, + context, R.color.grey_lighter ) ) } - kvBinding.model = KeyValueModel( - sectionEntry.label, - if (sectionModel.resource == "Binary") "View File" else sectionEntry.value - ) + val value = if (sectionModel.resource == "Binary") { + var returnText ="Open " + kvBinding.vText.apply { + setTextColor( + ContextCompat.getColor( + context, + R.color.blue + ) + ) + paintFlags = Paint.UNDERLINE_TEXT_FLAG + skipNextEntry = true + + + + val fileType = when (sectionModel.entries[index + 1].value) { + "application/pdf" -> { + returnText = "Open PDF" + FileType.PDF + } + "image/jpeg" -> { + returnText = "Open Image" + FileType.IMAGE + } + else -> { + FileType.INVALID + } + } + + tag = FileData(fileType, sectionEntry.value) + + setOnClickListener { + val fileData = tag as FileData + fileData?.let { + callback.invoke(it) + } + + } + } + returnText + } else { + skipNextEntry = false + sectionEntry.value + } + + kvBinding.model = KeyValueModel(sectionEntry.label, value) + sectionBinding.sectionEntryHolder.addView(kvBinding.root) } + binding.sectionHolder.addView(sectionBinding.root) binding.expanderView.setOnClickListener { if (binding.sectionHolder.visibility == View.GONE) { @@ -93,3 +149,9 @@ class HealthDataAdapter(private var dataList: List) : } } +data class FileData(val fileType: FileType, val fileData: String) + +enum class FileType { + IMAGE, PDF, INVALID +} + diff --git a/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/AbdmPdfViewer.kt b/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/AbdmPdfViewer.kt index 63ccf2f..73399c9 100644 --- a/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/AbdmPdfViewer.kt +++ b/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/AbdmPdfViewer.kt @@ -1,13 +1,48 @@ package org.commcare.dalvik.abha.ui.main.fragment +import android.net.Uri import android.os.Bundle +import android.view.LayoutInflater import android.view.View -import org.commcare.dalvik.abha.databinding.PdfBinding +import android.view.ViewGroup +import androidx.fragment.app.DialogFragment +import org.commcare.dalvik.abha.R +import org.commcare.dalvik.abha.databinding.ImgPdfBinding +import org.commcare.dalvik.abha.ui.main.adapters.FileData +import org.commcare.dalvik.abha.ui.main.adapters.FileType +import java.io.File +import java.io.FileOutputStream +import java.util.Base64 -class AbdmPdfViewer: BaseFragment(PdfBinding::inflate) { +class AbdmImgAndPdfViewer(private val fileData: FileData) : DialogFragment() { + + lateinit var binding: ImgPdfBinding + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + isCancelable = false + binding = ImgPdfBinding.inflate(inflater, container, false) + binding.model = fileData + return binding.root + } + + override fun getTheme(): Int { + return R.style.DialogTheme + } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + if (fileData.fileType == FileType.PDF) { + val decoded = Base64.getDecoder().decode(fileData.fileData) + binding.pdfView.fromBytes(decoded).load() + + } + + binding.closeDialog.setOnClickListener { + dismiss() + } } } \ No newline at end of file diff --git a/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/PatientHealthDataFragment.kt b/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/PatientHealthDataFragment.kt index cccaf66..e744a5e 100644 --- a/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/PatientHealthDataFragment.kt +++ b/app/src/main/java/org/commcare/dalvik/abha/ui/main/fragment/PatientHealthDataFragment.kt @@ -2,6 +2,7 @@ package org.commcare.dalvik.abha.ui.main.fragment import android.os.Bundle import android.view.View +import android.widget.Toast import androidx.fragment.app.activityViewModels import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope @@ -9,6 +10,8 @@ import androidx.lifecycle.repeatOnLifecycle import com.google.gson.Gson import kotlinx.coroutines.launch import org.commcare.dalvik.abha.databinding.PatientHealthDataBinding +import org.commcare.dalvik.abha.ui.main.adapters.FileData +import org.commcare.dalvik.abha.ui.main.adapters.FileType import org.commcare.dalvik.abha.ui.main.adapters.HealthDataAdapter import org.commcare.dalvik.abha.viewmodel.GenerateAbhaUiState import org.commcare.dalvik.abha.viewmodel.PatientViewModel @@ -27,7 +30,7 @@ class PatientHealthDataFragment : BaseFragment(Patient arguments?.getString("artefactId")?.let {artefactId -> - healthDataAdapter = HealthDataAdapter(healthDataList) + healthDataAdapter = HealthDataAdapter(healthDataList,this::launchImgAndPdfFragment) binding.patientHealthDataList.adapter = healthDataAdapter observeUiState() @@ -36,6 +39,13 @@ class PatientHealthDataFragment : BaseFragment(Patient } + private fun launchImgAndPdfFragment(fileData: FileData){ + if(fileData.fileType == FileType.INVALID){ + Toast.makeText(context, "Invalid file type.",Toast.LENGTH_LONG).show() + } + val dialogFragment = AbdmImgAndPdfViewer(fileData) + dialogFragment.show(parentFragmentManager, "healthData") + } private fun observeUiState() { viewLifecycleOwner.lifecycleScope.launch { repeatOnLifecycle(Lifecycle.State.STARTED) { diff --git a/app/src/main/res/layout/fragment_img_pdf_layout.xml b/app/src/main/res/layout/fragment_img_pdf_layout.xml new file mode 100644 index 0000000..328fcd8 --- /dev/null +++ b/app/src/main/res/layout/fragment_img_pdf_layout.xml @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_pdf.xml b/app/src/main/res/layout/fragment_pdf.xml deleted file mode 100644 index c530384..0000000 --- a/app/src/main/res/layout/fragment_pdf.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - diff --git a/app/src/main/res/layout/health_section_layout.xml b/app/src/main/res/layout/health_section_layout.xml index 58584c9..3fe3890 100644 --- a/app/src/main/res/layout/health_section_layout.xml +++ b/app/src/main/res/layout/health_section_layout.xml @@ -20,6 +20,8 @@ android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:text="@{model.section}" + style="@style/Headline3" + tools:text="Title" app:layout_constraintBottom_toTopOf="@+id/sectionEntryHolder" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" diff --git a/app/src/main/res/layout/key_value_layout.xml b/app/src/main/res/layout/key_value_layout.xml index 3d16703..b35bc5c 100644 --- a/app/src/main/res/layout/key_value_layout.xml +++ b/app/src/main/res/layout/key_value_layout.xml @@ -53,6 +53,7 @@ > @color/black 12sp + + \ No newline at end of file