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 36cd56e..c93b5ef 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 @@ -22,6 +22,7 @@ import com.example.hpostesting.data.Result import com.example.hpostesting.data.api.DeviceCommunicationHandler import com.example.hpostesting.data.constant.Constants import com.example.hpostesting.data.constant.HemoCubeCommands +import com.example.hpostesting.data.encryption.AESCrypt.decrypt import com.example.hpostesting.data.model.login.LoginRequest import com.example.hpostesting.data.model.login.LoginResponse import com.example.hpostesting.data.model.molbioresult.MolbioV2Result @@ -61,6 +62,7 @@ import java.util.Locale import java.util.zip.ZipEntry import java.util.zip.ZipInputStream + @AndroidEntryPoint class HomeFragment : Fragment() { private var _binding: FragmentHomeBinding? = null @@ -146,48 +148,70 @@ class HomeFragment : Fragment() { hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList -> // Ensure the list does not contain duplicates based on _id - val userDataList = originalUserDataList.distinctBy { it._id } + //val userDataList = originalUserDataList.distinctBy { it.testTime } + val userDataList = originalUserDataList // Initialize the resultList outside of the loop to avoid duplicates val resultList = MolbioV2ResultRequest(mutableListOf()) - val processedUserIds = mutableSetOf() // To track processed users based on _id + userDataList.forEach { userData -> // Ensure we process each user only once - if (!processedUserIds.contains(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 + // if (!processedUserIds.contains(userData._id)) { + Log.d(": USER DATA", userDataList.count().toString() + " : " + userData._id) - resultList.results?.add( - MolbioV2Result( - rawData = userData, - analysisId = userData._id, - analysisDate = currentTimeFormatted, - analysisStatus = userData.classificationResult ?: "defaultStatus", // Handle possible nulls - thresholds = bufferIntensityThreshold, - interpretation = userData.classificationResult ?: "defaultInterpretation", // Handle possible nulls - testId = userData._id, - testTime = currentTimeFormatted, - collectionTime = currentTimeFormatted, - expiryTime = currentTimeFormatted - ) + + 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 = currentTimeFormatted, + analysisStatus = userData.classificationResult + ?: "defaultStatus", // Handle possible nulls + thresholds = bufferIntensityThreshold, + interpretation = userData.classificationResult + ?: "defaultInterpretation", // Handle possible nulls + testId = userData._id, + testTime = currentTimeFormatted, + collectionTime = currentTimeFormatted, + expiryTime = currentTimeFormatted ) - // Mark this userData as processed to avoid re-processing - processedUserIds.add(userData._id) - } + ) + // Mark this userData as processed to avoid re-processing + // processedUserIds.add(userData.testTime.toString()) +// if (!userData.localFlag) { +// hemoCubeViewModel.bulkAddResultTestToDb(userData) +// userData.localFlag = true +// } + } - // Mark the userData for localFlag update, this will be handled in bulk after the loop + // Mark the userData for localFlag update, this will be handled in bulk after the loop + // } + } + Log.d("USER DATA LIST SIZE", resultList.results?.count().toString()) + + + // Perform bulk operations after the loop + // Set localFlag to true for all usersToUpdate and perform bulk database upd + + 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 } } } - - // Perform bulk operations after the loop - // Set localFlag to true for all usersToUpdate and perform bulk database upd - // Upload results after processing all userData to avoid duplicates and ensure all modifications are done if (resultList.results?.isNotEmpty() == true) { hemoCubeViewModel.uploadResult(resultList) @@ -304,7 +328,31 @@ class HomeFragment : Fragment() { hemoCubeViewModel.uploadLogs() hemoCubeViewModel.startPeriodicCheckUpdate() } - } else { + } +// } else if (fis.available()>0){ +// val buffer = ByteArray(fis.available()) +// fis.read(buffer) +// fis.close() +// val encryptedData = String(buffer) +// val parts = encryptedData.split(",".toRegex()).dropLastWhile { it.isEmpty() } +// .toTypedArray() +// val deviceID = parts[0] +// val decryptedUsername = decrypt(parts[1]) +// val decryptedPassword = decrypt(parts[2]) +// if (accessToken.isEmpty()) { +// hemoCubeViewModel.login(createLoginRequestData(decryptedUsername, decryptedPassword)) + //} +// else { +// // Continue with your existing logic if the token is not empty. +// isTokenAvailable = true +// hemoCubeViewModel.deviceUpdate(createDeviceUpdateRequestData()) +// hemoCubeViewModel.uploadLogs() +// hemoCubeViewModel.startPeriodicCheckUpdate() +// } + + + //} + else { Toast.makeText( requireContext(), "Contact Help and get your device provision done", diff --git a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt index 1feece9..41e7711 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/deviceprovision/DeviceProvisionFragment.kt @@ -4,7 +4,6 @@ import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.os.Bundle -import android.util.Base64 import android.util.Log import android.view.LayoutInflater import android.view.View @@ -24,8 +23,6 @@ import com.example.hpostesting.presentation.dashboard.DashboardActivity import com.google.firebase.crashlytics.ktx.crashlytics import com.google.firebase.ktx.Firebase import `in`.sminnovations.hpostesting.databinding.FragmentDeviceProvisionBinding -import javax.crypto.Cipher -import javax.crypto.spec.SecretKeySpec class DeviceProvisionFragment : Fragment() { @@ -229,14 +226,15 @@ class DeviceProvisionFragment : Fragment() { //saves username and password to a local file after encrypting it. - private fun saveDataToLocalFile(username : String, password : String){ + private fun saveDataToLocalFile( username : String, password : String){ + val deviceID = sharedPreferences.getString(Constants.DEVICE_ID, "").toString(); val encryptedUsername = AESCrypt.encrypt(username) val encryptedPassword = AESCrypt.encrypt(password) - val encryptedData = encryptedUsername + "\n" + encryptedPassword + val encryptedData = deviceID + "\n" + encryptedUsername + "\n" + encryptedPassword - val fos = requireContext().openFileOutput("credentials.txt", Context.MODE_PRIVATE) - fos.write(encryptedData.toByteArray()) - fos.close() + val fileOutputStream = requireContext().openFileOutput("credentials.txt", Context.MODE_PRIVATE) + fileOutputStream.write(encryptedData.toByteArray()) + fileOutputStream.close() } } \ No newline at end of file