From e0218a92ec29e4210e278d53117218e4acd72679 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Tue, 4 Jul 2017 10:30:57 +0200 Subject: [PATCH 1/9] Dialogs - general approach --- .../activities/CollectAbstractActivity.java | 60 +++++++ .../android/bundle/CollectDialogBundle.java | 154 ++++++++++++++++++ .../fragments/CollectDialogFragment.java | 87 ++++++++++ .../preferences/AdminPreferencesActivity.java | 4 +- .../preferences/ResetDialogPreference.java | 26 +-- 5 files changed, 305 insertions(+), 26 deletions(-) create mode 100644 collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java create mode 100644 collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java create mode 100644 collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java new file mode 100644 index 00000000000..5d2cc93f784 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 Nafundi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.odk.collect.android.activities; + +import android.os.Bundle; +import android.support.v4.app.FragmentManager; +import android.support.v7.app.AppCompatActivity; + +import org.odk.collect.android.R; +import org.odk.collect.android.bundle.CollectDialogBundle; +import org.odk.collect.android.fragments.CollectDialogFragment; + +public abstract class CollectAbstractActivity extends AppCompatActivity { + protected FragmentManager fragmentManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + fragmentManager = getSupportFragmentManager(); + } + + public void buildDialog(CollectDialogBundle collectDialogBundle) { + if (collectDialogBundle != null) { + createAndShowDialog(collectDialogBundle); + } + } + + private void createAndShowDialog(CollectDialogBundle collectDialogBundle) { + CollectDialogFragment dialogFragment = CollectDialogFragment.newInstance(collectDialogBundle); + dialogFragment.show(fragmentManager, collectDialogBundle.getDialogTag()); + } + + public void buildResetSettingsFinalDialog(String message) { + CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); + dialogBuilder + .setIcon(android.R.drawable.ic_dialog_info) + .setDialogTitle(getString(R.string.reset_app_state_result)) + .setDialogMessage(message) + .setRightButtonText(getString(R.string.ok)) + .setRightButtonAction(CollectDialogFragment.Action.RESETTING_SETTINGS_FINISHED) + .setCancelable(false); + + CollectDialogBundle collectDialogBundle = dialogBuilder.build(); + buildDialog(collectDialogBundle); + } +} diff --git a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java new file mode 100644 index 00000000000..d2599fc4977 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java @@ -0,0 +1,154 @@ +/* + * Copyright 2017 Nafundi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.odk.collect.android.bundle; + +import org.odk.collect.android.fragments.CollectDialogFragment; + +import java.io.Serializable; + +public class CollectDialogBundle implements Serializable { + private static final String COLLECT_DIALOG_TAG = "collectDialogTag"; + + private String dialogTag; + private String dialogTitle; + private String dialogMessage; + private String leftButtonText; + private String rightButtonText; + + private CollectDialogFragment.Action leftButtonAction; + private CollectDialogFragment.Action rightButtonAction; + + private boolean cancelable; + + private int icon; + + CollectDialogBundle(Builder builder) { + dialogTag = builder.dialogTag; + dialogTitle = builder.dialogTitle; + dialogMessage = builder.dialogMessage; + leftButtonText = builder.leftButtonText; + rightButtonText = builder.rightButtonText; + leftButtonAction = builder.leftButtonAction; + rightButtonAction = builder.rightButtonAction; + cancelable = builder.cancelable; + icon = builder.icon; + } + + public String getDialogTag() { + return dialogTag; + } + + public String getDialogTitle() { + return dialogTitle; + } + + public String getDialogMessage() { + return dialogMessage; + } + + public String getLeftButtonText() { + return leftButtonText; + } + + public String getRightButtonText() { + return rightButtonText; + } + + public CollectDialogFragment.Action getLeftButtonAction() { + return leftButtonAction; + } + + public CollectDialogFragment.Action getRightButtonAction() { + return rightButtonAction; + } + + public boolean isCancelable() { + return cancelable; + } + + public Integer getIcon() { + return icon; + } + + public static class Builder { + + private String dialogTag; + private String dialogTitle; + private String dialogMessage; + private String leftButtonText; + private String rightButtonText; + + private CollectDialogFragment.Action leftButtonAction; + private CollectDialogFragment.Action rightButtonAction; + + private boolean cancelable; + + private int icon; + + public Builder() { + this(COLLECT_DIALOG_TAG); + } + + public Builder(String dialogTag) { + this.dialogTag = dialogTag; + } + + public Builder setDialogTitle(String dialogTitle) { + this.dialogTitle = dialogTitle; + return this; + } + + public Builder setDialogMessage(String dialogMessage) { + this.dialogMessage = dialogMessage; + return this; + } + + public Builder setLeftButtonText(String leftButtonText) { + this.leftButtonText = leftButtonText; + return this; + } + + public Builder setRightButtonText(String rightButtonText) { + this.rightButtonText = rightButtonText; + return this; + } + + public Builder setLeftButtonAction(CollectDialogFragment.Action leftButtonAction) { + this.leftButtonAction = leftButtonAction; + return this; + } + + public Builder setRightButtonAction(CollectDialogFragment.Action rightButtonAction) { + this.rightButtonAction = rightButtonAction; + return this; + } + + public Builder setCancelable(Boolean cancelable) { + this.cancelable = cancelable; + return this; + } + + public Builder setIcon(int icon) { + this.icon = icon; + return this; + } + + public CollectDialogBundle build() { + return new CollectDialogBundle(this); + } + } +} \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java new file mode 100644 index 00000000000..c83f34d6712 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java @@ -0,0 +1,87 @@ +/* + * Copyright 2017 Nafundi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.odk.collect.android.fragments; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.DialogInterface; +import android.os.Bundle; +import android.support.v4.app.DialogFragment; + +import org.odk.collect.android.bundle.CollectDialogBundle; + +public class CollectDialogFragment extends DialogFragment { + + public enum Action { RESETTING_SETTINGS_FINISHED } + + public static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; + + public static CollectDialogFragment newInstance(CollectDialogBundle collectDialogBundle) { + CollectDialogFragment dialogFragment = new CollectDialogFragment(); + Bundle bundle = new Bundle(); + bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); + dialogFragment.setArguments(bundle); + return dialogFragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final CollectDialogBundle collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + + if (collectDialogBundle != null) { + builder + .setTitle(collectDialogBundle.getDialogTitle()) + .setMessage(collectDialogBundle.getDialogMessage()) + .setCancelable(collectDialogBundle.isCancelable()); + + if (collectDialogBundle.getLeftButtonText() != null) { + builder.setNegativeButton(collectDialogBundle.getLeftButtonText(), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + resolveButtonAction(collectDialogBundle.getLeftButtonAction()); + } + }); + } + + if (collectDialogBundle.getRightButtonText() != null) { + builder.setPositiveButton(collectDialogBundle.getRightButtonText(), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + resolveButtonAction(collectDialogBundle.getRightButtonAction()); + } + }); + } + + if (collectDialogBundle.getIcon() != null) { + builder.setIcon(collectDialogBundle.getIcon()); + } + + setCancelable(collectDialogBundle.isCancelable()); + } + + return builder.create(); + } + + private void resolveButtonAction(final Action action) { + switch (action) { + case RESETTING_SETTINGS_FINISHED: + dismiss(); + getActivity().recreate(); + break; + default: + } + } +} \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java index 5bc7992a038..75448947f9b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java @@ -18,9 +18,9 @@ import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; -import android.support.v7.app.AppCompatActivity; import org.odk.collect.android.R; +import org.odk.collect.android.activities.CollectAbstractActivity; import java.io.File; import java.io.FileOutputStream; @@ -36,7 +36,7 @@ * @author Thomas Smyth, Sassafras Tech Collective (tom@sassafrastech.com; constraint behavior * option) */ -public class AdminPreferencesActivity extends AppCompatActivity { +public class AdminPreferencesActivity extends CollectAbstractActivity { public static final String ADMIN_PREFERENCES = "admin_prefs"; public static final String TAG = "AdminPreferencesFragment"; diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index ce1efffa73b..fce3eea6d12 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -16,7 +16,6 @@ package org.odk.collect.android.preferences; -import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -26,6 +25,7 @@ import android.widget.CheckBox; import org.odk.collect.android.R; +import org.odk.collect.android.activities.CollectAbstractActivity; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -177,28 +177,6 @@ private void handleResult(List resetActions, List failedResetA resultMessage.append("\n\n"); } } - showResultDialog(String.valueOf(resultMessage)); - } - - private void showResultDialog(final String resultMessage) { - ((AdminPreferencesActivity) context).runOnUiThread(new Runnable() { - @Override - public void run() { - AlertDialog.Builder b = new AlertDialog.Builder(getContext()); - b.setTitle(getContext().getString(R.string.reset_app_state_result)); - b.setMessage(resultMessage); - b.setCancelable(false); - b.setIcon(android.R.drawable.ic_dialog_info); - b.setPositiveButton(getContext().getString(R.string.ok), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - ((AdminPreferencesActivity) context).recreate(); - } - }); - AlertDialog alertDialog = b.create(); - alertDialog.show(); - } - }); + ((CollectAbstractActivity) context).buildResetSettingsFinalDialog(String.valueOf(resultMessage)); } } \ No newline at end of file From 61b93b6995bced8b55b3117cd43b14b4aab75890 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Thu, 6 Jul 2017 13:48:45 +0200 Subject: [PATCH 2/9] Moved buildResetSettingsFinalDialog method to ResetDialogPreference class --- .../activities/CollectAbstractActivity.java | 15 --------------- .../preferences/ResetDialogPreference.java | 18 +++++++++++++++++- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java index 5d2cc93f784..13b06f915f0 100644 --- a/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java @@ -20,7 +20,6 @@ import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; -import org.odk.collect.android.R; import org.odk.collect.android.bundle.CollectDialogBundle; import org.odk.collect.android.fragments.CollectDialogFragment; @@ -43,18 +42,4 @@ private void createAndShowDialog(CollectDialogBundle collectDialogBundle) { CollectDialogFragment dialogFragment = CollectDialogFragment.newInstance(collectDialogBundle); dialogFragment.show(fragmentManager, collectDialogBundle.getDialogTag()); } - - public void buildResetSettingsFinalDialog(String message) { - CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); - dialogBuilder - .setIcon(android.R.drawable.ic_dialog_info) - .setDialogTitle(getString(R.string.reset_app_state_result)) - .setDialogMessage(message) - .setRightButtonText(getString(R.string.ok)) - .setRightButtonAction(CollectDialogFragment.Action.RESETTING_SETTINGS_FINISHED) - .setCancelable(false); - - CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - buildDialog(collectDialogBundle); - } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index fce3eea6d12..78614d1076e 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -26,6 +26,8 @@ import org.odk.collect.android.R; import org.odk.collect.android.activities.CollectAbstractActivity; +import org.odk.collect.android.bundle.CollectDialogBundle; +import org.odk.collect.android.fragments.CollectDialogFragment; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -177,6 +179,20 @@ private void handleResult(List resetActions, List failedResetA resultMessage.append("\n\n"); } } - ((CollectAbstractActivity) context).buildResetSettingsFinalDialog(String.valueOf(resultMessage)); + buildResetSettingsFinalDialog(String.valueOf(resultMessage)); + } + + private void buildResetSettingsFinalDialog(String message) { + CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); + dialogBuilder + .setIcon(android.R.drawable.ic_dialog_info) + .setDialogTitle(getContext().getString(R.string.reset_app_state_result)) + .setDialogMessage(message) + .setRightButtonText(getContext().getString(R.string.ok)) + .setRightButtonAction(CollectDialogFragment.Action.RESETTING_SETTINGS_FINISHED) + .setCancelable(false); + + CollectDialogBundle collectDialogBundle = dialogBuilder.build(); + ((CollectAbstractActivity) context).buildDialog(collectDialogBundle); } } \ No newline at end of file From c4f28faf6eca1ff89816b030e1b95b3ef49ec6a0 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Fri, 7 Jul 2017 09:22:52 +0200 Subject: [PATCH 3/9] Improved previous solution --- .../android/bundle/CollectDialogBundle.java | 87 ++++++++++++------- .../fragments/CollectDialogFragment.java | 34 ++++---- .../preferences/ResetDialogPreference.java | 15 ++-- 3 files changed, 83 insertions(+), 53 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java index d2599fc4977..12b9c4f760b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java +++ b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java @@ -16,21 +16,24 @@ package org.odk.collect.android.bundle; -import org.odk.collect.android.fragments.CollectDialogFragment; +import android.app.Dialog; import java.io.Serializable; public class CollectDialogBundle implements Serializable { + private static final String COLLECT_DIALOG_TAG = "collectDialogTag"; private String dialogTag; private String dialogTitle; private String dialogMessage; - private String leftButtonText; - private String rightButtonText; + private String negativeButtonText; + private String positiveButtonText; + private String neutralButtonText; - private CollectDialogFragment.Action leftButtonAction; - private CollectDialogFragment.Action rightButtonAction; + public SingleButtonCallback onNegativeButtonClickCallback; + public SingleButtonCallback onPositiveButtonClickCallback; + public SingleButtonCallback onNeutralButtonClickCallback; private boolean cancelable; @@ -40,10 +43,12 @@ public class CollectDialogBundle implements Serializable { dialogTag = builder.dialogTag; dialogTitle = builder.dialogTitle; dialogMessage = builder.dialogMessage; - leftButtonText = builder.leftButtonText; - rightButtonText = builder.rightButtonText; - leftButtonAction = builder.leftButtonAction; - rightButtonAction = builder.rightButtonAction; + negativeButtonText = builder.negativeButtonText; + positiveButtonText = builder.positiveButtonText; + neutralButtonText = builder.neutralButtonText; + onNegativeButtonClickCallback = builder.onNegativeButtonClickCallback; + onPositiveButtonClickCallback = builder.onPositiveButtonClickCallback; + onNeutralButtonClickCallback = builder.onNeutralButtonClickCallback; cancelable = builder.cancelable; icon = builder.icon; } @@ -60,20 +65,28 @@ public String getDialogMessage() { return dialogMessage; } - public String getLeftButtonText() { - return leftButtonText; + public String getNegativeButtonText() { + return negativeButtonText; + } + + public String getPositiveButtonText() { + return positiveButtonText; } - public String getRightButtonText() { - return rightButtonText; + public String getNeutralButtonText() { + return neutralButtonText; } - public CollectDialogFragment.Action getLeftButtonAction() { - return leftButtonAction; + public SingleButtonCallback getOnNegativeButtonClickCallback() { + return onNegativeButtonClickCallback; } - public CollectDialogFragment.Action getRightButtonAction() { - return rightButtonAction; + public SingleButtonCallback getOnPositiveButtonClickCallback() { + return onPositiveButtonClickCallback; + } + + public SingleButtonCallback getOnNeutralButtonClickCallback() { + return onNeutralButtonClickCallback; } public boolean isCancelable() { @@ -89,11 +102,13 @@ public static class Builder { private String dialogTag; private String dialogTitle; private String dialogMessage; - private String leftButtonText; - private String rightButtonText; + private String negativeButtonText; + private String positiveButtonText; + private String neutralButtonText; - private CollectDialogFragment.Action leftButtonAction; - private CollectDialogFragment.Action rightButtonAction; + private SingleButtonCallback onNegativeButtonClickCallback; + private SingleButtonCallback onPositiveButtonClickCallback; + private SingleButtonCallback onNeutralButtonClickCallback; private boolean cancelable; @@ -117,23 +132,33 @@ public Builder setDialogMessage(String dialogMessage) { return this; } - public Builder setLeftButtonText(String leftButtonText) { - this.leftButtonText = leftButtonText; + public Builder setNegativeButtonText(String negativeButtonText) { + this.negativeButtonText = negativeButtonText; return this; } - public Builder setRightButtonText(String rightButtonText) { - this.rightButtonText = rightButtonText; + public Builder setPositiveButtonText(String positiveButtonText) { + this.positiveButtonText = positiveButtonText; return this; } - public Builder setLeftButtonAction(CollectDialogFragment.Action leftButtonAction) { - this.leftButtonAction = leftButtonAction; + public Builder setNeutralButtonText(String neutralButtonText) { + this.neutralButtonText = neutralButtonText; return this; } - public Builder setRightButtonAction(CollectDialogFragment.Action rightButtonAction) { - this.rightButtonAction = rightButtonAction; + public Builder setOnNegatibeButtonClickCallback(SingleButtonCallback onNegativeButtonClickCallback) { + this.onNegativeButtonClickCallback = onNegativeButtonClickCallback; + return this; + } + + public Builder setOnPositiveButtonClickCallback(SingleButtonCallback onPositiveButtonClickCallback) { + this.onPositiveButtonClickCallback = onPositiveButtonClickCallback; + return this; + } + + public Builder setOnNeutralButtonClickCallback(SingleButtonCallback onNeutralButtonClickCallback) { + this.onNeutralButtonClickCallback = onNeutralButtonClickCallback; return this; } @@ -151,4 +176,8 @@ public CollectDialogBundle build() { return new CollectDialogBundle(this); } } + + public interface SingleButtonCallback { + void onClick(Dialog dialog); + } } \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java index c83f34d6712..61b34a07743 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java @@ -26,8 +26,6 @@ public class CollectDialogFragment extends DialogFragment { - public enum Action { RESETTING_SETTINGS_FINISHED } - public static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; public static CollectDialogFragment newInstance(CollectDialogBundle collectDialogBundle) { @@ -41,7 +39,7 @@ public static CollectDialogFragment newInstance(CollectDialogBundle collectDialo @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final CollectDialogBundle collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); if (collectDialogBundle != null) { builder @@ -49,18 +47,26 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { .setMessage(collectDialogBundle.getDialogMessage()) .setCancelable(collectDialogBundle.isCancelable()); - if (collectDialogBundle.getLeftButtonText() != null) { - builder.setNegativeButton(collectDialogBundle.getLeftButtonText(), new DialogInterface.OnClickListener() { + if (collectDialogBundle.getOnNegativeButtonClickCallback() != null) { + builder.setNegativeButton(collectDialogBundle.getNegativeButtonText(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - resolveButtonAction(collectDialogBundle.getLeftButtonAction()); + collectDialogBundle.onNegativeButtonClickCallback.onClick(getDialog()); } }); } - if (collectDialogBundle.getRightButtonText() != null) { - builder.setPositiveButton(collectDialogBundle.getRightButtonText(), new DialogInterface.OnClickListener() { + if (collectDialogBundle.getOnPositiveButtonClickCallback() != null) { + builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { - resolveButtonAction(collectDialogBundle.getRightButtonAction()); + collectDialogBundle.onPositiveButtonClickCallback.onClick(getDialog()); + } + }); + } + + if (collectDialogBundle.getOnNeutralButtonClickCallback() != null) { + builder.setNeutralButton(collectDialogBundle.getNeutralButtonText(), new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int id) { + collectDialogBundle.onNeutralButtonClickCallback.onClick(getDialog()); } }); } @@ -74,14 +80,4 @@ public void onClick(DialogInterface dialog, int id) { return builder.create(); } - - private void resolveButtonAction(final Action action) { - switch (action) { - case RESETTING_SETTINGS_FINISHED: - dismiss(); - getActivity().recreate(); - break; - default: - } - } } \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index 78614d1076e..8e263661288 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -16,6 +16,7 @@ package org.odk.collect.android.preferences; +import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -27,7 +28,6 @@ import org.odk.collect.android.R; import org.odk.collect.android.activities.CollectAbstractActivity; import org.odk.collect.android.bundle.CollectDialogBundle; -import org.odk.collect.android.fragments.CollectDialogFragment; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -188,11 +188,16 @@ private void buildResetSettingsFinalDialog(String message) { .setIcon(android.R.drawable.ic_dialog_info) .setDialogTitle(getContext().getString(R.string.reset_app_state_result)) .setDialogMessage(message) - .setRightButtonText(getContext().getString(R.string.ok)) - .setRightButtonAction(CollectDialogFragment.Action.RESETTING_SETTINGS_FINISHED) + .setPositiveButtonText(getContext().getString(R.string.ok)) + .setOnPositiveButtonClickCallback(new CollectDialogBundle.SingleButtonCallback() { + @Override + public void onClick(Dialog dialog) { + dialog.dismiss(); + ((AdminPreferencesActivity) context).recreate(); + } + }) .setCancelable(false); - CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - ((CollectAbstractActivity) context).buildDialog(collectDialogBundle); + ((CollectAbstractActivity) context).buildDialog(dialogBuilder.build()); } } \ No newline at end of file From 8ca4acee20ba224b45950c764a8b8ef6cc00210b Mon Sep 17 00:00:00 2001 From: grzegorz Date: Tue, 18 Jul 2017 11:51:12 +0200 Subject: [PATCH 4/9] Third approach --- .../activities/CollectAbstractActivity.java | 45 ------------- .../android/bundle/CollectDialogBundle.java | 44 ------------ .../fragments/CollectDialogFragment.java | 46 ++----------- .../fragments/ResetSettingsFinalDialog.java | 67 +++++++++++++++++++ .../preferences/AdminPreferencesActivity.java | 12 +++- .../preferences/ResetDialogPreference.java | 14 ++-- 6 files changed, 86 insertions(+), 142 deletions(-) delete mode 100644 collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java create mode 100644 collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java deleted file mode 100644 index 13b06f915f0..00000000000 --- a/collect_app/src/main/java/org/odk/collect/android/activities/CollectAbstractActivity.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2017 Nafundi - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.odk.collect.android.activities; - -import android.os.Bundle; -import android.support.v4.app.FragmentManager; -import android.support.v7.app.AppCompatActivity; - -import org.odk.collect.android.bundle.CollectDialogBundle; -import org.odk.collect.android.fragments.CollectDialogFragment; - -public abstract class CollectAbstractActivity extends AppCompatActivity { - protected FragmentManager fragmentManager; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - fragmentManager = getSupportFragmentManager(); - } - - public void buildDialog(CollectDialogBundle collectDialogBundle) { - if (collectDialogBundle != null) { - createAndShowDialog(collectDialogBundle); - } - } - - private void createAndShowDialog(CollectDialogBundle collectDialogBundle) { - CollectDialogFragment dialogFragment = CollectDialogFragment.newInstance(collectDialogBundle); - dialogFragment.show(fragmentManager, collectDialogBundle.getDialogTag()); - } -} diff --git a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java index 12b9c4f760b..e26b1f38ff9 100644 --- a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java +++ b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java @@ -16,8 +16,6 @@ package org.odk.collect.android.bundle; -import android.app.Dialog; - import java.io.Serializable; public class CollectDialogBundle implements Serializable { @@ -31,10 +29,6 @@ public class CollectDialogBundle implements Serializable { private String positiveButtonText; private String neutralButtonText; - public SingleButtonCallback onNegativeButtonClickCallback; - public SingleButtonCallback onPositiveButtonClickCallback; - public SingleButtonCallback onNeutralButtonClickCallback; - private boolean cancelable; private int icon; @@ -46,9 +40,6 @@ public class CollectDialogBundle implements Serializable { negativeButtonText = builder.negativeButtonText; positiveButtonText = builder.positiveButtonText; neutralButtonText = builder.neutralButtonText; - onNegativeButtonClickCallback = builder.onNegativeButtonClickCallback; - onPositiveButtonClickCallback = builder.onPositiveButtonClickCallback; - onNeutralButtonClickCallback = builder.onNeutralButtonClickCallback; cancelable = builder.cancelable; icon = builder.icon; } @@ -77,18 +68,6 @@ public String getNeutralButtonText() { return neutralButtonText; } - public SingleButtonCallback getOnNegativeButtonClickCallback() { - return onNegativeButtonClickCallback; - } - - public SingleButtonCallback getOnPositiveButtonClickCallback() { - return onPositiveButtonClickCallback; - } - - public SingleButtonCallback getOnNeutralButtonClickCallback() { - return onNeutralButtonClickCallback; - } - public boolean isCancelable() { return cancelable; } @@ -106,10 +85,6 @@ public static class Builder { private String positiveButtonText; private String neutralButtonText; - private SingleButtonCallback onNegativeButtonClickCallback; - private SingleButtonCallback onPositiveButtonClickCallback; - private SingleButtonCallback onNeutralButtonClickCallback; - private boolean cancelable; private int icon; @@ -147,21 +122,6 @@ public Builder setNeutralButtonText(String neutralButtonText) { return this; } - public Builder setOnNegatibeButtonClickCallback(SingleButtonCallback onNegativeButtonClickCallback) { - this.onNegativeButtonClickCallback = onNegativeButtonClickCallback; - return this; - } - - public Builder setOnPositiveButtonClickCallback(SingleButtonCallback onPositiveButtonClickCallback) { - this.onPositiveButtonClickCallback = onPositiveButtonClickCallback; - return this; - } - - public Builder setOnNeutralButtonClickCallback(SingleButtonCallback onNeutralButtonClickCallback) { - this.onNeutralButtonClickCallback = onNeutralButtonClickCallback; - return this; - } - public Builder setCancelable(Boolean cancelable) { this.cancelable = cancelable; return this; @@ -176,8 +136,4 @@ public CollectDialogBundle build() { return new CollectDialogBundle(this); } } - - public interface SingleButtonCallback { - void onClick(Dialog dialog); - } } \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java index 61b34a07743..97c1f2f0238 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java @@ -18,62 +18,26 @@ import android.app.AlertDialog; import android.app.Dialog; -import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import org.odk.collect.android.bundle.CollectDialogBundle; -public class CollectDialogFragment extends DialogFragment { +public abstract class CollectDialogFragment extends DialogFragment { public static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; - public static CollectDialogFragment newInstance(CollectDialogBundle collectDialogBundle) { - CollectDialogFragment dialogFragment = new CollectDialogFragment(); - Bundle bundle = new Bundle(); - bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); - dialogFragment.setArguments(bundle); - return dialogFragment; - } + protected AlertDialog.Builder builder; + + protected CollectDialogBundle collectDialogBundle; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - final CollectDialogBundle collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); - final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - if (collectDialogBundle != null) { builder .setTitle(collectDialogBundle.getDialogTitle()) .setMessage(collectDialogBundle.getDialogMessage()) - .setCancelable(collectDialogBundle.isCancelable()); - - if (collectDialogBundle.getOnNegativeButtonClickCallback() != null) { - builder.setNegativeButton(collectDialogBundle.getNegativeButtonText(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - collectDialogBundle.onNegativeButtonClickCallback.onClick(getDialog()); - } - }); - } - - if (collectDialogBundle.getOnPositiveButtonClickCallback() != null) { - builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - collectDialogBundle.onPositiveButtonClickCallback.onClick(getDialog()); - } - }); - } - - if (collectDialogBundle.getOnNeutralButtonClickCallback() != null) { - builder.setNeutralButton(collectDialogBundle.getNeutralButtonText(), new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int id) { - collectDialogBundle.onNeutralButtonClickCallback.onClick(getDialog()); - } - }); - } - - if (collectDialogBundle.getIcon() != null) { - builder.setIcon(collectDialogBundle.getIcon()); - } + .setIcon(collectDialogBundle.getIcon()); setCancelable(collectDialogBundle.isCancelable()); } diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java new file mode 100644 index 00000000000..286e01e39a2 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java @@ -0,0 +1,67 @@ +/* + * Copyright 2017 Nafundi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.odk.collect.android.fragments; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; + +import org.odk.collect.android.bundle.CollectDialogBundle; + +public class ResetSettingsFinalDialog extends CollectDialogFragment { + + public interface ResetSettingsFinalDialogCallbacks { + void finishResetSettings(DialogInterface dialog); + } + + private ResetSettingsFinalDialogCallbacks callback; + + public static ResetSettingsFinalDialog newInstance(CollectDialogBundle collectDialogBundle) { + ResetSettingsFinalDialog dialogFragment = new ResetSettingsFinalDialog(); + Bundle bundle = new Bundle(); + bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); + dialogFragment.setArguments(bundle); + return dialogFragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); + builder = new AlertDialog.Builder(getActivity()); + + builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if (callback != null) { + callback.finishResetSettings(dialog); + } + } + }); + + super.onCreateDialog(savedInstanceState); + + return builder.create(); + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + callback = (ResetSettingsFinalDialogCallbacks) context; + } +} diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java index 75448947f9b..02410af233f 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java @@ -15,12 +15,14 @@ package org.odk.collect.android.preferences; import android.content.Context; +import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; +import android.support.v7.app.AppCompatActivity; import org.odk.collect.android.R; -import org.odk.collect.android.activities.CollectAbstractActivity; +import org.odk.collect.android.fragments.ResetSettingsFinalDialog; import java.io.File; import java.io.FileOutputStream; @@ -36,7 +38,7 @@ * @author Thomas Smyth, Sassafras Tech Collective (tom@sassafrastech.com; constraint behavior * option) */ -public class AdminPreferencesActivity extends CollectAbstractActivity { +public class AdminPreferencesActivity extends AppCompatActivity implements ResetSettingsFinalDialog.ResetSettingsFinalDialogCallbacks { public static final String ADMIN_PREFERENCES = "admin_prefs"; public static final String TAG = "AdminPreferencesFragment"; @@ -82,4 +84,10 @@ protected void onCreate(Bundle savedInstanceState) { .commit(); } } + + @Override + public void finishResetSettings(DialogInterface dialog) { + dialog.dismiss(); + recreate(); + } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index 8e263661288..c0b2b211d8a 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -16,7 +16,6 @@ package org.odk.collect.android.preferences; -import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -26,8 +25,8 @@ import android.widget.CheckBox; import org.odk.collect.android.R; -import org.odk.collect.android.activities.CollectAbstractActivity; import org.odk.collect.android.bundle.CollectDialogBundle; +import org.odk.collect.android.fragments.ResetSettingsFinalDialog; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -189,15 +188,10 @@ private void buildResetSettingsFinalDialog(String message) { .setDialogTitle(getContext().getString(R.string.reset_app_state_result)) .setDialogMessage(message) .setPositiveButtonText(getContext().getString(R.string.ok)) - .setOnPositiveButtonClickCallback(new CollectDialogBundle.SingleButtonCallback() { - @Override - public void onClick(Dialog dialog) { - dialog.dismiss(); - ((AdminPreferencesActivity) context).recreate(); - } - }) .setCancelable(false); - ((CollectAbstractActivity) context).buildDialog(dialogBuilder.build()); + CollectDialogBundle collectDialogBundle = dialogBuilder.build(); + ResetSettingsFinalDialog dialogFragment = ResetSettingsFinalDialog.newInstance(collectDialogBundle); + dialogFragment.show(((AdminPreferencesActivity) context).getSupportFragmentManager(), collectDialogBundle.getDialogTag()); } } \ No newline at end of file From 6c9278cf89e1c0eaf3849e40ed067ea91e276105 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Wed, 19 Jul 2017 12:59:01 +0200 Subject: [PATCH 5/9] Code improvements --- .../CollectAbstractDialog.java} | 6 +++--- .../fragments/{ => dialogs}/ResetSettingsFinalDialog.java | 4 ++-- .../android/preferences/AdminPreferencesActivity.java | 2 +- .../collect/android/preferences/ResetDialogPreference.java | 6 +++--- 4 files changed, 9 insertions(+), 9 deletions(-) rename collect_app/src/main/java/org/odk/collect/android/fragments/{CollectDialogFragment.java => dialogs/CollectAbstractDialog.java} (87%) rename collect_app/src/main/java/org/odk/collect/android/fragments/{ => dialogs}/ResetSettingsFinalDialog.java (94%) diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java similarity index 87% rename from collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java rename to collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java index 97c1f2f0238..4b1e4821ff5 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/CollectDialogFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.odk.collect.android.fragments; +package org.odk.collect.android.fragments.dialogs; import android.app.AlertDialog; import android.app.Dialog; @@ -23,9 +23,9 @@ import org.odk.collect.android.bundle.CollectDialogBundle; -public abstract class CollectDialogFragment extends DialogFragment { +public abstract class CollectAbstractDialog extends DialogFragment { - public static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; + protected static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; protected AlertDialog.Builder builder; diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java similarity index 94% rename from collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java rename to collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java index 286e01e39a2..41135bcbf6d 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/ResetSettingsFinalDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.odk.collect.android.fragments; +package org.odk.collect.android.fragments.dialogs; import android.app.AlertDialog; import android.app.Dialog; @@ -24,7 +24,7 @@ import org.odk.collect.android.bundle.CollectDialogBundle; -public class ResetSettingsFinalDialog extends CollectDialogFragment { +public class ResetSettingsFinalDialog extends CollectAbstractDialog { public interface ResetSettingsFinalDialogCallbacks { void finishResetSettings(DialogInterface dialog); diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java index 02410af233f..f4eff103ffd 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java @@ -22,7 +22,7 @@ import android.support.v7.app.AppCompatActivity; import org.odk.collect.android.R; -import org.odk.collect.android.fragments.ResetSettingsFinalDialog; +import org.odk.collect.android.fragments.dialogs.ResetSettingsFinalDialog; import java.io.File; import java.io.FileOutputStream; diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index c0b2b211d8a..59a0e1a48cb 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -26,7 +26,7 @@ import org.odk.collect.android.R; import org.odk.collect.android.bundle.CollectDialogBundle; -import org.odk.collect.android.fragments.ResetSettingsFinalDialog; +import org.odk.collect.android.fragments.dialogs.ResetSettingsFinalDialog; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -178,10 +178,10 @@ private void handleResult(List resetActions, List failedResetA resultMessage.append("\n\n"); } } - buildResetSettingsFinalDialog(String.valueOf(resultMessage)); + buildAndShowResetSettingsFinalDialog(String.valueOf(resultMessage)); } - private void buildResetSettingsFinalDialog(String message) { + private void buildAndShowResetSettingsFinalDialog(String message) { CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); dialogBuilder .setIcon(android.R.drawable.ic_dialog_info) From a47d972a8449211c96295ca210777c4b214b8e80 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Wed, 19 Jul 2017 14:13:29 +0200 Subject: [PATCH 6/9] Replaced remove answer dialog --- .../android/activities/FormEntryActivity.java | 74 +++++++++---------- .../dialogs/RemoveResponseDialog.java | 73 ++++++++++++++++++ .../odk/collect/android/views/ODKView.java | 9 +++ 3 files changed, 118 insertions(+), 38 deletions(-) create mode 100644 collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java index a04df4d8271..aee303fe3ae 100644 --- a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java @@ -70,10 +70,12 @@ import org.javarosa.form.api.FormEntryPrompt; import org.odk.collect.android.R; import org.odk.collect.android.application.Collect; +import org.odk.collect.android.bundle.CollectDialogBundle; import org.odk.collect.android.dao.FormsDao; import org.odk.collect.android.dao.InstancesDao; import org.odk.collect.android.exception.GDriveConnectionException; import org.odk.collect.android.exception.JavaRosaException; +import org.odk.collect.android.fragments.dialogs.RemoveResponseDialog; import org.odk.collect.android.listeners.AdvanceToNextListener; import org.odk.collect.android.listeners.FormLoaderListener; import org.odk.collect.android.listeners.FormSavedListener; @@ -120,7 +122,7 @@ */ public class FormEntryActivity extends AppCompatActivity implements AnimationListener, FormLoaderListener, FormSavedListener, AdvanceToNextListener, - OnGestureListener, SavePointListener { + OnGestureListener, SavePointListener, RemoveResponseDialog.RemoveResponseDialogCallbacks { // save with every swipe forward or back. Timings indicate this takes .25 // seconds. @@ -183,6 +185,8 @@ public class FormEntryActivity extends AppCompatActivity implements AnimationLis // Tracks whether we are autosaving public static final String KEY_AUTO_SAVED = "autosaved"; + public static final String KEY_QUESTION_WIDGET_ID = "questionWidgetId"; + private static final int PROGRESS_DIALOG = 1; private static final int SAVING_DIALOG = 2; private static final int SAVING_IMAGE_DIALOG = 3; @@ -225,6 +229,8 @@ public class FormEntryActivity extends AppCompatActivity implements AnimationLis private String stepMessage = ""; private Toolbar toolbar; + private int questionWidgetId; + enum AnimationType { LEFT, RIGHT, FADE } @@ -318,6 +324,7 @@ public void onClick(View v) { if (savedInstanceState.containsKey(KEY_AUTO_SAVED)) { autoSaved = savedInstanceState.getBoolean(KEY_AUTO_SAVED); } + questionWidgetId = savedInstanceState.getInt(KEY_QUESTION_WIDGET_ID); } // If a parse error message is showing then nothing else is loaded @@ -578,6 +585,7 @@ protected void onSaveInstanceState(Bundle outState) { outState.putBoolean(NEWFORM, false); outState.putString(KEY_ERROR, errorMessage); outState.putBoolean(KEY_AUTO_SAVED, autoSaved); + outState.putInt(KEY_QUESTION_WIDGET_ID, questionWidgetId); } @Override @@ -1041,6 +1049,7 @@ public boolean onContextItemSelected(MenuItem item) { .getActivityLogger() .logInstanceAction(this, "onContextItemSelected", "createClearDialog", qw.getPrompt().getIndex()); + questionWidgetId = qw.getId(); createClearDialog(qw); break; } @@ -2047,14 +2056,10 @@ private void removeTempInstance() { * Confirm clear answer dialog */ private void createClearDialog(final QuestionWidget qw) { - Collect.getInstance() + Collect + .getInstance() .getActivityLogger() - .logInstanceAction(this, "createClearDialog", "show", - qw.getPrompt().getIndex()); - alertDialog = new AlertDialog.Builder(this).create(); - alertDialog.setIcon(android.R.drawable.ic_dialog_info); - - alertDialog.setTitle(getString(R.string.clear_answer_ask)); + .logInstanceAction(this, "createClearDialog", "show", qw.getPrompt().getIndex()); String question = qw.getPrompt().getLongText(); if (question == null) { @@ -2064,37 +2069,18 @@ private void createClearDialog(final QuestionWidget qw) { question = question.substring(0, 50) + "..."; } - alertDialog.setMessage(getString(R.string.clearanswer_confirm, - question)); - - DialogInterface.OnClickListener quitListener = new DialogInterface.OnClickListener() { + CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); + dialogBuilder + .setIcon(android.R.drawable.ic_dialog_info) + .setDialogTitle(getString(R.string.clear_answer_ask)) + .setDialogMessage(getString(R.string.clearanswer_confirm, question)) + .setPositiveButtonText(getString(R.string.discard_answer)) + .setNegativeButtonText(getString(R.string.clear_answer_no)) + .setCancelable(false); - @Override - public void onClick(DialogInterface dialog, int i) { - switch (i) { - case DialogInterface.BUTTON_POSITIVE: // yes - Collect.getInstance() - .getActivityLogger() - .logInstanceAction(this, "createClearDialog", - "clearAnswer", qw.getPrompt().getIndex()); - clearAnswer(qw); - saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS); - break; - case DialogInterface.BUTTON_NEGATIVE: // no - Collect.getInstance() - .getActivityLogger() - .logInstanceAction(this, "createClearDialog", - "cancel", qw.getPrompt().getIndex()); - break; - } - } - }; - alertDialog.setCancelable(false); - alertDialog - .setButton(getString(R.string.discard_answer), quitListener); - alertDialog.setButton2(getString(R.string.clear_answer_no), - quitListener); - alertDialog.show(); + CollectDialogBundle collectDialogBundle = dialogBuilder.build(); + RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(collectDialogBundle); + dialogFragment.show(getSupportFragmentManager(), collectDialogBundle.getDialogTag()); } /** @@ -2933,4 +2919,16 @@ public EmptyView(Context context) { public void allowSwiping(boolean doSwipe) { this.doSwipe = doSwipe; } + + @Override + public void removeAnswer(DialogInterface dialog) { + QuestionWidget qw = ((ODKView) currentView).getQuestionWidgetByID(questionWidgetId); + Collect + .getInstance() + .getActivityLogger() + .logInstanceAction(this, "createClearDialog", "clearAnswer", qw.getPrompt().getIndex()); + + clearAnswer(qw); + saveAnswersForCurrentScreen(DO_NOT_EVALUATE_CONSTRAINTS); + } } diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java new file mode 100644 index 00000000000..c163864b5bd --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java @@ -0,0 +1,73 @@ +/* + * Copyright 2017 Nafundi + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.odk.collect.android.fragments.dialogs; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.Context; +import android.content.DialogInterface; +import android.os.Bundle; + +import org.odk.collect.android.bundle.CollectDialogBundle; + +public class RemoveResponseDialog extends CollectAbstractDialog { + + public interface RemoveResponseDialogCallbacks { + void removeAnswer(DialogInterface dialog); + } + + private RemoveResponseDialogCallbacks callback; + + public static RemoveResponseDialog newInstance(CollectDialogBundle collectDialogBundle) { + RemoveResponseDialog dialogFragment = new RemoveResponseDialog(); + Bundle bundle = new Bundle(); + bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); + dialogFragment.setArguments(bundle); + return dialogFragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); + builder = new AlertDialog.Builder(getActivity()); + + builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if (callback != null) { + callback.removeAnswer(dialog); + } + } + }); + + builder.setNegativeButton(collectDialogBundle.getNegativeButtonText(), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + } + }); + + super.onCreateDialog(savedInstanceState); + + return builder.create(); + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + callback = (RemoveResponseDialogCallbacks) context; + } +} \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/views/ODKView.java b/collect_app/src/main/java/org/odk/collect/android/views/ODKView.java index 49f746fe029..715b0267a6e 100644 --- a/collect_app/src/main/java/org/odk/collect/android/views/ODKView.java +++ b/collect_app/src/main/java/org/odk/collect/android/views/ODKView.java @@ -442,4 +442,13 @@ public void stopAudio() { widgets.get(0).stopAudio(); } + public QuestionWidget getQuestionWidgetByID(int questionWidgetId) { + QuestionWidget questionWidget = null; + for (QuestionWidget qw : getWidgets()) { + if (qw.getId() == questionWidgetId) { + questionWidget = qw; + } + } + return questionWidget; + } } From 6e5bf0009d2e4248b3be2c5daba71904938a7617 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Wed, 19 Jul 2017 14:20:38 +0200 Subject: [PATCH 7/9] Improved ResetSettingsFinalDialog --- .../dialogs/ResetSettingsFinalDialog.java | 16 ---------------- .../preferences/AdminPreferencesActivity.java | 10 +--------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java index 41135bcbf6d..29327df1b57 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java @@ -18,7 +18,6 @@ import android.app.AlertDialog; import android.app.Dialog; -import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; @@ -26,12 +25,6 @@ public class ResetSettingsFinalDialog extends CollectAbstractDialog { - public interface ResetSettingsFinalDialogCallbacks { - void finishResetSettings(DialogInterface dialog); - } - - private ResetSettingsFinalDialogCallbacks callback; - public static ResetSettingsFinalDialog newInstance(CollectDialogBundle collectDialogBundle) { ResetSettingsFinalDialog dialogFragment = new ResetSettingsFinalDialog(); Bundle bundle = new Bundle(); @@ -48,9 +41,6 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { - if (callback != null) { - callback.finishResetSettings(dialog); - } } }); @@ -58,10 +48,4 @@ public void onClick(DialogInterface dialog, int id) { return builder.create(); } - - @Override - public void onAttach(Context context) { - super.onAttach(context); - callback = (ResetSettingsFinalDialogCallbacks) context; - } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java index f4eff103ffd..5bc7992a038 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/AdminPreferencesActivity.java @@ -15,14 +15,12 @@ package org.odk.collect.android.preferences; import android.content.Context; -import android.content.DialogInterface; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v7.app.AppCompatActivity; import org.odk.collect.android.R; -import org.odk.collect.android.fragments.dialogs.ResetSettingsFinalDialog; import java.io.File; import java.io.FileOutputStream; @@ -38,7 +36,7 @@ * @author Thomas Smyth, Sassafras Tech Collective (tom@sassafrastech.com; constraint behavior * option) */ -public class AdminPreferencesActivity extends AppCompatActivity implements ResetSettingsFinalDialog.ResetSettingsFinalDialogCallbacks { +public class AdminPreferencesActivity extends AppCompatActivity { public static final String ADMIN_PREFERENCES = "admin_prefs"; public static final String TAG = "AdminPreferencesFragment"; @@ -84,10 +82,4 @@ protected void onCreate(Bundle savedInstanceState) { .commit(); } } - - @Override - public void finishResetSettings(DialogInterface dialog) { - dialog.dismiss(); - recreate(); - } } From 9da599e6e3c2670cc6e1b152f4a93419833ac39d Mon Sep 17 00:00:00 2001 From: grzegorz Date: Thu, 20 Jul 2017 09:22:56 +0200 Subject: [PATCH 8/9] Code improvements --- .../android/activities/FormEntryActivity.java | 22 +++++++------- .../android/bundle/CollectDialogBundle.java | 18 ++--------- .../dialogs/CollectAbstractDialog.java | 17 +++++++---- .../dialogs/RemoveResponseDialog.java | 30 +++++++++++-------- ...ingsFinalDialog.java => SimpleDialog.java} | 10 +++++-- .../preferences/ResetDialogPreference.java | 7 ++--- 6 files changed, 51 insertions(+), 53 deletions(-) rename collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/{ResetSettingsFinalDialog.java => SimpleDialog.java} (81%) diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java index aee303fe3ae..4dfde1ed6ff 100644 --- a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java @@ -2061,6 +2061,15 @@ private void createClearDialog(final QuestionWidget qw) { .getActivityLogger() .logInstanceAction(this, "createClearDialog", "show", qw.getPrompt().getIndex()); + CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); + dialogBuilder.setDialogMessage(getString(R.string.clearanswer_confirm, getQuestionForRemoveResponseDialog(qw))); + + CollectDialogBundle collectDialogBundle = dialogBuilder.build(); + RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(collectDialogBundle); + dialogFragment.show(getSupportFragmentManager(), collectDialogBundle.getDialogTag()); + } + + private String getQuestionForRemoveResponseDialog(QuestionWidget qw) { String question = qw.getPrompt().getLongText(); if (question == null) { question = ""; @@ -2069,18 +2078,7 @@ private void createClearDialog(final QuestionWidget qw) { question = question.substring(0, 50) + "..."; } - CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); - dialogBuilder - .setIcon(android.R.drawable.ic_dialog_info) - .setDialogTitle(getString(R.string.clear_answer_ask)) - .setDialogMessage(getString(R.string.clearanswer_confirm, question)) - .setPositiveButtonText(getString(R.string.discard_answer)) - .setNegativeButtonText(getString(R.string.clear_answer_no)) - .setCancelable(false); - - CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(collectDialogBundle); - dialogFragment.show(getSupportFragmentManager(), collectDialogBundle.getDialogTag()); + return question; } /** diff --git a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java index e26b1f38ff9..06d526b095a 100644 --- a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java +++ b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java @@ -29,9 +29,7 @@ public class CollectDialogBundle implements Serializable { private String positiveButtonText; private String neutralButtonText; - private boolean cancelable; - - private int icon; + private Integer icon; CollectDialogBundle(Builder builder) { dialogTag = builder.dialogTag; @@ -40,7 +38,6 @@ public class CollectDialogBundle implements Serializable { negativeButtonText = builder.negativeButtonText; positiveButtonText = builder.positiveButtonText; neutralButtonText = builder.neutralButtonText; - cancelable = builder.cancelable; icon = builder.icon; } @@ -68,10 +65,6 @@ public String getNeutralButtonText() { return neutralButtonText; } - public boolean isCancelable() { - return cancelable; - } - public Integer getIcon() { return icon; } @@ -85,9 +78,7 @@ public static class Builder { private String positiveButtonText; private String neutralButtonText; - private boolean cancelable; - - private int icon; + private Integer icon; public Builder() { this(COLLECT_DIALOG_TAG); @@ -122,11 +113,6 @@ public Builder setNeutralButtonText(String neutralButtonText) { return this; } - public Builder setCancelable(Boolean cancelable) { - this.cancelable = cancelable; - return this; - } - public Builder setIcon(int icon) { this.icon = icon; return this; diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java index 4b1e4821ff5..a888a0f860e 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java @@ -34,12 +34,19 @@ public abstract class CollectAbstractDialog extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { if (collectDialogBundle != null) { - builder - .setTitle(collectDialogBundle.getDialogTitle()) - .setMessage(collectDialogBundle.getDialogMessage()) - .setIcon(collectDialogBundle.getIcon()); + if (collectDialogBundle.getDialogTitle() != null) { + builder.setTitle(collectDialogBundle.getDialogTitle()); + } - setCancelable(collectDialogBundle.isCancelable()); + if (collectDialogBundle.getDialogMessage() != null) { + builder.setMessage(collectDialogBundle.getDialogMessage()); + } + + if (collectDialogBundle.getIcon() != null) { + builder.setIcon(collectDialogBundle.getIcon()); + } + + setCancelable(false); } return builder.create(); diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java index c163864b5bd..17540ccc0c2 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java @@ -22,6 +22,7 @@ import android.content.DialogInterface; import android.os.Bundle; +import org.odk.collect.android.R; import org.odk.collect.android.bundle.CollectDialogBundle; public class RemoveResponseDialog extends CollectAbstractDialog { @@ -45,20 +46,23 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); builder = new AlertDialog.Builder(getActivity()); - builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - if (callback != null) { - callback.removeAnswer(dialog); - } - } - }); + builder + .setPositiveButton(getString(R.string.discard_answer), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + if (callback != null) { + callback.removeAnswer(dialog); + } + } + }) + .setNegativeButton(getString(R.string.clear_answer_no), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + } + }) + .setIcon(android.R.drawable.ic_dialog_info) + .setTitle(getContext().getString(R.string.clear_answer_ask)); - builder.setNegativeButton(collectDialogBundle.getNegativeButtonText(), new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int id) { - } - }); super.onCreateDialog(savedInstanceState); diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java similarity index 81% rename from collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java rename to collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java index 29327df1b57..643492a8206 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/ResetSettingsFinalDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java @@ -23,10 +23,14 @@ import org.odk.collect.android.bundle.CollectDialogBundle; -public class ResetSettingsFinalDialog extends CollectAbstractDialog { +/** + * This class might be used as an universal simple dialog. You can use it if you just need to + * display it and you don't need any callback. + */ +public class SimpleDialog extends CollectAbstractDialog { - public static ResetSettingsFinalDialog newInstance(CollectDialogBundle collectDialogBundle) { - ResetSettingsFinalDialog dialogFragment = new ResetSettingsFinalDialog(); + public static SimpleDialog newInstance(CollectDialogBundle collectDialogBundle) { + SimpleDialog dialogFragment = new SimpleDialog(); Bundle bundle = new Bundle(); bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); dialogFragment.setArguments(bundle); diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index 59a0e1a48cb..6dfba7392c2 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -26,7 +26,7 @@ import org.odk.collect.android.R; import org.odk.collect.android.bundle.CollectDialogBundle; -import org.odk.collect.android.fragments.dialogs.ResetSettingsFinalDialog; +import org.odk.collect.android.fragments.dialogs.SimpleDialog; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -187,11 +187,10 @@ private void buildAndShowResetSettingsFinalDialog(String message) { .setIcon(android.R.drawable.ic_dialog_info) .setDialogTitle(getContext().getString(R.string.reset_app_state_result)) .setDialogMessage(message) - .setPositiveButtonText(getContext().getString(R.string.ok)) - .setCancelable(false); + .setPositiveButtonText(getContext().getString(R.string.ok)); CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - ResetSettingsFinalDialog dialogFragment = ResetSettingsFinalDialog.newInstance(collectDialogBundle); + SimpleDialog dialogFragment = SimpleDialog.newInstance(collectDialogBundle); dialogFragment.show(((AdminPreferencesActivity) context).getSupportFragmentManager(), collectDialogBundle.getDialogTag()); } } \ No newline at end of file From 50e157f9bf9d1f9ef810b065131214a5619c673a Mon Sep 17 00:00:00 2001 From: grzegorz Date: Thu, 20 Jul 2017 21:24:03 +0200 Subject: [PATCH 9/9] Changes --- .../android/activities/FormEntryActivity.java | 10 +- .../android/bundle/CollectDialogBundle.java | 125 ------------------ .../dialogs/CollectAbstractDialog.java | 32 +++-- .../dialogs/RemoveResponseDialog.java | 6 +- .../fragments/dialogs/SimpleDialog.java | 14 +- .../preferences/ResetDialogPreference.java | 14 +- 6 files changed, 31 insertions(+), 170 deletions(-) delete mode 100644 collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java diff --git a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java index 4dfde1ed6ff..07f6a7e23ef 100644 --- a/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java +++ b/collect_app/src/main/java/org/odk/collect/android/activities/FormEntryActivity.java @@ -70,11 +70,11 @@ import org.javarosa.form.api.FormEntryPrompt; import org.odk.collect.android.R; import org.odk.collect.android.application.Collect; -import org.odk.collect.android.bundle.CollectDialogBundle; import org.odk.collect.android.dao.FormsDao; import org.odk.collect.android.dao.InstancesDao; import org.odk.collect.android.exception.GDriveConnectionException; import org.odk.collect.android.exception.JavaRosaException; +import org.odk.collect.android.fragments.dialogs.CollectAbstractDialog; import org.odk.collect.android.fragments.dialogs.RemoveResponseDialog; import org.odk.collect.android.listeners.AdvanceToNextListener; import org.odk.collect.android.listeners.FormLoaderListener; @@ -2061,12 +2061,8 @@ private void createClearDialog(final QuestionWidget qw) { .getActivityLogger() .logInstanceAction(this, "createClearDialog", "show", qw.getPrompt().getIndex()); - CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); - dialogBuilder.setDialogMessage(getString(R.string.clearanswer_confirm, getQuestionForRemoveResponseDialog(qw))); - - CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(collectDialogBundle); - dialogFragment.show(getSupportFragmentManager(), collectDialogBundle.getDialogTag()); + RemoveResponseDialog dialogFragment = RemoveResponseDialog.newInstance(getString(R.string.clearanswer_confirm, getQuestionForRemoveResponseDialog(qw))); + dialogFragment.show(getSupportFragmentManager(), CollectAbstractDialog.COLLECT_DIALOG_TAG); } private String getQuestionForRemoveResponseDialog(QuestionWidget qw) { diff --git a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java b/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java deleted file mode 100644 index 06d526b095a..00000000000 --- a/collect_app/src/main/java/org/odk/collect/android/bundle/CollectDialogBundle.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright 2017 Nafundi - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.odk.collect.android.bundle; - -import java.io.Serializable; - -public class CollectDialogBundle implements Serializable { - - private static final String COLLECT_DIALOG_TAG = "collectDialogTag"; - - private String dialogTag; - private String dialogTitle; - private String dialogMessage; - private String negativeButtonText; - private String positiveButtonText; - private String neutralButtonText; - - private Integer icon; - - CollectDialogBundle(Builder builder) { - dialogTag = builder.dialogTag; - dialogTitle = builder.dialogTitle; - dialogMessage = builder.dialogMessage; - negativeButtonText = builder.negativeButtonText; - positiveButtonText = builder.positiveButtonText; - neutralButtonText = builder.neutralButtonText; - icon = builder.icon; - } - - public String getDialogTag() { - return dialogTag; - } - - public String getDialogTitle() { - return dialogTitle; - } - - public String getDialogMessage() { - return dialogMessage; - } - - public String getNegativeButtonText() { - return negativeButtonText; - } - - public String getPositiveButtonText() { - return positiveButtonText; - } - - public String getNeutralButtonText() { - return neutralButtonText; - } - - public Integer getIcon() { - return icon; - } - - public static class Builder { - - private String dialogTag; - private String dialogTitle; - private String dialogMessage; - private String negativeButtonText; - private String positiveButtonText; - private String neutralButtonText; - - private Integer icon; - - public Builder() { - this(COLLECT_DIALOG_TAG); - } - - public Builder(String dialogTag) { - this.dialogTag = dialogTag; - } - - public Builder setDialogTitle(String dialogTitle) { - this.dialogTitle = dialogTitle; - return this; - } - - public Builder setDialogMessage(String dialogMessage) { - this.dialogMessage = dialogMessage; - return this; - } - - public Builder setNegativeButtonText(String negativeButtonText) { - this.negativeButtonText = negativeButtonText; - return this; - } - - public Builder setPositiveButtonText(String positiveButtonText) { - this.positiveButtonText = positiveButtonText; - return this; - } - - public Builder setNeutralButtonText(String neutralButtonText) { - this.neutralButtonText = neutralButtonText; - return this; - } - - public Builder setIcon(int icon) { - this.icon = icon; - return this; - } - - public CollectDialogBundle build() { - return new CollectDialogBundle(this); - } - } -} \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java index a888a0f860e..e73a790a279 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/CollectAbstractDialog.java @@ -21,34 +21,32 @@ import android.os.Bundle; import android.support.v4.app.DialogFragment; -import org.odk.collect.android.bundle.CollectDialogBundle; - public abstract class CollectAbstractDialog extends DialogFragment { - protected static final String COLLECT_DIALOG_BUNDLE = "collectDialogBundle"; + public static final String COLLECT_DIALOG_TAG = "collectDialogTag"; + protected static final String DIALOG_TITLE = "dialogTitle"; + protected static final String ICON_ID = "iconId"; + protected static final String MESSAGE = "message"; + protected static final String BUTTON_TITLE = "buttonTitle"; protected AlertDialog.Builder builder; - protected CollectDialogBundle collectDialogBundle; - @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - if (collectDialogBundle != null) { - if (collectDialogBundle.getDialogTitle() != null) { - builder.setTitle(collectDialogBundle.getDialogTitle()); - } - - if (collectDialogBundle.getDialogMessage() != null) { - builder.setMessage(collectDialogBundle.getDialogMessage()); - } + if (getArguments().getString(DIALOG_TITLE) != null) { + builder.setTitle(getArguments().getString(DIALOG_TITLE)); + } - if (collectDialogBundle.getIcon() != null) { - builder.setIcon(collectDialogBundle.getIcon()); - } + if (getArguments().getString(MESSAGE) != null) { + builder.setMessage(getArguments().getString(MESSAGE)); + } - setCancelable(false); + if (getArguments().getInt(ICON_ID) != 0) { + builder.setIcon(getArguments().getInt(ICON_ID)); } + setCancelable(false); + return builder.create(); } } \ No newline at end of file diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java index 17540ccc0c2..d0bc7705c01 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/RemoveResponseDialog.java @@ -23,7 +23,6 @@ import android.os.Bundle; import org.odk.collect.android.R; -import org.odk.collect.android.bundle.CollectDialogBundle; public class RemoveResponseDialog extends CollectAbstractDialog { @@ -33,17 +32,16 @@ public interface RemoveResponseDialogCallbacks { private RemoveResponseDialogCallbacks callback; - public static RemoveResponseDialog newInstance(CollectDialogBundle collectDialogBundle) { + public static RemoveResponseDialog newInstance(String message) { RemoveResponseDialog dialogFragment = new RemoveResponseDialog(); Bundle bundle = new Bundle(); - bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); + bundle.putString(MESSAGE, message); dialogFragment.setArguments(bundle); return dialogFragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); builder = new AlertDialog.Builder(getActivity()); builder diff --git a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java index 643492a8206..c6891f420f1 100644 --- a/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java @@ -21,28 +21,30 @@ import android.content.DialogInterface; import android.os.Bundle; -import org.odk.collect.android.bundle.CollectDialogBundle; - /** * This class might be used as an universal simple dialog. You can use it if you just need to * display it and you don't need any callback. */ public class SimpleDialog extends CollectAbstractDialog { - public static SimpleDialog newInstance(CollectDialogBundle collectDialogBundle) { + public static SimpleDialog newInstance(String dialogTitle, int iconId, String message, String buttonTitle) { SimpleDialog dialogFragment = new SimpleDialog(); + Bundle bundle = new Bundle(); - bundle.putSerializable(COLLECT_DIALOG_BUNDLE, collectDialogBundle); + bundle.putString(DIALOG_TITLE, dialogTitle); + bundle.putInt(ICON_ID, iconId); + bundle.putString(MESSAGE, message); + bundle.putString(BUTTON_TITLE, buttonTitle); + dialogFragment.setArguments(bundle); return dialogFragment; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { - collectDialogBundle = (CollectDialogBundle) getArguments().getSerializable(COLLECT_DIALOG_BUNDLE); builder = new AlertDialog.Builder(getActivity()); - builder.setPositiveButton(collectDialogBundle.getPositiveButtonText(), new DialogInterface.OnClickListener() { + builder.setPositiveButton(getArguments().getString(BUTTON_TITLE), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java index 6dfba7392c2..29ab70f412d 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/ResetDialogPreference.java @@ -25,7 +25,7 @@ import android.widget.CheckBox; import org.odk.collect.android.R; -import org.odk.collect.android.bundle.CollectDialogBundle; +import org.odk.collect.android.fragments.dialogs.CollectAbstractDialog; import org.odk.collect.android.fragments.dialogs.SimpleDialog; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; @@ -182,15 +182,7 @@ private void handleResult(List resetActions, List failedResetA } private void buildAndShowResetSettingsFinalDialog(String message) { - CollectDialogBundle.Builder dialogBuilder = new CollectDialogBundle.Builder(); - dialogBuilder - .setIcon(android.R.drawable.ic_dialog_info) - .setDialogTitle(getContext().getString(R.string.reset_app_state_result)) - .setDialogMessage(message) - .setPositiveButtonText(getContext().getString(R.string.ok)); - - CollectDialogBundle collectDialogBundle = dialogBuilder.build(); - SimpleDialog dialogFragment = SimpleDialog.newInstance(collectDialogBundle); - dialogFragment.show(((AdminPreferencesActivity) context).getSupportFragmentManager(), collectDialogBundle.getDialogTag()); + SimpleDialog dialogFragment = SimpleDialog.newInstance(getContext().getString(R.string.reset_app_state_result), android.R.drawable.ic_dialog_info, message, getContext().getString(R.string.ok)); + dialogFragment.show(((AdminPreferencesActivity) context).getSupportFragmentManager(), CollectAbstractDialog.COLLECT_DIALOG_TAG); } } \ No newline at end of file