From e341e77c8ecad955871e498270abe56eaa34173a Mon Sep 17 00:00:00 2001 From: Madhur Gupta <30932899+madhurgupta10@users.noreply.github.com> Date: Wed, 15 Apr 2020 10:34:34 +0200 Subject: [PATCH] Converted DialogUtil to Kotlin (#3621) * Converted DialogUtil to Kotlin * Kotlin syntax and standard changes * Removed ; * Added missing null * Added missing null * Removed unnecessary code * Reduced functions * added let to customView * reverted "let" changes * reverted "let" changes * removed if-statements * replaced with "it" * fixed overflow error * Function rename * Used named arguments * Fix Typo * single lined * Update DialogUtil.kt * changed default value --- .../fr/free/nrw/commons/utils/DialogUtil.java | 177 ------------------ .../fr/free/nrw/commons/utils/DialogUtil.kt | 161 ++++++++++++++++ 2 files changed, 161 insertions(+), 177 deletions(-) delete mode 100644 app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java create mode 100644 app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java deleted file mode 100644 index 25fae6e8e..000000000 --- a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.java +++ /dev/null @@ -1,177 +0,0 @@ -package fr.free.nrw.commons.utils; - -import android.app.Activity; -import android.app.AlertDialog; -import android.app.Dialog; -import android.content.DialogInterface; -import android.view.View; - -import org.apache.commons.lang3.StringUtils; - -import fr.free.nrw.commons.R; -import timber.log.Timber; - -public class DialogUtil { - - /** - * Shows a dialog safely. - * @param activity the activity - * @param dialog the dialog to be shown - */ - private static void showSafely(Activity activity, Dialog dialog) { - if (activity == null || dialog == null) { - Timber.d("Show called with null activity / dialog. Ignoring."); - return; - } - - boolean isActivityDestroyed = activity.isDestroyed(); - if (activity.isFinishing() || isActivityDestroyed) { - Timber.e("Activity is not running. Could not show dialog. "); - return; - } - try { - dialog.show(); - } catch (IllegalStateException e) { - Timber.e(e, "Could not show dialog."); - } - } - - public static void showAlertDialog(Activity activity, - String title, - String message, - final Runnable onPositiveBtnClick, - final Runnable onNegativeBtnClick) { - showAlertDialog(activity, - title, - message, - activity.getString(R.string.yes), - activity.getString(R.string.no), - onPositiveBtnClick, - onNegativeBtnClick); - } - - public static void showAlertDialog(Activity activity, - String title, - String message, - String positiveButtonText, - String negativeButtonText, - final Runnable onPositiveBtnClick, - final Runnable onNegativeBtnClick) { - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(title); - builder.setMessage(message); - - if (!StringUtils.isBlank(positiveButtonText)) { - builder.setPositiveButton(positiveButtonText, (dialogInterface, i) -> { - dialogInterface.dismiss(); - if (onPositiveBtnClick != null) { - onPositiveBtnClick.run(); - } - }); - } - - if (!StringUtils.isBlank(negativeButtonText)) { - builder.setNegativeButton(negativeButtonText, (DialogInterface dialogInterface, int i) -> { - dialogInterface.dismiss(); - if (onNegativeBtnClick != null) { - onNegativeBtnClick.run(); - } - }); - } - - AlertDialog dialog = builder.create(); - showSafely(activity, dialog); - } - - /* - Shows alert dialog with custom view - */ - public static void showAlertDialog(Activity activity, - String title, - String message, - final Runnable onPositiveBtnClick, - final Runnable onNegativeBtnClick, - View customView, - boolean cancelable) { - showAlertDialog(activity, - title, - message, - activity.getString(R.string.yes), - activity.getString(R.string.no), - onPositiveBtnClick, - onNegativeBtnClick, - customView, - cancelable); - } - - /* - Shows alert dialog with custom view - */ - private static void showAlertDialog(Activity activity, - String title, - String message, - String positiveButtonText, - String negativeButtonText, - final Runnable onPositiveBtnClick, - final Runnable onNegativeBtnClick, - View customView, - boolean cancelable) { - // If the custom view already has a parent, there is already a dialog showing with the view - // This happens for on resume - return to avoid creating a second dialog - the first one - // will still show - if (customView != null && customView.getParent() != null) { - return; - } - - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(title); - builder.setMessage(message); - builder.setView(customView); - builder.setCancelable(cancelable); - - builder.setPositiveButton(positiveButtonText, (dialogInterface, i) -> { - dialogInterface.dismiss(); - if (onPositiveBtnClick != null) { - onPositiveBtnClick.run(); - } - }); - - builder.setNegativeButton(negativeButtonText, (DialogInterface dialogInterface, int i) -> { - dialogInterface.dismiss(); - if (onNegativeBtnClick != null) { - onNegativeBtnClick.run(); - } - }); - - AlertDialog dialog = builder.create(); - showSafely(activity, dialog); - } - - - /** - * show a dialog with just a positive button - * @param activity - * @param title - * @param message - * @param positiveButtonText - * @param positiveButtonClick - * @param cancellable - */ - public static void showAlertDialog(Activity activity, String title, String message, String positiveButtonText, final Runnable positiveButtonClick, boolean cancellable) { - AlertDialog.Builder builder = new AlertDialog.Builder(activity); - builder.setTitle(title); - builder.setMessage(message); - builder.setCancelable(cancellable); - - builder.setPositiveButton(positiveButtonText, (dialogInterface, i) -> { - dialogInterface.dismiss(); - if (positiveButtonClick != null) { - positiveButtonClick.run(); - } - }); - - AlertDialog dialog = builder.create(); - showSafely(activity, dialog); - } - -} diff --git a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt new file mode 100644 index 000000000..40e9ad1ed --- /dev/null +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt @@ -0,0 +1,161 @@ +package fr.free.nrw.commons.utils + +import android.app.Activity +import android.app.AlertDialog +import android.app.Dialog +import android.view.View +import fr.free.nrw.commons.R +import timber.log.Timber + +object DialogUtil { + /** + * Shows a dialog safely. + * @param activity the activity + * @param dialog the dialog to be shown + */ + private fun showSafely(activity: Activity?, dialog: Dialog?) { + + if (activity == null || dialog == null) { + Timber.d("Show called with null activity / dialog. Ignoring.") + return + } + + if (activity.isFinishing || activity.isDestroyed) { + Timber.e("Activity is not running. Could not show dialog. ") + return + } + try { + dialog.show() + } catch (e: IllegalStateException) { + Timber.e(e, "Could not show dialog.") + } + } + + @JvmStatic + fun showAlertDialog( + activity: Activity, + title: String, + message: String, + onPositiveBtnClick: Runnable?, + onNegativeBtnClick: Runnable? + ) { + createAndShowDialogSafely( + activity = activity, + title = title, + message = message, + positiveButtonText = activity.getString(R.string.yes), + negativeButtonText = activity.getString(R.string.no), + onPositiveBtnClick = onPositiveBtnClick, + onNegativeBtnClick = onNegativeBtnClick + ) + } + + @JvmStatic + fun showAlertDialog( + activity: Activity, + title: String, + message: String, + positiveButtonText: String?, + negativeButtonText: String?, + onPositiveBtnClick: Runnable?, + onNegativeBtnClick: Runnable? + ) { + createAndShowDialogSafely( + activity = activity, + title = title, + message = message, + positiveButtonText = positiveButtonText, + negativeButtonText = negativeButtonText, + onPositiveBtnClick = onPositiveBtnClick, + onNegativeBtnClick = onNegativeBtnClick + ) + } + + @JvmStatic + fun showAlertDialog( + activity: Activity, + title: String, + message: String, + onPositiveBtnClick: Runnable?, + onNegativeBtnClick: Runnable?, + customView: View?, + cancelable: Boolean + ) { + createAndShowDialogSafely( + activity = activity, + title = title, + message = message, + positiveButtonText = activity.getString(R.string.yes), + negativeButtonText = activity.getString(R.string.no), + onPositiveBtnClick = onPositiveBtnClick, + onNegativeBtnClick = onNegativeBtnClick, + customView = customView, + cancelable = cancelable + ) + } + + @JvmStatic + fun showAlertDialog( + activity: Activity, + title: String, + message: String, + positiveButtonText: String?, + onPositiveBtnClick: Runnable?, + cancelable: Boolean + ) { + createAndShowDialogSafely( + activity = activity, + title = title, + message = message, + positiveButtonText = positiveButtonText, + onPositiveBtnClick = onPositiveBtnClick, + cancelable = cancelable + ) + } + + /** + * show a dialog + * @param activity + * @param title + * @param message + * @param positiveButtonText + * @param negativeButtonText + * @param onPositiveBtnClick + * @param onNegativeBtnClick + * @param customView + * @param cancelable + */ + private fun createAndShowDialogSafely( + activity: Activity, + title: String, + message: String, + positiveButtonText: String? = null, + negativeButtonText: String? = null, + onPositiveBtnClick: Runnable? = null, + onNegativeBtnClick: Runnable? = null, + customView: View? = null, + cancelable: Boolean = true + ) { + + /* If the custom view already has a parent, there is already a dialog showing with the view + * This happens for on resume - return to avoid creating a second dialog - the first one + * will still show + */ + if (customView?.parent != null) { + return + } + + showSafely(activity, AlertDialog.Builder(activity).apply { + setTitle(title) + setMessage(message) + setView(customView) + setCancelable(cancelable) + positiveButtonText?.let { + setPositiveButton(it) { _, _ -> onPositiveBtnClick?.run() } + } + negativeButtonText?.let { + setNegativeButton(it) { _, _ -> onNegativeBtnClick?.run() } + } + }.create()) + } +}