Skip to content

Commit

Permalink
Health Data rendering
Browse files Browse the repository at this point in the history
PDF and Image
  • Loading branch information
RawatH committed Sep 22, 2023
1 parent 124cc94 commit 511b6e4
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<HealthContentModel>) :
class HealthDataAdapter(
private var dataList: List<HealthContentModel>,
val callback: (fileData: FileData) -> Any
) :
RecyclerView.Adapter<HealthDataAdapter.HealthDataViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HealthDataViewHolder {
Expand All @@ -39,35 +45,85 @@ class HealthDataAdapter(private var dataList: List<HealthContentModel>) :
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) {
Expand All @@ -93,3 +149,9 @@ class HealthDataAdapter(private var dataList: List<HealthContentModel>) :
}
}

data class FileData(val fileType: FileType, val fileData: String)

enum class FileType {
IMAGE, PDF, INVALID
}

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

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ 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
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
Expand All @@ -27,7 +30,7 @@ class PatientHealthDataFragment : BaseFragment<PatientHealthDataBinding>(Patient

arguments?.getString("artefactId")?.let {artefactId ->

healthDataAdapter = HealthDataAdapter(healthDataList)
healthDataAdapter = HealthDataAdapter(healthDataList,this::launchImgAndPdfFragment)
binding.patientHealthDataList.adapter = healthDataAdapter

observeUiState()
Expand All @@ -36,6 +39,13 @@ class PatientHealthDataFragment : BaseFragment<PatientHealthDataBinding>(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) {
Expand Down
64 changes: 64 additions & 0 deletions app/src/main/res/layout/fragment_img_pdf_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<data class="ImgPdfBinding">

<variable
name="model"
type="org.commcare.dalvik.abha.ui.main.adapters.FileData" />

<import type="org.commcare.dalvik.abha.ui.main.adapters.FileType" />

<import type="android.view.View" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/closeDialog"
android:layout_width="@dimen/_32dp"
android:layout_height="@dimen/_32dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:elevation="@dimen/_10dp"
app:tint="@color/blue"
app:srcCompat="@drawable/ic_baseline_close_24"
/>

<ImageView
android:id="@+id/reportImage"
android:layout_width="0dp"
android:layout_height="0dp"
app:loadImage="@{model.fileData}"
android:visibility="@{model.fileType == FileType.IMAGE ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
tools:srcCompat="@tools:sample/avatars" />

<com.github.barteksc.pdfviewer.PDFView
android:id="@+id/pdfView"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="@{model.fileType == FileType.PDF ? View.VISIBLE : View.GONE}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5">

</com.github.barteksc.pdfviewer.PDFView>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

20 changes: 0 additions & 20 deletions app/src/main/res/layout/fragment_pdf.xml

This file was deleted.

2 changes: 2 additions & 0 deletions app/src/main/res/layout/health_section_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/key_value_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
>

<TextView
android:id="@+id/kText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical|start"
Expand All @@ -62,6 +63,7 @@
style="@style/Headline2"/>

<TextView
android:id="@+id/vText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical|end"
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/themes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<style name="HiTypeStyle" parent="@style/Widget.MaterialComponents.Chip.Entry">
<item name="android:textColor">@color/black</item>
<item name="android:textSize">12sp</item>
</style>

<style name="DialogTheme" parent="Theme.ABDMApp">
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">false</item>
<item name="android:windowIsFloating">false</item>
</style>
</resources>

0 comments on commit 511b6e4

Please sign in to comment.