Search in sources :

Example 16 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class FormEntryActivity method createViewForFormEnd.

/**
 * Creates the final screen in a form-filling interaction. Allows the user to set a display
 * name for the instance and to decide whether the form should be finalized or not. Presents
 * a button for saving and exiting.
 */
private View createViewForFormEnd(FormController formController) {
    if (formController.getSubmissionMetadata().instanceName != null) {
        saveName = formController.getSubmissionMetadata().instanceName;
    } else {
        // no meta/instanceName field in the form -- see if we have a
        // name for this instance from a previous save attempt...
        String uriMimeType = null;
        Uri instanceUri = getIntent().getData();
        if (instanceUri != null) {
            uriMimeType = getContentResolver().getType(instanceUri);
        }
        if (saveName == null && uriMimeType != null && uriMimeType.equals(InstancesContract.CONTENT_ITEM_TYPE)) {
            Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().get(ContentUriHelper.getIdFromUri(instanceUri));
            if (instance != null) {
                saveName = instance.getDisplayName();
            }
        }
        if (saveName == null) {
            saveName = formSaveViewModel.getFormName();
        }
    }
    FormEndView endView = new FormEndView(this, formSaveViewModel.getFormName(), saveName, InstancesDaoHelper.isInstanceComplete(true, settingsProvider.getUnprotectedSettings().getBoolean(KEY_COMPLETED_DEFAULT)), new FormEndView.Listener() {

        @Override
        public void onSaveAsChanged(String saveAs) {
            // Seems like this is needed for rotation?
            saveName = saveAs;
        }

        @Override
        public void onSaveClicked(boolean markAsFinalized) {
            if (saveName.length() < 1) {
                showShortToast(FormEntryActivity.this, R.string.save_as_error);
            } else {
                formSaveViewModel.saveForm(getIntent().getData(), markAsFinalized, saveName, true);
            }
        }
    });
    if (!settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_MARK_AS_FINALIZED)) {
        endView.findViewById(R.id.mark_finished).setVisibility(View.GONE);
    }
    if (formController.getSubmissionMetadata().instanceName != null) {
        // if instanceName is defined in form, this is the name -- no
        // revisions
        // display only the name, not the prompt, and disable edits
        endView.findViewById(R.id.save_form_as).setVisibility(View.GONE);
        endView.findViewById(R.id.save_name).setEnabled(false);
        endView.findViewById(R.id.save_name).setVisibility(View.VISIBLE);
    }
    // override the visibility settings based upon admin preferences
    if (!settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_SAVE_AS)) {
        endView.findViewById(R.id.save_form_as).setVisibility(View.GONE);
        endView.findViewById(R.id.save_name).setVisibility(View.GONE);
    }
    if (showNavigationButtons) {
        updateNavigationButtonVisibility();
    }
    return endView;
}
Also used : InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) Instance(org.odk.collect.forms.instances.Instance) FormEndView(org.odk.collect.android.formentry.FormEndView) Uri(android.net.Uri)

Example 17 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesRepositoryTest method getAllByFormIdAndVersionNotDeleted_excludesDeleted.

@Test
public void getAllByFormIdAndVersionNotDeleted_excludesDeleted() {
    InstancesRepository instancesRepository = buildSubject();
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", "display", Instance.STATUS_COMPLETE, null, getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid", "1", "display", Instance.STATUS_COMPLETE, System.currentTimeMillis(), getInstancesDir()).build());
    instancesRepository.save(InstanceUtils.buildInstance("formid2", "1", "display", Instance.STATUS_COMPLETE, null, getInstancesDir()).build());
    List<Instance> instances = instancesRepository.getAllNotDeletedByFormIdAndVersion("formid", "1");
    assertThat(instances.size(), is(3));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 18 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesRepositoryTest method save_whenStatusIsNull_usesIncomplete.

@Test
public void save_whenStatusIsNull_usesIncomplete() {
    InstancesRepository instancesRepository = buildSubject();
    Instance instance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).status(null).build());
    assertThat(instancesRepository.get(instance.getDbId()).getStatus(), is(Instance.STATUS_INCOMPLETE));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 19 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InstancesRepositoryTest method save_whenInstanceHasId_updatesExisting.

@Test
public void save_whenInstanceHasId_updatesExisting() {
    InstancesRepository instancesRepository = buildSubject();
    Instance originalInstance = instancesRepository.save(InstanceUtils.buildInstance("formid", "1", getInstancesDir()).displayName("Blah").build());
    instancesRepository.save(new Instance.Builder(originalInstance).displayName("A different blah").build());
    assertThat(instancesRepository.get(originalInstance.getDbId()).getDisplayName(), is("A different blah"));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InstancesRepository(org.odk.collect.forms.instances.InstancesRepository) Test(org.junit.Test)

Example 20 with Instance

use of org.odk.collect.forms.instances.Instance in project collect by opendatakit.

the class InMemInstancesRepository method delete.

@Override
public void delete(Long id) {
    Instance instance = get(id);
    deleteInstanceFiles(instance);
    instances.remove(instance);
}
Also used : Instance(org.odk.collect.forms.instances.Instance)

Aggregations

Instance (org.odk.collect.forms.instances.Instance)62 Test (org.junit.Test)28 InstancesRepository (org.odk.collect.forms.instances.InstancesRepository)23 InstancesRepositoryProvider (org.odk.collect.android.utilities.InstancesRepositoryProvider)10 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)9 File (java.io.File)7 DatabaseObjectMapper.getValuesFromInstance (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromInstance)7 Uri (android.net.Uri)6 Form (org.odk.collect.forms.Form)6 InstanceUtils.buildInstance (org.odk.collect.formstest.InstanceUtils.buildInstance)6 Pair (android.util.Pair)5 ArrayList (java.util.ArrayList)5 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)5 Date (java.util.Date)4 FormController (org.odk.collect.android.javarosawrapper.FormController)4 MapPoint (org.odk.collect.geo.maps.MapPoint)4 ContentValues (android.content.ContentValues)3 Intent (android.content.Intent)3 Cursor (android.database.Cursor)3 AsyncTask (android.os.AsyncTask)2