Skip to content

Commit

Permalink
Merge pull request #649 from opensrp/async_tasks_refactoring
Browse files Browse the repository at this point in the history
Async tasks refactoring
  • Loading branch information
junaidwarsivd authored Oct 27, 2022
2 parents 3a96e30 + 9820a39 commit f9147fc
Show file tree
Hide file tree
Showing 15 changed files with 188 additions and 197 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package com.vijay.jsonwizard.customviews;

import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS;

import android.app.Activity;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -16,6 +17,8 @@
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.vijay.jsonwizard.NativeFormLibrary;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.constants.JsonFormConstants;
Expand Down Expand Up @@ -43,8 +46,6 @@

import timber.log.Timber;

import static android.view.inputmethod.InputMethodManager.HIDE_NOT_ALWAYS;

public class GenericPopupDialog extends DialogFragment implements GenericDialogInterface {
private ViewGroup dialogView;
private JsonFormInteractor jsonFormInteractor = JsonFormInteractor.getInstance();
Expand Down Expand Up @@ -89,51 +90,50 @@ public void onActivityCreated(Bundle savedInstanceState) {
"The Context is not set. Did you forget to set context with Generic Dialog setContext method?");
}

getJsonApi().getAppExecutors().diskIO().execute(new Runnable() {
@Override
public void run() {
getJsonApi().getAppExecutors().diskIO().execute(() -> {
getJsonApi().getAppExecutors().mainThread().execute(() -> {
});

if (isDetached()) {
return;
}
// support translation of sub-forms
// support translation of sub-forms
Intent activityIntent = getActivity().getIntent();
translateSubForm = activityIntent != null && activityIntent.hasExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION) ?
activityIntent.getBooleanExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, false) :
NativeFormLibrary.getInstance().isPerformFormTranslation();
if (isDetached()) {
return;
}
// support translation of sub-forms
// support translation of sub-forms
Intent activityIntent = getActivity().getIntent();
translateSubForm = activityIntent != null && activityIntent.hasExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION) ?
activityIntent.getBooleanExtra(JsonFormConstants.PERFORM_FORM_TRANSLATION, false) :
NativeFormLibrary.getInstance().isPerformFormTranslation();

try {
setMainFormFields(formUtils.getFormFields(getStepName(), context));
loadPartialSecondaryValues();
createSecondaryValuesMap();
try {
setMainFormFields(formUtils.getFormFields(getStepName(), context));
loadPartialSecondaryValues();
createSecondaryValuesMap();

loadSubForms();
loadSubForms();

getJsonApi().updateGenericPopupSecondaryValues(specifyContent, stepName);
} catch (JSONException e) {
Timber.e(e, " --> onCreate");
}
getJsonApi().updateGenericPopupSecondaryValues(specifyContent, stepName);
} catch (JSONException e) {
Timber.e(e, " --> onCreate");
}

final List<View> views = initiateViews();
if (isDetached()) {
return;
}
getJsonApi().initializeDependencyMaps();
getJsonApi().getAppExecutors().mainThread().execute(new Runnable() {
@Override
public void run() {
if (isDetached()) {
return;
}
setViewList(views);
getJsonApi().invokeRefreshLogic(null, true, null, null, getStepName(), false);
if (getDialogView() != null) {
addWidgetViews(getDialogView());
}
}
});
final List<View> views = initiateViews();
if (isDetached()) {
return;
}
getJsonApi().initializeDependencyMaps();
getJsonApi().getAppExecutors().mainThread().execute(new Runnable() {
@Override
public void run() {
if (isDetached()) {
return;
}
setViewList(views);
getJsonApi().invokeRefreshLogic(null, true, null, null, getStepName(), false);
if (getDialogView() != null) {
addWidgetViews(getDialogView());
}
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import android.content.res.Resources;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
Expand All @@ -17,6 +15,9 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.activities.JsonFormActivity;
import com.vijay.jsonwizard.constants.JsonFormConstants;
Expand Down Expand Up @@ -286,11 +287,11 @@ public void onClick(View view) {
getJsonApi().setPreviousPressed(false);
Object nextStateTag = view.getTag(R.id.NEXT_STATE);
if (nextStateTag == null) {
new NextProgressDialogTask(getJsonFormFragment()).execute();
new NextProgressDialogTask(getJsonFormFragment()).init();
} else {
boolean next = (boolean) nextStateTag;
if (next) {
new NextProgressDialogTask(getJsonFormFragment()).execute();
new NextProgressDialogTask(getJsonFormFragment()).init();
} else {
save();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void onClick(View view) {
}

protected void initiateTask(View view) {
new ExpansionPanelGenericPopupDialogTask(view).execute();
new ExpansionPanelGenericPopupDialogTask(view).init();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ protected void nativeRadioButtonClickActions(View view) {
.equals(JsonFormConstants.DATE_PICKER)) {
NativeRadioButtonFactory.showDateDialog(view);
} else if (JsonFormConstants.CONTENT_INFO.equals(type) && !specifyWidget.equals(JsonFormConstants.DATE_PICKER)) {
new ExpansionPanelGenericPopupDialogTask(view).execute();
new ExpansionPanelGenericPopupDialogTask(view).init();
} else if (view.getId() == R.id.label_edit_button) {
setRadioViewsEditable(view);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package com.vijay.jsonwizard.task;

import static com.vijay.jsonwizard.constants.JsonFormConstants.CALCULATION;
import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS;
import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY;
import static com.vijay.jsonwizard.constants.JsonFormConstants.RELEVANCE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEPNAME;
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP_TITLE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MAX;
import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog;
import static com.vijay.jsonwizard.utils.Utils.showProgressDialog;

import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.text.SpannableString;
import android.text.style.StyleSpan;
import android.view.View;
Expand All @@ -20,6 +31,7 @@
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.FormWidgetFactory;
import com.vijay.jsonwizard.interfaces.JsonApi;
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.Utils;
import com.vijay.jsonwizard.utils.ValidationStatus;

Expand All @@ -38,19 +50,7 @@

import timber.log.Timber;

import static com.vijay.jsonwizard.constants.JsonFormConstants.CALCULATION;
import static com.vijay.jsonwizard.constants.JsonFormConstants.FIELDS;
import static com.vijay.jsonwizard.constants.JsonFormConstants.KEY;
import static com.vijay.jsonwizard.constants.JsonFormConstants.RELEVANCE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEPNAME;
import static com.vijay.jsonwizard.constants.JsonFormConstants.STEP_TITLE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.TYPE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.VALUE;
import static com.vijay.jsonwizard.constants.JsonFormConstants.V_RELATIVE_MAX;
import static com.vijay.jsonwizard.utils.Utils.hideProgressDialog;
import static com.vijay.jsonwizard.utils.Utils.showProgressDialog;

public class AttachRepeatingGroupTask extends AsyncTask<Void, Void, List<View>> {
public class AttachRepeatingGroupTask {

protected final int REPEATING_GROUP_LABEL_TEXT_COLOR = R.color.black;
private final ViewParent parent;
Expand All @@ -64,6 +64,7 @@ public class AttachRepeatingGroupTask extends AsyncTask<Void, Void, List<View>>
private Map<String, List<Map<String, Object>>> rulesFileMap = new HashMap<>();
private Map<Integer, String> repeatingGroupLayouts;
private int currNumRepeatingGroups;
private final AppExecutors appExecutors;

public AttachRepeatingGroupTask(final ViewParent parent, int numRepeatingGroups, Map<Integer, String> repeatingGroupLayouts, WidgetArgs widgetArgs, ImageButton doneButton) {
this.rootLayout = (LinearLayout) parent;
Expand All @@ -73,28 +74,34 @@ public AttachRepeatingGroupTask(final ViewParent parent, int numRepeatingGroups,
this.doneButton = doneButton;
this.repeatingGroupLayouts = repeatingGroupLayouts;
currNumRepeatingGroups = ((ViewGroup) parent).getChildCount() - 1;
appExecutors = new AppExecutors();
}

public void init() {
appExecutors.mainThread().execute(this::onPreExecute);
appExecutors.diskIO().execute(() -> {
this.updateRepeatingGrpInBackground();
appExecutors.mainThread().execute(this::onPostExecute);
});
onCancelled();

}

@Override

protected void onPreExecute() {
showProgressDialog(R.string.please_wait_title, R.string.creating_repeating_group_message, widgetArgs.getFormFragment().getContext());
}

@Override
protected List<View> doInBackground(Void... voids) {
protected List<View> updateRepeatingGrpInBackground() {
diff = numRepeatingGroups - currNumRepeatingGroups;
for (int i = 0; i < diff; i++) {
try {

repeatingGroups.add(buildRepeatingGroupLayout(parent));

} catch (Exception e) {
Timber.e(e);
}
}

updateRepeatingGrpCountObject();

return repeatingGroups;
}

Expand All @@ -109,11 +116,9 @@ private void updateRepeatingGrpCountObject() {
}
}

@Override
protected void onPostExecute(List<View> result) {
protected void onPostExecute() {
if (diff < 0) {
try {

JSONObject step = ((JsonApi) widgetArgs.getContext()).getmJSONObject().getJSONObject(widgetArgs.getStepName());
JSONArray fields = step.getJSONArray(FIELDS);
int currNumRepeatingGroups = rootLayout.getChildCount() - 1;
Expand Down Expand Up @@ -165,18 +170,15 @@ protected void onPostExecute(List<View> result) {

hideProgressDialog();
doneButton.setImageResource(R.drawable.ic_done_green);

//for validation
validationCleanUp();
}

private void validationCleanUp() {
doneButton.setTag(R.id.is_repeating_group_generated, true);

LinearLayout referenceLayout = (LinearLayout) ((LinearLayout) parent).getChildAt(0);
MaterialEditText materialEditText = (MaterialEditText) referenceLayout.getChildAt(0);
materialEditText.setError(null);

Map<String, ValidationStatus> validationStatusMap = widgetArgs.getFormFragment().getJsonApi().getInvalidFields();
if (validationStatusMap != null) {
String fieldKey = Utils.getFieldKeyPrefix(widgetArgs.getStepName(), widgetArgs.getFormFragment().getPresenter().getStepTitle()) + JsonFormConstants.REFERENCE_EDIT_TEXT;
Expand All @@ -186,20 +188,16 @@ private void validationCleanUp() {

private LinearLayout buildRepeatingGroupLayout(final ViewParent parent) throws Exception {
Context context = widgetArgs.getContext();

LinearLayout repeatingGroup = new LinearLayout(context);
repeatingGroup.setLayoutParams(WIDTH_MATCH_PARENT_HEIGHT_WRAP_CONTENT);
repeatingGroup.setOrientation(LinearLayout.VERTICAL);

LinearLayout rootLayout = (LinearLayout) ((LinearLayout) parent).getChildAt(0);
EditText referenceEditText = (EditText) rootLayout.getChildAt(0);
TextView repeatingGroupLabel = new TextView(context);
if (widgetArgs.getJsonObject().optBoolean(JsonFormConstants.RepeatingGroupFactory.SHOW_GROUP_LABEL, true)) {
formatRepeatingGroupLabelText(referenceEditText, repeatingGroupLabel, context);
}

repeatingGroup.addView(repeatingGroupLabel);

JSONArray repeatingGroupJson = new JSONArray(repeatingGroupLayouts.get(((LinearLayout) parent).getId()));
String groupUniqueId = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < repeatingGroupJson.length(); i++) {
Expand Down Expand Up @@ -255,7 +253,6 @@ protected void addUniqueIdentifiers(JSONObject element, String uniqueId) throws
}
}

@Override
protected void onCancelled() {
hideProgressDialog();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.vijay.jsonwizard.customviews.ExpansionPanelGenericPopupDialog;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.CommonListener;
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.FormUtils;
import com.vijay.jsonwizard.utils.Utils;
import com.vijay.jsonwizard.views.CustomTextView;
Expand All @@ -24,25 +25,28 @@
/**
* The {@link AsyncTask} to start and set the required variables on the {@link ExpansionPanelGenericPopupDialog}
*/
public class ExpansionPanelGenericPopupDialogTask extends AsyncTask<Void, Void, Void> {
public class ExpansionPanelGenericPopupDialogTask {
private FormUtils formUtils = new FormUtils();
private Utils utils = new Utils();
private View view;


public ExpansionPanelGenericPopupDialogTask(View view) {
this.view = view;

}

@Override
public void init(){
AppExecutors appExecutors = new AppExecutors();
appExecutors.mainThread().execute(this::onPreExecute);
appExecutors.diskIO().execute(this::processViewOnBackground);
}
protected void onPreExecute() {
super.onPreExecute();
Context context = (Context) view.getTag(R.id.specify_context);
Utils.showProgressDialog(R.string.loading, R.string.loading_form_message, context);
}

@Override
protected Void doInBackground(Void... voids) {
protected void processViewOnBackground(Void... voids) {
Context context = (Context) view.getTag(R.id.specify_context);
String specifyContent = (String) view.getTag(R.id.specify_content);
String specifyContentForm = (String) view.getTag(R.id.specify_content_form);
Expand Down Expand Up @@ -88,6 +92,5 @@ protected Void doInBackground(Void... voids) {
Toast.makeText(context, "Please specify the sub form to display ", Toast.LENGTH_LONG).show();
Timber.e("No sub form specified. Please specify one in order to use the expansion panel.");
}
return null;
}
}
Loading

0 comments on commit f9147fc

Please sign in to comment.