From 9842b8fd578d089077bf136f23a845e0d15822fd Mon Sep 17 00:00:00 2001 From: Stefano Ricci Date: Thu, 18 Apr 2024 23:16:08 +0200 Subject: [PATCH] Feature: submit data to collect partially --- .../gui/SubmitDataToCollectActivity.java | 19 +++-- .../android/gui/SurveyNodeActivity.java | 17 ++--- .../SendDataToCollectDialogFragment.java | 70 +++++++++++++++++++ .../main/res/menu/node_activity_actions.xml | 4 +- android/src/main/res/values/strings.xml | 5 +- .../collect/android/util/Collections.java | 8 +++ 6 files changed, 103 insertions(+), 20 deletions(-) create mode 100644 android/src/main/java/org/openforis/collect/android/gui/detail/SendDataToCollectDialogFragment.java diff --git a/android/src/main/java/org/openforis/collect/android/gui/SubmitDataToCollectActivity.java b/android/src/main/java/org/openforis/collect/android/gui/SubmitDataToCollectActivity.java index 4699f818..e8eb3f52 100644 --- a/android/src/main/java/org/openforis/collect/android/gui/SubmitDataToCollectActivity.java +++ b/android/src/main/java/org/openforis/collect/android/gui/SubmitDataToCollectActivity.java @@ -1,5 +1,10 @@ package org.openforis.collect.android.gui; +import static android.view.View.INVISIBLE; +import static android.view.View.VISIBLE; +import static org.openforis.collect.android.gui.CollectMobileApplication.LOG_TAG; + +import android.content.Intent; import android.content.SharedPreferences; import android.os.AsyncTask; import android.os.Bundle; @@ -22,6 +27,7 @@ import org.openforis.collect.android.collectadapter.SurveyExporter; import org.openforis.collect.android.gui.settings.SettingsActivity; import org.openforis.collect.android.gui.util.AppDirs; +import org.openforis.collect.android.util.Collections; import org.openforis.collect.android.util.HttpConnectionHelper; import org.openforis.collect.android.util.MultipartUtility; import org.openforis.collect.android.util.ProgressHandler; @@ -31,12 +37,9 @@ import java.util.Timer; import java.util.TimerTask; -import static android.view.View.INVISIBLE; -import static android.view.View.VISIBLE; -import static org.openforis.collect.android.gui.CollectMobileApplication.LOG_TAG; - public class SubmitDataToCollectActivity extends BaseActivity { + public static final String EXTRA_ONLY_RECORD_IDS = "only_current_record"; private static final String DATA_RESTORE_ENDPOINT = "/api/surveys/restore/data"; private static final String DATA_RESTORE_JOB_ENDPOINT = "/api/surveys/data/restorejobs/%s/status.json"; private static final long RESTORE_DATA_JOB_MONITOR_PERIOD = 3000L; @@ -209,8 +212,14 @@ private class ExportDataTask extends AsyncTask { @Override protected File doInBackground(Void... voids) { SubmitDataToCollectActivity context = SubmitDataToCollectActivity.this; + Intent intent = context.getIntent(); + int[] recordIds = intent.hasExtra(EXTRA_ONLY_RECORD_IDS) ? intent.getIntArrayExtra(SubmitDataToCollectActivity.EXTRA_ONLY_RECORD_IDS) : new int[0]; try { - return ServiceLocator.surveyService().exportSurvey(AppDirs.surveysDir(context), new SurveyDataExportParameters()); + SurveyDataExportParameters parameters = new SurveyDataExportParameters(); + if (recordIds.length > 0) { + parameters.filterRecordIds = Collections.intArrayToList(recordIds); + } + return ServiceLocator.surveyService().exportSurvey(AppDirs.surveysDir(context), parameters); } catch (SurveyExporter.AllRecordKeysNotSpecified e) { handleError(AllRecordKeysNotSpecifiedDialog.generateMessage(context)); } catch (Exception e) { diff --git a/android/src/main/java/org/openforis/collect/android/gui/SurveyNodeActivity.java b/android/src/main/java/org/openforis/collect/android/gui/SurveyNodeActivity.java index 887cf264..28b1d6da 100644 --- a/android/src/main/java/org/openforis/collect/android/gui/SurveyNodeActivity.java +++ b/android/src/main/java/org/openforis/collect/android/gui/SurveyNodeActivity.java @@ -24,6 +24,7 @@ import org.openforis.collect.android.SurveyService; import org.openforis.collect.android.gui.barcode.BarcodeCaptureActivity; import org.openforis.collect.android.gui.detail.ExportDialogFragment; +import org.openforis.collect.android.gui.detail.SendDataToCollectDialogFragment; import org.openforis.collect.android.gui.entitytable.EntityTableDialogFragment; import org.openforis.collect.android.gui.input.AudioFileAttributeComponent; import org.openforis.collect.android.gui.input.BarcodeTextAttributeComponent; @@ -273,9 +274,8 @@ public void navigateDown(View view) { navigateDown(); } - public void exportDialog(MenuItem item) { + public void openExportDialog(MenuItem item) { if (Permissions.checkStoragePermissionOrRequestIt(this)) { - new ExportDialogFragment().show(getSupportFragmentManager(), "export-dialog"); } } @@ -478,21 +478,16 @@ public void openSurveyGuide(MenuItem menuItem) { } } - public void navigateToSendDataToCollect(MenuItem menuItem) { - navigateToSendDataToCollect(); + public void openSendDataToCollectDialog(MenuItem menuItem) { + openSendDataToCollectDialog(); } - private void navigateToSendDataToCollect() { + private void openSendDataToCollectDialog() { SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); boolean remoteSyncEnabled = preferences.getBoolean(SettingsActivity.REMOTE_SYNC_ENABLED, false); if (remoteSyncEnabled) { if (Permissions.checkInternetPermissionOrRequestIt(this)) { - Dialogs.confirm(this, R.string.submit_to_collect_confirm_title, R.string.submit_to_collect_confirm_message, new Runnable() { - public void run() { - Keyboard.hide(SurveyNodeActivity.this); - SurveyNodeActivity.this.startActivity(new Intent(SurveyNodeActivity.this, SubmitDataToCollectActivity.class)); - } - }); + new SendDataToCollectDialogFragment().show(getSupportFragmentManager(), "send-data-to-collect-dialog"); } } else { Toast.makeText(this, R.string.submit_to_collect_remote_sync_not_configured, Toast.LENGTH_SHORT).show(); diff --git a/android/src/main/java/org/openforis/collect/android/gui/detail/SendDataToCollectDialogFragment.java b/android/src/main/java/org/openforis/collect/android/gui/detail/SendDataToCollectDialogFragment.java new file mode 100644 index 00000000..a407a947 --- /dev/null +++ b/android/src/main/java/org/openforis/collect/android/gui/detail/SendDataToCollectDialogFragment.java @@ -0,0 +1,70 @@ +package org.openforis.collect.android.gui.detail; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.content.Intent; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.fragment.app.DialogFragment; + +import org.openforis.collect.R; +import org.openforis.collect.android.SurveyService; +import org.openforis.collect.android.gui.ServiceLocator; +import org.openforis.collect.android.gui.SubmitDataToCollectActivity; +import org.openforis.collect.android.viewmodel.UiNode; +import org.openforis.collect.android.viewmodel.UiRecordCollection; + +import java.util.Arrays; +import java.util.List; + +public class SendDataToCollectDialogFragment extends DialogFragment { + private static final int ALL_RECORDS = 0; + private static final int ONLY_CURRENT_RECORD = 1; + + private Dialog dialog = null; + + @NonNull + public Dialog onCreateDialog(Bundle savedInstanceState) { + final SurveyService surveyService = ServiceLocator.surveyService(); + + final int[] checkedItem = {ALL_RECORDS}; + + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) + .setTitle(R.string.submit_to_collect_confirm_title) + .setPositiveButton(R.string.action_submit_data_to_collect, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + if (SendDataToCollectDialogFragment.this.dialog != null) { + SendDataToCollectDialogFragment.this.dialog.dismiss(); + } + Intent intent = new Intent(getContext(), SubmitDataToCollectActivity.class); + boolean onlyCurrentRecord = checkedItem[0] == ONLY_CURRENT_RECORD; + if (onlyCurrentRecord) { + int recordId = surveyService.selectedNode().getUiRecord().getId(); + intent.putExtra(SubmitDataToCollectActivity.EXTRA_ONLY_RECORD_IDS, new int[]{recordId}); + } + getContext().startActivity(intent); + } + }) + .setNegativeButton(android.R.string.cancel, null); + + UiNode selectedNode = surveyService.selectedNode(); + if (selectedNode instanceof UiRecordCollection) { + builder.setMessage(R.string.submit_to_collect_confirm_message); + } else { + List options = Arrays.asList( + getString(R.string.export_dialog_option_all_records), + getString(R.string.export_dialog_option_only_current_record) + ); + builder.setSingleChoiceItems(options.toArray(new String[0]), checkedItem[0], new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + checkedItem[0] = which; + } + }); + } + dialog = builder.create(); + return dialog; + } + +} \ No newline at end of file diff --git a/android/src/main/res/menu/node_activity_actions.xml b/android/src/main/res/menu/node_activity_actions.xml index 6f671bac..b1cb958f 100644 --- a/android/src/main/res/menu/node_activity_actions.xml +++ b/android/src/main/res/menu/node_activity_actions.xml @@ -17,13 +17,13 @@ android:title="@string/action_export" android:icon="?attr/exportIcon" app:showAsAction="never" - android:onClick="exportDialog"/> + android:onClick="openExportDialog"/> + android:onClick="openSendDataToCollectDialog"/> dd MMMM yyyy hh:mm - Send records to Collect - Send all collected records to Collect server? + Confirm submit data to Collect + Submit all collected records to Collect server? Collect server not configured in Settings 1/3 Exporting data @@ -213,6 +213,7 @@ Please select a valid Collect Mobile survey file (.%s) Export survey data + All records Only current record Only selected records Exclude image files diff --git a/model/src/main/java/org/openforis/collect/android/util/Collections.java b/model/src/main/java/org/openforis/collect/android/util/Collections.java index 9b850493..c3be6dfa 100644 --- a/model/src/main/java/org/openforis/collect/android/util/Collections.java +++ b/model/src/main/java/org/openforis/collect/android/util/Collections.java @@ -5,6 +5,14 @@ public abstract class Collections { + public static List intArrayToList(int[] arr) { + List list = new ArrayList(); + for (int recordId : arr) { + list.add(recordId); + } + return list; + } + public static List transform(List list, Transformer transformer) { List result = new ArrayList(list.size()); for (T item : list) {