Improved Code
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.example.hpostesting.data
|
||||
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import com.example.hpostesting.data.model.patient.HemoCubeTestData
|
||||
import com.example.hpostesting.data.model.patient.UserData
|
||||
import com.example.hpostesting.data.model.test.TestRightDeviceConstants
|
||||
import com.example.hpostesting.data.model.test.TestType
|
||||
|
||||
@@ -65,7 +65,8 @@ class MainActivity : AppCompatActivity() {
|
||||
locationCallback = object : LocationCallback() {
|
||||
override fun onLocationResult(locationResult: LocationResult) {
|
||||
locationResult.lastLocation?.let { location ->
|
||||
DataHolder.selectedTest!!.location = UserData.Location(location.latitude, location.longitude)
|
||||
DataHolder.selectedTest!!.location =
|
||||
UserData.Location(location.latitude, location.longitude)
|
||||
Log.i("Location", DataHolder.location.toString())
|
||||
}
|
||||
}
|
||||
@@ -87,6 +88,7 @@ class MainActivity : AppCompatActivity() {
|
||||
DataHolder.usbConnected.value = true
|
||||
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
|
||||
}
|
||||
|
||||
"Test" -> {
|
||||
|
||||
}
|
||||
@@ -113,7 +115,10 @@ class MainActivity : AppCompatActivity() {
|
||||
DataHolder.usbConnected.value = true
|
||||
DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HOMOCUBE)
|
||||
} else if (availableDrivers.isNotEmpty() && availableDrivers[0].device.productId == Constants.DEVICE_PRODUCT_ID && availableDrivers[0].device.vendorId == Constants.DEVICE_VENDOR_ID) {
|
||||
Log.d(TAG, "Device matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}")
|
||||
Log.d(
|
||||
TAG,
|
||||
"Device matched -> product = ${availableDrivers[0].device.productId}, vendor = ${availableDrivers[0].device.vendorId}"
|
||||
)
|
||||
DataHolder.usbConnected.value = true
|
||||
} else if (availableDrivers.isNotEmpty()) {
|
||||
Log.d(
|
||||
@@ -128,6 +133,7 @@ class MainActivity : AppCompatActivity() {
|
||||
DataHolder.usbConnected.value = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
binding.cvItem1.setOnClickListener {
|
||||
|
||||
@@ -1,63 +1,90 @@
|
||||
package com.example.hpostesting.presentation.hemocube
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.fragment.app.Fragment
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.viewModels
|
||||
import com.example.hpostesting.data.DataHolder
|
||||
import com.example.hpostesting.presentation.UsbServiceListener
|
||||
import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding
|
||||
private const val ARG_PARAM1 = "param1"
|
||||
private const val ARG_PARAM2 = "param2"
|
||||
|
||||
class HemoCubeFragment : Fragment() {
|
||||
private var testTime: String? = ""
|
||||
private var resultRatio: String? = ""
|
||||
private var kitSerial: String? = ""
|
||||
|
||||
private var testTime: String? = null
|
||||
private var resultRatio: String? = null
|
||||
private var kitSerial: String? = null
|
||||
private lateinit var binding: FragmentHemoCubeReferenceBinding
|
||||
private val viewModel: HemoCubeViewModel by activityViewModels()
|
||||
private val viewModel: HemoCubeViewModel by viewModels()
|
||||
private lateinit var sharedPreference: SharedPreferences
|
||||
private val TAG = "HemocubeReference"
|
||||
val testDetails = DataHolder.SelectTestType
|
||||
var isOnline = false
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
}
|
||||
private var isOnline = false
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
|
||||
): View {
|
||||
binding = FragmentHemoCubeReferenceBinding.inflate(inflater, container, false)
|
||||
|
||||
binding.btnShowResult.setOnClickListener {
|
||||
fetchHemoCubeResult()
|
||||
return binding.root
|
||||
}
|
||||
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
setupButtonClickListeners()
|
||||
|
||||
sharedPreference =
|
||||
requireContext().getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE)
|
||||
|
||||
viewModel.fireBaseUpload.observe(viewLifecycleOwner) {
|
||||
if (it == "Success") {
|
||||
Toast.makeText(
|
||||
requireContext(), "Test Results Upload Successfully", Toast.LENGTH_SHORT
|
||||
requireContext(),
|
||||
"Test Results Uploaded Successfully",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
binding.progressBar.visibility = View.GONE
|
||||
}
|
||||
binding.btnSubmit.setOnClickListener(View.OnClickListener {
|
||||
Log.e("upload","db")
|
||||
|
||||
viewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable ->
|
||||
apply {
|
||||
kitSerial = DataHolder.SelectTestType?.kitSerial
|
||||
testTime = DataHolder.SelectTestType?.testTime
|
||||
isOnline = isNetworkAvailable
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupButtonClickListeners() {
|
||||
binding.btnShowResult.setOnClickListener {
|
||||
fetchHemoCubeResult()
|
||||
}
|
||||
|
||||
binding.btnSubmit.setOnClickListener {
|
||||
if (isOnline) {
|
||||
if (resultRatio != null) {
|
||||
Log.e("uploadData","resultdb")
|
||||
viewModel.uploadHemocubeResultToDatabase(requireContext(), true,true, kitSerial, resultRatio!!,testTime)
|
||||
resultRatio?.let { ratio ->
|
||||
viewModel.uploadHemocubeResultToDatabase(
|
||||
requireContext(),
|
||||
isOnline,
|
||||
true,
|
||||
kitSerial,
|
||||
ratio,
|
||||
testTime
|
||||
)
|
||||
}
|
||||
binding.progressBar.visibility = View.VISIBLE
|
||||
} else {
|
||||
if (resultRatio != null) {
|
||||
viewModel.uploadHemocubeResultToDatabase(requireContext(), true,true, kitSerial, resultRatio!!,testTime)
|
||||
resultRatio?.let { ratio ->
|
||||
viewModel.uploadHemocubeResultToDatabase(
|
||||
requireContext(),
|
||||
isOnline,
|
||||
true,
|
||||
kitSerial,
|
||||
ratio,
|
||||
testTime
|
||||
)
|
||||
}
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
@@ -65,49 +92,27 @@ class HemoCubeFragment : Fragment() {
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
})
|
||||
viewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable ->
|
||||
kitSerial = testDetails?.kitSerial
|
||||
testTime = testDetails?.testTime
|
||||
isOnline = isNetworkAvailable
|
||||
}
|
||||
}
|
||||
|
||||
return binding.root
|
||||
}
|
||||
private fun fetchHemoCubeResult() {
|
||||
viewModel.progressBar.postValue(true)
|
||||
val fullReadOutput = StringBuilder()
|
||||
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(
|
||||
object : UsbServiceListener {
|
||||
(activity as HemocubeActivity).mService.eventDrivenWriteHemoCube(object :
|
||||
UsbServiceListener {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
data?.let {
|
||||
val stringData = String(it)
|
||||
fullReadOutput.append(stringData)
|
||||
binding.tvSubtitle4.text = fullReadOutput
|
||||
resultRatio = fullReadOutput.toString()
|
||||
Log.d(
|
||||
TAG,
|
||||
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
||||
)
|
||||
|
||||
viewModel.progressBar.postValue(false)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
|
||||
viewModel.progressBar.postValue(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
companion object {
|
||||
@JvmStatic
|
||||
fun newInstance(param1: String, param2: String) =
|
||||
HemoCubeFragment().apply {
|
||||
arguments = Bundle().apply {
|
||||
putString(ARG_PARAM1, param1)
|
||||
putString(ARG_PARAM2, param2)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ import com.example.hpostesting.presentation.UsbServiceListener
|
||||
import com.example.hpostesting.presentation.utils.MyDialogListener
|
||||
import com.example.hpostesting.presentation.utils.UIUtils
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import `in`.sminnovations.hpostesting.R
|
||||
import `in`.sminnovations.hpostesting.databinding.FragmentTestRightExpReferenceBinding
|
||||
|
||||
@@ -32,8 +31,7 @@ class TestRightExpReference : Fragment() {
|
||||
private val TAG = "TestRightExpReference"
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
|
||||
): View {
|
||||
// Inflate the layout for this fragment
|
||||
binding = FragmentTestRightExpReferenceBinding.inflate(inflater, container, false)
|
||||
@@ -49,28 +47,17 @@ class TestRightExpReference : Fragment() {
|
||||
//
|
||||
// DataHolder.sampleReadCounter <= Constants.MAXIMUM_TEST_ALLOWED_BEFORE_REFERENCE
|
||||
|
||||
Log.e("mariya", "onViewCreated 1")
|
||||
if (DataHolder.deviceType.equals(Constants.DEVICE_TYPE_HOMOCUBE) ) {
|
||||
Log.e("mariya", "onViewCreated if")
|
||||
fetchHemoCubeResult()
|
||||
Log.e("mariya", "onViewCreated 1")
|
||||
if (DataHolder.deviceType.equals(Constants.DEVICE_TYPE_HOMOCUBE) ) {
|
||||
Log.e("mariya", "onViewCreated if")
|
||||
// fetchHemoCubeResult()
|
||||
if (DataHolder.deviceConstant == null) {
|
||||
viewModel.progressBar.postValue(true)
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
{ sendCmdToFetchDeviceConstant() }, Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
}
|
||||
// if (DataHolder.deviceConstant == null) {
|
||||
// viewModel.progressBar.postValue(true)
|
||||
// Handler(Looper.getMainLooper()).postDelayed(
|
||||
// { sendCmdToFetchDeviceConstant() },
|
||||
// Constants.DELAY_BETWEEN_COMMANDS
|
||||
// )
|
||||
// }
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
binding.btnSetReference.setOnClickListener {
|
||||
UIUtils.createAlertDialog(
|
||||
requireContext(),
|
||||
UIUtils.createAlertDialog(requireContext(),
|
||||
getString(R.string.have_you_placed),
|
||||
getString(R.string.please_place),
|
||||
getString(R.string.no),
|
||||
@@ -79,9 +66,9 @@ class TestRightExpReference : Fragment() {
|
||||
override fun onClickNegativeButton() {
|
||||
|
||||
}
|
||||
|
||||
override fun onClickPositiveButton() {
|
||||
// startTakingReference()
|
||||
fetchHemoCubeResult()
|
||||
startTakingReference()
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -118,51 +105,7 @@ class TestRightExpReference : Fragment() {
|
||||
* 4. command print
|
||||
*/
|
||||
private fun startTakingReference() {
|
||||
// requireActivity().runOnUiThread {
|
||||
sendCmdToSetLed()
|
||||
// }
|
||||
}
|
||||
|
||||
// private fun sendCmdToFetchDeviceConstant(recursive: Boolean) {
|
||||
private fun fetchHemoCubeResult() {
|
||||
Log.d("mariya", "fetchHemoCubeResult() called")
|
||||
|
||||
viewModel.progressBar.postValue(true)
|
||||
val fullReadOutput = StringBuilder()
|
||||
|
||||
(activity as TestRightActivity).mService.eventDrivenWrite2(
|
||||
object : UsbServiceListener {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
Log.d("mariya", data.toString())
|
||||
data?.let {
|
||||
val stringData = String(it)
|
||||
fullReadOutput.append(stringData)
|
||||
Log.d("mariya", "fullReadOutput = $fullReadOutput")
|
||||
|
||||
if (stringData.contains("OK", true)) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
||||
)
|
||||
viewModel.mapDeviceConstants(fullReadOutput.toString())
|
||||
viewModel.progressBar.postValue(false)
|
||||
} else if (stringData.contains("FINE", true)) {
|
||||
Log.d(TAG, "results = FINE")
|
||||
viewModel.progressBar.postValue(false)
|
||||
|
||||
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again!!", Constants.ERROR_CRITICAL))
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
|
||||
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again this step", Constants.ERROR_NORMAL))
|
||||
|
||||
viewModel.progressBar.postValue(false)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -173,7 +116,8 @@ class TestRightExpReference : Fragment() {
|
||||
viewModel.progressBar.postValue(true)
|
||||
val fullReadOutput = StringBuilder()
|
||||
|
||||
(activity as TestRightActivity).mService.eventDrivenWrite(TestRightCommands.read,
|
||||
(activity as TestRightActivity).mService.eventDrivenWrite(
|
||||
TestRightCommands.read,
|
||||
object : UsbServiceListener {
|
||||
override fun onUsbRead(data: ByteArray?) {
|
||||
data?.let {
|
||||
@@ -183,8 +127,7 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
if (stringData.contains("OK", true)) {
|
||||
Log.d(
|
||||
TAG,
|
||||
"onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
||||
TAG, "onUsbRead() called in sendCmdToFetchDeviceConstant() found OK"
|
||||
)
|
||||
viewModel.mapDeviceConstants(fullReadOutput.toString())
|
||||
viewModel.progressBar.postValue(false)
|
||||
@@ -192,7 +135,11 @@ class TestRightExpReference : Fragment() {
|
||||
Log.d(TAG, "results = FINE")
|
||||
viewModel.progressBar.postValue(false)
|
||||
|
||||
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again!!", Constants.ERROR_CRITICAL))
|
||||
viewModel.errorTriggered.postValue(
|
||||
ErrorMessage(
|
||||
"Please Try Again!!", Constants.ERROR_CRITICAL
|
||||
)
|
||||
)
|
||||
|
||||
}
|
||||
}
|
||||
@@ -200,7 +147,11 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchDeviceConstant() -> $e")
|
||||
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again this step", Constants.ERROR_NORMAL))
|
||||
viewModel.errorTriggered.postValue(
|
||||
ErrorMessage(
|
||||
"Please Try Again this step", Constants.ERROR_NORMAL
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.progressBar.postValue(false)
|
||||
}
|
||||
@@ -225,8 +176,7 @@ class TestRightExpReference : Fragment() {
|
||||
Runnable {
|
||||
viewModel.progressBar.postValue(false)
|
||||
sendCmdToAutoset()
|
||||
},
|
||||
Constants.DELAY_BETWEEN_COMMANDS
|
||||
}, Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -257,8 +207,7 @@ class TestRightExpReference : Fragment() {
|
||||
Runnable {
|
||||
viewModel.progressBar.postValue(false)
|
||||
sendCmdToRun()
|
||||
},
|
||||
Constants.DELAY_BETWEEN_COMMANDS
|
||||
}, Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
|
||||
}
|
||||
@@ -291,8 +240,7 @@ class TestRightExpReference : Fragment() {
|
||||
Runnable {
|
||||
viewModel.progressBar.postValue(false)
|
||||
sendCmdToFetchLightIntensities()
|
||||
},
|
||||
Constants.DELAY_BETWEEN_COMMANDS
|
||||
}, Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -322,7 +270,12 @@ class TestRightExpReference : Fragment() {
|
||||
// Log.d(TAG, stringData)
|
||||
// if (stringData.contains("OK", true)) {
|
||||
// if (fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK", true)) {
|
||||
if (stringData.trim().isNotEmpty() && fullReadOutput.substring(Math.max(fullReadOutput.length - 15, 0)).contains("OK [0]", true)) {
|
||||
if (stringData.trim().isNotEmpty() && fullReadOutput.substring(
|
||||
Math.max(
|
||||
fullReadOutput.length - 15, 0
|
||||
)
|
||||
).contains("OK [0]", true)
|
||||
) {
|
||||
|
||||
Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()")
|
||||
// Log.d(TAG, fullReadOutput.toString())
|
||||
@@ -344,7 +297,11 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
override fun onUsbError(e: Exception?) {
|
||||
Log.e(TAG, "onUsbIoError() called in sendCmdToFetchLightIntensities() -> $e")
|
||||
viewModel.errorTriggered.postValue(ErrorMessage("Please Try Again this step", Constants.ERROR_NORMAL))
|
||||
viewModel.errorTriggered.postValue(
|
||||
ErrorMessage(
|
||||
"Please Try Again this step", Constants.ERROR_NORMAL
|
||||
)
|
||||
)
|
||||
|
||||
viewModel.progressBar.postValue(false)
|
||||
}
|
||||
|
||||
@@ -111,21 +111,6 @@
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tv_skip"
|
||||
style="@style/title1_2"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginHorizontal="24dp"
|
||||
android:layout_marginTop="36dp"
|
||||
android:text="@string/set_baseline_reference_again"
|
||||
android:textColor="@color/blue_app_dark"
|
||||
android:textStyle="italic"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.0"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/btn_submit" />
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user