Created
November 28, 2019 17:11
-
-
Save MamboBryan/2348eb4254c1191b3a6aa4e4886d5749 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PatientSeenRepository { | |
private static final String TAG = "PatientSeenRepository"; | |
private String url; | |
private String token; | |
private String requestHeader = "XMLHttpRequest"; | |
private static CollabmedClient mApiClient; | |
private MutableLiveData<PatientsSeenData> patientsSeenData = new MutableLiveData<>(); | |
private MutableLiveData<PatientSeenSummaryData> patientSeenSummaryData = new MutableLiveData<>(); | |
private Filters mFilters; | |
public PatientSeenRepository(String url, String token) { | |
this.url = url; | |
this.token = token; | |
mFilters = new Filters(); | |
generateService(); | |
} | |
private void generateService() { | |
ServiceGenerator mServiceGenerator = new ServiceGenerator(url); | |
mApiClient = mServiceGenerator.createService(CollabmedClient.class); | |
} | |
public MutableLiveData<PatientsSeenData> getPatientsSeenData(Filters filters) { | |
retrievePatientsSeenFromApi(); | |
return patientsSeenData; | |
} | |
public MutableLiveData<PatientSeenSummaryData> getPatientSeenSummaryData(Filters filters) { | |
retrievePatientsSeenSummaryFromApi(filters); | |
return patientSeenSummaryData; | |
} | |
private void retrievePatientsSeenSummaryFromApi(Filters filters) { | |
filters.setSummary("true"); | |
Call<PatientSeenSummaryData> call = mApiClient.getPatientsSeenSummay(requestHeader, token, filters); | |
call.enqueue(new Callback<PatientSeenSummaryData>() { | |
@Override | |
public void onResponse(Call<PatientSeenSummaryData> call, Response<PatientSeenSummaryData> response) { | |
//Checks if the connection was successful or not | |
if (!response.isSuccessful()) { | |
Log.i(TAG, "unsuccessful " + String.valueOf(response.code())); | |
return; | |
} | |
PatientSeenSummaryData data = response.body(); | |
if (data == null) { | |
Log.i(TAG, "The return value is null"); | |
return; | |
} | |
Log.i(TAG, response.message()); | |
Log.i(TAG, String.valueOf(response.code())); | |
Log.i(TAG, "Got summary data"); | |
List<PatientFacility> theFacilities = data.getFacilities(); | |
if (theFacilities == null) { | |
Log.i(TAG, "The return value is empty"); | |
return; | |
} | |
patientSeenSummaryData.setValue(data); | |
Log.i(TAG, "SummaryData onResponse: The data has been updated "); | |
Log.i(TAG, "SummaryData onResponse: " + data.getPaymentMode().get(0).getVisits()); | |
} | |
@Override | |
public void onFailure(Call<PatientSeenSummaryData> call, Throwable t) { | |
Log.i(TAG, "Failed to get theTAG"); | |
Log.i(TAG, Objects.requireNonNull(t.getMessage())); | |
patientSeenSummaryData.setValue(null); | |
Log.i(TAG, "SummaryData onResponse: The data has been updated "); | |
} | |
}); | |
} | |
private void retrievePatientsSeenFromApi() { | |
Call<PatientsSeenData> call = mApiClient.getPatientsSeen("1", requestHeader, token); | |
call.enqueue(new Callback<PatientsSeenData>() { | |
@Override | |
public void onResponse(Call<PatientsSeenData> call, Response<PatientsSeenData> response) { | |
//Checks if the connection was successful or not | |
if (!response.isSuccessful()) { | |
Log.i(TAG, String.valueOf(response.code())); | |
return; | |
} | |
PatientsSeenData data = response.body(); | |
if (data == null) { | |
Log.i(TAG, "The return value is null"); | |
return; | |
} | |
patientsSeenData.setValue(data); | |
Log.i(TAG, "got all the patients seen data"); | |
} | |
@Override | |
public void onFailure(Call<PatientsSeenData> call, Throwable t) { | |
Log.i(TAG, "Failed to get the patients seen data"); | |
Log.i(TAG, Objects.requireNonNull(t.getMessage())); | |
patientsSeenData.setValue(null); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment