Skip to content

Commit

Permalink
change error message function parameter to status code.
Browse files Browse the repository at this point in the history
  • Loading branch information
sraturi committed Sep 5, 2020
1 parent 1fc1d72 commit cf9022a
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 24 deletions.
3 changes: 1 addition & 2 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 @@ -62,7 +61,7 @@ class DataModule {

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

@Provides
@Singleton
Expand Down
11 changes: 5 additions & 6 deletions app/src/main/java/com/cradle/neptune/manager/ReadingManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ 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
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
* Service for interfacing with readings stored in the database.
Expand Down Expand Up @@ -200,21 +200,21 @@ class ReadingManager @Inject constructor(
* downloads the reading from the server and save it to the local database
* @return upload result.
*/
suspend fun downloadNewReadingFromServer(id: String):NetworkResult<Unit> =
suspend fun downloadNewReadingFromServer(id: String): NetworkResult<Unit> =
withContext(IO) {
val result = restApi.getReading(id)
when(result) {
when (result) {
is Success -> {
addReading(result.value)
}
}
result.map { Unit }
}

suspend fun downloadAssessmentsForReadings(assessmentId:String): NetworkResult<Unit> =
suspend fun downloadAssessmentsForReadings(assessmentId: String): NetworkResult<Unit> =
withContext(IO) {
val result = restApi.getAssessment(assessmentId)
when(result) {
when (result) {
is Success -> {
val reading = getReadingById(result.value.readingId)
reading?.followUp = result.value
Expand All @@ -225,5 +225,4 @@ class ReadingManager @Inject constructor(
}
result.map { Unit }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ class VolleyRequests(private val sharedPreferences: SharedPreferences) {
private const val NOT_FOUND = 404
private const val CONFLICT = 409

fun getServerErrorMessage(error: com.cradle.neptune.net.Failure<Any>): String {
var message = "Unable to upload to server (network error)"
message = when (error.statusCode) {
fun getServerErrorMessage(statusCode: Int): String {
return when (statusCode) {
UNAUTHORIZED -> "Server rejected credentials; check they are correct in settings."
BAD_REQUEST -> "Server rejected upload request; check server URL in settings."
NOT_FOUND -> "Server rejected URL; check server URL in settings."
CONFLICT -> "The reading or patient might already exists, check global patients"
else -> "Server rejected upload; check server URL in settings." +
" Code " + error.statusCode
" Code " + statusCode
}
return message
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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 @@ -88,8 +87,8 @@ class SyncStepperImplementation(
is Failure -> {
// let user know we failed.... probably cant continue?
errorHashMap[result.statusCode] =
"we need to get the error message"
//todo fix getting all the error.
VolleyRequests.getServerErrorMessage(result.statusCode)
// todo fix getting all the error.
withContext(Main) {
stepperCallback.onFetchDataCompleted(false)
}
Expand All @@ -115,7 +114,6 @@ class SyncStepperImplementation(
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
// downloaded patient id from the server to avoid conflicts
with(editedPatientsToUpload.iterator()) {
Expand Down Expand Up @@ -150,14 +148,12 @@ class SyncStepperImplementation(
val totalNum =
newPatientsToUpload.size + readingsToUpload.size + editedPatientsToUpload.size



// keep track of all the fail/pass status for uploaded requests
uploadRequestStatus = TotalRequestStatus(totalNum, 0, 0)

newPatientsToUpload.forEach {
val result = restApi.postPatient(it)
when(result) {
when (result) {
is Success -> {

it.patient.base = it.patient.lastEdited
Expand Down Expand Up @@ -224,7 +220,7 @@ class SyncStepperImplementation(
}

updateApiData.newReadingsIds.toList().forEach {
val result = readingManager.downloadNewReadingFromServer(it)
val result = readingManager.downloadNewReadingFromServer(it)
checkIfAllDataIsDownloaded(result)
}

Expand Down Expand Up @@ -268,7 +264,7 @@ class SyncStepperImplementation(
is Failure -> {
uploadRequestStatus.numFailed++
errorHashMap[result.statusCode] =
VolleyRequests.getServerErrorMessage(result as Failure<Any>)
VolleyRequests.getServerErrorMessage(result.statusCode)
}
}

Expand Down Expand Up @@ -301,7 +297,7 @@ class SyncStepperImplementation(
is Failure -> {
downloadRequestStatus.numFailed++
errorHashMap[result.statusCode] =
VolleyRequests.getServerErrorMessage(result as Failure<Any>)
VolleyRequests.getServerErrorMessage(result.statusCode)
}
}
withContext(Main) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class BaseFragment : Fragment() {
if (this.activity == null) {
return
}
val context = context
context
val view = this.requireActivity().currentFocus
if (view != null) {
val imm =
Expand Down

0 comments on commit cf9022a

Please sign in to comment.