reduced number api calls for bigtec

This commit is contained in:
Mariya
2024-03-01 12:38:19 +05:30
parent 80ab69c772
commit 1e5afa0a5c
5 changed files with 53 additions and 75 deletions

View File

@@ -109,20 +109,7 @@ class DatabaseRepository @Inject constructor(
}
override suspend fun addTestToDatabase(data: UserData?): Response<String> {
return try {
val userdata =
db.collection("patientData").whereEqualTo("_id", data!!._id).get().await()
if (userdata.documents.isNotEmpty()) {
userdata.documents.forEach {
db.collection("patientData").document(it.id).update("testStatus", true)
}
}
db.collection("testData").add(data).await()
Response.Success(data._id)
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
Response.Error(e)
}
TODO("Not yet implemented")
}
override suspend fun addTestToDatabaseforBufferCheck(data: BufferCheckData?): Response<String> {
@@ -247,4 +234,22 @@ class DatabaseRepository @Inject constructor(
override fun <UserData> addTestToDatabase(testDetails: UserData): Any {
TODO("Not yet implemented")
}
override suspend fun addTestToDatabasefornew(data: HemoCubeTestData?): Response<String> {
return try {
val userdata =
db.collection("patientData").whereEqualTo("_id", data!!._id).get().await()
if (userdata.documents.isNotEmpty()) {
userdata.documents.forEach {
db.collection("patientData").document(it.id).update("testStatus", true)
}
}
db.collection("testData").add(data).await()
Response.Success(data._id)
} catch (e: Exception) {
Firebase.crashlytics.recordException(e)
Response.Error(e)
}
}
}

View File

@@ -25,6 +25,7 @@ import okhttp3.ResponseBody
interface Repository {
suspend fun addTestToDatabase(data: HemoCubeTestData?): Response<String>
suspend fun addTestToDatabasefornew(data: HemoCubeTestData?): Response<String>
suspend fun addTestToDatabase(data: UserData?): Response<String>

View File

@@ -180,13 +180,13 @@ class HomeFragment : Fragment() {
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)
hemoCubeViewModel.uploadResultfornew(resultList)
}
}
@@ -268,35 +268,16 @@ class HomeFragment : Fragment() {
var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString()
deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").toString()
if (userID.isNotEmpty() && password.isNotEmpty()) {
if (!isTokenAvailable) {
if (!isTokenAvailable || isTokenExpired(accessToken)) {
hemoCubeViewModel.login(createLoginRequestData(userID, password))
} else {
if (isTokenExpired(accessToken)) {
hemoCubeViewModel.login(createLoginRequestData(userID, password))
} else {
isTokenAvailable = true
hemoCubeViewModel.uploadLogs()
hemoCubeViewModel.startPeriodicCheckUpdate()
hemoCubeViewModel.downloadClientCertificate()
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
}
}
} else if (deviceId.isNotEmpty()) {
} else if (userID.isEmpty() && password.isEmpty() && deviceId.isNotEmpty()) {
fetchDeviceCredentials()
// This code will execute after credentials have been successfully fetched and stored.
userID = sharedPreference.getString("username", "").toString()
password = sharedPreference.getString("password", "").toString()
accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
if (accessToken.isEmpty()) {
hemoCubeViewModel.login(createLoginRequestData(userID, password))
} else {
// Continue with your existing logic if the token is not empty.
isTokenAvailable = true
hemoCubeViewModel.uploadLogs()
hemoCubeViewModel.startPeriodicCheckUpdate()
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
}
} else {
Toast.makeText(
requireContext(),
@@ -339,13 +320,6 @@ class HomeFragment : Fragment() {
// Toast.LENGTH_SHORT
// ).show()
if (!hasLogsBeenUploaded()) {
// Your code to upload logs
// Assuming the upload process is initiated here
// After successful upload, save the flag
saveUploadSuccessFlag(true)
}
}
is Result.Error -> {
@@ -384,12 +358,12 @@ class HomeFragment : Fragment() {
is Result.Error -> {
response.exception.let { message ->
Toast.makeText(
activity,
"$message",
Toast.LENGTH_LONG
)
.show()
// Toast.makeText(
// activity,
// "$message",
// Toast.LENGTH_LONG
// )
// .show()
}
}
@@ -406,23 +380,21 @@ class HomeFragment : Fragment() {
is Result.Success -> {
val url = response.data
val fileName = "nats_certificate.zip"
val downloadDirectory = "NATS"
val file = downloadFile(url, requireContext(), fileName, downloadDirectory)
// Toast.makeText(
// requireContext(),
// "NATS certificate Downloaded",
// Toast.LENGTH_SHORT
// ).show()
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) {
// Assuming if the directory exists, the certificate has been downloaded and extracted.
// You can add more specific checks here, e.g., checking for specific files within the directory.
Toast.makeText(requireContext(), "NATS certificate already downloaded and extracted.", Toast.LENGTH_SHORT).show()
return@observe
}
val file = downloadFile(url, requireContext(), fileName, downloadDirectory)
val unzipDirectoryPath =
requireContext().getExternalFilesDir(null)?.absolutePath + "/$downloadDirectory"
unzip(file.absolutePath, unzipDirectoryPath)
// Toast.makeText(
// requireContext(),
// "NATS certificate Extracted",
// Toast.LENGTH_SHORT
// ).show()
}
is Result.Error -> {
@@ -1105,14 +1077,4 @@ class HomeFragment : Fragment() {
return matchResult?.groups?.get(1)?.value ?: ""
}
private fun saveUploadSuccessFlag(isSuccess: Boolean) {
sharedPreference = requireActivity().getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
sharedPreference.edit().putBoolean("LogsUploaded", isSuccess).apply()
}
private fun hasLogsBeenUploaded(): Boolean {
sharedPreference = requireActivity().getSharedPreferences("AppPrefs", Context.MODE_PRIVATE)
return sharedPreference.getBoolean("LogsUploaded", false)
}
}

View File

@@ -161,6 +161,8 @@ class HemoCubeFragment : Fragment() {
)
}
handleReadingFinish()
hemoCubeViewModel.uploadLogs()
hemoCubeViewModel.downloadClientCertificate()
}
is Result.Error -> {

View File

@@ -142,6 +142,14 @@ class HemoCubeViewModel @Inject constructor(
}
}
fun uploadResultfornew(molbioV2ResultRequest: MolbioV2ResultRequest) = viewModelScope.launch {
resultUpload.postValue(Result.Loading())
repository.uploadResults(molbioV2ResultRequest).let {
resultUpload.postValue(it)
}
}
fun checkUpdate(checkUpdateRequest: CheckUpdateRequest) = viewModelScope.launch {
checkUpdate.postValue(Result.Loading())
repository.checkUpdate(checkUpdateRequest).let {
@@ -363,7 +371,7 @@ class HemoCubeViewModel @Inject constructor(
userData.reportUploadTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
when (repository.addTestToDatabase(userData)) {
when (repository.addTestToDatabasefornew(userData)) {
is Response.Success -> {
fireBaseBulkUpload.postValue("Success")
updateLocalFlag(userData._id)