Skip to content

Commit

Permalink
Merge pull request #194 from opensrp/remove-asyncTask
Browse files Browse the repository at this point in the history
Remove async task
  • Loading branch information
hilpitome authored Mar 3, 2023
2 parents ce201bb + d00aae3 commit 180f890
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 297 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=5.0.0-SNAPSHOT
VERSION_NAME=5.1.0-SNAPSHOT
VERSION_CODE=3
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Immunization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.smartregister.util.Utils.getValue;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
Expand All @@ -24,7 +23,8 @@
import org.smartregister.immunization.util.VaccinatorUtils;
import org.smartregister.immunization.view.ImmunizationRowCard;
import org.smartregister.immunization.view.ImmunizationRowGroup;
import org.smartregister.util.Utils;
import org.smartregister.util.CallableInteractorCallBack;
import org.smartregister.util.GenericInteractor;

import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -33,10 +33,13 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import timber.log.Timber;

import timber.log.Timber;

/**
* Created by raihan on 13/03/2017.
*/
Expand All @@ -48,6 +51,7 @@ public class ImmunizationRowAdapter extends BaseAdapter {
private HashMap<String, ImmunizationRowCard> vaccineCards;
private List<Vaccine> vaccineList;
private List<Alert> alertList;
private GenericInteractor mInteractor;

public ImmunizationRowAdapter(Context context, ImmunizationRowGroup vaccineGroup,
boolean editmode, List<Vaccine> vaccineList, List<Alert> alertList) {
Expand All @@ -57,6 +61,7 @@ public ImmunizationRowAdapter(Context context, ImmunizationRowGroup vaccineGroup
this.vaccineList = vaccineList;
this.alertList = alertList;
vaccineCards = new HashMap<>();
mInteractor = new GenericInteractor();
}

@Override
Expand All @@ -81,13 +86,43 @@ public View getView(int position, View convertView, ViewGroup parent) {
.get(position);
String vaccineName = vaccineData.name;
if (!vaccineCards.containsKey(vaccineName)) {
ImmunizationRowCard vaccineCard = new ImmunizationRowCard(context, editmode);
ImmunizationRowCard vaccineCard = getImmunizationRowCard();
vaccineCard.setId((int) getItemId(position));
vaccineCards.put(vaccineName, vaccineCard);
ImmunizationRowTask immunizationRowTask = new ImmunizationRowTask(vaccineCard, vaccineName,
vaccineGroup.getVaccineData().days_after_birth_due,
vaccineGroup.getChildDetails());
Utils.startAsyncTask(immunizationRowTask, null);
Callable<VaccineWrapper> callable = () -> {
CommonPersonObjectClient childDetails = vaccineGroup.getChildDetails();
int days_after_birth_due = vaccineGroup.getVaccineData().days_after_birth_due;
VaccineWrapper vaccineWrapper = new VaccineWrapper();
vaccineWrapper.setId(childDetails.entityId());
vaccineWrapper.setGender(childDetails.getDetails().get("gender"));
vaccineWrapper.setName(vaccineName);

String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
if (StringUtils.isNotBlank(dobString)) {
Calendar dobCalender = Calendar.getInstance();
DateTime dateTime = new DateTime(dobString);
dobCalender.setTime(dateTime.toDate());
dobCalender.add(Calendar.DATE, days_after_birth_due);
vaccineWrapper.setVaccineDate(new DateTime(dobCalender.getTime()));
}


Photo photo = ImageUtils.profilePhotoByClient(childDetails);
vaccineWrapper.setPhoto(photo);

String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
vaccineWrapper.setPatientNumber(zeirId);
vaccineWrapper.setPatientName(
getValue(childDetails.getColumnmaps(), "first_name", true) + " " + getValue(childDetails.getColumnmaps(),
"last_name", true));

updateWrapper(vaccineWrapper);
updateWrapperStatus(vaccineWrapper, childDetails);
return vaccineWrapper;
};
ImmunizationRowCallableInteractorCallback immunizationRowCallableInteractorCallback = getImmunizationRowCallableInteractor(vaccineCard, vaccineName);
GenericInteractor interactor = getGenericInteractor();
interactor.execute(callable, immunizationRowCallableInteractorCallback);
}
return vaccineCards.get(vaccineName);
} catch (Exception e) {
Expand All @@ -96,6 +131,19 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}

public GenericInteractor getGenericInteractor() {
return mInteractor;
}

public ImmunizationRowCallableInteractorCallback getImmunizationRowCallableInteractor(ImmunizationRowCard vaccineCard, String vaccineName) {
return new ImmunizationRowCallableInteractorCallback(vaccineCard, vaccineName);
}


public ImmunizationRowCard getImmunizationRowCard() {
return new ImmunizationRowCard(context, editmode);
}

public void update(ArrayList<VaccineWrapper> vaccinesToUpdate) {
if (vaccineCards != null) {
if (vaccinesToUpdate == null) {// Update all vaccines
Expand Down Expand Up @@ -226,58 +274,18 @@ public void setAlertList(List<Alert> alertList) {
this.alertList = alertList;
}

class ImmunizationRowTask extends AsyncTask<Void, Void, VaccineWrapper> {
class ImmunizationRowCallableInteractorCallback implements CallableInteractorCallBack<VaccineWrapper> {

private ImmunizationRowCard vaccineCard;

private String vaccineName;

private int days_after_birth_due;

private CommonPersonObjectClient childDetails;

ImmunizationRowTask(ImmunizationRowCard vaccineCard, String vaccineName, int days_after_birth_due,
CommonPersonObjectClient childDetails) {
ImmunizationRowCallableInteractorCallback(ImmunizationRowCard vaccineCard, String vaccineName) {
this.vaccineCard = vaccineCard;
this.vaccineName = vaccineName;
this.days_after_birth_due = days_after_birth_due;
this.childDetails = childDetails;
}

@Override
protected VaccineWrapper doInBackground(Void... params) {

VaccineWrapper vaccineWrapper = new VaccineWrapper();
vaccineWrapper.setId(childDetails.entityId());
vaccineWrapper.setGender(childDetails.getDetails().get("gender"));
vaccineWrapper.setName(vaccineName);

String dobString = getValue(childDetails.getColumnmaps(), "dob", false);
if (StringUtils.isNotBlank(dobString)) {
Calendar dobCalender = Calendar.getInstance();
DateTime dateTime = new DateTime(dobString);
dobCalender.setTime(dateTime.toDate());
dobCalender.add(Calendar.DATE, days_after_birth_due);
vaccineWrapper.setVaccineDate(new DateTime(dobCalender.getTime()));
}


Photo photo = ImageUtils.profilePhotoByClient(childDetails);
vaccineWrapper.setPhoto(photo);

String zeirId = getValue(childDetails.getColumnmaps(), "zeir_id", false);
vaccineWrapper.setPatientNumber(zeirId);
vaccineWrapper.setPatientName(
getValue(childDetails.getColumnmaps(), "first_name", true) + " " + getValue(childDetails.getColumnmaps(),
"last_name", true));

updateWrapper(vaccineWrapper);
updateWrapperStatus(vaccineWrapper, childDetails);
return vaccineWrapper;
}

@Override
protected void onPostExecute(VaccineWrapper vaccineWrapper) {
public void onResult(VaccineWrapper vaccineWrapper) {
vaccineCard.setVaccineWrapper(vaccineWrapper);
vaccineGroup.toggleRecordAllTV();
if (vaccineWrapper.getStatus() == null) {
Expand All @@ -286,6 +294,11 @@ protected void onPostExecute(VaccineWrapper vaccineWrapper) {
notifyDataSetChanged();
}
}

@Override
public void onError(Exception ex) {
Timber.e(ex);
}
}

private void removeVaccine(String vaccineName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.smartregister.util.Utils.getValue;

import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
Expand All @@ -21,11 +20,12 @@
import org.smartregister.immunization.domain.ServiceWrapper;
import org.smartregister.immunization.repository.RecurringServiceRecordRepository;
import org.smartregister.immunization.repository.VaccineRepository;
import org.smartregister.util.CallableInteractorCallBack;
import org.smartregister.util.GenericInteractor;
import org.smartregister.immunization.util.ImageUtils;
import org.smartregister.immunization.util.VaccinatorUtils;
import org.smartregister.immunization.view.ServiceCard;
import org.smartregister.immunization.view.ServiceGroup;
import org.smartregister.util.Utils;

import java.util.ArrayList;
import java.util.Calendar;
Expand All @@ -34,10 +34,13 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import timber.log.Timber;

import timber.log.Timber;

/**
* Created by keyman on 15/05/2017.
*/
Expand All @@ -51,6 +54,7 @@ public class ServiceCardAdapter extends BaseAdapter {
private Map<String, List<ServiceType>> serviceTypeMap;

private boolean isChildActive = true;
private GenericInteractor mGenericInteractor;

public ServiceCardAdapter(Context context, ServiceGroup serviceGroup, List<ServiceRecord> serviceRecordList,
List<Alert> alertList, Map<String, List<ServiceType>> serviceTypeMap) {
Expand All @@ -60,6 +64,7 @@ public ServiceCardAdapter(Context context, ServiceGroup serviceGroup, List<Servi
this.alertList = alertList;
this.serviceTypeMap = serviceTypeMap;
serviceCards = new HashMap<>();
mGenericInteractor = new GenericInteractor();
}

public void updateAll() {
Expand Down Expand Up @@ -123,8 +128,12 @@ public View getView(int position, View convertView, ViewGroup parent) {
serviceCard.setId((int) getItemId(position));
serviceCards.put(type, serviceCard);

ServiceCardTask serviceRowTask = new ServiceCardTask(serviceCard, serviceGroup.getChildDetails(), type);
Utils.startAsyncTask(serviceRowTask, null);
ServiceCardTaskCallable callable = new ServiceCardTaskCallable(serviceGroup.getChildDetails(), type);
ServiceCardTaskCallableInteractorCallable callableInteractorCallaback = getServiceCardTaskCallableInteractorCallable(serviceCard);
GenericInteractor interactor = getGenericInteractor();

interactor.execute(callable, callableInteractorCallaback);

}

return serviceCards.get(type);
Expand All @@ -135,6 +144,10 @@ public View getView(int position, View convertView, ViewGroup parent) {

}

public ServiceCardTaskCallableInteractorCallable getServiceCardTaskCallableInteractorCallable(ServiceCard serviceCard) {
return new ServiceCardTaskCallableInteractorCallable(serviceCard);
}

public boolean atLeastOneVisibleCard() {
if (serviceCards != null) {
for (ServiceCard serviceCard : serviceCards.values()) {
Expand Down Expand Up @@ -292,22 +305,23 @@ public void updateWrapper(ServiceWrapper tag) {

}

class ServiceCardTask extends AsyncTask<Void, Void, ServiceWrapper> {
public GenericInteractor getGenericInteractor(){
return mGenericInteractor;
}

private ServiceCard serviceCard;
public class ServiceCardTaskCallable implements Callable<ServiceWrapper> {

private CommonPersonObjectClient childDetails;

private String type;

ServiceCardTask(ServiceCard serviceCard, CommonPersonObjectClient childDetails, String type) {
this.serviceCard = serviceCard;
ServiceCardTaskCallable(CommonPersonObjectClient childDetails, String type) {
this.childDetails = childDetails;
this.type = type;
}

@Override
protected ServiceWrapper doInBackground(Void... params) {
public ServiceWrapper call() {
ServiceWrapper serviceWrapper = new ServiceWrapper();
serviceWrapper.setId(childDetails.entityId());
serviceWrapper.setGender(childDetails.getDetails().get("gender"));
Expand Down Expand Up @@ -337,13 +351,27 @@ protected ServiceWrapper doInBackground(Void... params) {

return serviceWrapper;
}
}

public class ServiceCardTaskCallableInteractorCallable implements CallableInteractorCallBack<ServiceWrapper>{

private ServiceCard serviceCard;

public ServiceCardTaskCallableInteractorCallable(ServiceCard serviceCard){
this.serviceCard = serviceCard;
}

@Override
protected void onPostExecute(ServiceWrapper serviceWrapper) {
public void onResult(ServiceWrapper serviceWrapper) {
serviceCard.setServiceWrapper(serviceWrapper);
visibilityCheck();
notifyDataSetChanged();
}

@Override
public void onError(Exception ex) {
Timber.e(ex);
}
}

public void updateServiceRecordList(List<ServiceRecord> serviceRecordList) {
Expand Down
Loading

0 comments on commit 180f890

Please sign in to comment.