diff --git a/app/src/main/java/com/example/hpostesting/presentation/adapter/UserListAdapter.kt b/app/src/main/java/com/example/hpostesting/presentation/adapter/UserListAdapter.kt index 9d83d3e..53c65c8 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/adapter/UserListAdapter.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/adapter/UserListAdapter.kt @@ -1,6 +1,8 @@ package com.example.hpostesting.presentation.adapter import android.annotation.SuppressLint +import android.app.AlertDialog +import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -20,9 +22,20 @@ import java.text.SimpleDateFormat import java.util.Calendar import java.util.Date import java.util.Locale +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.google.firebase.firestore.FirebaseFirestore class UserListAdapter( - options: FirestoreRecyclerOptions, private val view: View, private val batLevel: Int + + private val context: Context, + private val hemoCubeViewModel: HemoCubeViewModel, + options: FirestoreRecyclerOptions, + private val view: View, + private val batLevel: Int, + private val testDetails: HemoCubeTestData? = DataHolder.selectedTest?.toHemoCubeTestData(), ) : FirestoreRecyclerAdapter(options) { class OrderItemViewHolder(val binding: UserItemViewBinding) : @@ -40,7 +53,7 @@ class UserListAdapter( userName.text = "Name: ${model.name}" userId.text = "User ID:${model._id}" if (model.incubationTime == "") { - btnStartIncubation.visibility = View.VISIBLE +// btnStartIncubation.visibility = View.VISIBLE } else { userId.text = "User ID:${model._id} \n Time: ${isBetween15And30Minutes(model.incubationTime)} \n Started at: ${ @@ -61,26 +74,106 @@ class UserListAdapter( } else { teststatus.text = "Test is Pending" } - btnStartIncubation.setOnClickListener { - it.visibility = View.GONE - Firebase.firestore.collection("patientData").whereEqualTo("_id", model._id).get() - .addOnSuccessListener { data -> - if (data.documents.isNotEmpty()) { - data.documents.forEach { userData -> - Firebase.firestore.collection("patientData").document(userData.id) - .update( - "incubationTime", SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss", Locale.getDefault() - ).format(Calendar.getInstance().time).toString() - ).addOnSuccessListener { - Toast.makeText( - view.context, "Incubation started", Toast.LENGTH_SHORT - ).show() - startListening() - } + + holder.binding.btnBlood.setOnClickListener { + 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(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, + "incubationTime", SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time).toString() + ) + .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 { +// it.visibility = View.GONE +// Firebase.firestore.collection("patientData").whereEqualTo("_id", model._id).get() +// .addOnSuccessListener { data -> +// if (data.documents.isNotEmpty()) { +// data.documents.forEach { userData -> +// Firebase.firestore.collection("patientData").document(userData.id) +// .update( +// "incubationTime", SimpleDateFormat( +// "yyyy-MM-dd HH:mm:ss", Locale.getDefault() +// ).format(Calendar.getInstance().time).toString() +// ).addOnSuccessListener { +// Toast.makeText( +// view.context, "Incubation started", Toast.LENGTH_SHORT +// ).show() +// startListening() +// } +// } +// } +// } } userCard.setOnClickListener { if (batLevel < 40) { @@ -130,6 +223,8 @@ class UserListAdapter( } } + + private fun isBetween15And30Minutes(createdAt: String): Long { val formatter = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()) val createdAtDate: Date = formatter.parse(createdAt)!! @@ -142,4 +237,9 @@ class UserListAdapter( } + + + + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckFragment.kt index 3d5220b..f8c28d6 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckFragment.kt @@ -56,8 +56,9 @@ class HemoCubeBufferCheckFragment : Fragment() { } private fun initViews() { - binding.btnKit.visibility = View.GONE - binding.etBloodGroup.visibility = View.GONE +// binding.btnKit.visibility = View.GONE + binding.btnSubmit.visibility = View.GONE +// binding.etBloodGroup.visibility = View.GONE listenToHemoCube() startBufferProcess() diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt index 7644cff..59f39b4 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt @@ -42,6 +42,9 @@ class HomeFragment : Fragment() { private val viewModel: TestRightViewModel by activityViewModels() private val hemoCubeViewModel: HemoCubeViewModel by activityViewModels() 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 override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? @@ -151,8 +154,12 @@ class HomeFragment : Fragment() { val bm = requireContext().getSystemService(BATTERY_SERVICE) as BatteryManager val batLevel: Int = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) - rvAdapter = UserListAdapter(recyclerViewOptions, binding.root, batLevel) + rvAdapter = view?.let { + UserListAdapter(requireContext(), hemoCubeViewModel, recyclerViewOptions, + it, batLevel) + }!! binding.rvOrder.adapter = rvAdapter + rvAdapter.startListening() } catch (e: Exception) { Firebase.crashlytics.recordException(e) @@ -195,11 +202,12 @@ class HomeFragment : Fragment() { val recyclerViewOptions = FirestoreRecyclerOptions.Builder().setQuery(query, UserData::class.java) .build() - 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 = UserListAdapter(recyclerViewOptions, binding.root, batLevel) + rvAdapter = view?.let { + UserListAdapter(requireContext(), hemoCubeViewModel, recyclerViewOptions, it, batLevel) + }!! binding.rvOrder.adapter = rvAdapter rvAdapter.startListening() } diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt index 5e5c261..87ef90d 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt @@ -64,25 +64,25 @@ class HemoCubeFragment : Fragment() { } private fun initViews() { - binding.btnSubmit.setOnClickListener { - val bloodGroupText = binding.etBloodGroup.text.toString() - if (bloodGroupText.isEmpty()) { - binding.etBloodGroup.error = "Please enter your blood group" - } else { - activity?.runOnUiThread { - binding.progressBar.visibility = View.VISIBLE - } - hemoCubeViewModel.uploadHemoCubeResultToDatabase( - isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") - ) - } - } - if (isTestOngoing) { - binding.btnKit.visibility = View.GONE - binding.btnKit.isEnabled = false - binding.btnKit.isClickable = false - - } +// binding.btnSubmit.setOnClickListener { +// val bloodGroupText = binding.etBloodGroup.text.toString() +// if (bloodGroupText.isEmpty()) { +// binding.etBloodGroup.error = "Please enter your blood group" +// } else { +// activity?.runOnUiThread { +// binding.progressBar.visibility = View.VISIBLE +// } +// hemoCubeViewModel.uploadHemoCubeResultToDatabase( +// isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") +// ) +// } +// } +// if (isTestOngoing) { +// binding.btnKit.visibility = View.GONE +// binding.btnKit.isEnabled = false +// binding.btnKit.isClickable = false +// +// } binding.btnSubmit.isEnabled = false binding.btnSubmit.isClickable = false @@ -90,70 +90,70 @@ class HemoCubeFragment : Fragment() { binding.tvName.text = "Name: ${testDetails?.name}\n ID: ${testDetails?._id}" Log.e("nametext",binding.tvName.text.toString()) - binding.btnKit.setOnClickListener { - if (isTestOngoing) { - val title = "WARNING" - val message = "There is a on going test, do you want to stop it" - val negativeText = getString(R.string.no) - val positiveText = "Yes" - - UIUtils.createAlertDialog(requireContext(), - title, - message, - negativeText, - positiveText, - object : MyDialogListener { - override fun onClickNegativeButton() { - - } - - override fun onClickPositiveButton() { - resetKitCount() - val intent = Intent(context, KitScanActivity::class.java) - startActivity(intent) - (activity as HemocubeActivity).finish() - } - }) - } else { - resetKitCount() - val intent = Intent(context, KitScanActivity::class.java) - startActivity(intent) - (activity as HemocubeActivity).finish() - } - } +// binding.btnKit.setOnClickListener { +// if (isTestOngoing) { +// val title = "WARNING" +// val message = "There is a on going test, do you want to stop it" +// val negativeText = getString(R.string.no) +// val positiveText = "Yes" +// +// UIUtils.createAlertDialog(requireContext(), +// title, +// message, +// negativeText, +// positiveText, +// object : MyDialogListener { +// override fun onClickNegativeButton() { +// +// } +// +// override fun onClickPositiveButton() { +// resetKitCount() +// val intent = Intent(context, KitScanActivity::class.java) +// startActivity(intent) +// (activity as HemocubeActivity).finish() +// } +// }) +// } else { +// resetKitCount() +// val intent = Intent(context, KitScanActivity::class.java) +// startActivity(intent) +// (activity as HemocubeActivity).finish() +// } +// } } - private suspend fun checkBloodGroup() { - binding.apply { - val bloodGroup = etBloodGroup.text.toString() - if (bloodGroup.isEmpty()) { - etBloodGroup.error = "Please enter your blood group" - } else { - val db: FirebaseFirestore = Firebase.firestore - val userdata = db.collection("patientData") - .whereEqualTo("_id", hemoCubeViewModel.testDetails?._id).get().await() - if (userdata.documents.isNotEmpty()) { - db.collection("patientData").document(userdata.documents[0].id) - .update("bloodGroup", bloodGroup) - withContext(Dispatchers.Main) { - val i = Intent( - requireContext().applicationContext, DashboardActivity::class.java - ) - startActivity(i) - (activity as HemocubeActivity).finish() - } - } - } - } - } +// private suspend fun checkBloodGroup() { +// binding.apply { +// val bloodGroup = etBloodGroup.text.toString() +// if (bloodGroup.isEmpty()) { +// etBloodGroup.error = "Please enter your blood group" +// } else { +// val db: FirebaseFirestore = Firebase.firestore +// val userdata = db.collection("patientData") +// .whereEqualTo("_id", hemoCubeViewModel.testDetails?._id).get().await() +// if (userdata.documents.isNotEmpty()) { +// db.collection("patientData").document(userdata.documents[0].id) +// .update("bloodGroup", bloodGroup) +// withContext(Dispatchers.Main) { +// val i = Intent( +// requireContext().applicationContext, DashboardActivity::class.java +// ) +// startActivity(i) +// (activity as HemocubeActivity).finish() +// } +// } +// } +// } +// } private fun observeViewModel() { hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> if (result == "Success") { showToast("Test Results Uploaded Successfully") - lifecycleScope.launch { - checkBloodGroup() - } +// lifecycleScope.launch { +// checkBloodGroup() +// } } if (result == "Local") { showToast("Internet not available, test details stored locally") @@ -176,7 +176,7 @@ class HemoCubeFragment : Fragment() { val coefficient1 = coefficients[0] val coefficient2 = coefficients[1] val result = coefficient1 * coefficient2 - showToast("coefficients: $coefficient1, $coefficient2") +// showToast("coefficients: $coefficient1, $coefficient2") } } isOnline = isNetworkAvailable diff --git a/app/src/main/res/layout/dialog_custom.xml b/app/src/main/res/layout/dialog_custom.xml new file mode 100644 index 0000000..afdf55e --- /dev/null +++ b/app/src/main/res/layout/dialog_custom.xml @@ -0,0 +1,24 @@ + + + + + + + + diff --git a/app/src/main/res/layout/fragment_hemo_cube_reference.xml b/app/src/main/res/layout/fragment_hemo_cube_reference.xml index dcf4f6c..3bfd5f9 100644 --- a/app/src/main/res/layout/fragment_hemo_cube_reference.xml +++ b/app/src/main/res/layout/fragment_hemo_cube_reference.xml @@ -109,40 +109,40 @@ android:textColor="@color/white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/til_blood_group" /> + app:layout_constraintTop_toBottomOf="@id/tv_subtitle4" /> - + + + + + + + + + + - + + + + + + + + - + -