From 3e362a22faeeb9f82fd7cf76afba3ca6a9f06d50 Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Thu, 12 Oct 2023 07:19:24 +0530 Subject: [PATCH] add view model for buffer check --- .../HemoCubeBufferCheckFragment.kt | 15 +- .../HemoCubeBufferCheckViewModel.kt | 211 ++++++++++++++++++ 2 files changed, 217 insertions(+), 9 deletions(-) create mode 100644 app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckViewModel.kt 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 f8c28d6..ed06f03 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 @@ -11,7 +11,6 @@ import android.widget.Toast import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.lifecycle.MutableLiveData -import androidx.lifecycle.lifecycleScope import com.example.hpostesting.data.DataHolder import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.HemoCubeCommands @@ -19,20 +18,18 @@ import com.example.hpostesting.data.model.patient.DeviceData import com.example.hpostesting.data.model.patient.toHemoCubeTestData import com.example.hpostesting.presentation.UsbServiceListener import com.example.hpostesting.presentation.dashboard.DashboardActivity -import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel import com.example.hpostesting.presentation.utils.MyDialogListener import com.example.hpostesting.presentation.utils.UIUtils import com.google.firebase.crashlytics.ktx.crashlytics import com.google.firebase.ktx.Firebase import `in`.sminnovations.hpostesting.R import `in`.sminnovations.hpostesting.databinding.FragmentHemoCubeReferenceBinding -import kotlinx.coroutines.launch import kotlin.math.log10 class HemoCubeBufferCheckFragment : Fragment() { private lateinit var binding: FragmentHemoCubeReferenceBinding - private val hemoCubeViewModel: HemoCubeViewModel by activityViewModels() + private val hemoCubeBufferCheckViewModel: HemoCubeBufferCheckViewModel by activityViewModels() private lateinit var sharedPreferences: SharedPreferences private var currentDeviceData: DeviceData? = null private var resultData: String = "" @@ -66,7 +63,7 @@ class HemoCubeBufferCheckFragment : Fragment() { activity?.runOnUiThread { binding.progressBar.visibility = View.VISIBLE } - hemoCubeViewModel.uploadHemoCubeResultToDatabaseforbuffercheckN( + hemoCubeBufferCheckViewModel.uploadHemoCubeResultToDatabaseforbuffercheckN( isOnline, true, sharedPreferences.getString(Constants.KIT_NUMBER, "") ) } @@ -75,7 +72,7 @@ class HemoCubeBufferCheckFragment : Fragment() { } private fun observeViewModel() { - hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> + hemoCubeBufferCheckViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> if (result == "Success") { showToast("Test Results Uploaded Successfully") } @@ -86,13 +83,13 @@ class HemoCubeBufferCheckFragment : Fragment() { binding.progressBar.visibility = View.GONE } - hemoCubeViewModel.deviceData.observe(viewLifecycleOwner) { + hemoCubeBufferCheckViewModel.deviceData.observe(viewLifecycleOwner) { currentDeviceData = it } - hemoCubeViewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable -> + hemoCubeBufferCheckViewModel.networkStatusLiveData.observe(viewLifecycleOwner) { isNetworkAvailable -> if (isNetworkAvailable) { - hemoCubeViewModel.getDeviceData(sharedPreferences.getString(Constants.USER_ID, "")) + hemoCubeBufferCheckViewModel.getDeviceData(sharedPreferences.getString(Constants.USER_ID, "")) } else { Toast.makeText( requireContext(), diff --git a/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckViewModel.kt b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckViewModel.kt new file mode 100644 index 0000000..cd7a588 --- /dev/null +++ b/app/src/main/java/com/example/hpostesting/presentation/buffercheck/HemoCubeBufferCheckViewModel.kt @@ -0,0 +1,211 @@ +package com.example.hpostesting.presentation.buffercheck + +import android.content.Context +import android.content.SharedPreferences +import android.util.Log +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope +import com.example.hpostesting.data.DataHolder +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.DeviceData +import com.example.hpostesting.data.model.patient.HemoCubeTestData +import com.example.hpostesting.data.model.patient.toHemoCubeTestData +import com.example.hpostesting.data.repository.DatabaseRepository +import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch +import java.text.SimpleDateFormat +import java.util.Calendar +import java.util.Locale +import javax.inject.Inject + +@HiltViewModel +class HemoCubeBufferCheckViewModel @Inject constructor( + private val hemoCubeDao: HemoCubeDao, + private val repository: DatabaseRepository, + context: Context, +) : ViewModel() { + var isServiceConnected = false + val progressBar = MutableLiveData(false) + val testDetails = DataHolder.selectedTest?.toHemoCubeTestData() + val messages = MutableLiveData() + private val sharedPreference: SharedPreferences = + context.getSharedPreferences("PREFERENCE_NAME", Context.MODE_PRIVATE) + + // Get the device ID of the device you want to retrieve data for (e.g., the first device in the list) + + private val _networkStatusLiveData = NetworkStatusLiveData(context) + val allUserData = hemoCubeDao.getAll() + val deviceData = MutableLiveData() + val networkStatusLiveData: LiveData + get() = _networkStatusLiveData + val fireBaseUpload = MutableLiveData() + val fireBaseBulkUpload = MutableLiveData() + fun uploadHemoCubeResultToDatabase(isOnline: Boolean, testStatus: Boolean, kitSerial: String?) = + viewModelScope.launch { + if (kitSerial != null) { + testDetails?.kitSerial = kitSerial + } + testDetails?.testStatus = testStatus + + try { + if (isOnline) { + parseData() + addResultTestToDb() + } else { + parseData() + testDetails?.testTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + hemoCubeDao.insertAll(testDetails!!) + fireBaseUpload.postValue("Local") + } + } catch (e: Exception) { + Log.e("Testdb", "Upload failed: ${e.message}") + } + } + + + + fun uploadHemoCubeResultToDatabaseforbuffercheckN(isOnline: Boolean, testStatus: Boolean, kitSerial: String?) = + viewModelScope.launch { + if (kitSerial != null) { + testDetails?.kitSerial = kitSerial + } + testDetails?.testStatus = testStatus + + try { + if (isOnline) { + parseData() + addResultTestToDbforbuffercheck() + } else { + parseData() + testDetails?.testTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + hemoCubeDao.insertAll(testDetails!!) + fireBaseUpload.postValue("Local") + } + } catch (e: Exception) { + Log.e("Testdb", "Upload failed: ${e.message}") + } + } + + + + + + fun getDeviceData(deviceId: String?) = viewModelScope.launch { + deviceData.postValue(deviceId?.let { repository.getDeviceDataById(it) }) + } + + private fun parseData() { + testDetails?.deviceRatio = DataHolder.hemocubeResult + testDetails?.resultData = DataHolder.hemoCubeTestData?.resultData.toString() + testDetails?.location = DataHolder.location + testDetails?.testTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + testDetails?.appVersion = DataHolder.hemoCubeTestData?.appVersion + testDetails?.deviceId = DataHolder.hemoCubeTestData?.deviceId + testDetails?.deviceSerialNumber = + sharedPreference.getString(Constants.USER_ID, "").toString() + testDetails?.kitSerial = sharedPreference.getString(Constants.KIT_NUMBER, "").toString() + testDetails?.led1Buffer = DataHolder.hemoCubeTestData?.led1Buffer + testDetails?.led2Buffer = DataHolder.hemoCubeTestData?.led2Buffer + testDetails?.led1Sample = DataHolder.hemoCubeTestData?.led1Sample + testDetails?.led2Sample = DataHolder.hemoCubeTestData?.led2Sample + testDetails?.led1Average = DataHolder.hemoCubeTestData?.led1Average + testDetails?.led2Average = DataHolder.hemoCubeTestData?.led2Average + testDetails?.deviceRatio = DataHolder.hemoCubeTestData?.deviceRatio + testDetails?.calculatedRatio = DataHolder.hemoCubeTestData?.calculatedRatio + testDetails?.classificationResult = DataHolder.hemoCubeTestData?.classificationResult!! + } + + private fun addResultTestToDb() { + viewModelScope.launch { + try { + testDetails!!.reportUploadTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + + when (val response = repository.addTestToDatabase(testDetails)) { + 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") + } + + is Response.Error -> { + Log.e("Testdb", "Error uploading data to Firestore: $response") + fireBaseUpload.postValue("Error") + } + } + } catch (e: Exception) { + Log.e("Testdb", "Exception during data upload: ${e.message}") + fireBaseUpload.postValue("Error") + } + } + } + + + private fun addResultTestToDbforbuffercheck() { + viewModelScope.launch { + try { + testDetails!!.reportUploadTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + + when (val response = repository.addTestToDatabaseforBufferCheck(testDetails)) { + 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") + } + + is Response.Error -> { + Log.e("Testdb", "Error uploading data to Firestore: $response") + fireBaseUpload.postValue("Error") + } + } + } catch (e: Exception) { + Log.e("Testdb", "Exception during data upload: ${e.message}") + fireBaseUpload.postValue("Error") + } + } + } + + fun bulkAddResultTestToDb(userData: HemoCubeTestData) { + viewModelScope.launch { + userData.reportUploadTime = SimpleDateFormat( + "yyyy-MM-dd HH:mm:ss", Locale.getDefault() + ).format(Calendar.getInstance().time) + when (repository.addTestToDatabase(userData)) { + is Response.Success -> { + fireBaseBulkUpload.postValue("Success") + deleteById(userData._id) + } + + else -> { + fireBaseBulkUpload.postValue("Error") + } + } + } + } + + fun deleteById(userId: String) = viewModelScope.launch { + hemoCubeDao.deleteById(id = userId) + } +} \ No newline at end of file