Blood Group Updation in database added , from user card successfully added
This commit is contained in:
@@ -23,15 +23,19 @@ import java.util.Calendar
|
|||||||
import java.util.Date
|
import java.util.Date
|
||||||
import java.util.Locale
|
import java.util.Locale
|
||||||
import android.widget.AutoCompleteTextView
|
import android.widget.AutoCompleteTextView
|
||||||
|
import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
||||||
|
import com.example.hpostesting.data.model.patient.toHemoCubeTestData
|
||||||
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
||||||
import com.google.firebase.firestore.FirebaseFirestore
|
import com.google.firebase.firestore.FirebaseFirestore
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
class UserListAdapter(
|
class UserListAdapter(
|
||||||
|
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
private val hemoCubeViewModel: HemoCubeViewModel,
|
private val hemoCubeViewModel: HemoCubeViewModel,
|
||||||
options: FirestoreRecyclerOptions<UserData>, private val view: View, private val batLevel: Int
|
options: FirestoreRecyclerOptions<UserData>,
|
||||||
|
private val view: View,
|
||||||
|
private val batLevel: Int,
|
||||||
|
private val testDetails: HemoCubeTestData? = DataHolder.selectedTest?.toHemoCubeTestData(),
|
||||||
) : FirestoreRecyclerAdapter<UserData, UserListAdapter.OrderItemViewHolder>(options) {
|
) : FirestoreRecyclerAdapter<UserData, UserListAdapter.OrderItemViewHolder>(options) {
|
||||||
|
|
||||||
class OrderItemViewHolder(val binding: UserItemViewBinding) :
|
class OrderItemViewHolder(val binding: UserItemViewBinding) :
|
||||||
@@ -72,7 +76,78 @@ class UserListAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
holder.binding.btnBlood.setOnClickListener {
|
holder.binding.btnBlood.setOnClickListener {
|
||||||
showCustomDialog()
|
val inflater = LayoutInflater.from(context)
|
||||||
|
val dialogView = inflater.inflate(R.layout.dialog_custom, null)
|
||||||
|
|
||||||
|
val builder = AlertDialog.Builder(context)
|
||||||
|
.setView(dialogView)
|
||||||
|
.setTitle("Add Blood Group")
|
||||||
|
|
||||||
|
val etBloodGroup = dialogView.findViewById<AutoCompleteTextView>(R.id.et_blood_group)
|
||||||
|
|
||||||
|
builder.setPositiveButton("OK") { dialog, which ->
|
||||||
|
val bloodGroup = etBloodGroup.text.toString()
|
||||||
|
if (bloodGroup.equals("Select Blood Group") || bloodGroup.isNullOrBlank()) {
|
||||||
|
etBloodGroup.error = "Please enter your blood group"
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Please enter your blood group",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
} else {
|
||||||
|
// Check if hemoCubeViewModel and testDetails are not null
|
||||||
|
val db: FirebaseFirestore = FirebaseFirestore.getInstance()
|
||||||
|
|
||||||
|
db.collection("patientData")
|
||||||
|
.whereEqualTo("_id", model._id)
|
||||||
|
.get()
|
||||||
|
.addOnSuccessListener { userdata ->
|
||||||
|
try {
|
||||||
|
val document = userdata.documents[0]
|
||||||
|
db.collection("patientData")
|
||||||
|
.document(document.id)
|
||||||
|
.update("bloodGroup", bloodGroup)
|
||||||
|
.addOnSuccessListener {
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Blood group updated successfully",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
.addOnFailureListener { e ->
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Failed to update blood group: ${e.message}",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
} catch (e: IndexOutOfBoundsException) {
|
||||||
|
// Handle the case where no documents are found for the specified ID
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"No user data found for the specified ID",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
.addOnFailureListener { e ->
|
||||||
|
Toast.makeText(
|
||||||
|
context,
|
||||||
|
"Error fetching user data: ${e.message}",
|
||||||
|
Toast.LENGTH_SHORT
|
||||||
|
).show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Handle the selected blood group here
|
||||||
|
}
|
||||||
|
|
||||||
|
builder.setNegativeButton("Cancel") { dialog, which ->
|
||||||
|
dialog.dismiss()
|
||||||
|
}
|
||||||
|
|
||||||
|
val alertDialog = builder.create()
|
||||||
|
alertDialog.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
btnStartIncubation.setOnClickListener {
|
btnStartIncubation.setOnClickListener {
|
||||||
@@ -144,80 +219,6 @@ class UserListAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showCustomDialog() {
|
|
||||||
val inflater = LayoutInflater.from(context)
|
|
||||||
val dialogView = inflater.inflate(R.layout.dialog_custom, null)
|
|
||||||
|
|
||||||
val builder = AlertDialog.Builder(context)
|
|
||||||
.setView(dialogView)
|
|
||||||
.setTitle("Add Blood Group")
|
|
||||||
|
|
||||||
val etBloodGroup = dialogView.findViewById<AutoCompleteTextView>(R.id.et_blood_group)
|
|
||||||
|
|
||||||
builder.setPositiveButton("OK") { dialog, which ->
|
|
||||||
val bloodGroup = etBloodGroup.text.toString()
|
|
||||||
if (bloodGroup.equals("Select Blood Group") || bloodGroup.isNullOrBlank()) {
|
|
||||||
etBloodGroup.error = "Please enter your blood group"
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Please enter your blood group",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
} else {
|
|
||||||
// Check if hemoCubeViewModel and testDetails are not null
|
|
||||||
val testDetails = hemoCubeViewModel.testDetails
|
|
||||||
val db: FirebaseFirestore = FirebaseFirestore.getInstance()
|
|
||||||
|
|
||||||
db.collection("patientData")
|
|
||||||
.whereEqualTo("_id", testDetails?._id)
|
|
||||||
.get()
|
|
||||||
.addOnSuccessListener { userdata ->
|
|
||||||
if (userdata.isEmpty) {
|
|
||||||
// Handle the case when no documents are found (e.g., display a message)
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"No user data found for the specified ID",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
} else {
|
|
||||||
val document = userdata.documents[0]
|
|
||||||
db.collection("patientData")
|
|
||||||
.document(document.id)
|
|
||||||
.update("bloodGroup", bloodGroup)
|
|
||||||
.addOnSuccessListener {
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Blood group updated successfully",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
|
||||||
.addOnFailureListener { e ->
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Failed to update blood group: ${e.message}",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.addOnFailureListener { e ->
|
|
||||||
Toast.makeText(
|
|
||||||
context,
|
|
||||||
"Error fetching user data: ${e.message}",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Handle the selected blood group here
|
|
||||||
}
|
|
||||||
|
|
||||||
builder.setNegativeButton("Cancel") { dialog, which ->
|
|
||||||
dialog.dismiss()
|
|
||||||
}
|
|
||||||
|
|
||||||
val alertDialog = builder.create()
|
|
||||||
alertDialog.show()
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private fun isBetween15And30Minutes(createdAt: String): Long {
|
private fun isBetween15And30Minutes(createdAt: String): Long {
|
||||||
|
|||||||
@@ -42,6 +42,7 @@ class HomeFragment : Fragment() {
|
|||||||
private val viewModel: TestRightViewModel by activityViewModels()
|
private val viewModel: TestRightViewModel by activityViewModels()
|
||||||
private val hemoCubeViewModel: HemoCubeViewModel by activityViewModels()
|
private val hemoCubeViewModel: HemoCubeViewModel by activityViewModels()
|
||||||
private lateinit var rvAdapter: UserListAdapter
|
private lateinit var rvAdapter: UserListAdapter
|
||||||
|
private var batLevel: Int = 0 // Initialize with a default value, or obtain the actual battery level
|
||||||
|
|
||||||
|
|
||||||
private lateinit var sharedPreference: SharedPreferences
|
private lateinit var sharedPreference: SharedPreferences
|
||||||
@@ -201,13 +202,11 @@ class HomeFragment : Fragment() {
|
|||||||
val recyclerViewOptions =
|
val recyclerViewOptions =
|
||||||
FirestoreRecyclerOptions.Builder<UserData>().setQuery(query, UserData::class.java)
|
FirestoreRecyclerOptions.Builder<UserData>().setQuery(query, UserData::class.java)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
val bm = requireContext().getSystemService(BATTERY_SERVICE) as BatteryManager
|
val bm = requireContext().getSystemService(BATTERY_SERVICE) as BatteryManager
|
||||||
val batLevel: Int = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
|
batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY)
|
||||||
|
|
||||||
rvAdapter = view?.let {
|
rvAdapter = view?.let {
|
||||||
UserListAdapter(requireContext(), hemoCubeViewModel, recyclerViewOptions,
|
UserListAdapter(requireContext(), hemoCubeViewModel, recyclerViewOptions, it, batLevel)
|
||||||
it, batLevel)
|
|
||||||
}!!
|
}!!
|
||||||
binding.rvOrder.adapter = rvAdapter
|
binding.rvOrder.adapter = rvAdapter
|
||||||
rvAdapter.startListening()
|
rvAdapter.startListening()
|
||||||
|
|||||||
Reference in New Issue
Block a user