Handling of error messages and showing toast from viewmodel.
This commit is contained in:
4
.idea/misc.xml
generated
4
.idea/misc.xml
generated
@@ -19,10 +19,10 @@
|
||||
<entry key="app/src/main/res/layout/activity_main.xml" value="0.165" />
|
||||
<entry key="app/src/main/res/layout/activity_splash.xml" value="0.24375" />
|
||||
<entry key="app/src/main/res/layout/activity_test_right.xml" value="0.24375" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_exp_reference.xml" value="0.2972222222222222" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_exp_reference.xml" value="0.25" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_exp_sample.xml" value="0.25" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_process.xml" value="0.24375" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_results.xml" value="0.33" />
|
||||
<entry key="app/src/main/res/layout/fragment_test_right_results.xml" value="0.25" />
|
||||
<entry key="app/src/main/res/layout/table_item.xml" value="0.22407407407407406" />
|
||||
<entry key="app/src/main/res/menu/menu.xml" value="0.25" />
|
||||
<entry key="app/src/main/res/menu/my_menu.xml" value="0.25" />
|
||||
|
||||
@@ -8,9 +8,7 @@ import android.hardware.usb.UsbManager
|
||||
import android.os.Bundle
|
||||
import android.os.IBinder
|
||||
import android.util.Log
|
||||
import android.view.Gravity
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import android.view.*
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.ContextCompat
|
||||
@@ -48,7 +46,7 @@ class TestRightActivity : AppCompatActivity() {
|
||||
connectUsb(true)
|
||||
}
|
||||
} else {
|
||||
onUsbError("permission denied for device")
|
||||
onErrorReported("permission denied for device")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,10 +89,12 @@ class TestRightActivity : AppCompatActivity() {
|
||||
Log.d("USB OBSERVE", "testrightactivity called -> $it")
|
||||
if (it){
|
||||
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24)
|
||||
// binding.clParent.setAllEnabled(true)
|
||||
} else {
|
||||
myMenu?.get(0)?.icon = ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24)
|
||||
// val myToast = Toast.makeText(this, "USB is Detached", Toast.LENGTH_SHORT)
|
||||
Toast.makeText(this, "Device is Disconnected", Toast.LENGTH_SHORT).show()
|
||||
// binding.clParent.setAllEnabled(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,7 +110,7 @@ class TestRightActivity : AppCompatActivity() {
|
||||
val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager)
|
||||
|
||||
if (availableDrivers.isEmpty()) {
|
||||
onUsbError("No Device is Connected")
|
||||
onErrorReported("No Device is Connected")
|
||||
} else {
|
||||
mDriver = availableDrivers[0]
|
||||
|
||||
@@ -182,7 +182,7 @@ class TestRightActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
fun onUsbError(msg: String) {
|
||||
fun onErrorReported(msg: String) {
|
||||
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
|
||||
// Snackbar.make(binding.clParent, msg, Snackbar.LENGTH_LONG)
|
||||
// .setAction("CLOSE") { Toast.makeText(this, "Will soon", Toast.LENGTH_SHORT).show() }
|
||||
@@ -217,5 +217,9 @@ class TestRightActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
// fun View.setAllEnabled(enabled: Boolean) {
|
||||
// isEnabled = enabled
|
||||
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -8,6 +8,7 @@ import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.core.view.children
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import com.example.hpos.R
|
||||
@@ -47,22 +48,16 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
if (DataHolder.deviceConstant == null) {
|
||||
viewModel.progressBar.postValue(true)
|
||||
Handler(Looper.getMainLooper()).postDelayed({ sendCmdToFetchDeviceConstant() }, Constants.DELAY_BETWEEN_COMMANDS)
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
{ sendCmdToFetchDeviceConstant() },
|
||||
Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
|
||||
// if (viewModel.progressBar.value != null){
|
||||
// return viewModel.progressBar.value!!
|
||||
// } else {
|
||||
// return false
|
||||
// }
|
||||
// }
|
||||
|
||||
private fun setupListeners() {
|
||||
binding.btnSetReference.setOnClickListener {
|
||||
UIUtils().createAlertDialog(
|
||||
UIUtils.createAlertDialog(
|
||||
requireContext(),
|
||||
getString(R.string.have_you_placed),
|
||||
getString(R.string.please_place),
|
||||
@@ -72,7 +67,6 @@ class TestRightExpReference : Fragment() {
|
||||
override fun onClickBtnOne() {
|
||||
startTakingReference()
|
||||
}
|
||||
|
||||
override fun onClickBtnTwo() {
|
||||
// Todo: Only for testing
|
||||
// viewModel.progressBar.postValue(true)
|
||||
@@ -80,6 +74,12 @@ class TestRightExpReference : Fragment() {
|
||||
})
|
||||
}
|
||||
|
||||
viewModel.errorTriggered.observe(viewLifecycleOwner) {
|
||||
if (!it.isNullOrEmpty()){
|
||||
UIUtils.onShowErrorToast(requireContext(), it)
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.progressBar.observe(viewLifecycleOwner) {
|
||||
// Log.d(TAG, "SURYA OBSERVER Activated outcome = $it")
|
||||
if (it) {
|
||||
@@ -129,10 +129,14 @@ class TestRightExpReference : Fragment() {
|
||||
)
|
||||
viewModel.mapDeviceConstants(fullReadOutput.toString())
|
||||
viewModel.progressBar.postValue(false)
|
||||
} else if (stringData.contains("FINE", true)){
|
||||
} else if (stringData.contains("FINE", true)) {
|
||||
Log.d(TAG, "results = FINE")
|
||||
viewModel.progressBar.postValue(false)
|
||||
Toast.makeText(requireContext(), "Please try again this step", Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
"Please try again this step",
|
||||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -225,8 +229,6 @@ class TestRightExpReference : Fragment() {
|
||||
|
||||
if (stringData.contains("OK", true)) {
|
||||
Log.d(TAG, "onUsbRead() called in sendCmdToRun()")
|
||||
// viewModel.progressBar.postValue(false)
|
||||
// sendCmdToFetchLightIntensities()
|
||||
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
Runnable {
|
||||
@@ -263,14 +265,12 @@ class TestRightExpReference : Fragment() {
|
||||
Log.d(TAG, "onUsbRead() called in sendCmdToFetchLightIntensities()")
|
||||
viewModel.mapIntensityValues(fullReadOutput.toString(), true)
|
||||
DataHolder.isReferenceTaken = true
|
||||
// viewModel.progressBar.postValue(false)
|
||||
|
||||
Handler(Looper.getMainLooper()).postDelayed(
|
||||
Runnable {
|
||||
{
|
||||
viewModel.progressBar.postValue(false)
|
||||
moveToSamplePage()
|
||||
},
|
||||
Constants.DELAY_BETWEEN_COMMANDS
|
||||
}, Constants.DELAY_BETWEEN_COMMANDS
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -288,6 +288,10 @@ class TestRightExpReference : Fragment() {
|
||||
.commit()
|
||||
}
|
||||
|
||||
// fun onErrorReported(msg: String) {
|
||||
// Toast.makeText(requireContext(), msg, Toast.LENGTH_SHORT).show()
|
||||
// }
|
||||
|
||||
// private fun testWriteConnection() {
|
||||
// Log.d(TAG, "testWriteConnection() called")
|
||||
// viewModel.progressBar.postValue(true)
|
||||
@@ -362,4 +366,10 @@ class TestRightExpReference : Fragment() {
|
||||
// }, Constants.DELAY_BETWEEN_COMMANDS)
|
||||
//
|
||||
// }
|
||||
|
||||
// fun View.setAllEnabled(enabled: Boolean) {
|
||||
// isEnabled = enabled
|
||||
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
|
||||
// }
|
||||
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.core.view.children
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import com.example.hpos.R
|
||||
@@ -16,6 +18,7 @@ import com.example.hpos.data.model.PatientData
|
||||
import com.example.hpos.data.model.TestRightResultType
|
||||
import com.example.hpos.databinding.FragmentTestRightExpSampleBinding
|
||||
import com.example.hpos.presentation.UsbServiceListener
|
||||
import com.example.hpos.presentation.utils.UIUtils
|
||||
|
||||
class TestRightExpSample : Fragment() {
|
||||
|
||||
@@ -58,6 +61,12 @@ class TestRightExpSample : Fragment() {
|
||||
}
|
||||
}
|
||||
|
||||
viewModel.errorTriggered.observe(viewLifecycleOwner) {
|
||||
if (!it.isNullOrEmpty()){
|
||||
UIUtils.onShowErrorToast(requireContext(), it)
|
||||
}
|
||||
}
|
||||
|
||||
binding.btnAcquire.setOnClickListener {
|
||||
val details = isValidInput()
|
||||
if (details != null) {
|
||||
@@ -172,6 +181,11 @@ class TestRightExpSample : Fragment() {
|
||||
.commit()
|
||||
}
|
||||
|
||||
// fun View.setAllEnabled(enabled: Boolean) {
|
||||
// isEnabled = enabled
|
||||
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
|
||||
// }
|
||||
|
||||
// private fun Button.disable() {
|
||||
// backgroundTintList = ContextCompat.getColorStateList(requireActivity(), R.color.gray)
|
||||
// isClickable = false
|
||||
@@ -182,9 +196,4 @@ class TestRightExpSample : Fragment() {
|
||||
//// background.setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY)
|
||||
// isClickable = true
|
||||
// }
|
||||
//
|
||||
// fun View.setAllEnabled(enabled: Boolean) {
|
||||
// isEnabled = enabled
|
||||
// if (this is ViewGroup) children.forEach { child -> child.setAllEnabled(enabled) }
|
||||
// }
|
||||
}
|
||||
@@ -24,24 +24,15 @@ class TestRightViewModel : ViewModel() {
|
||||
|
||||
private val TAG = "TestRightViewModel"
|
||||
|
||||
// var isReferenceDone = false
|
||||
var isServiceConnected = false
|
||||
val progressBar = MutableLiveData(false)
|
||||
|
||||
val isUsbConnected = MutableLiveData(false)
|
||||
val errorTriggered = MutableLiveData<String>("")
|
||||
|
||||
lateinit var patientDetails: PatientData
|
||||
// var sampleReadCounter = 0
|
||||
|
||||
// var deviceConstant: TestRightDeviceConstants? = null
|
||||
private lateinit var calculationData: TestRightCalculationData
|
||||
|
||||
// /* Contains wavelength -> pixel no.*/
|
||||
// val wavelengthToPixelArray = ArrayList<Double>()
|
||||
//
|
||||
// /* (For reference/baseline) Contains pixel no. -> Intensity of light falling on that pixel */
|
||||
// private val intensityReferenceArray = ArrayList<Double>()
|
||||
|
||||
/* (For exp sample) Contains pixel no. -> Intensity of light falling on that pixel */
|
||||
val intensitySampleArray = ArrayList<Double>()
|
||||
|
||||
@@ -65,9 +56,13 @@ class TestRightViewModel : ViewModel() {
|
||||
mapPixelNumberToWavelength()
|
||||
} else {
|
||||
// Todo: Throws error
|
||||
// "Error 201: In processing the data from device"
|
||||
errorTriggered.postValue("Error 201: In processing the data from device")
|
||||
}
|
||||
} else {
|
||||
// Todo: Throws error (showing error if empty by using a mutable error string)
|
||||
// "Error 202: Unable to fetch data from device."
|
||||
errorTriggered.postValue("Error 202: Unable to fetch data from device.")
|
||||
}
|
||||
// }
|
||||
}
|
||||
@@ -90,12 +85,13 @@ class TestRightViewModel : ViewModel() {
|
||||
}
|
||||
} else {
|
||||
// Todo: Throws error
|
||||
// "Error 203: Unable to fetch data from device."
|
||||
errorTriggered.postValue("Error 203: Unable to fetch data from device.")
|
||||
}
|
||||
// }
|
||||
}
|
||||
|
||||
fun mapIntensityValues(fullString: String, isReference: Boolean) {
|
||||
// viewModelScope.launch {
|
||||
|
||||
if (isReference) DataHolder.intensityReferenceArray.clear()
|
||||
else intensitySampleArray.clear()
|
||||
@@ -110,37 +106,30 @@ class TestRightViewModel : ViewModel() {
|
||||
// }
|
||||
// saveLogTest(DataHolder.appFolderPath, fileName, isReference, fullString)
|
||||
|
||||
Log.d(TAG, fullString)
|
||||
|
||||
val listOfString = fullString.split("\n")
|
||||
|
||||
// Log.d(TAG, "inside mapIntensityValues() -> isReference = $isReference")
|
||||
// Log.d(TAG, "listOfString size = ${listOfString.size}")
|
||||
|
||||
// if(!isReference){
|
||||
// Log.d(TAG, "fullString = $fullString")
|
||||
// }
|
||||
|
||||
Log.d(TAG, fullString)
|
||||
val listOfString = fullString.split("\n")
|
||||
|
||||
for (line in listOfString) {
|
||||
if ("Buf" in line) {
|
||||
/* Removing trailing spaces and "Buf" from the string then taking lhs & rhs of ':' */
|
||||
val numbers = line.trim()
|
||||
.substring(3).split(":")
|
||||
// Log.d(TAG, line)
|
||||
if (numbers.size >= 2) {
|
||||
// val temp = ArrayList<Double>(2)
|
||||
// temp.add(numbers[0].toDouble())
|
||||
// temp.add(numbers[1].toDouble())
|
||||
|
||||
// if(!isReference){
|
||||
// Log.d(TAG, "temp[0] = ${temp[0]} & temp[1] = ${temp[1]}")
|
||||
// }
|
||||
|
||||
if (isReference)
|
||||
DataHolder.intensityReferenceArray.add(numbers[1].toDouble())
|
||||
else
|
||||
intensitySampleArray.add(numbers[1].toDouble())
|
||||
} else {
|
||||
// Todo: Error handling
|
||||
// "Error 204: Unable to fetch data from device."
|
||||
errorTriggered.postValue("Error 204: Unable to fetch data from device.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,6 +167,7 @@ class TestRightViewModel : ViewModel() {
|
||||
|
||||
fun mapWavelengthToAbsorbance() {
|
||||
if (DataHolder.intensityReferenceArray.size != intensitySampleArray.size || DataHolder.wavelengthToPixelArray.size != intensitySampleArray.size || DataHolder.wavelengthToPixelArray.size != DataHolder.intensityReferenceArray.size) {
|
||||
errorTriggered.postValue("Error 205: Unable to fetch data from device, please try again by reopening the app")
|
||||
throw Exception("Inconsistency in the data, size of arrays are not same. \nintensityReferenceArray.size = ${DataHolder.intensityReferenceArray.size} ; intensitySampleArray.size = ${intensitySampleArray.size} ; wavelengthToPixelArray.size = ${DataHolder.wavelengthToPixelArray.size}")
|
||||
}
|
||||
|
||||
@@ -188,8 +178,6 @@ class TestRightViewModel : ViewModel() {
|
||||
// for ((index,wavelength) in wavelengthToPixelArray.withIndex()) {
|
||||
for (index in 0 until DataHolder.wavelengthToPixelArray.size) {
|
||||
|
||||
// Todo: handle indexInIntensityArrays <0
|
||||
|
||||
val invertedPixelIndex = (Constants.TEST_RIGHT_TOTAL_PIXEL) - (index + 1)
|
||||
|
||||
val i0 = DataHolder.intensityReferenceArray[invertedPixelIndex]
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.example.hpos.presentation.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.widget.Toast
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
class UIUtils {
|
||||
object UIUtils {
|
||||
|
||||
fun createAlertDialog(
|
||||
context: Context,
|
||||
@@ -25,4 +26,11 @@ class UIUtils {
|
||||
.show()
|
||||
}
|
||||
|
||||
fun onShowErrorToast(context: Context, msg: String) {
|
||||
if (msg.length > 50)
|
||||
Toast.makeText(context, msg, Toast.LENGTH_LONG).show()
|
||||
else
|
||||
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user