nats certificate downloading only calling once code added

This commit is contained in:
Mariya
2024-04-01 13:07:46 +05:30
parent fdb63d808d
commit 9c4a4290bb
2 changed files with 31 additions and 8 deletions

View File

@@ -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 -> {

View File

@@ -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(