Search in sources :

Example 11 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormEntryActivity method createView.

/**
 * Creates and returns a new view based on the event type passed in. The view returned is
 * of type {@link View} if the event passed in represents the end of the form or of type
 * {@link ODKView} otherwise.
 *
 * @param advancingPage -- true if this results from advancing through the form
 * @return newly created View
 */
private View createView(int event, boolean advancingPage) {
    releaseOdkView();
    FormController formController = getFormController();
    String formTitle = formController.getFormTitle();
    setTitle(formTitle);
    if (event != FormEntryController.EVENT_QUESTION) {
        formController.getAuditEventLogger().logEvent(AuditEvent.getAuditEventTypeFromFecType(event), formController.getFormIndex(), true, null, System.currentTimeMillis(), null);
    }
    switch(event) {
        case FormEntryController.EVENT_BEGINNING_OF_FORM:
            return createViewForFormBeginning(formController);
        case FormEntryController.EVENT_END_OF_FORM:
            return createViewForFormEnd(formController);
        case FormEntryController.EVENT_QUESTION:
        case FormEntryController.EVENT_GROUP:
        case FormEntryController.EVENT_REPEAT:
            // should only be a group here if the event_group is a field-list
            try {
                AuditUtils.logCurrentScreen(formController, formController.getAuditEventLogger(), System.currentTimeMillis());
                FormEntryCaption[] groups = formController.getGroupsForCurrentIndex();
                FormEntryPrompt[] prompts = formController.getQuestionPrompts();
                odkView = createODKView(advancingPage, prompts, groups);
                odkView.setWidgetValueChangedListener(this);
                Timber.i("Created view for group %s %s", groups.length > 0 ? groups[groups.length - 1].getLongText() : "[top]", prompts.length > 0 ? prompts[0].getQuestionText() : "[no question]");
            } catch (RuntimeException | RepeatsInFieldListException e) {
                if (e instanceof RuntimeException) {
                    Timber.e(e);
                }
                // this is badness to avoid a crash.
                try {
                    event = formController.stepToNextScreenEvent();
                    createErrorDialog(e.getMessage(), false);
                } catch (JavaRosaException e1) {
                    Timber.d(e1);
                    createErrorDialog(e.getMessage() + "\n\n" + e1.getCause().getMessage(), false);
                }
                return createView(event, advancingPage);
            }
            if (showNavigationButtons) {
                updateNavigationButtonVisibility();
            }
            return odkView;
        case EVENT_PROMPT_NEW_REPEAT:
            createRepeatDialog();
            return new EmptyView(this);
        default:
            Timber.e("Attempted to create a view that does not exist.");
            // this is badness to avoid a crash.
            try {
                event = formController.stepToNextScreenEvent();
                createErrorDialog(getString(R.string.survey_internal_error), true);
            } catch (JavaRosaException e) {
                Timber.d(e);
                createErrorDialog(e.getCause().getMessage(), true);
            }
            return createView(event, advancingPage);
    }
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) RepeatsInFieldListException(org.odk.collect.android.javarosawrapper.RepeatsInFieldListException) FormEntryCaption(org.javarosa.form.api.FormEntryCaption) JavaRosaException(org.odk.collect.android.exception.JavaRosaException)

Example 12 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormEntryActivity method onPause.

@Override
protected void onPause() {
    backgroundLocationViewModel.activityHidden();
    FormController formController = getFormController();
    // is getting constantly updated
    if (!formSaveViewModel.isSaving()) {
        if (currentView != null && formController != null && formController.currentPromptIsQuestion()) {
            saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS);
        }
    }
    super.onPause();
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController)

Example 13 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormHierarchyActivity method isGroupSizeLocked.

/**
 * Returns true if the current index is a group that's designated as `noAddRemove`
 * (e.g. if `jr:count` is explicitly set).
 */
private boolean isGroupSizeLocked(FormIndex index) {
    FormController formController = Collect.getInstance().getFormController();
    IFormElement element = formController.getCaptionPrompt(index).getFormElement();
    return element instanceof GroupDef && ((GroupDef) element).noAddRemove;
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController) IFormElement(org.javarosa.core.model.IFormElement) GroupDef(org.javarosa.core.model.GroupDef)

Example 14 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormHierarchyActivity method updateOptionsMenu.

private void updateOptionsMenu() {
    FormController formController = Collect.getInstance().getFormController();
    // Not ready yet. Menu will be updated automatically once it's been prepared.
    if (optionsMenu == null || formController == null) {
        return;
    }
    boolean isAtBeginning = screenIndex.isBeginningOfFormIndex() && !shouldShowRepeatGroupPicker();
    boolean shouldShowPicker = shouldShowRepeatGroupPicker();
    boolean isInRepeat = formController.indexContainsRepeatableGroup();
    boolean isGroupSizeLocked = shouldShowPicker ? isGroupSizeLocked(repeatGroupPickerIndex) : isGroupSizeLocked(screenIndex);
    boolean shouldShowDelete = isInRepeat && !shouldShowPicker && !isGroupSizeLocked;
    showDeleteButton(shouldShowDelete);
    boolean shouldShowAdd = shouldShowPicker && !isGroupSizeLocked;
    showAddButton(shouldShowAdd);
    boolean shouldShowGoUp = !isAtBeginning;
    showGoUpButton(shouldShowGoUp);
}
Also used : FormController(org.odk.collect.android.javarosawrapper.FormController)

Example 15 with FormController

use of org.odk.collect.android.javarosawrapper.FormController in project collect by opendatakit.

the class FormHierarchyActivity method createErrorDialog.

/**
 * Creates and displays dialog with the given errorMsg.
 */
protected void createErrorDialog(String errorMsg) {
    AlertDialog alertDialog = new MaterialAlertDialogBuilder(this).create();
    alertDialog.setTitle(getString(R.string.error_occured));
    alertDialog.setMessage(errorMsg);
    DialogInterface.OnClickListener errorListener = new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int i) {
            switch(i) {
                case DialogInterface.BUTTON_POSITIVE:
                    FormController formController = Collect.getInstance().getFormController();
                    formController.jumpToIndex(currentIndex);
                    break;
            }
        }
    };
    alertDialog.setCancelable(false);
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.ok), errorListener);
    alertDialog.show();
}
Also used : AlertDialog(androidx.appcompat.app.AlertDialog) FormController(org.odk.collect.android.javarosawrapper.FormController) DialogInterface(android.content.DialogInterface) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder)

Aggregations

FormController (org.odk.collect.android.javarosawrapper.FormController)53 FormIndex (org.javarosa.core.model.FormIndex)11 File (java.io.File)9 JavaRosaException (org.odk.collect.android.exception.JavaRosaException)9 FormDef (org.javarosa.core.model.FormDef)4 FormEntryPrompt (org.javarosa.form.api.FormEntryPrompt)4 Collect (org.odk.collect.android.application.Collect)4 RepeatsInFieldListException (org.odk.collect.android.javarosawrapper.RepeatsInFieldListException)4 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)4 TextView (android.widget.TextView)3 ArrayList (java.util.ArrayList)3 FormInstance (org.javarosa.core.model.instance.FormInstance)3 TreeReference (org.javarosa.core.model.instance.TreeReference)3 FormEntryCaption (org.javarosa.form.api.FormEntryCaption)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Form (org.odk.collect.forms.Form)3 Intent (android.content.Intent)2 View (android.view.View)2 WebView (android.webkit.WebView)2