Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: option to send only current record to Collect server #36

Merged
merged 3 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

## [1.7.13] - build 226 2024-04-18
### Added
- Option to submit only current record to Collect;

## [1.7.12] - build 225 2024-04-10
### Fixed
- Fixed cannot delete records from records list;
Expand Down
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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 23:12:41 CEST 2024
currentVersionCode=225
currentVersionName=1.7.12
currentVersionName=1.7.13
org.gradle.jvmargs=-Xmx1536M
android.enableJetifier=true
android.useAndroidX=true
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
Loading