diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt index 00e5a3d..79db431 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt @@ -151,7 +151,7 @@ class HomeFragment : Fragment() { } // Now re-subscribe to allUserData - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList -> + hemoCubeViewModel.allLocalData.observe(viewLifecycleOwner) { originalUserDataList -> Log.d("LOCAL_DB OBSERVE", "OBSERVE CALLED") @@ -237,30 +237,34 @@ class HomeFragment : Fragment() { logoutUser(requireContext()) } + binding.btnLogout1.setOnClickListener { + logoutUser(requireContext()) + } + binding.uploadData.setOnClickListener { // showUploadDialog(requireContext()) } - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userData -> - - val btnSaveLocalVisibility = - if (userData.any { it.testStatus == true }) View.GONE else View.GONE - - binding.downloadCSV.visibility = btnSaveLocalVisibility - - binding.downloadCSV.setOnClickListener { - if (btnSaveLocalVisibility == View.VISIBLE) { - // Execute the action when the button is visible (testStatus is true for at least one user) - showDownloadDialog(requireContext()) - } else { - // Handle the case when the button is not visible - Toast.makeText( - requireContext(), - "No test details stored locally", - Toast.LENGTH_SHORT - ).show() - } - } - } +// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userData -> +// +// val btnSaveLocalVisibility = +// if (userData.any { it.testStatus == true }) View.GONE else View.GONE +// +// binding.downloadCSV.visibility = btnSaveLocalVisibility +// +// binding.downloadCSV.setOnClickListener { +// if (btnSaveLocalVisibility == View.VISIBLE) { +// // Execute the action when the button is visible (testStatus is true for at least one user) +// showDownloadDialog(requireContext()) +// } else { +// // Handle the case when the button is not visible +// Toast.makeText( +// requireContext(), +// "No test details stored locally", +// Toast.LENGTH_SHORT +// ).show() +// } +// } +// } binding.btnNewKit.setOnClickListener { @@ -272,6 +276,17 @@ class HomeFragment : Fragment() { startActivity(Intent(requireContext(), KitScanActivity::class.java)) requireActivity().finish() } + + + binding.btnNewKitoffline.setOnClickListener { + with(sharedPreference.edit()) { + putString(Constants.KIT_NUMBER, "") + putInt(Constants.KIT_COUNT, 0) + apply() + } + startActivity(Intent(requireContext(), KitScanActivity::class.java)) + requireActivity().finish() + } binding.btnQuickCapture.setOnClickListener { DataHolder.hemoCubeTestData = HemoCubeTestData() @@ -847,16 +862,16 @@ class HomeFragment : Fragment() { } private fun checkForLocalDBData() { - viewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val uploadDataVisibility = if (userDataList.isEmpty()) View.GONE else View.VISIBLE - binding.uploadData.visibility = uploadDataVisibility - } - - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val uploadDataVisibility = - if (userDataList.any { !it.localFlag && !it.molbioFlag && it.testStatus == true }) View.VISIBLE else View.GONE - binding.uploadData.visibility = uploadDataVisibility - } +// viewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// val uploadDataVisibility = if (userDataList.isEmpty()) View.GONE else View.VISIBLE +// binding.uploadData.visibility = uploadDataVisibility +// } +// +// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// val uploadDataVisibility = +// if (userDataList.any { !it.localFlag && !it.molbioFlag && it.testStatus == true }) View.VISIBLE else View.GONE +// binding.uploadData.visibility = uploadDataVisibility +// } hemoCubeViewModel.allKitTestData.observe(viewLifecycleOwner) { bufferData -> val uploadDataVisibility = @@ -866,11 +881,11 @@ class HomeFragment : Fragment() { } private fun checkUnprocessedCSVData() { - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val downloadDataVisibility = - if (userDataList.any { !it.isCSVCreated && it.testStatus == true }) View.GONE else View.GONE - binding.downloadCSV.visibility = downloadDataVisibility - } +// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// val downloadDataVisibility = +// if (userDataList.any { !it.isCSVCreated && it.testStatus == true }) View.GONE else View.GONE +// binding.downloadCSV.visibility = downloadDataVisibility +// } } private fun sanitizeDoubleValues(hemoCubeTestData: HemoCubeTestData): HemoCubeTestData { @@ -923,23 +938,23 @@ class HomeFragment : Fragment() { // } private fun uploadLocalDBData(dialog: DialogInterface) { - viewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - if (userDataList.isEmpty()) { - dialog.dismiss() - } else { - userDataList.forEach { userData -> - viewModel.bulkUploadResultToDatabase(userData) - viewModel.deleteById(userData._id) - } - dialog.dismiss() - Toast.makeText( - requireContext(), R.string.upload_success_message, Toast.LENGTH_SHORT - ).show() - } - } +// viewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// if (userDataList.isEmpty()) { +// dialog.dismiss() +// } else { +// userDataList.forEach { userData -> +// viewModel.bulkUploadResultToDatabase(userData) +// viewModel.deleteById(userData._id) +// } +// dialog.dismiss() +// Toast.makeText( +// requireContext(), R.string.upload_success_message, Toast.LENGTH_SHORT +// ).show() +// } +// } - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result())) +// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result())) // userDataList.forEach { userData -> // if (!userData.molbioFlag && isTokenAvailable) { // resultList.results?.add( @@ -968,8 +983,8 @@ class HomeFragment : Fragment() { // hemoCubeViewModel.uploadResult(resultList) // } // } - dialog.dismiss() - } +// dialog.dismiss() +// } hemoCubeViewModel.allKitTestData.observe(viewLifecycleOwner) { kitDataList -> kitDataList.forEach { userData -> @@ -1044,7 +1059,7 @@ class HomeFragment : Fragment() { builder.setMessage(R.string.download_db_registration_message) builder.setPositiveButton(R.string.downloadcsv) { dialog, _ -> - downloadLocalDBData(dialog) +// downloadLocalDBData(dialog) } builder.setNegativeButton(R.string.cancel) { dialog, _ -> @@ -1055,49 +1070,49 @@ class HomeFragment : Fragment() { dialog.show() } - private fun downloadLocalDBData(dialog: DialogInterface) { - var csvDownloaded = false - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - try { - if (!csvDownloaded) { - val downloadList = mutableListOf() - - userDataList.forEach { userData -> - if (userData.testStatus == true) { -// if (!userData.isCSVCreated) { - downloadList.add(userData) // Add the userData to downloadList - } +// private fun downloadLocalDBData(dialog: DialogInterface) { +// var csvDownloaded = false +// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> +// try { +// if (!csvDownloaded) { +// val downloadList = mutableListOf() +// +// userDataList.forEach { userData -> +// if (userData.testStatus == true) { +//// if (!userData.isCSVCreated) { +// downloadList.add(userData) // Add the userData to downloadList // } - } - - if (downloadList.isNotEmpty()) { - // Call ViewModel function to create CSV with filtered data - hemoCubeViewModel.createCSV(downloadList, requireContext()) - csvDownloaded = true - Toast.makeText( - requireContext(), - "CSV file downloaded successfully", - Toast.LENGTH_SHORT - ).show() - } else { - Toast.makeText( - requireContext(), - "No data to download", - Toast.LENGTH_SHORT - ) - .show() - } - } - - } catch (e: Exception) { - e.printStackTrace() - Toast.makeText(requireContext(), "Error downloading CSV file", Toast.LENGTH_SHORT) - .show() - } finally { - dialog.dismiss() - } - } - } +//// } +// } +// +// if (downloadList.isNotEmpty()) { +// // Call ViewModel function to create CSV with filtered data +// hemoCubeViewModel.createCSV(downloadList, requireContext()) +// csvDownloaded = true +// Toast.makeText( +// requireContext(), +// "CSV file downloaded successfully", +// Toast.LENGTH_SHORT +// ).show() +// } else { +// Toast.makeText( +// requireContext(), +// "No data to download", +// Toast.LENGTH_SHORT +// ) +// .show() +// } +// } +// +// } catch (e: Exception) { +// e.printStackTrace() +// Toast.makeText(requireContext(), "Error downloading CSV file", Toast.LENGTH_SHORT) +// .show() +// } finally { +// dialog.dismiss() +// } +// } +// } private fun getDeviceId() { Log.d("HomeFragmentUSb","getDeviceId") 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 d4a5855..d075631 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 @@ -89,6 +89,7 @@ class HemoCubeViewModel @Inject constructor( private val _networkStatusLiveData = NetworkStatusLiveData(context) val allUserData = hemoCubeDao.getAll() + val allLocalData = hemoCubeDao.getAll() val allPendingUserToUpload = MutableLiveData>() val allKitTestData = hemoCubeBufferDao.getAll() val deviceData = MutableLiveData() diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index 7343952..88225c1 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -38,10 +38,20 @@ android:visibility="gone" android:padding="24dp"> + + + + + +