mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-26 20:33:53 +01:00
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
This commit is contained in:
parent
75fe4ae5c1
commit
e341e77c8e
2 changed files with 161 additions and 177 deletions
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
161
app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt
Normal file
161
app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt
Normal file
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue