csv file downloading changes added

This commit is contained in:
Mariya
2024-03-23 15:29:40 +05:30
parent 9aa89f810e
commit 71a364a9a2

View File

@@ -1072,24 +1072,64 @@ class HomeFragment : Fragment() {
} }
private fun downloadLocalDBData(dialog: DialogInterface) { private fun downloadLocalDBData(dialog: DialogInterface) {
var csvDownloaded = false
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
val downloadList = mutableListOf<HemoCubeTestData>()
userDataList.forEach { userData -> //
if (!userData.isCSVCreated) { // userDataList.forEach { userData ->
downloadList.add(userData) // Add the userData to downloadList // 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())
// Toast.makeText(requireContext(), "Done", Toast.LENGTH_SHORT).show()
// } else {
// Toast.makeText(requireContext(), "", Toast.LENGTH_SHORT).show()
// }
try {
if (!csvDownloaded) {
val downloadList = mutableListOf<HemoCubeTestData>()
userDataList.forEach { userData ->
Log.d(
"DownloadDebug",
"userData: $userData, isCSVCreated: ${userData.isCSVCreated}"
)
// if (!userData.isCSVCreated) {
// userData.isCSVCreated = false
downloadList.add(userData)
// }
}
// Log the size of downloadList for debugging
Log.d("DownloadDebug", "downloadList size: ${downloadList.size}")
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())
Toast.makeText(requireContext(), "Done", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(requireContext(), "", Toast.LENGTH_SHORT).show()
}
dialog.dismiss()
} }
} }