Search in sources :

Example 1 with Instance

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

the class FormDeleter method delete.

public void delete(Long id) {
    Form form = formsRepository.get(id);
    List<Instance> instancesForVersion = instancesRepository.getAllNotDeletedByFormIdAndVersion(form.getFormId(), form.getVersion());
    // If there's more than one form with the same formid/version, trust the user that they want to truly delete this one
    // because otherwise it may not ever be removed (instance deletion only deletes one corresponding form).
    List<Form> formsWithSameFormIdVersion = formsRepository.getAllByFormIdAndVersion(form.getFormId(), form.getVersion());
    if (instancesForVersion.isEmpty() || formsWithSameFormIdVersion.size() > 1) {
        formsRepository.delete(id);
    } else {
        formsRepository.softDelete(form.getDbId());
    }
}
Also used : Form(org.odk.collect.forms.Form) Instance(org.odk.collect.forms.instances.Instance)

Example 2 with Instance

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

the class QuitFormDialogFragment method onCreateDialog.

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
    super.onCreateDialog(savedInstanceState);
    String title = formSaveViewModel.getFormName() == null ? getActivity().getString(R.string.no_form_loaded) : formSaveViewModel.getFormName();
    List<IconMenuItem> items;
    if (settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_SAVE_MID)) {
        items = ImmutableList.of(new IconMenuItem(R.drawable.ic_save, R.string.keep_changes), new IconMenuItem(R.drawable.ic_delete, R.string.do_not_save));
    } else {
        items = ImmutableList.of(new IconMenuItem(R.drawable.ic_delete, R.string.do_not_save));
    }
    ListView listView = DialogUtils.createActionListView(getActivity());
    final IconMenuListAdapter adapter = new IconMenuListAdapter(getActivity(), items);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener((parent, view, position, id) -> {
        IconMenuItem item = (IconMenuItem) adapter.getItem(position);
        if (item.getTextResId() == R.string.keep_changes) {
            if (listener != null) {
                listener.onSaveChangesClicked();
            }
        } else {
            formSaveViewModel.ignoreChanges();
            String action = getActivity().getIntent().getAction();
            if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_EDIT.equals(action)) {
                // caller is waiting on a picked form
                Uri uri = null;
                String path = formSaveViewModel.getAbsoluteInstancePath();
                if (path != null) {
                    Instance instance = new InstancesRepositoryProvider(requireContext()).get().getOneByPath(path);
                    if (instance != null) {
                        uri = InstancesContract.getUri(currentProjectProvider.getCurrentProject().getUuid(), instance.getDbId());
                    }
                }
                if (uri != null) {
                    getActivity().setResult(RESULT_OK, new Intent().setData(uri));
                }
            }
            getActivity().finish();
        }
        if (getDialog() != null) {
            getDialog().dismiss();
        }
    });
    return new MaterialAlertDialogBuilder(getActivity()).setTitle(getString(R.string.quit_application, title)).setNegativeButton(getActivity().getString(R.string.do_not_exit), (dialog, id) -> {
        dialog.cancel();
        dismiss();
    }).setView(listView).create();
}
Also used : InstancesRepositoryProvider(org.odk.collect.android.utilities.InstancesRepositoryProvider) ListView(android.widget.ListView) Instance(org.odk.collect.forms.instances.Instance) IconMenuListAdapter(org.odk.collect.android.adapters.IconMenuListAdapter) IconMenuItem(org.odk.collect.android.adapters.model.IconMenuItem) Intent(android.content.Intent) Uri(android.net.Uri) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) NonNull(androidx.annotation.NonNull)

Example 3 with Instance

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

the class FormMapActivityTest method tappingOnDeletedInstances_showsSubmissionSummaryWithAppropriateMessage.

// Geometry is removed from the database on instance deletion but just in case there is a
// deleted instance with geometry available, show a deleted toast.
@Test
public void tappingOnDeletedInstances_showsSubmissionSummaryWithAppropriateMessage() {
    Pair<Instance, MapPoint> deleted = new Pair<>(testInstances[0], new MapPoint(10.0, 125.6));
    int featureId = map.getFeatureIdFor(deleted.second);
    activity.onFeatureClicked(featureId);
    assertSubmissionSummaryContent(deleted.first.getDisplayName(), deleted.first.getStatus(), new Date(deleted.first.getLastStatusChangeDate()), DELETED_TOAST);
}
Also used : Instance(org.odk.collect.forms.instances.Instance) MapPoint(org.odk.collect.geo.maps.MapPoint) MapPoint(org.odk.collect.geo.maps.MapPoint) Date(java.util.Date) Pair(android.util.Pair) Test(org.junit.Test)

Example 4 with Instance

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

the class FormMapViewModelTest method addingAnInstance_isReflectedInInstanceCountsAndList.

@Test
public void addingAnInstance_isReflectedInInstanceCountsAndList() {
    FormMapViewModel viewModel = new FormMapViewModel(TEST_FORM_1, testInstancesRepository);
    List<FormMapViewModel.MappableFormInstance> instances = viewModel.getMappableFormInstances();
    assertThat(viewModel.getTotalInstanceCount(), is(7));
    assertThat(instances.size(), is(6));
    Instance newInstance = new Instance.Builder().dbId(8L).formId("formId1").formVersion("2019103101").geometryType("Point").geometry("{\"type\":\"Point\",\"coordinates\":[127.6, 11.1]}").canEditWhenComplete(true).status(Instance.STATUS_COMPLETE).build();
    ((InMemInstancesRepository) testInstancesRepository).save(newInstance);
    instances = viewModel.getMappableFormInstances();
    assertThat(viewModel.getTotalInstanceCount(), is(8));
    assertThat(instances.size(), is(7));
    assertThat(instances.get(6).getClickAction(), is(FormMapViewModel.ClickAction.OPEN_EDIT));
}
Also used : Instance(org.odk.collect.forms.instances.Instance) InMemInstancesRepository(org.odk.collect.formstest.InMemInstancesRepository) FormMapViewModel(org.odk.collect.android.activities.viewmodels.FormMapViewModel) Test(org.junit.Test)

Example 5 with Instance

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

the class FormMapActivityTest method tappingOnUneditableInstances_showsSubmissionSummaryWithAppropriateMessage.

@Test
public void tappingOnUneditableInstances_showsSubmissionSummaryWithAppropriateMessage() {
    Pair<Instance, MapPoint> sent = new Pair<>(testInstances[5], new MapPoint(10.3, 125.7));
    int featureId = map.getFeatureIdFor(sent.second);
    activity.onFeatureClicked(featureId);
    assertSubmissionSummaryContent(sent.first.getDisplayName(), sent.first.getStatus(), new Date(sent.first.getLastStatusChangeDate()), OPEN_READ_ONLY);
}
Also used : Instance(org.odk.collect.forms.instances.Instance) MapPoint(org.odk.collect.geo.maps.MapPoint) MapPoint(org.odk.collect.geo.maps.MapPoint) Date(java.util.Date) Pair(android.util.Pair) Test(org.junit.Test)

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