Skip to content

Commit

Permalink
Merge pull request getodk#5766 from seadowg/save-point-bulk-finalize
Browse files Browse the repository at this point in the history
Prevent bulk finalization on instances with save points
  • Loading branch information
seadowg committed Oct 30, 2023
1 parent e0b93e5 commit 3aca465
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,30 @@ class BulkFinalizationTest {
}

@Test
fun doesNotFinalizeOtherTypesOfInstance() {
fun doesNotFinalizeInstancesWithSavePoints() {
rule.startAtMainMenu()
.copyForm("one-question.xml")
.startBlankForm("One Question")
.swipeToEndScreen()
.clickSaveAsDraft()

.clickDrafts()
.clickOnForm("One Question")
.killAndReopenApp(MainMenuPage())

.clickDrafts()
.clickOptionsIcon(string.finalize_all_drafts)
.clickOnString(string.finalize_all_drafts)
.checkIsSnackbarWithQuantityDisplayed(plurals.bulk_finalize_failure, 1)
.assertText("One Question")
.pressBack(MainMenuPage())

.assertNumberOfEditableForms(1)
.assertNumberOfFinalizedForms(0)
}

@Test
fun doesNotFinalizeAlreadyFinalizedInstances() {
rule.startAtMainMenu()
.copyForm("one-question.xml")
.startBlankForm("One Question")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ object FormEntryUseCases {

@JvmStatic
fun loadFormDef(xForm: File, projectRootDir: File, formMediaDir: File): FormDef? {
FormUtils.setupReferenceManagerForForm(ReferenceManager.instance(), projectRootDir, formMediaDir)
FormUtils.setupReferenceManagerForForm(
ReferenceManager.instance(),
projectRootDir,
formMediaDir
)
return createFormDefFromCacheOrXml(xForm)
}

Expand Down Expand Up @@ -62,6 +66,17 @@ object FormEntryUseCases {
)
}

fun getSavePoint(formController: FormController, cacheDir: File): File? {
val instanceXml = formController.getInstanceFile()!!
val savepointFile = File(cacheDir, "${instanceXml.name}.save")

return if (savepointFile.exists() && savepointFile.lastModified() > instanceXml.lastModified()) {
savepointFile
} else {
null
}
}

fun saveDraft(
formController: JavaRosaFormController,
instancesRepository: InstancesRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import org.odk.collect.android.entities.EntitiesRepositoryProvider
import org.odk.collect.android.formentry.FormEntryUseCases
import org.odk.collect.android.storage.StoragePathProvider
import org.odk.collect.android.storage.StorageSubdirectory
import org.odk.collect.android.utilities.FileUtils
import org.odk.collect.android.utilities.FormsRepositoryProvider
import org.odk.collect.android.utilities.InstancesRepositoryProvider
Expand Down Expand Up @@ -70,16 +71,21 @@ class InstancesDataService(
val formController =
FormEntryUseCases.loadDraft(formEntryController, formMediaDir, instanceFile)

val instance = FormEntryUseCases.finalizeDraft(
formController,
instancesRepository,
entitiesRepository
)
val cacheDir = storagePathProvider.getOdkDirPath(StorageSubdirectory.CACHE)
if (FormEntryUseCases.getSavePoint(formController, File(cacheDir)) == null) {
val finalizedInstance = FormEntryUseCases.finalizeDraft(
formController,
instancesRepository,
entitiesRepository
)

if (instance == null) {
failCount + 1
if (finalizedInstance == null) {
failCount + 1
} else {
failCount
}
} else {
failCount
failCount + 1
}
}

Expand Down

0 comments on commit 3aca465

Please sign in to comment.