From 71a364a9a2ee797ac10630b994123496827a1a26 Mon Sep 17 00:00:00 2001 From: Mariya Date: Sat, 23 Mar 2024 15:29:40 +0530 Subject: [PATCH] csv file downloading changes added --- .../presentation/dashboard/HomeFragment.kt | 68 +++++++++++++++---- 1 file changed, 54 insertions(+), 14 deletions(-) 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 fdef86a..32372bf 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 @@ -1072,24 +1072,64 @@ class HomeFragment : Fragment() { } private fun downloadLocalDBData(dialog: DialogInterface) { + var csvDownloaded = false hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val downloadList = mutableListOf() - userDataList.forEach { userData -> - if (!userData.isCSVCreated) { - downloadList.add(userData) // Add the userData to downloadList +// +// userDataList.forEach { userData -> +// 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() + + 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() } }