Skip to content

Commit 839513c

Browse files
committed
Handle scrolling to question by introducing a separate parameter
1 parent 62d9287 commit 839513c

File tree

4 files changed

+32
-20
lines changed

4 files changed

+32
-20
lines changed

collect_app/src/main/java/org/odk/collect/android/activities/FormFillingActivity.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -564,14 +564,15 @@ private void setupViewModels(FormEntryViewModelFactory formEntryViewModelFactory
564564

565565
formEntryViewModel.getCurrentIndex().observe(this, indexAndValidationResult -> {
566566
if (indexAndValidationResult != null) {
567-
FormIndex formIndex = indexAndValidationResult.component1();
568-
FailedValidationResult validationResult = indexAndValidationResult.component2();
569-
formIndexAnimationHandler.handle(formIndex);
567+
FormIndex screenIndex = indexAndValidationResult.getFirst();
568+
FormIndex questionIndex = indexAndValidationResult.getSecond();
569+
FailedValidationResult validationResult = indexAndValidationResult.getThird();
570+
formIndexAnimationHandler.handle(screenIndex);
570571
if (validationResult != null) {
571572
handleValidationResult(validationResult);
572573
} else {
573574
if (odkView != null) {
574-
odkView.scrollToTopOf(formIndex);
575+
odkView.scrollToTopOf(questionIndex);
575576
}
576577
}
577578
}

collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryViewModel.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
import java.util.Objects;
5555
import java.util.function.Supplier;
5656

57-
import kotlin.Pair;
57+
import kotlin.Triple;
5858
import timber.log.Timber;
5959

6060
public class FormEntryViewModel extends ViewModel implements SelectChoiceLoader {
@@ -63,7 +63,7 @@ public class FormEntryViewModel extends ViewModel implements SelectChoiceLoader
6363

6464
private final MutableLiveData<FormError> error = new MutableLiveData<>(null);
6565
private final MutableNonNullLiveData<Boolean> hasBackgroundRecording = new MutableNonNullLiveData<>(false);
66-
private final MutableLiveData<Pair<FormIndex, FailedValidationResult>> currentIndex = new MutableLiveData<>(null);
66+
private final MutableLiveData<Triple<FormIndex, FormIndex, FailedValidationResult>> currentIndex = new MutableLiveData<>(null);
6767
private final MutableLiveData<Consumable<ValidationResult>>
6868
validationResult = new MutableLiveData<>(new Consumable<>(null));
6969
@NonNull
@@ -118,7 +118,7 @@ public FormController getFormController() {
118118
return formController;
119119
}
120120

121-
public LiveData<Pair<FormIndex, FailedValidationResult>> getCurrentIndex() {
121+
public LiveData<Triple<FormIndex, FormIndex, FailedValidationResult>> getCurrentIndex() {
122122
return currentIndex;
123123
}
124124

@@ -379,9 +379,9 @@ private void updateIndex(boolean isAsync, @Nullable FailedValidationResult valid
379379

380380
AuditUtils.logCurrentScreen(formController, formController.getAuditEventLogger(), clock.get());
381381
if (isAsync) {
382-
currentIndex.postValue(new Pair<>(questionIndex != null ? questionIndex : formController.getFormIndex(), validationResult));
382+
currentIndex.postValue(new Triple<>(formController.getFormIndex(), questionIndex, validationResult));
383383
} else {
384-
currentIndex.setValue(new Pair<>(questionIndex != null ? questionIndex : formController.getFormIndex(), validationResult));
384+
currentIndex.setValue(new Triple<>(formController.getFormIndex(), questionIndex, validationResult));
385385
}
386386
}
387387
}

collect_app/src/main/java/org/odk/collect/android/formentry/FormIndexAnimationHandler.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,16 @@ class FormIndexAnimationHandler(private val listener: Listener) {
1515
private var lastIndex: FormIndex? = null
1616

1717
fun handle(index: FormIndex) {
18-
lastIndex?.let {
19-
if (index.toString().startsWith(it.toString())) {
20-
listener.onScreenRefresh(false)
21-
} else if (index > it) {
18+
if (lastIndex == null) {
19+
listener.onScreenRefresh(true)
20+
} else {
21+
if (index > lastIndex) {
2222
listener.onScreenChange(Direction.FORWARDS)
23-
} else if (index < it) {
23+
} else if (index < lastIndex) {
2424
listener.onScreenChange(Direction.BACKWARDS)
2525
} else {
2626
listener.onScreenRefresh(false)
2727
}
28-
} ?: run {
29-
listener.onScreenRefresh(true)
3028
}
3129

3230
lastIndex = index

collect_app/src/test/java/org/odk/collect/android/formentry/FormEntryViewModelTest.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,29 +510,34 @@ public void answerQuestion_whenQuestionIsAutoAdvance_andAnswerViolatesConstraint
510510
FormIndex originalIndex = formController.getFormIndex();
511511
viewModel.answerQuestion(formIndex, new StringData("answer"));
512512
scheduler.flush(true);
513-
assertThat(getOrAwaitValue(viewModel.getCurrentIndex()).getSecond(), equalTo(failedValidationResult));
513+
assertThat(getOrAwaitValue(viewModel.getCurrentIndex()).getThird(), equalTo(failedValidationResult));
514514
assertThat(formController.getFormIndex(), equalTo(new FormIndex(null, originalIndex.getLocalIndex(), 0, new TreeReference())));
515515
}
516516

517517
@Test
518-
public void answerQuestion_setsCurrentIndexToUpdatedQuestionIndex() {
518+
public void answerQuestion_setsQuestionIndexToUpdatedQuestionIndex() {
519519
TreeReference reference = new TreeReference();
520520
reference.add("blah", TreeReference.INDEX_UNBOUND);
521521
FormIndex formIndex = new FormIndex(null, 1, 1, reference);
522522
FormEntryPrompt prompt = new MockFormEntryPromptBuilder()
523523
.build();
524524
formController.setPrompt(formIndex, prompt);
525525

526+
FormIndex originalIndex = formController.getFormIndex();
526527
viewModel.answerQuestion(formIndex, new StringData("answer"));
527528
scheduler.flush(true);
528529
assertThat(
529530
getOrAwaitValue(viewModel.getCurrentIndex()).getFirst(),
531+
equalTo(originalIndex)
532+
);
533+
assertThat(
534+
getOrAwaitValue(viewModel.getCurrentIndex()).getSecond(),
530535
equalTo(formIndex)
531536
);
532537
}
533538

534539
@Test
535-
public void answerQuestion_whenQuestionIsAutoAdvance_setsCurrentIndexToNextScreenIndex() {
540+
public void answerQuestion_whenQuestionIsAutoAdvance_setsScreenIndexToNextScreenIndex() {
536541
TreeReference reference = new TreeReference();
537542
reference.add("blah", TreeReference.INDEX_UNBOUND);
538543
FormIndex formIndex = new FormIndex(null, 1, 1, reference);
@@ -549,10 +554,14 @@ public void answerQuestion_whenQuestionIsAutoAdvance_setsCurrentIndexToNextScree
549554
getOrAwaitValue(viewModel.getCurrentIndex()).getFirst(),
550555
equalTo(new FormIndex(null, originalIndex.getLocalIndex() + 1, 0, new TreeReference()))
551556
);
557+
assertThat(
558+
getOrAwaitValue(viewModel.getCurrentIndex()).getSecond(),
559+
equalTo(null)
560+
);
552561
}
553562

554563
@Test
555-
public void answerQuestion_whenQuestionIsAutoAdvance_andAnswerViolatesConstraint_setsCurrentIndexToCurrentScreenIndex() {
564+
public void answerQuestion_whenQuestionIsAutoAdvance_andAnswerViolatesConstraint_setsScreenIndexToCurrentScreenIndex() {
556565
TreeReference reference = new TreeReference();
557566
reference.add("blah", TreeReference.INDEX_UNBOUND);
558567
FormIndex formIndex = new FormIndex(null, 1, 1, reference);
@@ -572,5 +581,9 @@ public void answerQuestion_whenQuestionIsAutoAdvance_andAnswerViolatesConstraint
572581
getOrAwaitValue(viewModel.getCurrentIndex()).getFirst(),
573582
equalTo(originalIndex)
574583
);
584+
assertThat(
585+
getOrAwaitValue(viewModel.getCurrentIndex()).getSecond(),
586+
equalTo(null)
587+
);
575588
}
576589
}

0 commit comments

Comments
 (0)