Skip to content

Commit

Permalink
Add setting to disable bulk finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Oct 14, 2023
1 parent 561c7db commit 5bb89af
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.runner.RunWith
import org.odk.collect.android.support.TestDependencies
import org.odk.collect.android.support.pages.AccessControlPage
import org.odk.collect.android.support.pages.EditSavedFormPage
import org.odk.collect.android.support.pages.FormEntryPage.QuestionAndAnswer
import org.odk.collect.android.support.pages.MainMenuPage
import org.odk.collect.android.support.pages.ProjectSettingsPage
import org.odk.collect.android.support.pages.SaveOrDiscardFormDialog
import org.odk.collect.android.support.rules.CollectTestRule
import org.odk.collect.android.support.rules.TestRuleChain
Expand Down Expand Up @@ -189,4 +191,23 @@ class BulkFinalizationTest {

.assertNumberOfEditableForms(1)
}

@Test
fun canBeDisabled() {
rule.withProject("http://example.com")
.openProjectSettingsDialog()
.clickSettings()
.clickAccessControl()
.clickFormEntrySettings()
.clickOnString(string.finalize_all_forms)
.pressBack(AccessControlPage())
.pressBack(ProjectSettingsPage())
.pressBack(MainMenuPage())

.copyForm("one-question.xml", "example.com")
.startBlankForm("One Question")
.fillOutAndSave(QuestionAndAnswer("what is your age", "1892"))
.clickDrafts()
.assertNoOptionsMenu()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.odk.collect.android.support.pages

import android.app.Application
import android.content.pm.ActivityInfo
import android.view.View
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import androidx.test.core.app.ApplicationProvider
Expand Down Expand Up @@ -34,6 +35,8 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector
import org.hamcrest.CoreMatchers.not
import org.hamcrest.Matcher
import org.hamcrest.Matchers
import org.hamcrest.Matchers.allOf
import org.hamcrest.core.StringContains.containsString
import org.hamcrest.core.StringEndsWith.endsWith
Expand All @@ -42,7 +45,6 @@ import org.odk.collect.android.BuildConfig
import org.odk.collect.android.R
import org.odk.collect.android.application.Collect
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.support.ActivityHelpers
import org.odk.collect.android.support.CollectHelpers
import org.odk.collect.android.support.WaitFor.wait250ms
import org.odk.collect.android.support.WaitFor.waitFor
Expand Down Expand Up @@ -448,7 +450,7 @@ abstract class Page<T : Page<T>> {

fun clickOptionsIcon(expectedOptionString: String): T {
tryAgainOnFail({
Espresso.openActionBarOverflowOrOptionsMenu(ActivityHelpers.getActivity())
onView(OVERFLOW_BUTTON_MATCHER).perform(click())
assertText(expectedOptionString)
})

Expand All @@ -474,6 +476,11 @@ abstract class Page<T : Page<T>> {
return destination!!.assertOnPage()
}

fun assertNoOptionsMenu(): T {
onView(OVERFLOW_BUTTON_MATCHER).check(doesNotExist())
return this as T
}

companion object {
private fun rotateToLandscape(): ViewAction {
return RotateAction(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Expand All @@ -482,5 +489,10 @@ abstract class Page<T : Page<T>> {
private fun rotateToPortrait(): ViewAction {
return RotateAction(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
}

private val OVERFLOW_BUTTON_MATCHER: Matcher<View> = Matchers.anyOf(
allOf(isDisplayed(), withContentDescription("More options")),
allOf(isDisplayed(), withClassName(Matchers.endsWith("OverflowMenuButton")))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public void onCreate(Bundle savedInstanceState) {

BulkFinalizationViewModel bulkFinalizationViewModel = new BulkFinalizationViewModel(
scheduler,
instancesDataService
instancesDataService,
settingsProvider
);

MaterialProgressDialogFragment.showOn(this, bulkFinalizationViewModel.isFinalizing(), getSupportFragmentManager(), () -> {
Expand All @@ -155,17 +156,19 @@ public void onCreate(Bundle savedInstanceState) {
return dialog;
});

DraftsMenuProvider draftsMenuProvider = new DraftsMenuProvider(this, bulkFinalizationViewModel::finalizeAllDrafts);
addMenuProvider(draftsMenuProvider, this);
bulkFinalizationViewModel.getDraftsCount().observe(this, draftsCount -> {
draftsMenuProvider.setDraftsCount(draftsCount);
invalidateMenu();
});

bulkFinalizationViewModel.getFinalizedForms().observe(
this,
new FinalizeAllSnackbarPresenter(this.findViewById(android.R.id.content), this)
);
if (bulkFinalizationViewModel.isEnabled()) {
DraftsMenuProvider draftsMenuProvider = new DraftsMenuProvider(this, bulkFinalizationViewModel::finalizeAllDrafts);
addMenuProvider(draftsMenuProvider, this);
bulkFinalizationViewModel.getDraftsCount().observe(this, draftsCount -> {
draftsMenuProvider.setDraftsCount(draftsCount);
invalidateMenu();
});

bulkFinalizationViewModel.getFinalizedForms().observe(
this,
new FinalizeAllSnackbarPresenter(this.findViewById(android.R.id.content), this)
);
}
}

private void init() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import org.odk.collect.androidshared.data.Consumable
import org.odk.collect.androidshared.livedata.MutableNonNullLiveData
import org.odk.collect.androidshared.livedata.NonNullLiveData
import org.odk.collect.async.Scheduler
import org.odk.collect.settings.SettingsProvider
import org.odk.collect.settings.keys.ProtectedProjectKeys

class BulkFinalizationViewModel(
private val scheduler: Scheduler,
private val instancesDataService: InstancesDataService
private val instancesDataService: InstancesDataService,
private val settingsProvider: SettingsProvider
) {
private val _finalizedForms = MutableLiveData<Consumable<FinalizeAllResult>>()
val finalizedForms: LiveData<Consumable<FinalizeAllResult>> = _finalizedForms
Expand All @@ -20,6 +23,8 @@ class BulkFinalizationViewModel(
val isFinalizing: NonNullLiveData<Boolean> = _isFinalizing

val draftsCount = instancesDataService.editableCount
val isEnabled =
settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_BULK_FINALIZE)

fun finalizeAllDrafts() {
_isFinalizing.value = true
Expand Down
13 changes: 12 additions & 1 deletion collect_app/src/main/res/xml/form_entry_access_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,16 @@
android:key="finalize"
android:title="@string/finalize"
app:iconSpaceReserved="false" />
<CheckBoxPreference
android:key="bulk_finalize"
android:title="@string/finalize_all_forms"
app:iconSpaceReserved="false" />
</PreferenceCategory>

<PreferenceCategory android:title="@string/uncheck_to_hide_from_drafts">
<CheckBoxPreference
android:key="bulk_finalize"
android:title="@string/finalize_all_forms"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object ProtectedProjectKeys {
const val KEY_FINALIZE = "finalize"

const val ALLOW_OTHER_WAYS_OF_EDITING_FORM = "allow_other_ways_of_editing_form"
const val KEY_BULK_FINALIZE = "bulk_finalize"

fun allKeys() = listOf(
KEY_ADMIN_PW,
Expand Down Expand Up @@ -82,6 +83,7 @@ object ProtectedProjectKeys {
KEY_SAVE_MID,
KEY_SAVE_AS_DRAFT,
KEY_FINALIZE,
ALLOW_OTHER_WAYS_OF_EDITING_FORM
ALLOW_OTHER_WAYS_OF_EDITING_FORM,
KEY_BULK_FINALIZE
)
}
3 changes: 3 additions & 0 deletions strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1255,4 +1255,7 @@
<item quantity="one">Do you want to finalize %d form?</item>
<item quantity="other">Do you want to finalize %d forms?</item>
</plurals>

<!-- Title of section of features that the user can uncheck to hide from the Drafts screen -->
<string name="uncheck_to_hide_from_drafts">Uncheck to hide from Drafts</string>
</resources>

0 comments on commit 5bb89af

Please sign in to comment.