From cf429b4fc38faad68f3e2884f3cf82c7a10c7133 Mon Sep 17 00:00:00 2001 From: chandrashekhar reddy Date: Wed, 11 Sep 2024 17:11:53 +0530 Subject: [PATCH] Changes in 127, added HBTest screen option --- app/src/main/AndroidManifest.xml | 4 + .../hpostesting/data/model/test/TestType.kt | 3 +- .../presentation/KitScanActivity.kt | 33 +- .../hpostesting/presentation/MainActivity.kt | 19 +- .../presentation/hb_test/HBTestActivity.kt | 194 +++++++++++ .../presentation/hb_test/HBTestFragment.kt | 317 ++++++++++++++++++ .../presentation/hb_test/HBTestViewModel.kt | 83 +++++ app/src/main/res/layout/activity_hb_test.xml | 53 +++ app/src/main/res/layout/fragment_gallery.xml | 2 +- app/src/main/res/layout/fragment_hb_test.xml | 158 +++++++++ 10 files changed, 845 insertions(+), 21 deletions(-) create mode 100644 app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestActivity.kt create mode 100644 app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestFragment.kt create mode 100644 app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestViewModel.kt create mode 100644 app/src/main/res/layout/activity_hb_test.xml create mode 100644 app/src/main/res/layout/fragment_hb_test.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index a518393..183d582 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -74,6 +74,10 @@ android:name="com.example.hpostesting.presentation.diagnostics.DiagnosticsActivity" android:exported="false" android:theme="@style/Theme.HPOS.NoActionBar" /> + - when (deviceType) { - Constants.DEVICE_TYPE_HEMOCUBE -> { - val i = Intent(applicationContext, HemocubeActivity::class.java) - startActivity(i) - } + if(DataHolder.selectedTestType == TestType.HB_EST){ + val i = Intent(applicationContext, HBTestActivity::class.java) + startActivity(i) + }else{ + DataHolder.deviceType.observe(this) { deviceType -> + when (deviceType) { + Constants.DEVICE_TYPE_HEMOCUBE -> { + val i = Intent(applicationContext, HemocubeActivity::class.java) + startActivity(i) + } - Constants.DEVICE_TYPE_TEST_RIGHT -> { - val i = Intent(applicationContext, TestRightActivity::class.java) - startActivity(i) - } + Constants.DEVICE_TYPE_TEST_RIGHT -> { + val i = Intent(applicationContext, TestRightActivity::class.java) + startActivity(i) + } - Constants.DEVICE_TYPE_TRUEHEME -> { - val i = Intent(applicationContext, HemocubeActivity::class.java) - startActivity(i) + Constants.DEVICE_TYPE_TRUEHEME -> { + val i = Intent(applicationContext, HemocubeActivity::class.java) + startActivity(i) + } } } } diff --git a/app/src/main/java/com/example/hpostesting/presentation/MainActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/MainActivity.kt index 6faec8a..fec0c30 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/MainActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/MainActivity.kt @@ -103,7 +103,7 @@ class MainActivity : AppCompatActivity() { with(binding) { if (deviceType == Constants.DEVICE_TYPE_HEMOCUBE) { cvItem1.visibility = View.VISIBLE - cvItem3.visibility = View.GONE + cvItem3.visibility = View.VISIBLE cvItem2.visibility = View.GONE cvItem4.visibility = View.GONE } @@ -132,7 +132,7 @@ class MainActivity : AppCompatActivity() { when { device.productId == Constants.HOMO_CUBE_ID && device.vendorId == Constants.VENDOR_ID -> { binding.cvItem1.visibility = View.VISIBLE - binding.cvItem3.visibility = View.GONE + binding.cvItem3.visibility = View.VISIBLE binding.cvItem2.visibility = View.GONE binding.cvItem4.visibility = View.GONE DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HEMOCUBE) @@ -141,7 +141,7 @@ class MainActivity : AppCompatActivity() { device.productId == Constants.HEMO_CUBE_V2_PRODUCT_ID && device.vendorId == Constants.HEMO_CUBE_V2_VENDOR_ID -> { binding.cvItem1.visibility = View.VISIBLE - binding.cvItem3.visibility = View.GONE + binding.cvItem3.visibility = View.VISIBLE binding.cvItem2.visibility = View.GONE binding.cvItem4.visibility = View.GONE DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HEMOCUBE) @@ -150,7 +150,7 @@ class MainActivity : AppCompatActivity() { device.productId == 24577 && device.vendorId == 1027 -> { binding.cvItem1.visibility = View.VISIBLE - binding.cvItem3.visibility = View.GONE + binding.cvItem3.visibility = View.VISIBLE binding.cvItem2.visibility = View.GONE binding.cvItem4.visibility = View.GONE DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_TRUEHEME) @@ -159,7 +159,7 @@ class MainActivity : AppCompatActivity() { device.productId == 8963 && device.vendorId == 1659 -> { binding.cvItem1.visibility = View.VISIBLE - binding.cvItem3.visibility = View.GONE + binding.cvItem3.visibility = View.VISIBLE binding.cvItem2.visibility = View.GONE binding.cvItem4.visibility = View.GONE DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HEMOCUBE) @@ -168,7 +168,7 @@ class MainActivity : AppCompatActivity() { device.productId == 4614 && device.vendorId == 7111 -> { binding.cvItem1.visibility = View.VISIBLE - binding.cvItem3.visibility = View.GONE + binding.cvItem3.visibility = View.VISIBLE binding.cvItem2.visibility = View.GONE binding.cvItem4.visibility = View.GONE DataHolder.deviceType.postValue(Constants.DEVICE_TYPE_HEMOCUBE) @@ -232,6 +232,13 @@ class MainActivity : AppCompatActivity() { startActivity(i) finish() } + binding.cvItem3.setOnClickListener { + DataHolder.selectedTestType = TestType.HB_EST + val i = Intent(applicationContext, KitScanActivity::class.java) + i.putExtra("fromWhere","Main") + startActivity(i) + finish() + } DataHolder.usbConnected.observe(this) { if (it) { diff --git a/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestActivity.kt new file mode 100644 index 0000000..8f1ebf2 --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestActivity.kt @@ -0,0 +1,194 @@ +/* + * // Copyright (c) 2024 ShanMukha Innovations Pvt. Ltd. All rights reserved. + * // Notice: All information contained herein is, and remains + * // the property of ShanMukha Innovations Pvt. Ltd. and its suppliers, + * // if any. The intellectual and technical concepts contained + * // herein are proprietary to ShanMukha Innovations Pvt. Ltd. + * // and its suppliers and may be covered by Indian and Foreign Patents, + * // patents in process, and are protected by trade secret or copyright law. + * // Dissemination of this information or reproduction of this material + * // is strictly forbidden unless prior written permission is obtained + * // from ShanMukha Innovations Pvt. Ltd. + */ + +package com.example.hpostesting.presentation.hb_test + +import android.annotation.SuppressLint +import android.app.PendingIntent +import android.content.BroadcastReceiver +import android.content.ComponentName +import android.content.Context +import android.content.Intent +import android.content.IntentFilter +import android.content.ServiceConnection +import android.hardware.usb.UsbDevice +import android.hardware.usb.UsbDeviceConnection +import android.hardware.usb.UsbManager +import android.os.Bundle +import android.os.IBinder +import android.util.Log +import android.view.Menu +import android.widget.Toast +import androidx.activity.viewModels +import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat +import androidx.core.view.get +import com.example.hpostesting.data.constant.DataHolder +import com.example.hpostesting.data.constant.Constants +import com.example.hpostesting.data.constant.LanguageManager +import com.example.hpostesting.util.UsbService +import com.hoho.android.usbserial.driver.UsbSerialDriver +import com.hoho.android.usbserial.driver.UsbSerialProber +import dagger.hilt.android.AndroidEntryPoint +import `in`.sminnovations.hpostesting.R +import `in`.sminnovations.hpostesting.databinding.ActivityAutoDacBinding +import `in`.sminnovations.hpostesting.databinding.ActivityHbTestBinding + +@AndroidEntryPoint +class HBTestActivity : AppCompatActivity() { + private lateinit var binding: ActivityHbTestBinding + val viewModel: HBTestViewModel by viewModels() + private var myMenu: Menu? = null + + private lateinit var mDriver: UsbSerialDriver + private var mConnection: UsbDeviceConnection? = null + lateinit var mService: UsbService + + private val TAG = "HemoCube" + + private val broadcastReceiver = object : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) { + + synchronized(this) { + val device: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) + + if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) { + device?.apply { + connectUsb(true) + DataHolder.usbConnected.postValue(true) + } + } else { + onErrorReported("permission denied for device") + DataHolder.usbConnected.postValue(true) + } + } + } + } + + private val connection = object : ServiceConnection { + override fun onServiceConnected(className: ComponentName, service: IBinder) { + val binder = service as UsbService.UsbServiceBinder + mService = binder.getService() + viewModel.isServiceConnected = true + mConnection.let { mService.connect(mDriver, mConnection!!) } + moveToNext() + } + + override fun onServiceDisconnected(arg0: ComponentName) { + viewModel.isServiceConnected = false + } + } + + override fun attachBaseContext(newBase: Context?) { + val languageCode = LanguageManager.getSavedLanguage(newBase!!) + LanguageManager.setLocale(newBase, languageCode) + super.attachBaseContext(newBase) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = ActivityHbTestBinding.inflate(layoutInflater) + setContentView(binding.root) +// setSupportActionBar(binding.myToolbar) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + setupListener() + connectUsb(false) + + } + + private fun setupListener() { + DataHolder.usbConnected.observe(this) { + Log.d("USB OBSERVE", "HemoCube called -> $it") + if (it) { + myMenu?.get(0)?.icon = + ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_24) + } else { + myMenu?.get(0)?.icon = + ContextCompat.getDrawable(this, R.drawable.ic_baseline_usb_off_24) + Toast.makeText(this, "Device is Disconnected", Toast.LENGTH_SHORT).show() + } + } + } + + open fun connectUsb(permissionGranted: Boolean) { + Log.d(TAG, "connectUsb() called, permission variable = $permissionGranted") + val manager = getSystemService(Context.USB_SERVICE) as UsbManager + val availableDrivers = UsbSerialProber.getDefaultProber().findAllDrivers(manager) + + if (availableDrivers.isEmpty()) { + onErrorReported("No Device is Connected") + } else { + mDriver = availableDrivers[0] + mConnection = manager.openDevice(mDriver.device) + + if (mConnection == null) { + requestUserPermission(manager, mDriver.device) + } else { + setupService() + } + } + } + + fun onErrorReported(msg: String) { + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show() + if (!isFinishing) onBackPressed() + } + + + private fun moveToNext() { + if (supportFragmentManager.isDestroyed) return + + supportFragmentManager.beginTransaction().replace(binding.fgHbTest.id, HBTestFragment()) + .commit() + } + + @SuppressLint("MutableImplicitPendingIntent") + private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { + val mPendingIntent: PendingIntent + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + mPendingIntent = PendingIntent.getBroadcast( + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + ) + } else { + mPendingIntent = PendingIntent.getBroadcast( + this, + 0, + Intent(Constants.HEMOCUBE_USB_PERMISSION), + PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE + ) + } + + val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) + registerReceiver(broadcastReceiver, filter) + manager.requestPermission(device, mPendingIntent) + } + + fun setupService() { + val intent = Intent(this, UsbService::class.java) + bindService(intent, connection, Context.BIND_AUTO_CREATE) + } + + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + menuInflater.inflate(R.menu.my_menu, menu) + return true + } + + override fun onDestroy() { + super.onDestroy() + if (viewModel.isServiceConnected) { + mService.disconnect() + unbindService(connection) + viewModel.isServiceConnected = false + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestFragment.kt new file mode 100644 index 0000000..ffdc477 --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestFragment.kt @@ -0,0 +1,317 @@ +/* + * // Copyright (c) 2024 ShanMukha Innovations Pvt. Ltd. All rights reserved. + * // Notice: All information contained herein is, and remains + * // the property of ShanMukha Innovations Pvt. Ltd. and its suppliers, + * // if any. The intellectual and technical concepts contained + * // herein are proprietary to ShanMukha Innovations Pvt. Ltd. + * // and its suppliers and may be covered by Indian and Foreign Patents, + * // patents in process, and are protected by trade secret or copyright law. + * // Dissemination of this information or reproduction of this material + * // is strictly forbidden unless prior written permission is obtained + * // from ShanMukha Innovations Pvt. Ltd. + */ + +package com.example.hpostesting.presentation.hb_test + +import android.content.Context +import android.content.SharedPreferences +import android.os.Bundle +import android.text.method.ScrollingMovementMethod +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Toast +import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels +import androidx.lifecycle.MutableLiveData +import com.example.hpostesting.data.constant.Constants +import com.example.hpostesting.data.constant.HemoCubeCommands +import com.example.hpostesting.data.model.diagnostics.DiagnosticsData +import com.example.hpostesting.data.model.patient.DeviceData +import com.example.hpostesting.data.model.patient.HemoCubeTestData +import com.example.hpostesting.presentation.utils.UsbServiceListener +import com.google.firebase.crashlytics.ktx.crashlytics +import com.google.firebase.ktx.Firebase +import `in`.sminnovations.hpostesting.R +import `in`.sminnovations.hpostesting.databinding.FragmentAutoDacBinding +import `in`.sminnovations.hpostesting.databinding.FragmentHbTestBinding +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale +import kotlin.math.log10 + +class HBTestFragment : Fragment() { + private var readClick = false + private var hbEst = 0.0 + private var testStatusCode = 0.0 + private var testDetails: HemoCubeTestData = HemoCubeTestData() + private lateinit var binding: FragmentHbTestBinding + private val hBTestViewModel: HBTestViewModel by activityViewModels() + private lateinit var sharedPreferences: SharedPreferences + private var currentDeviceData: DeviceData? = null + private var resultData: String = "" + // private val messages = MutableLiveData() + private var startListening = MutableLiveData(false) + + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?, + ): View { + binding = FragmentHbTestBinding.inflate(inflater, container, false) + sharedPreferences = + requireContext().getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + initViews() + observeViewModel() + } + + private fun initViews() { + binding.btnSubmit.visibility = View.GONE + listenToHemoCube() + getDeviceId() + binding.tvSubtitle4.movementMethod = ScrollingMovementMethod() + binding.btnSubmit.setOnClickListener { + hBTestViewModel.messages.postValue(resultData) + binding.btnSubmit.visibility = View.GONE + + hBTestViewModel.uploadFirebaseQc(testDetails) + } + binding.btnBuffer.setOnClickListener { + binding.btnBuffer.visibility = View.GONE + runBCommand() + } + binding.btnSample.setOnClickListener { + binding.btnSample.visibility = View.GONE + runSCommand() + } + binding.btnSubmit.setOnClickListener { + binding.btnSubmit.visibility = View.GONE + + } + + } + + private fun observeViewModel() { + + hBTestViewModel.deviceData.observe(viewLifecycleOwner) { + currentDeviceData = it + } + + hBTestViewModel.messages.observe(viewLifecycleOwner) { + binding.tvSubtitle4.text = it + } + + hBTestViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> + if (result == "Success") { + showToast("Auto Dac data uploaded successfully") + } + if (result == "Local") { + showToast("Auto Dac uploading failed, note it down manually") + } + + binding.progressBar.visibility = View.GONE + } + } + + private fun getDeviceId() { + hBTestViewModel.progressBar.postValue(true) + (activity as HBTestActivity).mService.sendAndListenToHemoCube( + HemoCubeCommands.DEVICE_CONFIGURATION_COMMAND, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + hBTestViewModel.messages.postValue(stringData) + binding.tvSubtitle4.text = stringData + } + } + + override fun onUsbError(e: Exception?) { + hBTestViewModel.progressBar.postValue(false) + } + }) + } + private fun runBCommand() { + hBTestViewModel.progressBar.postValue(true) + (activity as HBTestActivity).mService.sendAndListenToHemoCube( + HemoCubeCommands.START_BUFFER_COMMAND, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + } + + override fun onUsbError(e: Exception?) { + hBTestViewModel.progressBar.postValue(false) + } + }) + } + private fun runSCommand() { + hBTestViewModel.progressBar.postValue(true) + (activity as HBTestActivity).mService.sendAndListenToHemoCube( + HemoCubeCommands.START_SAMPLE, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + } + + override fun onUsbError(e: Exception?) { + hBTestViewModel.progressBar.postValue(false) + } + }) + } + private fun runPCommand() { + hBTestViewModel.progressBar.postValue(true) + (activity as HBTestActivity).mService.sendAndListenToHemoCube( + HemoCubeCommands.PRINT_COMMAND, + object : UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + } + + override fun onUsbError(e: Exception?) { + hBTestViewModel.progressBar.postValue(false) + } + }) + } + + private fun listenToHemoCube() { + + val fullReadOutput = StringBuilder() + startListening.postValue(true) + + try { + (activity as HBTestActivity).mService.listenToHemoCube(object : + UsbServiceListener { + override fun onUsbRead(data: ByteArray?) { + data?.let { + val stringData = String(it) + fullReadOutput.append(stringData) + resultData += stringData + if (sharedPreferences.getString(Constants.USER_ID, "").toString() == "ADMIN") { + hBTestViewModel.messages.postValue(resultData) + } + // binding.tvSubtitle4.text = resultData + if (stringData.contains("SN")) { + val slData = stringData.split(" ") + if (slData.size > 1) { + val hardwareId = slData[1].trim() +// with(sharedPreferences.edit()) { +// putString(Constants.DEVICE_ID, hardwareId) +// apply() +// } + } + activity?.runOnUiThread { + binding.btnBuffer.visibility = View.VISIBLE + } + } + } + if (resultData.contains("#BC") && testStatusCode < 1.0) { + testStatusCode = 1.1 + hBTestViewModel.messages.postValue( + getString(R.string.buffer_completed) + "\n" + getString(R.string.gathering_data) + ) + activity?.runOnUiThread { + binding.btnSample.visibility = View.VISIBLE + } + } + + if (resultData.contains("#SC") && testStatusCode < 1.3) { + testStatusCode = 1.4 + hBTestViewModel.messages.postValue( + getString(R.string.sample_completed) + "\n" + getString(R.string.gathering_data) + ) + activity?.runOnUiThread { + binding.btnSubmit.visibility = View.VISIBLE + } + runPCommand() + } + + if (resultData.contains("REND") && readClick && testStatusCode < 2.1) { + testStatusCode = 2.2 + runResult() + } + } + + override fun onUsbError(e: Exception?) { + hBTestViewModel.progressBar.postValue(false) + } + }) + } catch (e: Exception) { + Firebase.crashlytics.recordException(e) + } + } + + private fun runResult() { + hBTestViewModel.messages.postValue("Fetching results...") + val resultLines = resultData.split("\\s+(?=LB|LS)".toRegex()) + var bufferIntensity = resultLines[1].split(' ')[1].trim() + val led1BufferForDevice = bufferIntensity.toDoubleOrNull()!! + + bufferIntensity = resultLines[2].split(' ')[1].trim() + val led2BufferForDevice = bufferIntensity.toDoubleOrNull()!! + + bufferIntensity = resultLines[3].split(' ')[1].trim() + val led3BufferForDevice = bufferIntensity.toDoubleOrNull()!! + + bufferIntensity = resultLines[4].split(' ')[1].trim() + val led4BufferForDevice = bufferIntensity.toDoubleOrNull()!! + + + val led1SampleForDevice = resultLines[5].split(' ')[1].trim().toDoubleOrNull()!! + val led2SampleForDevice = resultLines[6].split(' ')[1].trim().toDoubleOrNull()!! + val led3SampleForDevice = resultLines[7].split(' ')[1].trim().toDoubleOrNull()!! + val led4SampleForDevice = resultLines[8].split(' ')[1].split('\r')[0].trim().toDoubleOrNull()!! + + val led1Average = log10(led1BufferForDevice.div(led1SampleForDevice)) + val led2Average = log10(led2BufferForDevice.div(led2SampleForDevice)) + val led3Average = log10(led3BufferForDevice.div(led3SampleForDevice)) + val led4Average = log10(led4BufferForDevice.div(led4SampleForDevice)) + + val x = led1Average - led3Average + + hbEst = (7.347 * x * x) + (12.704 * x) + 0.9033 + + hBTestViewModel.messages.postValue("Result: HB EST: $hbEst") + } + + fun extractSubstring(input: String): String { + val regex = "#CS(.*?)#CC".toRegex() + val matchResult = regex.find(input) + return matchResult?.groups?.get(1)?.value?.trim() ?: "" + } + private fun validateDacValues(): Boolean { + val pattern = Regex("(LED:\\d+)__DAC:(\\d+)__ADC:(\\d+)") + val matches = pattern.findAll(resultData) + for (match in matches) { + val ledName = match.groupValues[1] + val xValue = match.groupValues[2].toDouble() + val yValue = match.groupValues[3].toDouble() + + if(xValue == 3200.0){ + if(yValue < 500.0){ + return false + } + } + } + return true + } + + fun parseData(inputData: List): List> { + val pattern = Regex("([A-Z]+)\\s(\\d+)") + val parsedData = mutableListOf>() + + for (item in inputData) { + val matchResult = pattern.find(item) + if (matchResult != null) { + val (letters, numbers) = matchResult.destructured + parsedData.add(Pair(letters, numbers)) + } + } + + return parsedData + } + + private fun showToast(message: String) { + Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show() + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestViewModel.kt new file mode 100644 index 0000000..8be379d --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/presentation/hb_test/HBTestViewModel.kt @@ -0,0 +1,83 @@ +/* + * // Copyright (c) 2024 ShanMukha Innovations Pvt. Ltd. All rights reserved. + * // Notice: All information contained herein is, and remains + * // the property of ShanMukha Innovations Pvt. Ltd. and its suppliers, + * // if any. The intellectual and technical concepts contained + * // herein are proprietary to ShanMukha Innovations Pvt. Ltd. + * // and its suppliers and may be covered by Indian and Foreign Patents, + * // patents in process, and are protected by trade secret or copyright law. + * // Dissemination of this information or reproduction of this material + * // is strictly forbidden unless prior written permission is obtained + * // from ShanMukha Innovations Pvt. Ltd. + */ + +package com.example.hpostesting.presentation.hb_test + +import android.content.Context +import android.content.SharedPreferences +import android.util.Log +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.hpostesting.data.dao.HemoCubeDao +import com.example.hpostesting.data.model.Response +import com.example.hpostesting.data.model.diagnostics.DiagnosticsData +import com.example.hpostesting.data.model.patient.DeviceData +import com.example.hpostesting.data.model.patient.HemoCubeTestData +import com.example.hpostesting.data.repository.Repository +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import javax.inject.Inject + +@HiltViewModel +class HBTestViewModel @Inject constructor( + private val hemoCubeDao: HemoCubeDao, + private val repository: Repository, + context: Context, +) : ViewModel() { + var isServiceConnected = false + val progressBar = MutableLiveData(false) + val messages = MutableLiveData() + private val sharedPreference: SharedPreferences = + context.getSharedPreferences("HEMOCUBE", Context.MODE_PRIVATE) + + val deviceData = MutableLiveData() + val fireBaseUpload = MutableLiveData() + fun uploadFirebaseQc(testDetails: HemoCubeTestData){ + viewModelScope.launch { + when (val response = repository.addQcTestToDatabase(testDetails)) { + is Response.Success -> { + Log.i("Testdb", "Data uploaded to Firestore successfully") + fireBaseUpload.postValue("Success") + testDetails.localFlag = true + hemoCubeDao.updateTest(testDetails) + } + + is Response.Error -> { + Log.e("Testdb", "Error uploading data to Firestore: $response") + fireBaseUpload.postValue("Error") + hemoCubeDao.updateTest(testDetails) + } + + else -> {} + } + } + } + fun addAutoDacDataToDb(data: DiagnosticsData) { + viewModelScope.launch { + try { + when (val response = repository.addDiagnostics(data)) { + is Response.Success -> { + fireBaseUpload.postValue("Success") + } + + is Response.Error -> { + fireBaseUpload.postValue("Error") + } + } + } catch (e: Exception) { + fireBaseUpload.postValue("Error") + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_hb_test.xml b/app/src/main/res/layout/activity_hb_test.xml new file mode 100644 index 0000000..fcc87eb --- /dev/null +++ b/app/src/main/res/layout/activity_hb_test.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_gallery.xml b/app/src/main/res/layout/fragment_gallery.xml index be4894a..7f9d735 100644 --- a/app/src/main/res/layout/fragment_gallery.xml +++ b/app/src/main/res/layout/fragment_gallery.xml @@ -165,7 +165,7 @@ app:cornerRadius="16dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" - app:layout_constraintTop_toBottomOf="@id/btn_deviceProvision" /> + app:layout_constraintTop_toBottomOf="@id/btn_reset_password" /> + + + + + + + + + + + + + + + + + + + + + + + +