Skip to content

Commit

Permalink
Merge pull request #6516 from seadowg/view-only
Browse files Browse the repository at this point in the history
Convert hierarchy to Fragment
  • Loading branch information
grzesiek2010 authored Jan 8, 2025
2 parents 184e878 + 9aec982 commit 467c317
Show file tree
Hide file tree
Showing 24 changed files with 1,216 additions and 1,188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.runner.RunWith;
import org.odk.collect.android.R;
import org.odk.collect.android.support.pages.EndOfFormPage;
import org.odk.collect.android.support.pages.FormEndPage;
import org.odk.collect.android.support.pages.FormEntryPage;
import org.odk.collect.android.support.rules.CollectTestRule;
import org.odk.collect.android.support.rules.TestRuleChain;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void whenInRepeatWithFixedCount_noPlusButtonAppears() {
}

@Test
public void whenInHierarchyForRepeat_clickingPlus_addsRepeatAtEndOfSeries() {
public void whenInHierarchyForRepeatGroup_clickingPlus_addsRepeatAtEndOfSeries() {
rule.startAtMainMenu()
.copyForm(ONE_QUESTION_REPEAT)
.startBlankForm("One Question Repeat")
Expand All @@ -130,4 +131,19 @@ public void whenInRepeatWithoutLabel_swipingNext_andClickingAdd_addsAnotherRepea
.clickOnAdd(new FormEntryPage("Repeat without label"))
.assertText("> 2");
}

@Test
public void whenViewFormInHierarchyForRepeatGroup_noAddButtonAppears() {
rule.startAtMainMenu()
.copyForm(ONE_QUESTION_REPEAT)
.startBlankForm("One Question Repeat")
.swipeToNextQuestionWithRepeatGroup("Person")
.clickOnDoNotAdd(new FormEndPage("One Question Repeat"))
.clickFinalize()

.clickSendFinalizedForm(1)
.clickOnForm("One Question Repeat")
.clickOnGroup("Person")
.assertNoId(R.id.menu_add_repeat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.odk.collect.android.R;
import org.odk.collect.android.support.rules.CollectTestRule;
import org.odk.collect.android.support.rules.TestRuleChain;
import org.odk.collect.testshared.RecyclerViewMatcher;
import org.odk.collect.android.support.pages.AddNewRepeatDialog;
import org.odk.collect.android.support.pages.FormEndPage;
import org.odk.collect.android.support.pages.FormEntryPage;
import org.odk.collect.android.support.pages.FormHierarchyPage;
import org.odk.collect.android.support.pages.ViewFormPage;
import org.odk.collect.android.support.rules.CollectTestRule;
import org.odk.collect.android.support.rules.TestRuleChain;
import org.odk.collect.testshared.RecyclerViewMatcher;

public class FormHierarchyTest {

Expand Down Expand Up @@ -238,4 +240,33 @@ public void theListOfQuestionsShouldBeScrolledToTheLastDisplayedQuestionAfterOpe
.assertTextDoesNotExist("t1")
.assertTextDoesNotExist("t2");
}

@Test
public void whenViewFormInHierarchyForRepeatGroup_noDeleteButtonAppears() {
rule.startAtMainMenu()
.copyForm("one-question-repeat.xml")
.startBlankForm("One Question Repeat")
.swipeToNextQuestionWithRepeatGroup("Person")
.clickOnDoNotAdd(new FormEndPage("One Question Repeat"))
.clickFinalize()

.clickSendFinalizedForm(1)
.clickOnForm("One Question Repeat")
.clickOnGroup("Person")
.clickOnGroup("Person > 1")
.assertNoId(R.id.menu_delete_child);
}

@Test
public void whenViewFormInHierarchy_clickingOnQuestion_doesNothing() {
rule.startAtMainMenu()
.copyForm("one-question.xml")
.startBlankForm("One Question")
.fillOutAndFinalize()

.clickSendFinalizedForm(1)
.clickOnForm("One Question")
.clickOnText("what is your age")
.assertOnPage(new ViewFormPage("One Question"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,11 @@ abstract class Page<T : Page<T>> {
return destination
}

fun assertNoId(id: Int): T {
onView(withId(id)).check(doesNotExist())
return this as T
}

companion object {
private fun rotateToLandscape(): ViewAction {
return RotateAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package org.odk.collect.android.support.pages

import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.contrib.RecyclerViewActions.scrollTo
import androidx.test.espresso.matcher.ViewMatchers.hasDescendant
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import org.odk.collect.android.R

class ViewFormPage(private val formName: String) : Page<ViewFormPage>() {

override fun assertOnPage(): ViewFormPage {
assertToolbarTitle(formName)
assertText(org.odk.collect.strings.R.string.exit)
return this
}

fun clickOnGroup(groupLabel: String): ViewFormPage {
onView(withId(R.id.list)).perform(scrollTo<RecyclerView.ViewHolder>(
hasDescendant(withText(groupLabel)))
)

clickOnText(groupLabel)
return this
}
}
3 changes: 1 addition & 2 deletions collect_app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ the specific language governing permissions and limitations under the License.
<activity
android:name=".preferences.screens.ProjectPreferencesActivity"
android:theme="@style/Theme.Collect.Settings" />
<activity android:name=".formhierarchy.FormHierarchyActivity" />
<activity android:name=".formhierarchy.ViewOnlyFormHierarchyActivity" />
<activity
android:name="org.odk.collect.geo.geopoint.GeoPointActivity"
android:theme="@style/Theme.Collect"
Expand Down Expand Up @@ -328,6 +326,7 @@ the specific language governing permissions and limitations under the License.
<data android:mimeType="vnd.android.cursor.item/vnd.odk.instance" />
</intent-filter>
</activity>
<activity android:name="org.odk.collect.android.formhierarchy.FormHierarchyFragmentHostActivity" />

</application>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.javarosa.form.api.FormEntryController.EVENT_PROMPT_NEW_REPEAT;
import static org.odk.collect.android.formentry.FormIndexAnimationHandler.Direction.BACKWARDS;
import static org.odk.collect.android.formentry.FormIndexAnimationHandler.Direction.FORWARDS;
import static org.odk.collect.android.formentry.repeats.DeleteRepeatDialogFragment.REQUEST_DELETE_REPEAT;
import static org.odk.collect.android.utilities.AnimationUtils.areAnimationsEnabled;
import static org.odk.collect.android.utilities.ApplicationConstants.RequestCodes;
import static org.odk.collect.android.utilities.DialogUtils.getDialog;
Expand Down Expand Up @@ -124,8 +125,7 @@
import org.odk.collect.android.formentry.saving.SaveAnswerFileErrorDialogFragment;
import org.odk.collect.android.formentry.saving.SaveAnswerFileProgressDialogFragment;
import org.odk.collect.android.formentry.saving.SaveFormProgressDialogFragment;
import org.odk.collect.android.formhierarchy.FormHierarchyActivity;
import org.odk.collect.android.formhierarchy.ViewOnlyFormHierarchyActivity;
import org.odk.collect.android.formhierarchy.FormHierarchyFragmentHostActivity;
import org.odk.collect.android.fragments.MediaLoadingFragment;
import org.odk.collect.android.fragments.dialogs.LocationProvidersDisabledDialog;
import org.odk.collect.android.fragments.dialogs.NumberPickerDialog;
Expand Down Expand Up @@ -229,7 +229,6 @@ public class FormFillingActivity extends LocalizedActivity implements AnimationL
RankingWidgetDialog.RankingListener, SaveFormIndexTask.SaveFormIndexListener,
WidgetValueChangedListener, ScreenContext, FormLoadingDialogFragment.FormLoadingDialogFragmentListener,
AudioControllerView.SwipableParent, FormIndexAnimationHandler.Listener,
DeleteRepeatDialogFragment.DeleteRepeatDialogCallback,
SelectMinimalDialog.SelectMinimalDialogListener, CustomDatePickerDialog.DateChangeListener,
CustomTimePickerDialog.TimeChangeListener {

Expand Down Expand Up @@ -449,6 +448,8 @@ public void onCreate(Bundle savedInstanceState) {
.forClass(SelectOneFromMapDialogFragment.class, () -> new SelectOneFromMapDialogFragment(viewModelFactory))
.build());

getSupportFragmentManager().setFragmentResultListener(REQUEST_DELETE_REPEAT, this, (requestKey, result) -> deleteGroup());

if (ProcessRestoreDetector.isProcessRestoring(this, savedInstanceState)) {
if (savedInstanceState.containsKey(KEY_XPATH)) {
startingXPath = savedInstanceState.getString(KEY_XPATH);
Expand Down Expand Up @@ -1057,7 +1058,6 @@ public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}

@Override
public void deleteGroup() {
FormController formController = getFormController();
if (formController != null && !formController.indexIsInFieldList()) {
Expand Down Expand Up @@ -1899,7 +1899,7 @@ public void onAnimationStart(Animation animation) {

/**
* Given a {@link FormLoaderTask} which has created a {@link FormController} for either a new or
* existing instance, shows that instance to the user. Either launches {@link FormHierarchyActivity}
* existing instance, shows that instance to the user. Either launches {@link FormHierarchyFragmentHostActivity}
* if an existing instance is being edited or builds the view for the current question(s) if a
* new instance is being created.
* <p>
Expand Down Expand Up @@ -2030,17 +2030,18 @@ && new PlayServicesChecker().isGooglePlayServicesAvailable(this)) {
} else {
formController.getAuditEventLogger().logEvent(AuditEvent.AuditEventType.HIERARCHY, true, System.currentTimeMillis());
formControllerAvailable(formController, form, instance);
Intent intent = new Intent(this, FormHierarchyActivity.class);
intent.putExtra(FormHierarchyActivity.EXTRA_SESSION_ID, sessionId);
Intent intent = new Intent(this, FormHierarchyFragmentHostActivity.class);
intent.putExtra(FormHierarchyFragmentHostActivity.EXTRA_SESSION_ID, sessionId);
startActivityForResult(intent, RequestCodes.HIERARCHY_ACTIVITY);
}
}
});
} else {
formControllerAvailable(formController, form, instance);
if (ApplicationConstants.FormModes.VIEW_SENT.equalsIgnoreCase(formMode)) {
Intent intent = new Intent(this, ViewOnlyFormHierarchyActivity.class);
intent.putExtra(FormHierarchyActivity.EXTRA_SESSION_ID, sessionId);
Intent intent = new Intent(this, FormHierarchyFragmentHostActivity.class);
intent.putExtra(FormHierarchyFragmentHostActivity.EXTRA_SESSION_ID, sessionId);
intent.putExtra(FormHierarchyFragmentHostActivity.EXTRA_VIEW_ONLY, true);
startActivity(intent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.odk.collect.android.R
import org.odk.collect.android.formentry.backgroundlocation.BackgroundLocationViewModel
import org.odk.collect.android.formentry.questions.AnswersProvider
import org.odk.collect.android.formhierarchy.FormHierarchyActivity
import org.odk.collect.android.formhierarchy.FormHierarchyFragmentHostActivity
import org.odk.collect.android.preferences.screens.ProjectPreferencesActivity
import org.odk.collect.android.utilities.ApplicationConstants
import org.odk.collect.androidshared.system.PlayServicesChecker
Expand Down Expand Up @@ -126,8 +126,8 @@ class FormEntryMenuProvider(
} else {
formEntryViewModel.updateAnswersForScreen(answersProvider.answers, false)
formEntryViewModel.openHierarchy()
val i = Intent(activity, FormHierarchyActivity::class.java)
i.putExtra(FormHierarchyActivity.EXTRA_SESSION_ID, formEntryViewModel.sessionId)
val i = Intent(activity, FormHierarchyFragmentHostActivity::class.java)
i.putExtra(FormHierarchyFragmentHostActivity.EXTRA_SESSION_ID, formEntryViewModel.sessionId)
activity.startActivityForResult(i, ApplicationConstants.RequestCodes.HIERARCHY_ACTIVITY)
}
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

public class DeleteRepeatDialogFragment extends DialogFragment {

public static final String REQUEST_DELETE_REPEAT = "DELETE_REPEAT";

private final ViewModelProvider.Factory viewModelFactory;
private FormEntryViewModel formEntryViewModel;

private DeleteRepeatDialogCallback callback;

public DeleteRepeatDialogFragment(ViewModelProvider.Factory viewModelFactory) {
this.viewModelFactory = viewModelFactory;
}
Expand All @@ -38,10 +38,6 @@ public void onAttach(@NonNull Context context) {
DaggerUtils.getComponent(context).inject(this);

formEntryViewModel = new ViewModelProvider(requireActivity(), viewModelFactory).get(FormEntryViewModel.class);

if (context instanceof DeleteRepeatDialogCallback) {
callback = (DeleteRepeatDialogCallback) context;
}
}

@NonNull
Expand All @@ -64,7 +60,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
if (i == BUTTON_POSITIVE) { // yes
formController.getAuditEventLogger().logEvent(AuditEvent.AuditEventType.DELETE_REPEAT, true, System.currentTimeMillis());
formController.deleteRepeat();
callback.deleteGroup();
getParentFragmentManager().setFragmentResult(REQUEST_DELETE_REPEAT, new Bundle());
}
alertDialog.cancel();
dismiss();
Expand All @@ -76,8 +72,4 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

return alertDialog;
}

public interface DeleteRepeatDialogCallback {
void deleteGroup();
}
}
Loading

0 comments on commit 467c317

Please sign in to comment.