Skip to content

Commit

Permalink
Update code to use ClientFormContract interfaces from client-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ekigamba committed Jul 1, 2020
1 parent 5cf3a28 commit 8b37ba3
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.support.annotation.NonNull;

import org.smartregister.client.utils.contract.ClientFormDao;
import org.smartregister.client.utils.contract.ClientFormContract;

/**
* Created by Ephraim Kigamba - [email protected] on 29-06-2020.
Expand All @@ -18,7 +18,7 @@ public static final boolean init() {
return false;
}

public ClientFormDao getClientFormDao() {
public ClientFormContract.Dao getClientFormDao() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.ClientFormContract;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -29,7 +27,7 @@
/**
* Created by Ephraim Kigamba - [email protected] on 29-06-2020.
*/
public class FormConfigurationJsonFormActivity extends JsonFormActivity implements ClientFormContract.View {
public class FormConfigurationJsonFormActivity extends JsonFormActivity {

private FormUtils formUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;
import org.smartregister.client.utils.contract.ClientFormContract;

import java.io.BufferedReader;
import java.io.IOException;
Expand All @@ -44,7 +44,7 @@

import static com.vijay.jsonwizard.utils.NativeFormLangUtils.getTranslatedString;

abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnFieldsInvalid, JsonSubFormAndRulesLoader {
abstract class JsonFormBaseActivity extends MultiLanguageActivity implements OnFieldsInvalid, ClientFormContract.View {
protected static final String TAG = JsonFormActivity.class.getSimpleName();
protected static final String JSON_STATE = "jsonState";
protected static final String FORM_STATE = "formState";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.ClientFormContract;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;

import java.util.Collection;
Expand All @@ -17,7 +18,7 @@
/**
* Created by vijay on 5/16/15.
*/
public interface JsonApi extends JsonSubFormAndRulesLoader {
public interface JsonApi extends ClientFormContract.View {
JSONObject getStep(String stepName);

void writeValue(String stepName, String key, String value, String openMrsEntityParent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import android.support.annotation.NonNull;

import org.smartregister.client.utils.contract.ClientFormDao;
import org.smartregister.client.utils.contract.ClientFormContract;

/**
* Created by Ephraim Kigamba - [email protected] on 21-05-2020.
*/
public interface RollbackDialogCallback {

void onFormSelected(@NonNull ClientFormDao.ClientFormModel selectedForm);
void onFormSelected(@NonNull ClientFormContract.Model selectedForm);

void onCancelClicked();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import com.google.gson.Gson;
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.interfaces.JsonSubFormAndRulesLoader;
import com.vijay.jsonwizard.utils.Utils;

import org.jeasy.rules.api.Facts;
Expand All @@ -19,6 +18,7 @@
import org.jeasy.rules.mvel.MVELRuleFactory;
import org.json.JSONArray;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.ClientFormContract;

import java.io.BufferedReader;
import java.io.IOException;
Expand Down Expand Up @@ -144,8 +144,8 @@ private Rules getRulesFromAsset(String fileName) {
if (!ruleMap.containsKey(fileName)) {
BufferedReader bufferedReader;
boolean loadedFromDb = false;
if (context instanceof JsonSubFormAndRulesLoader) {
bufferedReader = ((JsonSubFormAndRulesLoader) context).getRules(context, fileName);
if (context instanceof ClientFormContract.View) {
bufferedReader = ((ClientFormContract.View) context).getRules(context, fileName);
loadedFromDb = true;
} else {
bufferedReader = new BufferedReader(new InputStreamReader(context.getAssets().open(fileName)));
Expand All @@ -158,7 +158,7 @@ private Rules getRulesFromAsset(String fileName) {
Timber.e(ex);

if (loadedFromDb) {
((JsonSubFormAndRulesLoader) context).handleFormError(true, fileName);
((ClientFormContract.View) context).handleFormError(true, fileName);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.interfaces.RollbackDialogCallback;

import org.smartregister.client.utils.contract.ClientFormDao;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;
import org.smartregister.client.utils.contract.ClientFormContract;

import java.util.List;

Expand All @@ -23,9 +22,9 @@
public class FormRollbackDialogUtil {

public static AlertDialog showAvailableRollbackFormsDialog(@NonNull final Context context
, @NonNull final ClientFormDao clientFormRepository
, @NonNull final List<ClientFormDao.ClientFormModel> clientFormList
, @NonNull final ClientFormDao.ClientFormModel currentClientForm
, @NonNull final ClientFormContract.Dao clientFormRepository
, @NonNull final List<ClientFormContract.Model> clientFormList
, @NonNull final ClientFormContract.Model currentClientForm
, final @NonNull RollbackDialogCallback rollbackDialogCallback) {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
builderSingle.setIcon(R.drawable.ic_icon_danger);
Expand All @@ -36,7 +35,7 @@ public static AlertDialog showAvailableRollbackFormsDialog(@NonNull final Contex
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.select_dialog_singlechoice);

int counter = 0;
for (ClientFormDao.ClientFormModel clientForm : clientFormList) {
for (ClientFormContract.Model clientForm : clientFormList) {
if (clientForm.getVersion().equals(currentClientForm.getVersion())) {
selectedItem = counter;
arrayAdapter.add("v" + clientForm.getVersion() + context.getString(R.string.current_corrupted_form));
Expand Down Expand Up @@ -74,26 +73,26 @@ public void onClick(DialogInterface dialog, int which) {
builderSingle.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (context instanceof JsonSubFormAndRulesLoader) {
((JsonSubFormAndRulesLoader) context).setVisibleFormErrorAndRollbackDialog(false);
if (context instanceof ClientFormContract.View) {
((ClientFormContract.View) context).setVisibleFormErrorAndRollbackDialog(false);
}
}
});


if (context instanceof JsonSubFormAndRulesLoader) {
((JsonSubFormAndRulesLoader) context).setVisibleFormErrorAndRollbackDialog(true);
if (context instanceof ClientFormContract.View) {
((ClientFormContract.View) context).setVisibleFormErrorAndRollbackDialog(true);
}
return builderSingle.show();
}

@VisibleForTesting
protected static boolean selectForm(@NonNull ClientFormDao clientFormRepository, int pos, @NonNull String formVersion, @NonNull Context context, @NonNull List<ClientFormDao.ClientFormModel> clientFormList, @NonNull ClientFormDao.ClientFormModel currentClientForm, @NonNull RollbackDialogCallback rollbackDialogCallback) {
protected static boolean selectForm(@NonNull ClientFormContract.Dao clientFormRepository, int pos, @NonNull String formVersion, @NonNull Context context, @NonNull List<ClientFormContract.Model> clientFormList, @NonNull ClientFormContract.Model currentClientForm, @NonNull RollbackDialogCallback rollbackDialogCallback) {
if (formVersion.contains(context.getString(R.string.current_corrupted_form))) {
Toast.makeText(context, R.string.cannot_select_corrupted_form_rollback, Toast.LENGTH_LONG).show();
return false;
} else {
ClientFormDao.ClientFormModel selectedClientForm;
ClientFormContract.Model selectedClientForm;
if (formVersion.equals(JsonFormConstants.CLIENT_FORM_ASSET_VERSION)) {
selectedClientForm = clientFormRepository.createNewClientFormModel();
selectedClientForm.setVersion(JsonFormConstants.CLIENT_FORM_ASSET_VERSION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;
import org.smartregister.client.utils.contract.ClientFormContract;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
Expand Down Expand Up @@ -1784,16 +1783,16 @@ public String setValues(JSONArray jsonArray, String type) {
}

@Nullable
public JSONObject getFormJsonFromRepositoryOrAssets(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, @NonNull String formIdentity) {
public JSONObject getFormJsonFromRepositoryOrAssets(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, @NonNull String formIdentity) {
return getFormJsonFromRepositoryOrAssetsWithOptionalCallback(context, clientFormRepository, formIdentity, null);
}

public void getFormJsonFromRepositoryOrAssets(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, @NonNull String formIdentity, @Nullable OnFormFetchedCallback<JSONObject> onFormFetchedCallback) {
public void getFormJsonFromRepositoryOrAssets(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, @NonNull String formIdentity, @Nullable OnFormFetchedCallback<JSONObject> onFormFetchedCallback) {
getFormJsonFromRepositoryOrAssetsWithOptionalCallback(context, clientFormRepository, formIdentity, onFormFetchedCallback);
}

private JSONObject getFormJsonFromRepositoryOrAssetsWithOptionalCallback(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, String formIdentity, @Nullable final OnFormFetchedCallback<JSONObject> onFormFetchedCallback) {
ClientFormDao.ClientFormModel clientForm = getClientFormFromRepository(context, clientFormRepository, formIdentity);
private JSONObject getFormJsonFromRepositoryOrAssetsWithOptionalCallback(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, String formIdentity, @Nullable final OnFormFetchedCallback<JSONObject> onFormFetchedCallback) {
ClientFormContract.Model clientForm = getClientFormFromRepository(context, clientFormRepository, formIdentity);

try {
if (clientForm != null) {
Expand Down Expand Up @@ -1840,7 +1839,7 @@ public void onFormFetched(@Nullable String form) {
}
}

public JSONObject getFormJson(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, String formIdentity) {
public JSONObject getFormJson(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, String formIdentity) {
if (context != null) {
try {
String locale = context.getResources().getConfiguration().locale.getLanguage();
Expand Down Expand Up @@ -1873,15 +1872,15 @@ public JSONObject getFormJson(@NonNull Context context, @NonNull ClientFormDao c
return null;
}

private ClientFormDao.ClientFormModel getClientFormFromRepository(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, String formIdentity) {
private ClientFormContract.Model getClientFormFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, String formIdentity) {
//Check the current locale of the app to load the correct version of the form in the desired language
String locale = context.getResources().getConfiguration().locale.getLanguage();
String localeFormIdentity = formIdentity;
if (!Locale.ENGLISH.getLanguage().equals(locale)) {
localeFormIdentity = localeFormIdentity + "-" + locale;
}

ClientFormDao.ClientFormModel clientForm = clientFormRepository.getActiveClientFormByIdentifier(localeFormIdentity);
ClientFormContract.Model clientForm = clientFormRepository.getActiveClientFormByIdentifier(localeFormIdentity);

if (clientForm == null) {
String revisedFormName = extractFormNameWithoutExtension(localeFormIdentity);
Expand All @@ -1890,25 +1889,25 @@ private ClientFormDao.ClientFormModel getClientFormFromRepository(@NonNull Conte
return clientForm;
}

public void handleJsonFormOrRulesError(@NonNull Context context, @NonNull ClientFormDao clientFormRepository, @NonNull String formIdentity, @NonNull OnFormFetchedCallback<String> onFormFetchedCallback) {
public void handleJsonFormOrRulesError(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormRepository, @NonNull String formIdentity, @NonNull OnFormFetchedCallback<String> onFormFetchedCallback) {
handleJsonFormOrRulesError(context, clientFormRepository, false, formIdentity, onFormFetchedCallback);
}

public void handleJsonFormOrRulesError(@NonNull final Context context, @NonNull final ClientFormDao clientFormRepository, final boolean isRulesFile, @NonNull final String formIdentity, @NonNull final OnFormFetchedCallback<String> onFormFetchedCallback) {
final ClientFormDao.ClientFormModel clientForm = getClientFormFromRepository(context, clientFormRepository, formIdentity);
List<ClientFormDao.ClientFormModel> clientForms = clientFormRepository.getClientFormByIdentifier(clientForm.getIdentifier());
public void handleJsonFormOrRulesError(@NonNull final Context context, @NonNull final ClientFormContract.Dao clientFormRepository, final boolean isRulesFile, @NonNull final String formIdentity, @NonNull final OnFormFetchedCallback<String> onFormFetchedCallback) {
final ClientFormContract.Model clientForm = getClientFormFromRepository(context, clientFormRepository, formIdentity);
List<ClientFormContract.Model > clientForms = clientFormRepository.getClientFormByIdentifier(clientForm.getIdentifier());

if (clientForms.size() > 0) {
// Show dialog asking user if they want to rollback to the previous available version X
// if YES, then provide that form instead
// if NO, then continue down

boolean dialogIsShowing = (context instanceof JsonSubFormAndRulesLoader) && ((JsonSubFormAndRulesLoader) context).isVisibleFormErrorAndRollbackDialog();
boolean dialogIsShowing = (context instanceof ClientFormContract.View) && ((ClientFormContract.View) context).isVisibleFormErrorAndRollbackDialog();

if (!dialogIsShowing) {
FormRollbackDialogUtil.showAvailableRollbackFormsDialog(context, clientFormRepository, clientForms, clientForm, new RollbackDialogCallback() {
@Override
public void onFormSelected(@NonNull ClientFormDao.ClientFormModel selectedForm) {
public void onFormSelected(@NonNull ClientFormContract.Model selectedForm) {
if (selectedForm.getJson() == null && selectedForm.getVersion().equals(JsonFormConstants.CLIENT_FORM_ASSET_VERSION)) {

if (isRulesFile) {
Expand Down Expand Up @@ -1939,7 +1938,7 @@ public void onCancelClicked() {
}

@Nullable
public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNull ClientFormDao clientFormDao, String formIdentity, String subFormsLocation, boolean translateSubForm) throws JSONException {
public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormDao, String formIdentity, String subFormsLocation, boolean translateSubForm) throws JSONException {
String locale = context.getResources().getConfiguration().locale.getLanguage();

//Check the current locale of the app to load the correct version of the form in the desired language
Expand All @@ -1949,7 +1948,7 @@ public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNul
}

String dbFormName = StringUtils.isBlank(subFormsLocation) ? localeFormIdentity : subFormsLocation + "/" + localeFormIdentity;
ClientFormDao.ClientFormModel clientForm = clientFormDao.getActiveClientFormByIdentifier(dbFormName);
ClientFormContract.Model clientForm = clientFormDao.getActiveClientFormByIdentifier(dbFormName);

if (clientForm == null) {
String revisedFormName = extractFormNameWithoutExtension(dbFormName);
Expand Down Expand Up @@ -1978,7 +1977,7 @@ public JSONObject getSubFormJsonFromRepository(@NonNull Context context, @NonNul
}

@Nullable
public BufferedReader getRulesFromRepository(@NonNull Context context, @NonNull ClientFormDao clientFormDao, @NonNull String fileName) {
public BufferedReader getRulesFromRepository(@NonNull Context context, @NonNull ClientFormContract.Dao clientFormDao, @NonNull String fileName) {
String locale = context.getResources().getConfiguration().locale.getLanguage();

//Check the current locale of the app to load the correct version of the form in the desired language
Expand All @@ -1987,7 +1986,7 @@ public BufferedReader getRulesFromRepository(@NonNull Context context, @NonNull
localeFormIdentity = localeFormIdentity + "-" + locale;
}

ClientFormDao.ClientFormModel clientForm = clientFormDao.getActiveClientFormByIdentifier(localeFormIdentity);
ClientFormContract.Model clientForm = clientFormDao.getActiveClientFormByIdentifier(localeFormIdentity);
if (clientForm != null) {
Timber.d("============%s form loaded from db============", localeFormIdentity);
String originalJson = clientForm.getJson();
Expand All @@ -2005,7 +2004,7 @@ protected String extractFormNameWithoutExtension(String localeFormIdentity) {
localeFormIdentity + JsonFormConstants.JSON_FILE_EXTENSION;
}

public void injectFormStatus(@NonNull JSONObject jsonObject, @NonNull ClientFormDao.ClientFormModel clientForm) {
public void injectFormStatus(@NonNull JSONObject jsonObject, @NonNull ClientFormContract.Model clientForm) {
if (clientForm.isNew()) {
try {
jsonObject.put(JsonFormConstants.Properties.IS_NEW, clientForm.isNew());
Expand Down

0 comments on commit 8b37ba3

Please sign in to comment.