From b0659317c3c376f51d10ff3f489b3b2930f5b1f7 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Fri, 21 Jul 2017 08:56:02 +0200 Subject: [PATCH 1/2] SimpleDialog --- .../fragments/dialogs/SimpleDialog.java | 66 +++++++++++++++++++ .../preferences/ResetDialogPreference.java | 29 +++----- 2 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java 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 new file mode 100644 index 00000000000..84ba38e2047 --- /dev/null +++ b/collect_app/src/main/java/org/odk/collect/android/fragments/dialogs/SimpleDialog.java @@ -0,0 +1,66 @@ +/* + * 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.DialogInterface; +import android.os.Bundle; + +import android.support.v4.app.DialogFragment; + +/** + * 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 DialogFragment { + + public static final String COLLECT_DIALOG_TAG = "collectDialogTag"; + + private static final String DIALOG_TITLE = "dialogTitle"; + private static final String ICON_ID = "iconId"; + private static final String MESSAGE = "message"; + private static final String BUTTON_TITLE = "buttonTitle"; + + public static SimpleDialog newInstance(String dialogTitle, int iconId, String message, String buttonTitle) { + Bundle bundle = new Bundle(); + bundle.putString(DIALOG_TITLE, dialogTitle); + bundle.putInt(ICON_ID, iconId); + bundle.putString(MESSAGE, message); + bundle.putString(BUTTON_TITLE, buttonTitle); + + SimpleDialog dialogFragment = new SimpleDialog(); + dialogFragment.setArguments(bundle); + return dialogFragment; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + setCancelable(false); + + return new AlertDialog.Builder(getActivity()) + .setTitle(getArguments().getString(DIALOG_TITLE)) + .setIcon(getArguments().getInt(ICON_ID)) + .setMessage(getArguments().getString(MESSAGE)) + .setPositiveButton(getArguments().getString(BUTTON_TITLE), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int id) { + } + }) + .create(); + } +} \ 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 ce1efffa73b..b3d6b704434 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,12 +25,15 @@ import android.widget.CheckBox; import org.odk.collect.android.R; +import org.odk.collect.android.fragments.dialogs.SimpleDialog; import org.odk.collect.android.utilities.ResetUtility; import org.odk.collect.android.utilities.ToastUtils; import java.util.ArrayList; import java.util.List; +import timber.log.Timber; + public class ResetDialogPreference extends DialogPreference { private CheckBox preferences; private CheckBox instances; @@ -181,24 +183,11 @@ private void handleResult(List resetActions, List failedResetA } 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(); - } - }); + SimpleDialog simpleDialog = SimpleDialog.newInstance(getContext().getString(R.string.reset_app_state_result), android.R.drawable.ic_dialog_info, resultMessage, getContext().getString(R.string.ok)); + try { + simpleDialog.show(((AdminPreferencesActivity) getContext()).getSupportFragmentManager(), SimpleDialog.COLLECT_DIALOG_TAG); + } catch (ClassCastException e) { + Timber.i(e); + } } } \ No newline at end of file From 447fe2f5afcdbae0ad3f39c8cd44ef05761dc848 Mon Sep 17 00:00:00 2001 From: grzegorz Date: Tue, 25 Jul 2017 08:51:16 +0200 Subject: [PATCH 2/2] Code improvements --- .../collect/android/preferences/ResetDialogPreference.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 b3d6b704434..4d27af7277a 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 @@ -183,7 +183,12 @@ private void handleResult(List resetActions, List failedResetA } private void showResultDialog(final String resultMessage) { - SimpleDialog simpleDialog = SimpleDialog.newInstance(getContext().getString(R.string.reset_app_state_result), android.R.drawable.ic_dialog_info, resultMessage, getContext().getString(R.string.ok)); + String dialogTitle = getContext().getString(R.string.reset_app_state_result); + int iconID = android.R.drawable.ic_dialog_info; + String buttonTitle = getContext().getString(R.string.ok); + + SimpleDialog simpleDialog = SimpleDialog.newInstance(dialogTitle, iconID, resultMessage, buttonTitle); + try { simpleDialog.show(((AdminPreferencesActivity) getContext()).getSupportFragmentManager(), SimpleDialog.COLLECT_DIALOG_TAG); } catch (ClassCastException e) {