Skip to content

Commit

Permalink
Merge branch 'sync-network-integration' into 'new-network'
Browse files Browse the repository at this point in the history
Sync network integration

See merge request 415-cradle/cradlemobile!52
  • Loading branch information
sraturi committed Sep 5, 2020
2 parents 46b7113 + b7c5c84 commit c2272a8
Show file tree
Hide file tree
Showing 15 changed files with 237 additions and 508 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/com/cradle/neptune/dagger/AppComponent.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.cradle.neptune.dagger

import com.cradle.neptune.manager.VolleyRequestManager
import com.cradle.neptune.sync.ListUploader
import com.cradle.neptune.sync.SyncStepperImplementation
import com.cradle.neptune.view.GlobalPatientProfileActivity
import com.cradle.neptune.view.GlobalPatientSearchActivity
Expand Down Expand Up @@ -45,7 +44,6 @@ interface AppComponent {
fun inject(globalPatientSearchActivity: GlobalPatientSearchActivity?)
fun inject(healthFacilityViewModel: HealthFacilityViewModel?)
fun inject(volleyRequestManager: VolleyRequestManager?)
fun inject(listUploader: ListUploader?)
fun inject(syncStepperImplementation: SyncStepperImplementation?)
fun inject(syncActivity: SyncActivity)
fun inject(myApp: MyApp)
Expand Down
9 changes: 1 addition & 8 deletions app/src/main/java/com/cradle/neptune/dagger/DataModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.cradle.neptune.database.HealthFacilityDaoAccess
import com.cradle.neptune.database.PatientDaoAccess
import com.cradle.neptune.database.ReadingDaoAccess
import com.cradle.neptune.manager.HealthCentreManager
import com.cradle.neptune.manager.ReadingManager
import com.cradle.neptune.manager.VolleyRequestManager
import com.cradle.neptune.net.Http
import com.cradle.neptune.network.VolleyRequestQueue
Expand Down Expand Up @@ -48,12 +47,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 All @@ -68,7 +61,7 @@ class DataModule {

@Provides
@Singleton
fun providesHttp(application: Application?): Http = Http()
fun providesHttp(): Http = Http()

@Provides
@Singleton
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)
}
}
56 changes: 55 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,7 +3,12 @@ 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 javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
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,50 @@ 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 downloadAssessment(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 }
}
}
194 changes: 0 additions & 194 deletions app/src/main/java/com/cradle/neptune/manager/SyncManager.kt

This file was deleted.

16 changes: 8 additions & 8 deletions app/src/main/java/com/cradle/neptune/model/SyncUpdate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@ import org.json.JSONObject
* API response model for the `/sync/updates` endpoint. Contains lists of
* new/edited patients, readings, and assessments (aka. followups).
*
* @property newPatients List of ids of new patients that have been created
* @property newPatientsIds List of ids of new patients that have been created
* since the last sync
* @property editedPatients List of ids of patients which have been edited
* @property editedPatientsIds List of ids of patients which have been edited
* since the last sync
* @property readings List of ids of new readings since the last sync
* @property followups List of ids of new followups since the last sync
* @property newReadingsIds List of ids of new readings since the last sync
* @property followupIds List of ids of new followups since the last sync
*/
data class SyncUpdate(
val newPatients: Set<String>,
val editedPatients: Set<String>,
val readings: Set<String>,
val followups: Set<String>
val newPatientsIds: Set<String>,
val editedPatientsIds: Set<String>,
val newReadingsIds: Set<String>,
val followupIds: Set<String>
) {
companion object : Unmarshal<SyncUpdate, JSONObject> {
override fun unmarshal(data: JSONObject): SyncUpdate =
Expand Down
Loading

0 comments on commit c2272a8

Please sign in to comment.