diff --git a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java index 3e0d59ebd..04d39937d 100644 --- a/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java @@ -13,7 +13,6 @@ import static fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment. import static fr.free.nrw.commons.utils.LangCodeUtils.getLocalizedResources; import android.Manifest; -import android.Manifest.permission; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; @@ -84,6 +83,7 @@ import fr.free.nrw.commons.ui.widget.HtmlTextView; import fr.free.nrw.commons.upload.categories.UploadCategoriesFragment; import fr.free.nrw.commons.upload.depicts.DepictsFragment; import fr.free.nrw.commons.upload.UploadMediaDetail; +import fr.free.nrw.commons.utils.DialogUtil; import fr.free.nrw.commons.utils.PermissionUtils; import fr.free.nrw.commons.utils.ViewUtilWrapper; import io.reactivex.Single; @@ -1126,15 +1126,15 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements spinner.setAdapter(languageAdapter); spinner.setGravity(17); - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setView(spinner); - builder.setTitle(R.string.nominate_delete) - .setPositiveButton(R.string.about_translate_proceed, - (dialog, which) -> onDeleteClicked(spinner)); - builder.setNegativeButton(R.string.about_translate_cancel, - (dialog, which) -> dialog.dismiss()); - AlertDialog dialog = builder.create(); - dialog.show(); + AlertDialog dialog = DialogUtil.showAlertDialog(getActivity(), + getString(R.string.nominate_delete), + null, + getString(R.string.about_translate_proceed), + getString(R.string.about_translate_cancel), + () -> onDeleteClicked(spinner), + () -> {}, + spinner, + true); if (isDeleted) { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); } @@ -1143,19 +1143,20 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements //But how does this if (delete.getVisibility() == View.VISIBLE) { // enableDeleteButton(true); makes sense ? else { - AlertDialog.Builder alert = new AlertDialog.Builder(getActivity()); - alert.setMessage( - getString(R.string.dialog_box_text_nomination, media.getDisplayTitle())); final EditText input = new EditText(getActivity()); - alert.setView(input); input.requestFocus(); - alert.setPositiveButton(R.string.ok, (dialog1, whichButton) -> { - String reason = input.getText().toString(); - onDeleteClickeddialogtext(reason); - }); - alert.setNegativeButton(R.string.cancel, (dialog12, whichButton) -> { - }); - AlertDialog d = alert.create(); + AlertDialog d = DialogUtil.showAlertDialog(getActivity(), + null, + getString(R.string.dialog_box_text_nomination, media.getDisplayTitle()), + getString(R.string.ok), + getString(R.string.cancel), + () -> { + String reason = input.getText().toString(); + onDeleteClickeddialogtext(reason); + }, + () -> {}, + input, + true); input.addTextChangedListener(new TextWatcher() { private void handleText() { final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); @@ -1179,7 +1180,6 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements public void onTextChanged(CharSequence s, int start, int before, int count) { } }); - d.show(); d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); } } 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 index 88e9a690f..f52667783 100644 --- a/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt +++ b/app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt @@ -2,7 +2,6 @@ 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 @@ -13,22 +12,23 @@ object DialogUtil { * @param activity the activity * @param dialog the dialog to be shown */ - private fun showSafely(activity: Activity?, dialog: Dialog?) { + private fun showSafely(activity: Activity?, dialog: AlertDialog?):AlertDialog? { if (activity == null || dialog == null) { Timber.d("Show called with null activity / dialog. Ignoring.") - return + return null } if (activity.isFinishing || activity.isDestroyed) { Timber.e("Activity is not running. Could not show dialog. ") - return + return dialog } try { dialog.show() } catch (e: IllegalStateException) { Timber.e(e, "Could not show dialog.") } + return dialog } @JvmStatic @@ -38,8 +38,8 @@ object DialogUtil { message: String?, onPositiveBtnClick: Runnable?, onNegativeBtnClick: Runnable? - ) { - createAndShowDialogSafely( + ): AlertDialog? { + return createAndShowDialogSafely( activity = activity, title = title, message = message, @@ -58,9 +58,9 @@ object DialogUtil { positiveButtonText: String?, negativeButtonText: String?, onPositiveBtnClick: Runnable?, - onNegativeBtnClick: Runnable? - ) { - createAndShowDialogSafely( + onNegativeBtnClick: Runnable?, + ): AlertDialog? { + return createAndShowDialogSafely( activity = activity, title = title, message = message, @@ -80,8 +80,8 @@ object DialogUtil { onNegativeBtnClick: Runnable?, customView: View?, cancelable: Boolean - ) { - createAndShowDialogSafely( + ): AlertDialog? { + return createAndShowDialogSafely( activity = activity, title = title, message = message, @@ -105,8 +105,8 @@ object DialogUtil { onNegativeBtnClick: Runnable?, customView: View?, cancelable: Boolean - ) { - createAndShowDialogSafely( + ): AlertDialog? { + return createAndShowDialogSafely( activity = activity, title = title, message = message, @@ -127,8 +127,8 @@ object DialogUtil { positiveButtonText: String?, onPositiveBtnClick: Runnable?, cancelable: Boolean - ) { - createAndShowDialogSafely( + ): AlertDialog? { + return createAndShowDialogSafely( activity = activity, title = title, message = message, @@ -160,19 +160,19 @@ object DialogUtil { onNegativeBtnClick: Runnable? = null, customView: View? = null, cancelable: Boolean = true - ) { + ): AlertDialog? { /* 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 + return null } - showSafely(activity, AlertDialog.Builder(activity).apply { + return showSafely(activity, AlertDialog.Builder(activity).apply { title?.also{setTitle(title)} - title?.also{setMessage(message)} + message?.also{setMessage(message)} setView(customView) setCancelable(cancelable) positiveButtonText?.let {