diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/formmanagement/BulkFinalizationTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/formmanagement/BulkFinalizationTest.kt index e805dfa7901..1ebc7a1614c 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/formmanagement/BulkFinalizationTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/formmanagement/BulkFinalizationTest.kt @@ -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") diff --git a/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryUseCases.kt b/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryUseCases.kt index 54384fca25b..a93854a4177 100644 --- a/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryUseCases.kt +++ b/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryUseCases.kt @@ -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) } @@ -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, diff --git a/collect_app/src/main/java/org/odk/collect/android/formmanagement/InstancesDataService.kt b/collect_app/src/main/java/org/odk/collect/android/formmanagement/InstancesDataService.kt index 024acd2c82a..e42c8749404 100644 --- a/collect_app/src/main/java/org/odk/collect/android/formmanagement/InstancesDataService.kt +++ b/collect_app/src/main/java/org/odk/collect/android/formmanagement/InstancesDataService.kt @@ -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 @@ -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 } }