Skip to content

Commit

Permalink
feat: drug record click/fetch data and expandable list with interacti…
Browse files Browse the repository at this point in the history
…ng data
  • Loading branch information
feveral committed Apr 15, 2023
1 parent 57be24c commit cbb157d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.junting.drug_android_frontend
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.View.GONE
import android.widget.CheckBox
import android.widget.EditText
import android.widget.LinearLayout
Expand All @@ -14,6 +15,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.timepicker.MaterialTimePicker
import com.google.android.material.timepicker.TimeFormat
import com.junting.drug_android_frontend.databinding.ActivityEditDrugRecordBinding
import com.junting.drug_android_frontend.model.drug_record.InteractingDrug
import com.junting.drug_android_frontend.ui.libs.ExpandableListUtils
import java.util.*

Expand All @@ -22,14 +24,15 @@ class EditDrugRecordActivity : AppCompatActivity() {
private lateinit var binding: ActivityEditDrugRecordBinding

internal var adapter: EditRrugExpandableListAdapter? = null
internal var titleList: List<String>? = null

private lateinit var viewModel: EditDrugRecordViewModel

private var timeSlots = mutableListOf<String>()

private var checkBoxes: Array<CheckBox> = arrayOf()

private var drugId: Int? = null


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -44,20 +47,18 @@ class EditDrugRecordActivity : AppCompatActivity() {
binding.cbBeforeSleep
)

drugId = intent.getIntExtra("drugId", 0)
initDrugRecordViewModel()

initDrugNameTextView()
initExpandableListInteraction()
initOndemandCheckbox()
initTimingsCheckbox()
initButton()

}

private fun initDrugRecordViewModel() {
binding.progressBar.visibility = View.VISIBLE
viewModel = EditDrugRecordViewModel()
viewModel.fetchRecord(20)
viewModel.fetchRecord(drugId!!)
viewModel.record.observe(this, Observer {
binding.tvDrugName.text = it.drug.name
binding.tvHospital.text = it.hospitalName
Expand All @@ -68,6 +69,7 @@ class EditDrugRecordActivity : AppCompatActivity() {
binding.cbOnDemand.isChecked = it.onDemand
timeSlots = it.timeSlots.toMutableList()
initTimeSection()
initExpandableListInteraction(it.interactingDrugs!!)
for (i in it.timings) {
checkBoxes[i].isChecked = true
}
Expand Down Expand Up @@ -220,10 +222,11 @@ class EditDrugRecordActivity : AppCompatActivity() {
}
}

private fun initExpandableListInteraction() {
val listData = data
titleList = ArrayList(listData.keys)
adapter = EditRrugExpandableListAdapter(this, titleList as ArrayList<String>, listData)
private fun initExpandableListInteraction(interactingDrugs: List<InteractingDrug>) {
adapter = EditRrugExpandableListAdapter(this, interactingDrugs)
if (interactingDrugs.isEmpty()) {
binding.expandableListInteraction!!.visibility = GONE
}
binding.expandableListInteraction!!.setAdapter(adapter)
ExpandableListUtils.setupExpandHeight(binding.expandableListInteraction!!, adapter!!)
}
Expand All @@ -236,19 +239,4 @@ class EditDrugRecordActivity : AppCompatActivity() {
super.onBackPressed()
}
}

val data: HashMap<String, List<String>>
get() {
val listData = HashMap<String, List<String>>()

val interactionGroup = ArrayList<String>()
interactionGroup.add("Acertil")
interactionGroup.add("Rifampin")


// set multiple list to header title position
listData["交互作用"] = interactionGroup

return listData
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.junting.drug_android_frontend

import android.app.AlertDialog
import android.content.Context
import android.graphics.Typeface
import android.view.LayoutInflater
Expand All @@ -9,15 +8,16 @@ import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import android.widget.ImageView
import android.widget.TextView
import com.junting.drug_android_frontend.model.drug_record.InteractingDrug


class EditRrugExpandableListAdapter internal constructor(
private val context: Context,
private val titleList: List<String>,
private val dataList: HashMap<String, List<String>>) : BaseExpandableListAdapter() {
private val interactingDrugs: List<InteractingDrug>
) : BaseExpandableListAdapter() {

override fun getChild(listPosition: Int, expandedListPosition: Int): Any {
return this.dataList[this.titleList[listPosition]]!![expandedListPosition]
return interactingDrugs.get(expandedListPosition).id.toString()
}

override fun getChildId(listPosition: Int, expandedListPosition: Int): Long {
Expand All @@ -32,29 +32,29 @@ class EditRrugExpandableListAdapter internal constructor(
val layoutInflater = this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
convertView = layoutInflater.inflate(R.layout.drug_interaction_view, null)
}
// var viewModel: EditDrugRecordViewModel
// viewModel = EditDrugRecordViewModel()
// viewModel.fetchRecord(20)
// viewModel.record.observe(this, Observer {
//
// })

val tvDrugName = convertView!!.findViewById<TextView>(R.id.tv_drug_name)
tvDrugName.text = expandedListText

val tvDegree = convertView!!.findViewById<TextView>(R.id.tv_degree)
tvDegree.text = interactingDrugs.get(expandedListPosition).level

val tvCause = convertView!!.findViewById<TextView>(R.id.tv_cause)
tvCause.text = interactingDrugs.get(expandedListPosition).cause

return convertView
}

override fun getChildrenCount(listPosition: Int): Int {
return this.dataList[this.titleList[listPosition]]!!.size
return interactingDrugs.size
}

override fun getGroup(listPosition: Int): Any {
return this.titleList[listPosition]
return "交互作用"
}

override fun getGroupCount(): Int {
return this.titleList.size
return 1
}

override fun getGroupId(listPosition: Int): Long {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class DrugsRecordViewAdapter(private val context: Context, private val viewModel

holder.drugItemViewBinding.cardView.setOnClickListener {
val intent = Intent(context, EditDrugRecordActivity::class.java)
intent.putExtra("drugId", drugRecord.id)
context.startActivity(intent)
Toast.makeText(context, String.format("You clicked %s", drugRecord.drug.name), Toast.LENGTH_SHORT).show()
}
Expand Down

0 comments on commit cbb157d

Please sign in to comment.