From f7dc879c75d98005356dc04132db3e75ffd646f9 Mon Sep 17 00:00:00 2001 From: Mariya Date: Wed, 28 Feb 2024 15:17:36 +0530 Subject: [PATCH] added code related offline bulkupload --- .../presentation/dashboard/HomeFragment.kt | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 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 ddaabe9..cee240b 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 @@ -135,37 +135,59 @@ class HomeFragment : Fragment() { checkForTokenAndUpdate() } - hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> - val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result())) - userDataList.forEach { userData -> + hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList -> + + Log.d("LOCAL_DB OBSERVE", "OBSERVE CALLED") + + val resultList = MolbioV2ResultRequest(mutableListOf()) + originalUserDataList.forEach { userData -> + Log.d( + ": USER DATA", + originalUserDataList.count().toString() + " : " + userData._id + ) + if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) { + val currentTimeFormatted = SimpleDateFormat( + "yyyy-MM-dd'T'HH:mm:ssZZZZZ", + Locale.getDefault() + ).format(Calendar.getInstance().time) + val bufferIntensityThreshold = + Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId]?.toString() + ?: "defaultThreshold" // Handle possible nulls safely resultList.results?.add( MolbioV2Result( rawData = userData, analysisId = userData._id, - analysisDate = "2024-02-08 16:33:56",//userData.testTime, - analysisStatus = userData.classificationResult, - thresholds = Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId].toString(), - interpretation = userData.classificationResult, + analysisDate = currentTimeFormatted, + analysisStatus = userData.classificationResult + ?: "defaultStatus", // Handle possible nulls + thresholds = bufferIntensityThreshold, + interpretation = userData.classificationResult + ?: "defaultInterpretation", // Handle possible nulls testId = userData._id, - testTime = "2024-02-08 16:33:56",//userData.testTime, - collectionTime = "2024-02-08 16:33:56",//userData.testTime, - expiryTime = "2024-02-08 16:33:56"//userData.testTime, + testTime = currentTimeFormatted, + collectionTime = currentTimeFormatted, + expiryTime = currentTimeFormatted ) ) } - if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) { - userData.molbioFlag = true - hemoCubeViewModel.uploadResult(resultList) - } - - if (!userData.localFlag) { - userData.localFlag = true - hemoCubeViewModel.bulkAddResultTestToDb(userData) - } - } + Log.d("USER DATA LIST SIZE", resultList.results?.count().toString()) + resultList.results?.forEach { result -> + val userData = result.rawData + Log.d("UserData", userData.toString()) + if (userData != null) { + if (!userData.localFlag) { + hemoCubeViewModel.bulkAddResultTestToDb(userData) + userData.localFlag = true + } + } + } + // Upload results after processing all userData to avoid duplicates and ensure all modifications are done + if (resultList.results?.isNotEmpty() == true) { + hemoCubeViewModel.uploadResult(resultList) + } } } else {