use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsRepositoryTest method save_whenFormHasId_updatesExisting.
@Test
public void save_whenFormHasId_updatesExisting() {
FormsRepository formsRepository = buildSubject();
Form originalForm = formsRepository.save(FormUtils.buildForm("id", "version", getFormFilesPath()).displayName("original").build());
formsRepository.save(new Form.Builder(originalForm).displayName("changed").build());
assertThat(formsRepository.get(originalForm.getDbId()).getDisplayName(), is("changed"));
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsRepositoryTest method save_addsMediaPath_whereMediaDirCanBeCreated.
@Test
public void save_addsMediaPath_whereMediaDirCanBeCreated() {
FormsRepository formsRepository = buildSubject();
Form form = FormUtils.buildForm("id", "version", getFormFilesPath()).build();
assertThat(form.getFormMediaPath(), equalTo(null));
Form savedForm = formsRepository.save(form);
assertThat(new File(savedForm.getFormMediaPath()).mkdir(), is(true));
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormEntryActivity method loadFromIntent.
private void loadFromIntent(Intent intent) {
Uri uri = intent.getData();
String uriMimeType = null;
if (uri != null) {
uriMimeType = getContentResolver().getType(uri);
}
if (uriMimeType != null && uriMimeType.equals(InstancesContract.CONTENT_ITEM_TYPE)) {
Instance instance = new InstancesRepositoryProvider(Collect.getInstance()).get().get(ContentUriHelper.getIdFromUri(uri));
if (instance == null) {
createErrorDialog(getString(R.string.bad_uri, uri), true);
return;
}
instancePath = instance.getInstanceFilePath();
if (!new File(instancePath).exists()) {
analytics.logEvent(AnalyticsEvents.OPEN_DELETED_INSTANCE);
new InstanceDeleter(new InstancesRepositoryProvider(Collect.getInstance()).get(), formsRepository).delete(instance.getDbId());
createErrorDialog(getString(R.string.instance_deleted_message), true);
return;
}
List<Form> candidateForms = formsRepository.getAllByFormIdAndVersion(instance.getFormId(), instance.getFormVersion());
if (candidateForms.isEmpty()) {
createErrorDialog(getString(R.string.parent_form_not_present, instance.getFormId()) + ((instance.getFormVersion() == null) ? "" : "\n" + getString(R.string.version) + " " + instance.getFormVersion()), true);
return;
} else if (candidateForms.stream().filter(f -> !f.isDeleted()).count() > 1) {
createErrorDialog(getString(R.string.survey_multiple_forms_error), true);
return;
}
formPath = candidateForms.get(0).getFormFilePath();
} else if (uriMimeType != null && uriMimeType.equals(FormsContract.CONTENT_ITEM_TYPE)) {
Form form = formsRepositoryProvider.get().get(ContentUriHelper.getIdFromUri(uri));
if (form != null) {
formPath = form.getFormFilePath();
}
if (formPath == null) {
createErrorDialog(getString(R.string.bad_uri, uri), true);
return;
} else {
/**
* This is the fill-blank-form code path.See if there is a savepoint for this form
* that has never been explicitly saved by the user. If there is, open this savepoint(resume this filled-in form).
* Savepoints for forms that were explicitly saved will be recovered when that
* explicitly saved instance is edited via edit-saved-form.
*/
instancePath = loadSavePoint();
}
} else {
Timber.i("Unrecognized URI: %s", uri);
createErrorDialog(getString(R.string.unrecognized_uri, uri), true);
return;
}
formLoaderTask = new FormLoaderTask(instancePath, null, null);
showIfNotShowing(FormLoadingDialogFragment.class, getSupportFragmentManager());
formLoaderTask.execute(formPath);
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormEntryActivity method onCreate.
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
// this issue by including WebViews
if (Build.VERSION.SDK_INT >= 24) {
try {
new WebView(this);
} catch (Exception | Error e) {
// Don't crash if WebView not available
}
}
super.onCreate(savedInstanceState);
Collect.getInstance().getComponent().inject(this);
formsRepository = formsRepositoryProvider.get();
setContentView(R.layout.form_entry);
setupViewModels();
swipeHandler = new SwipeHandler(this, settingsProvider.getUnprotectedSettings());
compositeDisposable.add(eventBus.register(ReadPhoneStatePermissionRxEvent.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(event -> {
readPhoneStatePermissionRequestNeeded = true;
}));
errorMessage = null;
questionHolder = findViewById(R.id.questionholder);
initToolbar();
formIndexAnimationHandler = new FormIndexAnimationHandler(this);
menuDelegate = new FormEntryMenuDelegate(this, () -> getAnswers(), formIndexAnimationHandler, formSaveViewModel, formEntryViewModel, audioRecorder, backgroundLocationViewModel, backgroundAudioViewModel, settingsProvider);
nextButton = findViewById(R.id.form_forward_button);
nextButton.setOnClickListener(v -> {
swipeHandler.setBeenSwiped(true);
onSwipeForward();
});
backButton = findViewById(R.id.form_back_button);
backButton.setOnClickListener(v -> {
swipeHandler.setBeenSwiped(true);
onSwipeBackward();
});
if (savedInstanceState == null) {
mediaLoadingFragment = new MediaLoadingFragment();
getSupportFragmentManager().beginTransaction().add(mediaLoadingFragment, TAG_MEDIA_LOADING_FRAGMENT).commit();
} else {
mediaLoadingFragment = (MediaLoadingFragment) getSupportFragmentManager().findFragmentByTag(TAG_MEDIA_LOADING_FRAGMENT);
}
setupFields(savedInstanceState);
loadForm();
}
use of org.odk.collect.forms.FormsRepository in project collect by opendatakit.
the class FormsRepositoryTest method deleteAll_deletesAllForms.
@Test
public void deleteAll_deletesAllForms() {
FormsRepository formsRepository = buildSubject();
formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
formsRepository.save(FormUtils.buildForm("id2", "version", getFormFilesPath()).build());
List<Form> forms = formsRepository.getAll();
formsRepository.deleteAll();
assertThat(formsRepository.getAll().size(), is(0));
for (Form form : forms) {
assertThat(new File(form.getFormFilePath()).exists(), is(false));
assertThat(new File(form.getFormMediaPath()).exists(), is(false));
}
}
Aggregations