Skip to content

Commit

Permalink
Modified form exit dialog UI.
Browse files Browse the repository at this point in the history
Added "save_by_default" setting to save forms by default
on exit.
  • Loading branch information
chinmoy12c committed Mar 17, 2023
1 parent 7ad5408 commit 1d01d40
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,26 @@
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.common.collect.ImmutableList;

import org.odk.collect.analytics.Analytics;
import org.odk.collect.android.R;
import org.odk.collect.android.adapters.IconMenuListAdapter;
import org.odk.collect.android.adapters.model.IconMenuItem;
import org.odk.collect.android.external.InstancesContract;
import org.odk.collect.android.formentry.saving.FormSaveViewModel;
import org.odk.collect.android.injection.DaggerUtils;
import org.odk.collect.android.projects.CurrentProjectProvider;
import org.odk.collect.android.utilities.DialogUtils;
import org.odk.collect.android.utilities.InstancesRepositoryProvider;
import org.odk.collect.async.Scheduler;
import org.odk.collect.forms.instances.Instance;
import org.odk.collect.settings.SettingsProvider;
import org.odk.collect.settings.keys.ProtectedProjectKeys;

import java.util.List;

import javax.inject.Inject;

public class QuitFormDialogFragment extends DialogFragment {
Expand Down Expand Up @@ -84,26 +77,57 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

String title = formSaveViewModel.getFormName() == null ? getActivity().getString(R.string.no_form_loaded) : formSaveViewModel.getFormName();

List<IconMenuItem> items;
if (settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_SAVE_MID)) {
items = ImmutableList.of(new IconMenuItem(R.drawable.ic_save, R.string.keep_changes),
new IconMenuItem(R.drawable.ic_delete, R.string.do_not_save));
} else {
items = ImmutableList.of(new IconMenuItem(R.drawable.ic_delete, R.string.do_not_save));
}

ListView listView = DialogUtils.createActionListView(getActivity());

final IconMenuListAdapter adapter = new IconMenuListAdapter(getActivity(), items);
listView.setAdapter(adapter);
listView.setOnItemClickListener((parent, view, position, id) -> {
IconMenuItem item = (IconMenuItem) adapter.getItem(position);
// WARNING: Custom ODK Changes
MaterialAlertDialogBuilder formExitDialogBuilder = new MaterialAlertDialogBuilder(getActivity())
.setTitle(getString(R.string.quit_application, title))
.setNegativeButton(getActivity().getString(R.string.do_not_exit), (dialog, id) -> {
dialog.cancel();
dismiss();
});

if (item.getTextResId() == R.string.keep_changes) {
if (settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_SAVE_MID)) {
if (settingsProvider.getProtectedSettings().getBoolean(ProtectedProjectKeys.KEY_SAVE_BY_DEFAULT)) {
formExitDialogBuilder.setMessage(R.string.confirm_exit_with_save);
}
else {
formExitDialogBuilder.setMessage(R.string.confirm_exit);
formExitDialogBuilder.setNeutralButton(getActivity().getString(R.string.exit), (dialog, id) -> {
formSaveViewModel.ignoreChanges();
formEntryViewModel.exit();

String action = getActivity().getIntent().getAction();
if (Intent.ACTION_PICK.equals(action) || Intent.ACTION_EDIT.equals(action)) {
// caller is waiting on a picked form
Uri uri = null;
String path = formSaveViewModel.getAbsoluteInstancePath();
if (path != null) {
Instance instance = new InstancesRepositoryProvider(requireContext()).get().getOneByPath(path);
if (instance != null) {
uri = InstancesContract.getUri(currentProjectProvider.getCurrentProject().getUuid(), instance.getDbId());
}
}
if (uri != null) {
getActivity().setResult(RESULT_OK, new Intent().setData(uri));
}
}
getActivity().finish();
if (getDialog() != null) {
getDialog().dismiss();
}
});
}
formExitDialogBuilder.setPositiveButton(getActivity().getString(R.string.keep_changes), (dialog, id) -> {
if (listener != null) {
listener.onSaveChangesClicked();
}
} else {
if (getDialog() != null) {
getDialog().dismiss();
}
});
}
else {
formExitDialogBuilder.setMessage(R.string.confirm_exit_without_save);
formExitDialogBuilder.setPositiveButton(getActivity().getString(R.string.exit), (dialog, id) -> {
formSaveViewModel.ignoreChanges();
formEntryViewModel.exit();

Expand All @@ -118,27 +142,17 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
uri = InstancesContract.getUri(currentProjectProvider.getCurrentProject().getUuid(), instance.getDbId());
}
}

if (uri != null) {
getActivity().setResult(RESULT_OK, new Intent().setData(uri));
}
}
getActivity().finish();
}

if (getDialog() != null) {
getDialog().dismiss();
}
});

return new MaterialAlertDialogBuilder(getActivity())
.setTitle(getString(R.string.quit_application, title))
.setNegativeButton(getActivity().getString(R.string.do_not_exit), (dialog, id) -> {
dialog.cancel();
dismiss();
})
.setView(listView)
.create();
if (getDialog() != null) {
getDialog().dismiss();
}
});
}
return formExitDialogBuilder.create();
}

public interface Listener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ object ProtectedProjectKeys {
const val KEY_CHANGE_LANGUAGE = "change_language"
const val KEY_JUMP_TO = "jump_to"
const val KEY_SAVE_MID = "save_mid"
const val KEY_SAVE_BY_DEFAULT = "save_by_default"
const val KEY_SAVE_AS = "save_as"
const val KEY_MARK_AS_FINALIZED = "mark_as_finalized"

Expand Down Expand Up @@ -82,6 +83,7 @@ object ProtectedProjectKeys {
KEY_CHANGE_LANGUAGE,
KEY_JUMP_TO,
KEY_SAVE_MID,
KEY_SAVE_BY_DEFAULT,
KEY_SAVE_AS,
KEY_MARK_AS_FINALIZED,
ALLOW_OTHER_WAYS_OF_EDITING_FORM
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,9 @@
"save_mid": {
"type": "boolean"
},
"save_by_default": {
"type": "boolean"
},
"save_as": {
"type": "boolean"
},
Expand Down
3 changes: 3 additions & 0 deletions odk/collect/strings/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
<string name="no_form_loaded">&lt;no form loaded&gt;</string>
<string name="do_not_save">Ignore Changes</string>
<string name="keep_changes">Save Changes</string>
<string name="confirm_exit_without_save">Do you want to exit? The data filled so far will be lost.</string>
<string name="confirm_exit_with_save">Are you sure that you want to save the data and exit the form?</string>
<string name="confirm_exit">Are you sure that you want to exit the form? You may save the filled data for later use.</string>

<string name="saving_form">Saving Form</string>
<string name="survey_saving_validating_message">Validating answers…</string>
Expand Down

0 comments on commit 1d01d40

Please sign in to comment.