Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle process death during filling a form #4286

Merged
merged 6 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ private void nonblockingCreateSavePointData() {
}
}

// This method may return null if called before form loading is finished
@Nullable
private FormController getFormController() {
return Collect.getInstance().getFormController();
Expand Down Expand Up @@ -2315,9 +2316,8 @@ public void denied() {
if (pendingActivityResult) {
Timber.w("Calling onActivityResult from loadingComplete");

// set the current view to whatever group we were at...
formControllerAvailable(formController);
onScreenRefresh();
// process the pending activity request...
onActivityResult(task.getRequestCode(), task.getResultCode(), task.getIntent());
return;
}
Expand Down Expand Up @@ -2644,7 +2644,7 @@ private void displayUIFor(@Nullable BackgroundLocationManager.BackgroundLocation

@Override
public void widgetValueChanged(QuestionWidget changedWidget) {
FormController formController = Collect.getInstance().getFormController();
FormController formController = getFormController();
if (formController == null) {
// TODO: As usual, no idea if/how this is possible.
return;
Expand Down Expand Up @@ -2687,15 +2687,15 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
*/
private void updateFieldListQuestions(FormIndex lastChangedIndex) throws FormDesignException {
// Save the user-visible state for all questions in this field-list
FormEntryPrompt[] questionsBeforeSave = Collect.getInstance().getFormController().getQuestionPrompts();
FormEntryPrompt[] questionsBeforeSave = getFormController().getQuestionPrompts();
List<ImmutableDisplayableQuestion> immutableQuestionsBeforeSave = new ArrayList<>();
for (FormEntryPrompt questionBeforeSave : questionsBeforeSave) {
immutableQuestionsBeforeSave.add(new ImmutableDisplayableQuestion(questionBeforeSave));
}

saveAnswersForCurrentScreen(questionsBeforeSave, immutableQuestionsBeforeSave);

FormEntryPrompt[] questionsAfterSave = Collect.getInstance().getFormController().getQuestionPrompts();
FormEntryPrompt[] questionsAfterSave = getFormController().getQuestionPrompts();

Map<FormIndex, FormEntryPrompt> questionsAfterSaveByIndex = new HashMap<>();
for (FormEntryPrompt question : questionsAfterSave) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,13 @@
import org.javarosa.form.api.FormEntryController;
import org.jetbrains.annotations.NotNull;
import org.odk.collect.android.analytics.Analytics;
import org.odk.collect.android.application.Collect;
import org.odk.collect.android.exception.JavaRosaException;
import org.odk.collect.android.formentry.audit.AuditEvent;
import org.odk.collect.android.javarosawrapper.FormController;
import org.odk.collect.utilities.Clock;

import javax.inject.Inject;

import timber.log.Timber;

import static org.odk.collect.android.javarosawrapper.FormIndexUtils.getRepeatGroupIndex;

public class FormEntryViewModel extends ViewModel implements RequiresFormController {
Expand Down Expand Up @@ -133,8 +130,6 @@ public boolean canAddRepeat() {
}

public void moveForward() {
ensureFormController();

try {
formController.stepToNextScreenEvent();
} catch (JavaRosaException e) {
Expand All @@ -146,8 +141,6 @@ public void moveForward() {
}

public void moveBackward() {
ensureFormController();

try {
int event = formController.stepToPreviousScreenEvent();

Expand All @@ -164,7 +157,6 @@ public void moveBackward() {
}

public void openHierarchy() {
ensureFormController();
formController.getAuditEventLogger().logEvent(AuditEvent.AuditEventType.HIERARCHY, true, clock.getCurrentTime());
}

Expand All @@ -180,13 +172,6 @@ private String getFormIdentifierHash() {
}
}

private void ensureFormController() {
if (this.formController == null) {
Timber.e(new IllegalStateException("ensureFormController called before formLoaded"));
this.formController = Collect.getInstance().getFormController();
}
}

public static class Factory implements ViewModelProvider.Factory {

private final Clock clock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public AuditEventLogger getAuditEventLogger() {
*/
public String getXPath(FormIndex index) {
String value;
switch (getEvent()) {
switch (getEvent(index)) {
case FormEntryController.EVENT_BEGINNING_OF_FORM:
value = "beginningOfForm";
break;
Expand Down