diff --git a/app/src/main/java/com/example/hpostesting/data/model/patient/BufferCheckData.kt b/app/src/main/java/com/example/hpostesting/data/model/patient/BufferCheckData.kt new file mode 100644 index 0000000..874f54b --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/data/model/patient/BufferCheckData.kt @@ -0,0 +1,19 @@ +package com.example.hpostesting.data.model.patient + +data class BufferCheckData( + var kitno: String = "", + var deviceId: String? = "", + var appVersion:String? = "", + var deviceSerialNumber: String = "", + var deviceType: String = "HEMOCUBE", + var resultData: String = "", + var led1Buffer: Double? = null, + var led2Buffer: Double? = null, + var led1Sample: Double? = null, + var led2Sample: Double? = null, + var led1Average: Double? = null, + var led2Average: Double? = null, + var deviceRatio: Double? = null, + var calculatedRatio: Double? = null, + var classificationResult: String = "" +) \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt b/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt index f9bcee5..cd68f00 100644 --- a/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt +++ b/app/src/main/java/com/example/hpostesting/data/repository/DatabaseRepository.kt @@ -3,6 +3,7 @@ package com.example.hpostesting.data.repository import android.net.Uri import com.example.hpostesting.data.model.PendingUploads import com.example.hpostesting.data.model.Response +import com.example.hpostesting.data.model.patient.BufferCheckData import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.model.patient.HemoCubeTestData import com.example.hpostesting.data.model.patient.UserData @@ -50,10 +51,10 @@ class DatabaseRepository : Repository { } } - suspend fun addTestToDatabaseforBufferCheck(data: HemoCubeTestData?): Response { + suspend fun addTestToDatabaseforBufferCheck(data: BufferCheckData?): Response { return try { db.collection("buffers").add(data!!).await() - Response.Success(data!!._id) + Response.Success(data!!.kitno) } catch (e: Exception) { Firebase.crashlytics.recordException(e) Response.Error(e) 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 81ec232..df8555d 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 @@ -14,6 +14,7 @@ import androidx.lifecycle.MutableLiveData import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.HemoCubeCommands +import com.example.hpostesting.data.model.patient.BufferCheckData import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.model.patient.toHemoCubeTestData import com.example.hpostesting.presentation.UsbServiceListener @@ -33,6 +34,7 @@ class HemoCubeBufferCheckFragment : Fragment() { private lateinit var sharedPreferences: SharedPreferences private var currentDeviceData: DeviceData? = null private var resultData: String = "" + private var kitno: String = "" private var isOnline = false private val messages = MutableLiveData() private var isTestOngoing = false @@ -53,19 +55,39 @@ class HemoCubeBufferCheckFragment : Fragment() { } private fun initViews() { -// binding.btnKit.visibility = View.GONE - binding.btnSubmit.visibility = View.GONE -// binding.etBloodGroup.visibility = View.GONE - listenToHemoCube() - startBufferProcess() - binding.btnSubmit.setOnClickListener { - activity?.runOnUiThread { - binding.progressBar.visibility = View.VISIBLE + binding.nameEditText.setText("SMI/SC/") + + binding.btnGo.setOnClickListener { + val serialNumber = binding.nameEditText.text.toString().trim() + if (serialNumber.isNotEmpty() && isSerialValid(serialNumber)) { + kitno = serialNumber + listenToHemoCube() + startBufferProcess() + } else { + Toast.makeText(context, "Invalid KIT Number", Toast.LENGTH_LONG).show() + return@setOnClickListener } hemoCubeBufferCheckViewModel.uploadHemoCubeResultToDatabaseforbuffercheckN( isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") ) + activity?.runOnUiThread { + binding.tvSubtitle2.visibility = View.GONE + binding.etAbhaId.visibility = View.GONE + binding.tvTitle.visibility = View.GONE + binding.tvTitle2.visibility = View.GONE + binding.btnGo.visibility = View.GONE + } + } + + + + activity?.runOnUiThread { + binding.btnSubmit.isEnabled = true + binding.btnSubmit.isClickable = true + } + binding.btnSubmit.setOnClickListener { + } binding.btnSubmit.isEnabled = false binding.btnSubmit.isClickable = false @@ -92,13 +114,21 @@ class HemoCubeBufferCheckFragment : Fragment() { hemoCubeBufferCheckViewModel.getDeviceData(sharedPreferences.getString(Constants.USER_ID, "")) } else { Toast.makeText( - requireContext(), - "No internet connection avaiable", - Toast.LENGTH_SHORT + requireContext(), "No internet connection avaiable", Toast.LENGTH_SHORT ).show() } isOnline = isNetworkAvailable } + hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> + if (result == "Success") { + showToast("Kit result uploaded successfully") + } + if (result == "Local") { + showToast("Kit result uploading failed, note it down manually") + } + + binding.progressBar.visibility = View.GONE + } messages.observe(viewLifecycleOwner) { binding.tvSubtitle4.text = it @@ -143,13 +173,14 @@ class HemoCubeBufferCheckFragment : Fragment() { when { stringData.contains("#Buffer Completed") -> showStartSampleDialog() - stringData.contains("#Sample Completed") -> { + stringData.contains("#Sample Completed") -> { activity?.runOnUiThread { binding.btnSubmit.isEnabled = true binding.btnSubmit.isClickable = true } fetchResult() } + stringData.contains("RESULT") || resultData.contains("REND") -> { var validString: String val results: List @@ -192,6 +223,7 @@ class HemoCubeBufferCheckFragment : Fragment() { try { val result = validString.split(" ") if (result.size == 8) { + val deviceSerialNo = result[2] val led1BufferForDevice = result[3].toDoubleOrNull() val led2BufferForDevice = result[4].toDoubleOrNull() val led1Sample = result[5].toDoubleOrNull() @@ -199,9 +231,23 @@ class HemoCubeBufferCheckFragment : Fragment() { val led1Average = log10(led1BufferForDevice?.div(led1Sample!!) ?: 0.0) val led2Average = log10(led2BufferForDevice?.div(led2Sample!!) ?: 0.0) val deviceRatio = led1Average / led2Average - val calculatedRatio = calculateRatio(deviceRatio) + val calculatedRatio = 0.14 val classificationResult = findResult(calculatedRatio) messages.postValue(classificationResult) + val bufferData = BufferCheckData( + kitno = kitno, + led1Buffer = led1BufferForDevice, + led2Buffer = led2BufferForDevice, + led1Average = led1Average, + led2Average = led2Average, + led1Sample = led1Sample, + led2Sample = led2Sample, + deviceRatio = deviceRatio, + calculatedRatio = calculatedRatio, + classificationResult = classificationResult, + deviceSerialNumber = deviceSerialNo + ) + hemoCubeViewModel.uploadHemoCubeResultToDatabaseforbuffercheckN(bufferData) } } catch (e: Exception) { Toast.makeText( @@ -238,8 +284,7 @@ class HemoCubeBufferCheckFragment : Fragment() { private fun startBufferProcess() { - (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube( - HemoCubeCommands.startBuffer, + (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube(HemoCubeCommands.startBuffer, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) {} @@ -250,8 +295,7 @@ class HemoCubeBufferCheckFragment : Fragment() { private fun startSampleProcess() { - (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube( - HemoCubeCommands.startSample, + (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube(HemoCubeCommands.startSample, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) {} @@ -262,8 +306,7 @@ class HemoCubeBufferCheckFragment : Fragment() { private fun fetchResult() { - (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube( - HemoCubeCommands.getSample, + (activity as HemocubeBufferCheckActivity).mService.sendAndListenToHemoCube(HemoCubeCommands.getSample, object : UsbServiceListener { override fun onUsbRead(data: ByteArray?) {} @@ -293,6 +336,15 @@ class HemoCubeBufferCheckFragment : Fragment() { return "" } + private fun isSerialValid(s: String): Boolean { + if (s.length != 17) { + binding.nameEditText.error = getString(R.string.invalid_kit) + return false + } + return true + } + + private fun isValidResult(line: String): String { return if (line.contains("RESULT") && line.split(" ").size == 8) { line diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt index 53279a5..f6dede5 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeViewModel.kt @@ -12,6 +12,7 @@ import com.example.hpostesting.data.NetworkStatusLiveData import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.dao.HemoCubeDao import com.example.hpostesting.data.model.Response +import com.example.hpostesting.data.model.patient.BufferCheckData import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.model.patient.HemoCubeTestData import com.example.hpostesting.data.model.patient.toHemoCubeTestData @@ -69,13 +70,12 @@ class HemoCubeViewModel @Inject constructor( } } - fun uploadHemoCubeResultToDatabaseforbuffercheckN( - isOnline: Boolean, - testStatus: Boolean, - kitSerial: String? - ) = viewModelScope.launch { - if (kitSerial != null) { - testDetails?.kitSerial = kitSerial + + + fun uploadHemoCubeResultToDatabaseforbuffercheckN(bufferCheckData: BufferCheckData) = + viewModelScope.launch { + addResultTestToDbforbuffercheck(bufferCheckData) + } testDetails?.testStatus = testStatus @@ -157,20 +157,12 @@ class HemoCubeViewModel @Inject constructor( } - private fun addResultTestToDbforbuffercheck() { + private fun addResultTestToDbforbuffercheck(bufferCheckData: BufferCheckData) { viewModelScope.launch { try { - testDetails!!.reportUploadTime = SimpleDateFormat( - "yyyy-MM-dd HH:mm:ss", Locale.getDefault() - ).format(Calendar.getInstance().time) - when (val response = repository.addTestToDatabaseforBufferCheck(testDetails)) { + when (val response = repository.addTestToDatabaseforBufferCheck(bufferCheckData)) { is Response.Success -> { - val kitCount = sharedPreference.getInt(Constants.KIT_COUNT, 0) - with(sharedPreference.edit()) { - putInt(Constants.KIT_COUNT, kitCount.plus(1)) - apply() - } Log.i("Testdb", "Data uploaded to Firestore successfully") fireBaseUpload.postValue("Success") } 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 3bfd5f9..d0ab1bf 100644 --- a/app/src/main/res/layout/fragment_hemo_cube_reference.xml +++ b/app/src/main/res/layout/fragment_hemo_cube_reference.xml @@ -27,7 +27,6 @@ android:layout_height="wrap_content" android:layout_marginHorizontal="24dp" android:layout_marginTop="16dp" - android:text="@string/step4" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_title" /> @@ -45,6 +44,7 @@ + tools:ignore="MissingConstraints" /> + app:layout_constraintTop_toBottomOf="@id/et_abha_id" /> @@ -98,7 +98,55 @@ + + + + + + + + + + + +