From 9c4a4290bb411d0042998d71bcf5ada98f2a6177 Mon Sep 17 00:00:00 2001 From: Mariya Date: Mon, 1 Apr 2024 13:07:46 +0530 Subject: [PATCH] nats certificate downloading only calling once code added --- .../presentation/dashboard/HomeFragment.kt | 29 ++++++++++++++----- .../presentation/hemocube/HemoCubeFragment.kt | 10 ++++++- 2 files changed, 31 insertions(+), 8 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 97bef54..187d5f7 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 @@ -377,10 +377,10 @@ class HomeFragment : Fragment() { val encryptedString = file.readText() val encryptionKey = Settings.Secure.getString(context?.contentResolver, Settings.Secure.ANDROID_ID) - // val decryptedMessage: String = Encryption.decrypt(encryptedString, encryptionKey) - // val credentials = decryptedMessage.split("\n") - // userID = credentials[0] - // password = credentials[1] + val decryptedMessage: String = Encryption.decrypt(encryptedString, encryptionKey) + val credentials = decryptedMessage.split("\n") + userID = credentials[0] + password = credentials[1] // Toast.makeText(context, "DECRYPTED: $credentials", Toast.LENGTH_SHORT).show() if (!isTokenAvailable) { hemoCubeViewModel.login(createLoginRequestData(userID, password)) @@ -559,6 +559,8 @@ class HomeFragment : Fragment() { } hemoCubeViewModel.downloadcertificate.observe(viewLifecycleOwner) { response -> + // Check if more than a year has passed since the last download (365 days * 24 hours * 60 minutes * 60 seconds * 1000 milliseconds) + when (response) { is Result.Success -> { val url = response.data @@ -567,7 +569,6 @@ class HomeFragment : Fragment() { val fileName = "nats_certificate.zip" val unzipDirectoryPath = requireContext().getExternalFilesDir(null)?.absolutePath + "/$downloadDirectory" - // Check if the directory with extracted files exists. val directory = File(unzipDirectoryPath) if (directory.exists() && directory.isDirectory) { @@ -580,9 +581,23 @@ class HomeFragment : Fragment() { ).show() return@observe } - val file = downloadFile(url, requireContext(), fileName, downloadDirectory) + val sharedPreferences = requireContext().getSharedPreferences("NATSCertificatePrefs", Context.MODE_PRIVATE) + val lastDownloadTime = sharedPreferences.getLong("lastDownloadTime", 0) + val currentTime = System.currentTimeMillis() + + if (currentTime - lastDownloadTime < 365L * 24 * 60 * 60 * 1000) { + Toast.makeText(requireContext(), "NATS certificate download is not required yet.", Toast.LENGTH_SHORT).show() + return@observe + }else{ + val file = downloadFile(url, requireContext(), fileName, downloadDirectory) + unzip(file.absolutePath, unzipDirectoryPath) + val sharedPreferences = requireContext().getSharedPreferences("NATSCertificatePrefs", Context.MODE_PRIVATE) + sharedPreferences.edit().apply { + putLong("lastDownloadTime", System.currentTimeMillis()) + apply() + } + } - unzip(file.absolutePath, unzipDirectoryPath) } is Result.Error -> { diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt index e0c96e7..46c7562 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemoCubeFragment.kt @@ -207,7 +207,15 @@ class HemoCubeFragment : Fragment() { } handleReadingFinish() hemoCubeViewModel.uploadLogs() - hemoCubeViewModel.downloadClientCertificate() + val sharedPreferences = requireContext().getSharedPreferences("NATSCertificatePrefs", Context.MODE_PRIVATE) + val lastDownloadTime = sharedPreferences.getLong("lastDownloadTime", 0) + val currentTime = System.currentTimeMillis() + if (currentTime - lastDownloadTime < 365L * 24 * 60 * 60 * 1000) { + Toast.makeText(requireContext(), "NATS certificate download is not required yet.", Toast.LENGTH_SHORT).show() + return@observe + }else{ + hemoCubeViewModel.downloadClientCertificate() + } activity?.runOnUiThread { binding.btnSubmit.visibility = View.GONE val i = Intent(