-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ClientFormDao, ClientFormDao.Model & JsonSubFormAndRulesLoa…
…der from opensrp-client-utils
- Loading branch information
Showing
12 changed files
with
822 additions
and
1 deletion.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
android-json-form-wizard/src/main/java/com/vijay/jsonwizard/NativeFormLibrary.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...zard/src/main/java/com/vijay/jsonwizard/activities/FormConfigurationJsonFormActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...json-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/OnFormFetchedCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
15 changes: 15 additions & 0 deletions
15
...son-form-wizard/src/main/java/com/vijay/jsonwizard/interfaces/RollbackDialogCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
121 changes: 121 additions & 0 deletions
121
...oid-json-form-wizard/src/main/java/com/vijay/jsonwizard/utils/FormRollbackDialogUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.