Skip to content

Commit

Permalink
replace restapi calls with PatientManager.kt and ReadingManager.kt calls
Browse files Browse the repository at this point in the history
  • Loading branch information
sraturi committed Sep 4, 2020
1 parent 2a79624 commit 07cb18b
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 85 deletions.
6 changes: 0 additions & 6 deletions app/src/main/java/com/cradle/neptune/dagger/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/com/cradle/neptune/manager/PatientManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {

Expand Down Expand Up @@ -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<Unit> =
suspend fun uploadNewPatient(patientAndReadings: PatientAndReadings): NetworkResult<Unit> =
withContext(IO) {
val result = restApi.postPatient(patientAndReadings)
if (result is Success) {
Expand Down Expand Up @@ -132,12 +131,22 @@ class PatientManager @Inject constructor(
result.map { Unit }
}

suspend fun downloadEditedPatientInfoFromServer(patientId: String): NetworkResult<Unit> =
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<PatientAndReadings> =
suspend fun downloadPatientAndReading(id: String): NetworkResult<PatientAndReadings> =
restApi.getPatient(id)

/**
Expand Down Expand Up @@ -166,6 +175,6 @@ class PatientManager @Inject constructor(
return@withContext associateResult.cast()
}

downloadPatient(id)
downloadPatientAndReading(id)
}
}
57 changes: 56 additions & 1 deletion app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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<Unit> =
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<Unit> =
withContext(IO) {
val result = restApi.getReading(id)
when(result) {
is Success -> {
addReading(result.value)
}
}
result.map { Unit }
}

suspend fun downloadAssessmentsForReadings(assessmentId:String): NetworkResult<Unit> =
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 }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -105,11 +106,14 @@ class SyncStepperImplementation(
override suspend fun setupUploadingPatientReadings(lastSyncTime: Long) {

// get the brand new patients to upload
val newPatientsToUpload: ArrayList<PatientAndReadings> = patientManager.getUnUploadedPatients() as ArrayList<PatientAndReadings>
val newPatientsToUpload: ArrayList<PatientAndReadings> =
patientManager.getUnUploadedPatients() as ArrayList<PatientAndReadings>

val readingsToUpload: ArrayList<Reading> = readingManager.getUnUploadedReadingsForServerPatients() as ArrayList<Reading>
// mock the time for now, in the future, this will be from shared pref
val editedPatientsToUpload: ArrayList<Patient> = patientManager.getEditedPatients(lastSyncTime) as ArrayList<Patient>
val readingsToUpload: ArrayList<Reading> =
readingManager.getUnUploadedReadingsForServerPatients() as ArrayList<Reading>

val editedPatientsToUpload: ArrayList<Patient> =
patientManager.getEditedPatients(lastSyncTime) as ArrayList<Patient>


// this will be edited patients that were not edited in the server, we match against the
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 07cb18b

Please sign in to comment.