Skip to content

Commit

Permalink
Implement ClientFormDao, ClientFormDao.Model & JsonSubFormAndRulesLoa…
Browse files Browse the repository at this point in the history
…der from opensrp-client-utils
  • Loading branch information
ekigamba committed Jul 1, 2020
1 parent bebe821 commit 30fde2a
Show file tree
Hide file tree
Showing 12 changed files with 822 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.vijay.jsonwizard;

import android.support.annotation.NonNull;

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

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

@NonNull
public static final NativeFormLibrary getInstance() {
return null;
}

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

public ClientFormDao getClientFormDao() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
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 org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;

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 implements JsonSubFormAndRulesLoader {

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

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.JsonSubFormAndRulesLoader;

import java.io.BufferedReader;
import java.io.IOException;
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,6 +9,7 @@
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.smartregister.client.utils.contract.JsonSubFormAndRulesLoader;

import java.util.Collection;
import java.util.Map;
Expand Down
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.ClientFormDao;

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

void onFormSelected(@NonNull ClientFormDao.ClientFormModel selectedForm);

void onCancelClicked();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.vijay.jsonwizard.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import com.vijay.jsonwizard.R;
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 java.util.List;

/**
* Created by Ephraim Kigamba - [email protected] on 22-05-2020.
*/
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
, final @NonNull RollbackDialogCallback rollbackDialogCallback) {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
builderSingle.setIcon(R.drawable.ic_icon_danger);
builderSingle.setTitle(R.string.rollback_dialog_title);
int selectedItem = -1;
//builderSingle.setMessage("Due to an error on the current form, the form cannot be openned. Kindly select another rollback form to use for the time being.");

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(context, android.R.layout.select_dialog_singlechoice);

int counter = 0;
for (ClientFormDao.ClientFormModel clientForm : clientFormList) {
if (clientForm.getVersion().equals(currentClientForm.getVersion())) {
selectedItem = counter;
arrayAdapter.add("v" + clientForm.getVersion() + context.getString(R.string.current_corrupted_form));
} else {
arrayAdapter.add("v" + clientForm.getVersion());
}

counter++;
}

arrayAdapter.add(JsonFormConstants.CLIENT_FORM_ASSET_VERSION);

builderSingle.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
rollbackDialogCallback.onCancelClicked();
}
});

builderSingle.setSingleChoiceItems(arrayAdapter, selectedItem, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String formVersion = arrayAdapter.getItem(which);

if (formVersion != null) {
boolean wasClickHandled = selectForm(clientFormRepository, which, formVersion, context, clientFormList, currentClientForm, rollbackDialogCallback);

if (wasClickHandled) {
dialog.dismiss();
}
}
}
});
builderSingle.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (context instanceof JsonSubFormAndRulesLoader) {
((JsonSubFormAndRulesLoader) context).setVisibleFormErrorAndRollbackDialog(false);
}
}
});


if (context instanceof JsonSubFormAndRulesLoader) {
((JsonSubFormAndRulesLoader) 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) {
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;
if (formVersion.equals(JsonFormConstants.CLIENT_FORM_ASSET_VERSION)) {
selectedClientForm = clientFormRepository.createNewClientFormModel();
selectedClientForm.setVersion(JsonFormConstants.CLIENT_FORM_ASSET_VERSION);
} else {
if (pos >= clientFormList.size()) {
return false;
}

selectedClientForm = clientFormList.get(pos);

if (selectedClientForm == null) {
return false;
} else {
selectedClientForm.setActive(true);
clientFormRepository.addOrUpdate(selectedClientForm);
}
}

currentClientForm.setActive(false);
clientFormRepository.addOrUpdate(currentClientForm);
rollbackDialogCallback.onFormSelected(selectedClientForm);
return true;
}
}
}
7 changes: 7 additions & 0 deletions android-json-form-wizard/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,11 @@
<string name="x_years">%1$dy</string>
<string name="x_years_months">%1$dy %2$dm</string>

<string name="form_update_title">Form Update</string>
<string name="form_update_message">This form has content updates</string>
<string name="form_changed_reopen_to_take_effect">Form has been successfully selected! Reopen this form for the changes to take effect</string>
<string name="rollback_dialog_title">Select a rollback form</string>
<string name="current_corrupted_form">(Current Corrupted Form)</string>
<string name="cannot_select_corrupted_form_rollback">You cannot select this form because it\'s corrupted!</string>

</resources>
Loading

0 comments on commit 30fde2a

Please sign in to comment.