Save Data in Local storage in ever condition

This commit is contained in:
Kaif
2023-10-13 19:42:34 +05:30
parent 7c4717ebb7
commit 0408ef3fbd
5 changed files with 43 additions and 41 deletions

View File

@@ -20,4 +20,7 @@ interface HemoCubeDao {
@Query("DELETE FROM hemo_cube_test_table WHERE _id = :id") @Query("DELETE FROM hemo_cube_test_table WHERE _id = :id")
suspend fun deleteById(id: String) suspend fun deleteById(id: String)
@Query("UPDATE hemo_cube_test_table SET localFlag = :newValue WHERE _id = :id")
suspend fun updateFieldById(id: String, newValue: Boolean)
} }

View File

@@ -236,11 +236,4 @@ class UserListAdapter(
return diffMillis / (60 * 1000) return diffMillis / (60 * 1000)
} }
} }

View File

@@ -258,7 +258,7 @@ class HomeFragment : Fragment() {
} }
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
val uploadDataVisibility = if (userDataList.isEmpty()) View.GONE else View.VISIBLE val uploadDataVisibility = if (userDataList.any { !it.localFlag }) View.VISIBLE else View.GONE
binding.uploadData.visibility = uploadDataVisibility binding.uploadData.visibility = uploadDataVisibility
} }
} }
@@ -297,15 +297,13 @@ class HomeFragment : Fragment() {
} }
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
if (userDataList.isEmpty()) { userDataList.forEach { userData ->
dialog.dismiss() if (!userData.localFlag) {
} else {
userDataList.forEach { userData ->
userData.localFlag = true userData.localFlag = true
hemoCubeViewModel.bulkAddResultTestToDb(userData) hemoCubeViewModel.bulkAddResultTestToDb(userData)
} }
dialog.dismiss()
} }
dialog.dismiss()
} }
} }

View File

@@ -165,6 +165,10 @@ class HemoCubeFragment : Fragment() {
showToast("Internet not available, test details stored locally") showToast("Internet not available, test details stored locally")
startActivity(Intent(requireActivity(), DashboardActivity::class.java)) startActivity(Intent(requireActivity(), DashboardActivity::class.java))
} }
if (result == "Error") {
showToast("Error uploading data, test details stored locally")
startActivity(Intent(requireActivity(), DashboardActivity::class.java))
}
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
} }

View File

@@ -69,35 +69,32 @@ class HemoCubeViewModel @Inject constructor(
} }
} }
fun uploadHemoCubeResultToDatabaseforbuffercheckN(
isOnline: Boolean,
fun uploadHemoCubeResultToDatabaseforbuffercheckN(isOnline: Boolean, testStatus: Boolean, kitSerial: String?) = testStatus: Boolean,
viewModelScope.launch { kitSerial: String?
if (kitSerial != null) { ) = viewModelScope.launch {
testDetails?.kitSerial = kitSerial if (kitSerial != null) {
} testDetails?.kitSerial = kitSerial
testDetails?.testStatus = testStatus
try {
if (isOnline) {
parseData()
addResultTestToDbforbuffercheck()
} else {
parseData()
testDetails?.testTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
hemoCubeDao.insertAll(testDetails!!)
fireBaseUpload.postValue("Local")
}
} catch (e: Exception) {
Log.e("Testdb", "Upload failed: ${e.message}")
}
} }
testDetails?.testStatus = testStatus
try {
if (isOnline) {
parseData()
addResultTestToDbforbuffercheck()
} else {
parseData()
testDetails?.testTime = SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss", Locale.getDefault()
).format(Calendar.getInstance().time)
hemoCubeDao.insertAll(testDetails!!)
fireBaseUpload.postValue("Local")
}
} catch (e: Exception) {
Log.e("Testdb", "Upload failed: ${e.message}")
}
}
fun getDeviceData(deviceId: String?) = viewModelScope.launch { fun getDeviceData(deviceId: String?) = viewModelScope.launch {
deviceData.postValue(deviceId?.let { repository.getDeviceDataById(it) }) deviceData.postValue(deviceId?.let { repository.getDeviceDataById(it) })
@@ -142,11 +139,14 @@ class HemoCubeViewModel @Inject constructor(
} }
Log.i("Testdb", "Data uploaded to Firestore successfully") Log.i("Testdb", "Data uploaded to Firestore successfully")
fireBaseUpload.postValue("Success") fireBaseUpload.postValue("Success")
testDetails.localFlag = true
hemoCubeDao.insertAll(testDetails)
} }
is Response.Error -> { is Response.Error -> {
Log.e("Testdb", "Error uploading data to Firestore: $response") Log.e("Testdb", "Error uploading data to Firestore: $response")
fireBaseUpload.postValue("Error") fireBaseUpload.postValue("Error")
hemoCubeDao.insertAll(testDetails)
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
@@ -195,7 +195,7 @@ class HemoCubeViewModel @Inject constructor(
when (repository.addTestToDatabase(userData)) { when (repository.addTestToDatabase(userData)) {
is Response.Success -> { is Response.Success -> {
fireBaseBulkUpload.postValue("Success") fireBaseBulkUpload.postValue("Success")
deleteById(userData._id) updateLocalFlag(userData._id)
} }
else -> { else -> {
@@ -205,6 +205,10 @@ class HemoCubeViewModel @Inject constructor(
} }
} }
private fun updateLocalFlag(userId: String) = viewModelScope.launch {
hemoCubeDao.updateFieldById(id = userId, true)
}
fun deleteById(userId: String) = viewModelScope.launch { fun deleteById(userId: String) = viewModelScope.launch {
hemoCubeDao.deleteById(id = userId) hemoCubeDao.deleteById(id = userId)
} }