Skip to content

Commit

Permalink
Feature: submit data to collect partially
Browse files Browse the repository at this point in the history
  • Loading branch information
SteRiccio committed Apr 18, 2024
1 parent 1efa25d commit 9842b8f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -209,8 +212,14 @@ private class ExportDataTask extends AsyncTask<Void, Void, File> {
@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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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;
}

}
4 changes: 2 additions & 2 deletions android/src/main/res/menu/node_activity_actions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
android:title="@string/action_export"
android:icon="?attr/exportIcon"
app:showAsAction="never"
android:onClick="exportDialog"/>
android:onClick="openExportDialog"/>
<item
android:id="@+id/action_send_to_collect"
android:title="@string/action_submit_data_to_collect"
android:icon="?attr/submitToCollectIcon"
app:showAsAction="never"
android:onClick="navigateToSendDataToCollect"/>
android:onClick="openSendDataToCollectDialog"/>
<item
android:id="@+id/action_survey_list"
android:title="@string/action_survey_list"
Expand Down
5 changes: 3 additions & 2 deletions android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@
<string name="hint_date_pattern">dd MMMM yyyy</string>
<string name="hint_time_pattern">hh:mm</string>

<string name="submit_to_collect_confirm_title">Send records to Collect</string>
<string name="submit_to_collect_confirm_message">Send all collected records to Collect server?</string>
<string name="submit_to_collect_confirm_title">Confirm submit data to Collect</string>
<string name="submit_to_collect_confirm_message">Submit all collected records to Collect server?</string>
<string name="submit_to_collect_remote_sync_not_configured">Collect server not configured in Settings</string>

<string name="submit_to_collect_exporting_data_title">1/3 Exporting data</string>
Expand Down Expand Up @@ -213,6 +213,7 @@
<string name="import_text_unsupported_file_type_selected">Please select a valid Collect Mobile survey file (.%s)</string>

<string name="export_dialog_title">Export survey data</string>
<string name="export_dialog_option_all_records">All records</string>
<string name="export_dialog_option_only_current_record">Only current record</string>
<string name="export_dialog_option_only_selected_records">Only selected records</string>
<string name="export_dialog_option_exclude_binary_file">Exclude image files</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@

public abstract class Collections {

public static List<Integer> intArrayToList(int[] arr) {
List<Integer> list = new ArrayList<Integer>();
for (int recordId : arr) {
list.add(recordId);
}
return list;
}

public static <T> List<T> transform(List<T> list, Transformer<T> transformer) {
List<T> result = new ArrayList<T>(list.size());
for (T item : list) {
Expand Down

0 comments on commit 9842b8f

Please sign in to comment.