diff --git a/app/src/main/java/com/cradle/neptune/dagger/DataModule.kt b/app/src/main/java/com/cradle/neptune/dagger/DataModule.kt index 1e9e1a9..e4de2db 100644 --- a/app/src/main/java/com/cradle/neptune/dagger/DataModule.kt +++ b/app/src/main/java/com/cradle/neptune/dagger/DataModule.kt @@ -48,12 +48,6 @@ class DataModule { fun provideHealthFacilityDao(database: CradleDatabase): HealthFacilityDaoAccess = database.healthFacilityDaoAccess() - @Provides - @Singleton - fun provideReadingService(database: CradleDatabase): ReadingManager { - return ReadingManager(database.readingDaoAccess()) - } - @Provides @Singleton fun provideHealthCentreService(database: CradleDatabase?): HealthCentreManager { diff --git a/app/src/main/java/com/cradle/neptune/manager/PatientManager.kt b/app/src/main/java/com/cradle/neptune/manager/PatientManager.kt index 538377a..1a7412a 100644 --- a/app/src/main/java/com/cradle/neptune/manager/PatientManager.kt +++ b/app/src/main/java/com/cradle/neptune/manager/PatientManager.kt @@ -22,7 +22,6 @@ import kotlinx.coroutines.withContext @Suppress("RedundantSuspendModifier") class PatientManager @Inject constructor( private val daoAccess: PatientDaoAccess, - private val urlManager: UrlManager, private val restApi: RestApi ) { @@ -97,7 +96,7 @@ class PatientManager @Inject constructor( * @param patientAndReadings the patient to upload * @return whether the upload succeeded or not */ - suspend fun uploadPatient(patientAndReadings: PatientAndReadings): NetworkResult = + suspend fun uploadNewPatient(patientAndReadings: PatientAndReadings): NetworkResult = withContext(IO) { val result = restApi.postPatient(patientAndReadings) if (result is Success) { @@ -132,12 +131,22 @@ class PatientManager @Inject constructor( result.map { Unit } } + suspend fun downloadEditedPatientInfoFromServer(patientId: String): NetworkResult = + withContext(IO) { + val result = restApi.getPatientInfo(patientId) + if (result is Success) { + add(result.value) + } + + result.map { Unit } + } + /** * Downloads all the information for a patient from the server. * * @param id id of the patient to download */ - suspend fun downloadPatient(id: String): NetworkResult = + suspend fun downloadPatientAndReading(id: String): NetworkResult = restApi.getPatient(id) /** @@ -166,6 +175,6 @@ class PatientManager @Inject constructor( return@withContext associateResult.cast() } - downloadPatient(id) + downloadPatientAndReading(id) } } diff --git a/app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt b/app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt index 97ec086..9f8d026 100644 --- a/app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt +++ b/app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt @@ -3,11 +3,16 @@ package com.cradle.neptune.manager import com.cradle.neptune.database.ReadingDaoAccess import com.cradle.neptune.model.Reading import com.cradle.neptune.model.RetestGroup +import com.cradle.neptune.net.NetworkResult +import com.cradle.neptune.net.RestApi +import com.cradle.neptune.net.Success import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import kotlinx.coroutines.withContext +import javax.inject.Inject /** * Service for interfacing with readings stored in the database. @@ -25,7 +30,10 @@ import kotlinx.coroutines.withContext * main thread rather than run time error */ @Suppress("RedundantSuspendModifier") -class ReadingManager(private val daoAccess: ReadingDaoAccess) { +class ReadingManager @Inject constructor( + private val daoAccess: ReadingDaoAccess, + private val restApi: RestApi +) { /** * Adds a new reading to the database. @@ -171,4 +179,51 @@ class ReadingManager(private val daoAccess: ReadingDaoAccess) { suspend fun deleteAllData() { daoAccess.deleteAllReading() } + + /** + * upload new reading to the server and mark it uploaded based on the result. + * @return upload result + */ + suspend fun uploadNewReadingToServer(reading: Reading): NetworkResult = + withContext(IO) { + val result = restApi.postReading(reading) + when (result) { + is Success -> { + reading.isUploadedToServer = true + daoAccess.update(reading) + } + } + result.map { Unit } + } + + /** + * downloads the reading from the server and save it to the local database + * @return upload result. + */ + suspend fun downloadNewReadingFromServer(id: String):NetworkResult = + withContext(IO) { + val result = restApi.getReading(id) + when(result) { + is Success -> { + addReading(result.value) + } + } + result.map { Unit } + } + + suspend fun downloadAssessmentsForReadings(assessmentId:String): NetworkResult = + withContext(IO) { + val result = restApi.getAssessment(assessmentId) + when(result) { + is Success -> { + val reading = getReadingById(result.value.readingId) + reading?.followUp = result.value + if (reading != null) { + updateReading(reading) + } + } + } + result.map { Unit } + } + } diff --git a/app/src/main/java/com/cradle/neptune/manager/SyncManager.kt b/app/src/main/java/com/cradle/neptune/manager/SyncManager.kt index 9022dcc..f963bad 100644 --- a/app/src/main/java/com/cradle/neptune/manager/SyncManager.kt +++ b/app/src/main/java/com/cradle/neptune/manager/SyncManager.kt @@ -49,7 +49,7 @@ class SyncManager @Inject constructor( val results = patientManager .getUnUploadedPatients() .filterNot { updates.value.newPatientsIds.contains(it.patient.id) } - .map { patient -> patientManager.uploadPatient(patient) } + .map { patient -> patientManager.uploadNewPatient(patient) } monadicSequence(Unit, results) } diff --git a/app/src/main/java/com/cradle/neptune/sync/SyncStepperImplementation.kt b/app/src/main/java/com/cradle/neptune/sync/SyncStepperImplementation.kt index 22e62c3..d538f2b 100644 --- a/app/src/main/java/com/cradle/neptune/sync/SyncStepperImplementation.kt +++ b/app/src/main/java/com/cradle/neptune/sync/SyncStepperImplementation.kt @@ -2,6 +2,7 @@ package com.cradle.neptune.sync import android.content.Context import android.content.SharedPreferences +import android.util.Log import com.cradle.neptune.dagger.MyApp import com.cradle.neptune.manager.PatientManager import com.cradle.neptune.manager.ReadingManager @@ -105,11 +106,14 @@ class SyncStepperImplementation( override suspend fun setupUploadingPatientReadings(lastSyncTime: Long) { // get the brand new patients to upload - val newPatientsToUpload: ArrayList = patientManager.getUnUploadedPatients() as ArrayList + val newPatientsToUpload: ArrayList = + patientManager.getUnUploadedPatients() as ArrayList - val readingsToUpload: ArrayList = readingManager.getUnUploadedReadingsForServerPatients() as ArrayList - // mock the time for now, in the future, this will be from shared pref - val editedPatientsToUpload: ArrayList = patientManager.getEditedPatients(lastSyncTime) as ArrayList + val readingsToUpload: ArrayList = + readingManager.getUnUploadedReadingsForServerPatients() as ArrayList + + val editedPatientsToUpload: ArrayList = + patientManager.getEditedPatients(lastSyncTime) as ArrayList // this will be edited patients that were not edited in the server, we match against the @@ -151,32 +155,29 @@ class SyncStepperImplementation( // keep track of all the fail/pass status for uploaded requests uploadRequestStatus = TotalRequestStatus(totalNum, 0, 0) - // ListUploader(context, PATIENT, newPatientsToUpload) { result -> - // // once finished call uploading a single patient, need to update the base time - // checkIfAllDataUploaded(result) - // }.startUpload() - // - // // these will be new readings for existing patients in server - // ListUploader(context, READING, readingsToUpload) { result -> - // checkIfAllDataUploaded(result) - // }.startUpload() - // - // ListUploader(context, PATIENT, editedPatientsToUpload) { - // checkIfAllDataUploaded(it) - // }.startUpload() - newPatientsToUpload.forEach { val result = restApi.postPatient(it) + when(result) { + is Success -> { + + it.patient.base = it.patient.lastEdited + patientManager.add(it.patient) + result.value.readings.forEach { reading -> + reading.isUploadedToServer = true + } + readingManager.addAllReadings(result.value.readings) + } + } checkIfAllDataUploaded(result) } readingsToUpload.forEach { - val result = restApi.postReading(it) + val result = readingManager.uploadNewReadingToServer(it) checkIfAllDataUploaded(result) } editedPatientsToUpload.forEach { - val result = restApi.putPatient(it) + val result = patientManager.updatePatientOnServer(it) checkIfAllDataUploaded(result) } @@ -201,26 +202,7 @@ class SyncStepperImplementation( // download brand new patients that are not on the device updateApiData.newPatientsIds.toList().forEach { - // volleyRequestManager.getFullPatientById(it) { result -> - // // need to put them in the DB - // when (result) { - // is Success -> { - // val patient = result.value.first - // val readings = result.value.second - // readings.forEach { reading -> - // reading.isUploadedToServer = true - // } - // readingManager.addAllReadings(readings) - // patientManager.add(patient) - // checkIfAllDataIsDownloaded(result) - // } - // is Failure -> { - // checkIfAllDataIsDownloaded(result) - // } - // } - // } - val result = restApi.getPatient(it) - + val result = patientManager.downloadPatientAndReading(it) when (result) { is Success -> { patientManager.add(result.value.patient) @@ -237,46 +219,17 @@ class SyncStepperImplementation( // download all the patients that we have but were edited by the server.... // these include the patients we rejected to upload in step 2 updateApiData.editedPatientsIds.toList().forEach { - // volleyRequestManager.getPatientOnlyInfo(it) { result -> - // checkIfAllDataIsDownloaded(result) - // } - val result = restApi.getPatientInfo(it) - when(result) { - is Success -> { - patientManager.add(result.value) - } - } + val result = patientManager.downloadEditedPatientInfoFromServer(it) checkIfAllDataIsDownloaded(result) } updateApiData.newReadingsIds.toList().forEach { - // get all the readings that were created for existing patients from the server - // volleyRequestManager.getReadingById(it) { result -> - // checkIfAllDataIsDownloaded(result) - // } - val result = restApi.getReading(it) - when(result) { - is Success -> { - readingManager.addReading(result.value) - } - } + val result = readingManager.downloadNewReadingFromServer(it) checkIfAllDataIsDownloaded(result) } updateApiData.followupIds.toList().forEach { - // make a volley request to get the requests - // volleyRequestManager.updateFollowUpById(it) { result -> - // checkIfAllDataIsDownloaded(result) - // } - val result = restApi.getAssessment(it) - when(result) { - is Success -> { - val associatedReading = readingManager.getReadingById(result.value.readingId) - associatedReading?.followUp = result.value - // todo any normal case where this would crash?? - readingManager.updateReading(associatedReading!!) - } - } + val result = readingManager.downloadAssessmentsForReadings(it) checkIfAllDataIsDownloaded(result) } // if there is nothing to download, call next step diff --git a/app/src/main/java/com/cradle/neptune/view/GlobalPatientProfileActivity.kt b/app/src/main/java/com/cradle/neptune/view/GlobalPatientProfileActivity.kt index 9610a40..4f34eb3 100644 --- a/app/src/main/java/com/cradle/neptune/view/GlobalPatientProfileActivity.kt +++ b/app/src/main/java/com/cradle/neptune/view/GlobalPatientProfileActivity.kt @@ -113,7 +113,7 @@ class GlobalPatientProfileActivity : PatientProfileActivity() { progressDialog.setMessage("Fetching the patient...") progressDialog.show() MainScope().launch { - val result = patientManager.downloadPatient(globalPatient.id) + val result = patientManager.downloadPatientAndReading(globalPatient.id) if (result !is Success) { progressDialog.cancel() Snackbar.make(