Fix #5151: Use DialogUtil for MediaDetailFragment (#5153)

* Update DialogUtil to return the dialog object

* Update first instance of Builder to DialogAlert

* Update DialogUtil to allow null instead of empty string

* Update MediaDetailFragment to use DialogUtil

* Remove unnecessary semicolons
This commit is contained in:
Chan Jun Da 2023-02-25 14:34:57 +08:00 committed by GitHub
parent 20100c4960
commit 23ee2b60d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 41 deletions

View file

@ -13,7 +13,6 @@ import static fr.free.nrw.commons.upload.mediaDetails.UploadMediaDetailFragment.
import static fr.free.nrw.commons.utils.LangCodeUtils.getLocalizedResources; import static fr.free.nrw.commons.utils.LangCodeUtils.getLocalizedResources;
import android.Manifest; import android.Manifest;
import android.Manifest.permission;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; 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.categories.UploadCategoriesFragment;
import fr.free.nrw.commons.upload.depicts.DepictsFragment; import fr.free.nrw.commons.upload.depicts.DepictsFragment;
import fr.free.nrw.commons.upload.UploadMediaDetail; 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.PermissionUtils;
import fr.free.nrw.commons.utils.ViewUtilWrapper; import fr.free.nrw.commons.utils.ViewUtilWrapper;
import io.reactivex.Single; import io.reactivex.Single;
@ -1126,15 +1126,15 @@ public class MediaDetailFragment extends CommonsDaggerSupportFragment implements
spinner.setAdapter(languageAdapter); spinner.setAdapter(languageAdapter);
spinner.setGravity(17); spinner.setGravity(17);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); AlertDialog dialog = DialogUtil.showAlertDialog(getActivity(),
builder.setView(spinner); getString(R.string.nominate_delete),
builder.setTitle(R.string.nominate_delete) null,
.setPositiveButton(R.string.about_translate_proceed, getString(R.string.about_translate_proceed),
(dialog, which) -> onDeleteClicked(spinner)); getString(R.string.about_translate_cancel),
builder.setNegativeButton(R.string.about_translate_cancel, () -> onDeleteClicked(spinner),
(dialog, which) -> dialog.dismiss()); () -> {},
AlertDialog dialog = builder.create(); spinner,
dialog.show(); true);
if (isDeleted) { if (isDeleted) {
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); 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) { //But how does this if (delete.getVisibility() == View.VISIBLE) {
// enableDeleteButton(true); makes sense ? // enableDeleteButton(true); makes sense ?
else { 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()); final EditText input = new EditText(getActivity());
alert.setView(input);
input.requestFocus(); input.requestFocus();
alert.setPositiveButton(R.string.ok, (dialog1, whichButton) -> { AlertDialog d = DialogUtil.showAlertDialog(getActivity(),
String reason = input.getText().toString(); null,
onDeleteClickeddialogtext(reason); getString(R.string.dialog_box_text_nomination, media.getDisplayTitle()),
}); getString(R.string.ok),
alert.setNegativeButton(R.string.cancel, (dialog12, whichButton) -> { getString(R.string.cancel),
}); () -> {
AlertDialog d = alert.create(); String reason = input.getText().toString();
onDeleteClickeddialogtext(reason);
},
() -> {},
input,
true);
input.addTextChangedListener(new TextWatcher() { input.addTextChangedListener(new TextWatcher() {
private void handleText() { private void handleText() {
final Button okButton = d.getButton(AlertDialog.BUTTON_POSITIVE); 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) { public void onTextChanged(CharSequence s, int start, int before, int count) {
} }
}); });
d.show();
d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false); d.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
} }
} }

View file

@ -2,7 +2,6 @@ package fr.free.nrw.commons.utils
import android.app.Activity import android.app.Activity
import android.app.AlertDialog import android.app.AlertDialog
import android.app.Dialog
import android.view.View import android.view.View
import fr.free.nrw.commons.R import fr.free.nrw.commons.R
import timber.log.Timber import timber.log.Timber
@ -13,22 +12,23 @@ object DialogUtil {
* @param activity the activity * @param activity the activity
* @param dialog the dialog to be shown * @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) { if (activity == null || dialog == null) {
Timber.d("Show called with null activity / dialog. Ignoring.") Timber.d("Show called with null activity / dialog. Ignoring.")
return return null
} }
if (activity.isFinishing || activity.isDestroyed) { if (activity.isFinishing || activity.isDestroyed) {
Timber.e("Activity is not running. Could not show dialog. ") Timber.e("Activity is not running. Could not show dialog. ")
return return dialog
} }
try { try {
dialog.show() dialog.show()
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Timber.e(e, "Could not show dialog.") Timber.e(e, "Could not show dialog.")
} }
return dialog
} }
@JvmStatic @JvmStatic
@ -38,8 +38,8 @@ object DialogUtil {
message: String?, message: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
onNegativeBtnClick: Runnable? onNegativeBtnClick: Runnable?
) { ): AlertDialog? {
createAndShowDialogSafely( return createAndShowDialogSafely(
activity = activity, activity = activity,
title = title, title = title,
message = message, message = message,
@ -58,9 +58,9 @@ object DialogUtil {
positiveButtonText: String?, positiveButtonText: String?,
negativeButtonText: String?, negativeButtonText: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
onNegativeBtnClick: Runnable? onNegativeBtnClick: Runnable?,
) { ): AlertDialog? {
createAndShowDialogSafely( return createAndShowDialogSafely(
activity = activity, activity = activity,
title = title, title = title,
message = message, message = message,
@ -80,8 +80,8 @@ object DialogUtil {
onNegativeBtnClick: Runnable?, onNegativeBtnClick: Runnable?,
customView: View?, customView: View?,
cancelable: Boolean cancelable: Boolean
) { ): AlertDialog? {
createAndShowDialogSafely( return createAndShowDialogSafely(
activity = activity, activity = activity,
title = title, title = title,
message = message, message = message,
@ -105,8 +105,8 @@ object DialogUtil {
onNegativeBtnClick: Runnable?, onNegativeBtnClick: Runnable?,
customView: View?, customView: View?,
cancelable: Boolean cancelable: Boolean
) { ): AlertDialog? {
createAndShowDialogSafely( return createAndShowDialogSafely(
activity = activity, activity = activity,
title = title, title = title,
message = message, message = message,
@ -127,8 +127,8 @@ object DialogUtil {
positiveButtonText: String?, positiveButtonText: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
cancelable: Boolean cancelable: Boolean
) { ): AlertDialog? {
createAndShowDialogSafely( return createAndShowDialogSafely(
activity = activity, activity = activity,
title = title, title = title,
message = message, message = message,
@ -160,19 +160,19 @@ object DialogUtil {
onNegativeBtnClick: Runnable? = null, onNegativeBtnClick: Runnable? = null,
customView: View? = null, customView: View? = null,
cancelable: Boolean = true cancelable: Boolean = true
) { ): AlertDialog? {
/* If the custom view already has a parent, there is already a dialog showing with the view /* 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 * This happens for on resume - return to avoid creating a second dialog - the first one
* will still show * will still show
*/ */
if (customView?.parent != null) { 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{setTitle(title)}
title?.also{setMessage(message)} message?.also{setMessage(message)}
setView(customView) setView(customView)
setCancelable(cancelable) setCancelable(cancelable)
positiveButtonText?.let { positiveButtonText?.let {