Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import client-utils 0.0.2-SNAPSHOT version & implement form config activity #438

Merged
merged 10 commits into from
Jul 2, 2020
3 changes: 3 additions & 0 deletions android-json-form-wizard/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}

implementation 'org.smartregister:opensrp-client-utils:0.0.2-SNAPSHOT'

implementation 'org.jeasy:easy-rules-core:3.3.0'
implementation 'org.jeasy:easy-rules-mvel:3.3.0'
implementation 'joda-time:joda-time:2.10.5'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.vijay.jsonwizard;

import android.support.annotation.NonNull;

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

/**
* Created by Ephraim Kigamba - [email protected] on 29-06-2020.
*/
public class NativeFormLibrary {

private ClientFormContract.Dao clientFormDao;
private static NativeFormLibrary instance;

@NonNull
public static NativeFormLibrary getInstance() {
if (instance == null) {
instance = new NativeFormLibrary();
}

return instance;
}

/*
public static final boolean init() {
return false;
}*/

public ClientFormContract.Dao getClientFormDao() {
return clientFormDao;
}

public void setClientFormDao(@NonNull ClientFormContract.Dao clientFormDao) {
this.clientFormDao = clientFormDao;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.vijay.jsonwizard.activities;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;

import com.vijay.jsonwizard.NativeFormLibrary;
import com.vijay.jsonwizard.R;
import com.vijay.jsonwizard.interfaces.OnFormFetchedCallback;
import com.vijay.jsonwizard.utils.AppExecutors;
import com.vijay.jsonwizard.utils.FormUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;

import timber.log.Timber;


/**
* Created by Ephraim Kigamba - [email protected] on 29-06-2020.
*/
public class FormConfigurationJsonFormActivity extends JsonFormActivity {

private FormUtils formUtils;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

formUtils = new FormUtils();
JSONObject jsonObject = getmJSONObject();
checkIfFormUpdate(jsonObject);
}

private void checkIfFormUpdate(@NonNull JSONObject formJsonObject) {
if (FormUtils.isFormNew(formJsonObject)) {
showFormVersionUpdateDialog(formJsonObject, getString(R.string.form_update_title), getString(R.string.form_update_message));
}
}

public void showFormVersionUpdateDialog(@NonNull JSONObject formJsonObject, @NonNull String title, @NonNull String message) {
final int clientId = FormUtils.getClientFormId(formJsonObject);
new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message)
.setCancelable(true)
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
negateIsNewClientForm(clientId);
dialog.dismiss();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
negateIsNewClientForm(clientId);
}
})
.show();
}

@VisibleForTesting
protected void negateIsNewClientForm(final int clientFormId) {
AppExecutors appExecutors = new AppExecutors();

appExecutors.diskIO()
.execute(new Runnable() {
@Override
public void run() {
NativeFormLibrary.getInstance().getClientFormDao()
.setIsNew(false, clientFormId);
}
});
}

@Nullable
@Override
public BufferedReader getRules(@NonNull Context context, @NonNull String fileName) throws IOException {
try {
BufferedReader bufferedReader = formUtils.getRulesFromRepository(context, NativeFormLibrary.getInstance().getClientFormDao(), fileName);
if (bufferedReader != null) {
return bufferedReader;
}
} catch (Exception e) {
Timber.e(e);
}

return super.getRules(context, fileName);
}

@Nullable
@Override
public JSONObject getSubForm(String formIdentity, String subFormsLocation, Context context, boolean translateSubForm) throws Exception {
JSONObject dbForm = null;
try {
dbForm = formUtils.getSubFormJsonFromRepository(context, NativeFormLibrary.getInstance().getClientFormDao(), formIdentity, subFormsLocation, translateSubForm);

} catch (JSONException ex) {
Timber.e(ex);
handleFormError(false, formIdentity);
return null;
}

if (dbForm == null) {
return super.getSubForm(formIdentity, subFormsLocation, context, translateSubForm);
}

return dbForm;
}

@Override
public void handleFormError(boolean isRulesFile, @NonNull String formIdentifier) {
formUtils.handleJsonFormOrRulesError(this, NativeFormLibrary.getInstance().getClientFormDao(), isRulesFile, formIdentifier, new OnFormFetchedCallback<String>() {
@Override
public void onFormFetched(@Nullable String form) {
if (form != null) {
Toast.makeText(FormConfigurationJsonFormActivity.this, R.string.form_changed_reopen_to_take_effect, Toast.LENGTH_LONG)
.show();
}

FormConfigurationJsonFormActivity.this.finish();
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.vijay.jsonwizard.constants.JsonFormConstants;
import com.vijay.jsonwizard.domain.Form;
import com.vijay.jsonwizard.fragments.JsonFormFragment;
import com.vijay.jsonwizard.interfaces.JsonSubFormAndRulesLoader;
import com.vijay.jsonwizard.interfaces.LifeCycleListener;
import com.vijay.jsonwizard.interfaces.OnActivityRequestPermissionResultListener;
import com.vijay.jsonwizard.interfaces.OnActivityResultListener;
Expand All @@ -28,6 +27,7 @@

import org.json.JSONException;
import org.json.JSONObject;
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 @@ -221,6 +221,9 @@ public class JsonFormConstants {
public static final String FORM_NAME = "form_name";
public static final String FORM_CONFIG_LOCATION = "json.form/json.form.config.json";

public static final String JSON_FILE_EXTENSION = ".json";
public static final String CLIENT_FORM_ASSET_VERSION = "base version";

public interface MultiSelectUtils {
String IS_HEADER = "isHeader";
String TEXT = "text";
Expand All @@ -244,6 +247,9 @@ public static class Properties {
public static final String APP_VERSION_NAME = "appVersionName";
public static final String APP_FORM_VERSION = "formVersion";
public static final String DETAILS = "details";
public static final String IS_NEW = "is_new";
public static final String CLIENT_FORM_ID = "client_form_id";
public static final String FORM_VERSION = "form_version";
}

public static class JsonFormConstantsUtils {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.ClientFormContract;

import java.util.Collection;
import java.util.Map;

/**
* 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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.vijay.jsonwizard.interfaces;

import android.support.annotation.Nullable;

/**
* Created by Ephraim Kigamba - [email protected] on 21-05-2020.
*/
public interface OnFormFetchedCallback<T> {

void onFormFetched(@Nullable T form);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.vijay.jsonwizard.interfaces;

import android.support.annotation.NonNull;

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

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

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
Loading