api calls managing, and error resolving for molbio

This commit is contained in:
Mariya
2024-03-02 19:37:24 +05:30
parent c8d5d41c81
commit 20f408230e
4 changed files with 57 additions and 83 deletions

View File

@@ -19,8 +19,8 @@ android {
applicationId "in.sminnovations.hpostesting.dev" applicationId "in.sminnovations.hpostesting.dev"
minSdk 21 minSdk 21
targetSdk 34 targetSdk 34
versionCode 117 versionCode 119
versionName "2.1.117" versionName "2.1.119"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }

View File

@@ -188,9 +188,14 @@ class HomeFragment : Fragment() {
} }
} }
} }
var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
// Upload results after processing all userData to avoid duplicates and ensure all modifications are done // Upload results after processing all userData to avoid duplicates and ensure all modifications are done
if (resultList.results?.isNotEmpty() == true) { if(accessToken.isNotEmpty()) {
hemoCubeViewModel.uploadResult(resultList) if (resultList.results?.isNotEmpty() == true) {
hemoCubeViewModel.uploadResult(resultList)
Log.d("resultcount1","resultcount")
}
} }
} }
@@ -217,7 +222,7 @@ class HomeFragment : Fragment() {
} }
binding.uploadData.setOnClickListener { binding.uploadData.setOnClickListener {
showUploadDialog(requireContext()) // showUploadDialog(requireContext())
} }
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userData -> hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userData ->
@@ -274,26 +279,23 @@ class HomeFragment : Fragment() {
deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").toString() deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").toString()
if(userID.isNotEmpty() && password.isNotEmpty()) { if(userID.isNotEmpty() && password.isNotEmpty()) {
Log.d("istoken",isTokenAvailable.toString()) Log.d("istoken",isTokenAvailable.toString())
if (!isTokenAvailable) { if (isTokenAvailable) {
Log.d("istoken1",isTokenAvailable.toString()) Log.d("istoken7",isTokenAvailable.toString())
if(isTokenExpired(accessToken)) {
hemoCubeViewModel.login(createLoginRequestData(userID, password))
}
}else{
isTokenAvailable = true isTokenAvailable = true
hemoCubeViewModel.startPeriodicCheckUpdate() hemoCubeViewModel.startPeriodicCheckUpdate()
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData()) hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
}else{
if(isTokenExpired(accessToken)) {
Log.d("istoken8",isTokenAvailable.toString())
hemoCubeViewModel.login(createLoginRequestData(userID, password))
}else {
Log.d("istoken1",isTokenAvailable.toString())
hemoCubeViewModel.login(createLoginRequestData(userID, password))
isTokenAvailable = true
}
} }
} else if (userID.isEmpty() && password.isEmpty() && deviceId.isNotEmpty()) { } else if (userID.isEmpty() && password.isEmpty() && deviceId.isNotEmpty()) {
fetchDeviceCredentials() fetchDeviceCredentials()
var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
var userID = sharedPreference.getString("username", "").toString()
var password = sharedPreference.getString("password", "").toString()
if(accessToken.isNotEmpty()){
isTokenAvailable = true
hemoCubeViewModel.startPeriodicCheckUpdate()
hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData())
}
} else { } else {
Toast.makeText( Toast.makeText(
requireContext(), requireContext(),
@@ -624,6 +626,7 @@ class HomeFragment : Fragment() {
} }
} }
@RequiresApi(Build.VERSION_CODES.P)
private fun fetchDeviceCredentials() { private fun fetchDeviceCredentials() {
try { try {
val db = Firebase.firestore val db = Firebase.firestore
@@ -650,13 +653,13 @@ class HomeFragment : Fragment() {
) )
// Save credentials in SharedPreferences // Save credentials in SharedPreferences
with(sharedPreference.edit()) { with(sharedPreference.edit()) {
putString("username", username) putString(Constants.DEVICE_ID_API, username)
putString("password", password) putString(Constants.DEVICE_PASSWORD_API, password)
putString(Constants.NATS_TOKEN, natsToken) putString(Constants.NATS_TOKEN, natsToken)
apply() apply()
} }
if (!isTokenAvailable ) { if (!isTokenAvailable ) {
Log.d("istoken1", isTokenAvailable.toString()) Log.d("istoken0", isTokenAvailable.toString())
hemoCubeViewModel.login(createLoginRequestData(username, password)) hemoCubeViewModel.login(createLoginRequestData(username, password))
} }
@@ -854,7 +857,7 @@ class HomeFragment : Fragment() {
builder.setMessage(R.string.upload_db_registration_message) builder.setMessage(R.string.upload_db_registration_message)
builder.setPositiveButton(R.string.upload) { dialog, _ -> builder.setPositiveButton(R.string.upload) { dialog, _ ->
uploadLocalDBData(dialog) // uploadLocalDBData(dialog)
} }
builder.setNegativeButton(R.string.cancel) { dialog, _ -> builder.setNegativeButton(R.string.cancel) { dialog, _ ->
@@ -900,34 +903,34 @@ class HomeFragment : Fragment() {
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList -> hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result())) val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result()))
userDataList.forEach { userData -> // userDataList.forEach { userData ->
if (!userData.molbioFlag && isTokenAvailable) { // if (!userData.molbioFlag && isTokenAvailable) {
resultList.results?.add( // resultList.results?.add(
MolbioV2Result( // MolbioV2Result(
rawData = userData, // rawData = userData,
analysisId = userData._id, // analysisId = userData._id,
analysisDate = "2024-02-08 16:33:56", //userData.reportUploadTime, // analysisDate = "2024-02-08 16:33:56", //userData.reportUploadTime,
analysisStatus = userData.classificationResult, // analysisStatus = userData.classificationResult,
thresholds = Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId].toString(), // thresholds = Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId].toString(),
interpretation = userData.classificationResult, // interpretation = userData.classificationResult,
testId = userData._id, // testId = userData._id,
testTime = userData.testTime, // testTime = userData.testTime,
collectionTime = "2024-02-08 16:33:56",//userData.testTime, // collectionTime = "2024-02-08 16:33:56",//userData.testTime,
expiryTime = "2024-02-08 16:33:56",//userData.testTime, // expiryTime = "2024-02-08 16:33:56",//userData.testTime,
) // )
) // )
//
// }
} // if (!userData.localFlag) {
// userData.localFlag = true
if (!userData.localFlag) { // hemoCubeViewModel.bulkAddResultTestToDb(userData)
userData.localFlag = true // }
hemoCubeViewModel.bulkAddResultTestToDb(userData) // if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) {
} // userData.molbioFlag = true
if (!userData.molbioFlag && isTokenAvailable && Constants.MOLBIO_INTEGRATION) { // hemoCubeViewModel.uploadResult(resultList)
userData.molbioFlag = true // }
hemoCubeViewModel.uploadResult(resultList) // }
}
}
dialog.dismiss() dialog.dismiss()
} }
@@ -940,37 +943,6 @@ class HomeFragment : Fragment() {
} }
dialog.dismiss() dialog.dismiss()
} }
hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
val resultList = MolbioV2ResultRequest(mutableListOf(MolbioV2Result()))
userDataList.forEach { userData ->
if (!userData.molbioFlag && isTokenAvailable) {
resultList.results?.add(
MolbioV2Result(
rawData = userData,
analysisId = userData._id,
analysisDate = "2024-02-08 16:33:56",//userData.testTime,
analysisStatus = userData.classificationResult,
thresholds = Constants.BUFFER_INTENSITY_THRESHOLDS[userData.deviceId].toString(),
interpretation = userData.classificationResult,
testId = userData._id,
testTime = "2024-02-08 16:33:56",//userData.testTime,
collectionTime = "2024-02-08 16:33:56",//userData.testTime,
expiryTime = "2024-02-08 16:33:56"//userData.testTime,
)
)
userData.molbioFlag = true
hemoCubeViewModel.uploadResult(resultList)
}
if (!userData.localFlag) {
userData.localFlag = true
hemoCubeViewModel.bulkAddResultTestToDb(userData)
}
}
dialog.dismiss()
}
} }
// private fun downloadLocalDBData(dialog: DialogInterface) { // private fun downloadLocalDBData(dialog: DialogInterface) {

View File

@@ -149,8 +149,9 @@ class HemoCubeFragment : Fragment() {
hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result -> hemoCubeViewModel.fireBaseUpload.observe(viewLifecycleOwner) { result ->
if (result == "Success") { if (result == "Success") {
uploadedToCloud = true uploadedToCloud = true
var accessToken = sharedPreferences.getString(Constants.ACCESS_TOKEN, "").toString()
showToast(R.string.test_upload) showToast(R.string.test_upload)
if (Constants.MOLBIO_INTEGRATION) { if (Constants.MOLBIO_INTEGRATION && accessToken.isNotEmpty()) {
hemoCubeViewModel.resultUpload.observe(viewLifecycleOwner) { hemoCubeViewModel.resultUpload.observe(viewLifecycleOwner) {
when (it) { when (it) {
is Result.Success -> { is Result.Success -> {

View File

@@ -328,7 +328,8 @@ 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 testDetails.localFlag = true
if (Constants.MOLBIO_INTEGRATION) { var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString()
if (Constants.MOLBIO_INTEGRATION && accessToken.isNotEmpty()) {
uploadResult( uploadResult(
MolbioV2ResultRequest( MolbioV2ResultRequest(
mutableListOf( mutableListOf(