From 53c36d3cf9494009f747375c2e0a8ad8aa49b268 Mon Sep 17 00:00:00 2001 From: Mariya Date: Thu, 7 Mar 2024 12:11:27 +0530 Subject: [PATCH] added different id and set as primary key for testing twice with same sample Id and uploading the result, offline ,issue fixed, and set ADMIN as user that can only download and export CSV file, for collecting user Data --- .../hpostesting/data/dao/HemoCubeDao.kt | 1 + .../hpostesting/data/dao/MyDataBase.kt | 2 +- .../datasource/LocalFileDataSourceImpl.kt | 5 +- .../data/model/patient/HemoCubeTestData.kt | 3 +- .../presentation/dashboard/HomeFragment.kt | 161 +++++++++--------- 5 files changed, 90 insertions(+), 82 deletions(-) diff --git a/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt b/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt index b414b3e..3c278a2 100644 --- a/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt +++ b/app/src/main/java/com/example/hpostesting/data/dao/HemoCubeDao.kt @@ -5,6 +5,7 @@ import androidx.room.Dao import androidx.room.Insert import androidx.room.OnConflictStrategy import androidx.room.Query +import androidx.room.Update import com.example.hpostesting.data.model.patient.HemoCubeTestData import com.example.hpostesting.data.model.patient.UserData @Dao diff --git a/app/src/main/java/com/example/hpostesting/data/dao/MyDataBase.kt b/app/src/main/java/com/example/hpostesting/data/dao/MyDataBase.kt index a5e10be..6776455 100644 --- a/app/src/main/java/com/example/hpostesting/data/dao/MyDataBase.kt +++ b/app/src/main/java/com/example/hpostesting/data/dao/MyDataBase.kt @@ -10,7 +10,7 @@ import com.example.hpostesting.data.model.patient.UserData @Database( entities = [UserData::class, HemoCubeTestData::class, DeviceData::class, BufferCheckData::class], - version = 28, + version = 29, exportSchema = false ) @TypeConverters(Converters::class) diff --git a/app/src/main/java/com/example/hpostesting/data/datasource/LocalFileDataSourceImpl.kt b/app/src/main/java/com/example/hpostesting/data/datasource/LocalFileDataSourceImpl.kt index a388503..a6c8f37 100644 --- a/app/src/main/java/com/example/hpostesting/data/datasource/LocalFileDataSourceImpl.kt +++ b/app/src/main/java/com/example/hpostesting/data/datasource/LocalFileDataSourceImpl.kt @@ -89,8 +89,9 @@ class LocalFileDataSourceImpl @Inject constructor() : LocalFileDataSource { ) csvWriter.writeNext(header) - // Write data rows - for (data in dataList) { + // Filter and write data rows where testStatus is true + val filteredDataList = dataList.filter { it.testStatus == true } + for (data in filteredDataList) { val row = arrayOf( data._id, data.name, diff --git a/app/src/main/java/com/example/hpostesting/data/model/patient/HemoCubeTestData.kt b/app/src/main/java/com/example/hpostesting/data/model/patient/HemoCubeTestData.kt index 43a164c..88fbf60 100644 --- a/app/src/main/java/com/example/hpostesting/data/model/patient/HemoCubeTestData.kt +++ b/app/src/main/java/com/example/hpostesting/data/model/patient/HemoCubeTestData.kt @@ -5,7 +5,8 @@ import androidx.room.PrimaryKey @Entity(tableName = "hemo_cube_test_table") data class HemoCubeTestData( - @PrimaryKey + @PrimaryKey(autoGenerate = true) + var sampleid: Int = 0, var _id: String = "", var name: String = "", var incubationTime: String = "", 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 79db431..a79414d 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 @@ -122,6 +122,8 @@ class HomeFragment : Fragment() { userData.forEach { if (it.testStatus == false) { userList.add(it) + }else if(it.testStatus == true){ + userList.removeAll(userData) } } val bm = requireContext().getSystemService(BATTERY_SERVICE) as BatteryManager @@ -244,27 +246,28 @@ class HomeFragment : Fragment() { 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 -> + if (sharedPreference.getString(Constants.USER_ID, "").toString() == "ADMIN") { + val btnSaveLocalVisibility = + if (userData.any { it.testStatus == true }) View.VISIBLE 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() + } + } + }else{ + binding.downloadCSV.visibility = View.GONE + } + } binding.btnNewKit.setOnClickListener { @@ -881,11 +884,15 @@ 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 -> + if (sharedPreference.getString(Constants.USER_ID, "").toString() == "ADMIN") { + val downloadDataVisibility = + if (userDataList.any { !it.isCSVCreated && it.testStatus == true }) View.VISIBLE else View.GONE + binding.downloadCSV.visibility = downloadDataVisibility + }else{ + binding.downloadCSV.visibility = View.GONE + } + } } private fun sanitizeDoubleValues(hemoCubeTestData: HemoCubeTestData): HemoCubeTestData { @@ -920,22 +927,22 @@ class HomeFragment : Fragment() { dialog.show() } -// private fun showDownloadDialog(context: Context) { -// val builder = AlertDialog.Builder(context) -// builder.setTitle(R.string.download_db_registration_title) -// builder.setMessage(R.string.download_db_registration_message) -// -// builder.setPositiveButton(R.string.downloadcsv) { dialog, _ -> -// downloadLocalDBData(dialog) -// } -// -// builder.setNegativeButton(R.string.cancel) { dialog, _ -> -// dialog.dismiss() -// } -// -// val dialog = builder.create() -// dialog.show() -// } + private fun showDownloadDialog(context: Context) { + val builder = AlertDialog.Builder(context) + builder.setTitle(R.string.download_db_registration_title) + builder.setMessage(R.string.download_db_registration_message) + + builder.setPositiveButton(R.string.downloadcsv) { dialog, _ -> + downloadLocalDBData(dialog) + } + + builder.setNegativeButton(R.string.cancel) { dialog, _ -> + dialog.dismiss() + } + + val dialog = builder.create() + dialog.show() + } private fun uploadLocalDBData(dialog: DialogInterface) { // viewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> @@ -997,27 +1004,25 @@ class HomeFragment : Fragment() { } } -// private fun downloadLocalDBData(dialog: DialogInterface) { -// hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> -// val downloadList = mutableListOf() -// -// 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(), "Failed", Toast.LENGTH_SHORT).show() -// } -// -// dialog.dismiss() -// } -// } + private fun downloadLocalDBData(dialog: DialogInterface) { + hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> + val downloadList = mutableListOf() + + 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() + } + + dialog.dismiss() + } + } private fun deleteIncompleteRegistrations(userDataList: List) { userDataList.forEach { userData -> @@ -1053,22 +1058,22 @@ class HomeFragment : Fragment() { } } - private fun showDownloadDialog(context: Context) { - val builder = AlertDialog.Builder(context) - builder.setTitle(R.string.download_db_registration_title) - builder.setMessage(R.string.download_db_registration_message) - - builder.setPositiveButton(R.string.downloadcsv) { dialog, _ -> +// private fun showDownloadDialog(context: Context) { +// val builder = AlertDialog.Builder(context) +// builder.setTitle(R.string.download_db_registration_title) +// builder.setMessage(R.string.download_db_registration_message) +// +// builder.setPositiveButton(R.string.downloadcsv) { dialog, _ -> // downloadLocalDBData(dialog) - } - - builder.setNegativeButton(R.string.cancel) { dialog, _ -> - dialog.dismiss() - } - - val dialog = builder.create() - dialog.show() - } +// } +// +// builder.setNegativeButton(R.string.cancel) { dialog, _ -> +// dialog.dismiss() +// } +// +// val dialog = builder.create() +// dialog.show() +// } // private fun downloadLocalDBData(dialog: DialogInterface) { // var csvDownloaded = false