Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sraturi committed Sep 5, 2020
1 parent cf9022a commit 446aed5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/com/cradle/neptune/sync/SyncStepper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ interface SyncStepper {
* the api returns arrays of ids for new patients,edited patients, new readings, new assessments
* Once we have all the data we move to step number 2
*/
suspend fun fetchUpdatesFromServer()
suspend fun stepOneFetchUpdatesFromServer()

/**
* This is the step number two. Here we start uploading data to the server.
* The data can include new patients, edited patients, new readings etc.
* NOTE: we do not upload the patients edited by local user as well as the server
*/
suspend fun setupUploadingPatientReadings(lastSyncTime: Long)
suspend fun stepTwoSetupUploadingPatientReadings(lastSyncTime: Long)

/**
* This is the third step. Here we download all the new data from the server
* The data includes, new readings, patients, edited patients, follow ups etc.
* NOTE: in step number 2, we avoided uploading patient info that was also edited by the server
* in this step, we override our changes with the server since server changes are always prioritized.
*/
suspend fun downloadAllInfo()
suspend fun stepThreeDownloadAllInfo()

/**
* This is the last step of the sync process.
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 @@ -70,30 +71,28 @@ class SyncStepperImplementation(
/**
* step number 1, get all the newest data from the server. and go to step number 2.
*/
override suspend fun fetchUpdatesFromServer() {
override suspend fun stepOneFetchUpdatesFromServer() = withContext(IO) {
val lastSyncTime = sharedPreferences.getLong(LAST_SYNC, LAST_SYNC_DEFAULT)
// give a timestamp to provide, again this will be from shared pref eventually
withContext(IO) {
when (val result = restApi.getUpdates(lastSyncTime)) {
is Success -> {
// result was success
updateApiData = result.value
// let caller know of the next step
withContext(Main) {
stepperCallback.onFetchDataCompleted(true)
}
setupUploadingPatientReadings(lastSyncTime)
when (val result = restApi.getUpdates(lastSyncTime)) {
is Success -> {
// result was success
updateApiData = result.value
// let caller know of the next step
withContext(Main) {
stepperCallback.onFetchDataCompleted(true)
}
is Failure -> {
// let user know we failed.... probably cant continue?
errorHashMap[result.statusCode] =
VolleyRequests.getServerErrorMessage(result.statusCode)
// todo fix getting all the error.
withContext(Main) {
stepperCallback.onFetchDataCompleted(false)
}
finish(false)
Log.d("bugg", "current: ${Thread.currentThread().name}")
stepTwoSetupUploadingPatientReadings(lastSyncTime)
}
is Failure -> {
// let user know we failed.... probably cant continue?
errorHashMap[result.statusCode] =
VolleyRequests.getServerErrorMessage(result.statusCode)
withContext(Main) {
stepperCallback.onFetchDataCompleted(false)
}
finish(false)
}
}
}
Expand All @@ -102,7 +101,7 @@ class SyncStepperImplementation(
* step 2 -> starts uploading readings and patients starting with patients.
* Be careful changing code in this function.
*/
override suspend fun setupUploadingPatientReadings(lastSyncTime: Long) {
override suspend fun stepTwoSetupUploadingPatientReadings(lastSyncTime: Long) = withContext(IO) {

// get the brand new patients to upload
val newPatientsToUpload: ArrayList<PatientAndReadings> =
Expand Down Expand Up @@ -182,14 +181,14 @@ class SyncStepperImplementation(
withContext(Main) {
stepperCallback.onNewPatientAndReadingUploadFinish(uploadRequestStatus)
}
downloadAllInfo()
stepThreeDownloadAllInfo()
}
}

/**
* step 3 -> now we download all the information one by one
*/
override suspend fun downloadAllInfo() {
override suspend fun stepThreeDownloadAllInfo() = withContext(IO) {
val totalRequestNum =
(updateApiData.editedPatientsIds.size + updateApiData.newReadingsIds.size +
updateApiData.followupIds.size + updateApiData.newPatientsIds.size)
Expand Down Expand Up @@ -240,7 +239,7 @@ class SyncStepperImplementation(
/**
* saved last sync time in the shared pref. let the caller know
*/
override suspend fun finish(success: Boolean) {
override suspend fun finish(success: Boolean) = withContext(IO) {
if (success) {
sharedPreferences.edit()
.putLong(LAST_SYNC, ZonedDateTime.now().toEpochSecond()).apply()
Expand Down Expand Up @@ -278,7 +277,7 @@ class SyncStepperImplementation(
withContext(Main) {
stepperCallback.onNewPatientAndReadingUploadFinish(uploadRequestStatus)
}
downloadAllInfo()
stepThreeDownloadAllInfo()
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/cradle/neptune/view/SyncActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SyncActivity : AppCompatActivity(),
MainScope().launch {

SyncStepperImplementation(this@SyncActivity,
this@SyncActivity).fetchUpdatesFromServer()
this@SyncActivity).stepOneFetchUpdatesFromServer()
it.visibility = View.GONE
}
}
Expand Down

0 comments on commit 446aed5

Please sign in to comment.