From 20100c4960c24690701aaca2fffa75540209b474 Mon Sep 17 00:00:00 2001 From: Chan Jun Da <65345505+chan-j-d@users.noreply.github.com> Date: Fri, 24 Feb 2023 17:00:58 +0800 Subject: [PATCH] Fix #5151: Use DialogUtil methods instead of AlertDialog.Builder (#5152) * Update AboutActivity to use DialogUtil * Update ProfileActivity to use DialogUtil * Update AchievementsFragment to use DialogUtil * Remove wrong message set in ProfileActivity * Update DialogUtil to accept null instead of empty string --- .../fr/free/nrw/commons/AboutActivity.java | 25 ++++---- .../nrw/commons/profile/ProfileActivity.java | 19 ++++--- .../achievements/AchievementsFragment.java | 57 +++++++++---------- .../fr/free/nrw/commons/utils/DialogUtil.kt | 28 ++++----- 4 files changed, 64 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/fr/free/nrw/commons/AboutActivity.java b/app/src/main/java/fr/free/nrw/commons/AboutActivity.java index c9f8bc565a..e4b281af81 100644 --- a/app/src/main/java/fr/free/nrw/commons/AboutActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/AboutActivity.java @@ -1,7 +1,6 @@ package fr.free.nrw.commons; import android.annotation.SuppressLint; -import android.app.AlertDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -16,6 +15,7 @@ import fr.free.nrw.commons.databinding.ActivityAboutBinding; import fr.free.nrw.commons.theme.BaseActivity; import fr.free.nrw.commons.utils.ConfigUtils; +import fr.free.nrw.commons.utils.DialogUtil; import java.util.Collections; import java.util.List; @@ -161,17 +161,20 @@ public void launchTranslate(View view) { spinner.setAdapter(languageAdapter); spinner.setGravity(17); spinner.setPadding(50,0,0,0); - AlertDialog.Builder builder = new AlertDialog.Builder(AboutActivity.this); - builder.setView(spinner); - builder.setTitle(R.string.about_translate_title) - .setMessage(R.string.about_translate_message) - .setPositiveButton(R.string.about_translate_proceed, (dialog, which) -> { - String langCode = CommonsApplication.getInstance().getLanguageLookUpTable().getCodes().get(spinner.getSelectedItemPosition()); - Utils.handleWebUrl(AboutActivity.this, Uri.parse(Urls.TRANSLATE_WIKI_URL + langCode)); - }); - builder.setNegativeButton(R.string.about_translate_cancel, (dialog, which) -> dialog.cancel()); - builder.create().show(); + Runnable positiveButtonRunnable = () -> { + String langCode = CommonsApplication.getInstance().getLanguageLookUpTable().getCodes().get(spinner.getSelectedItemPosition()); + Utils.handleWebUrl(AboutActivity.this, Uri.parse(Urls.TRANSLATE_WIKI_URL + langCode)); + }; + DialogUtil.showAlertDialog(this, + getString(R.string.about_translate_title), + getString(R.string.about_translate_message), + getString(R.string.about_translate_proceed), + getString(R.string.about_translate_cancel), + positiveButtonRunnable, + () -> {}, + spinner, + true); } } diff --git a/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java b/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java index b7bbe5acaa..1876e3dc4a 100644 --- a/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java +++ b/app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java @@ -1,6 +1,5 @@ package fr.free.nrw.commons.profile; -import android.app.AlertDialog; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; @@ -14,11 +13,9 @@ import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; -import androidx.appcompat.widget.Toolbar; import androidx.core.content.FileProvider; import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentManager; -import androidx.viewpager.widget.ViewPager; import butterknife.BindView; import butterknife.ButterKnife; import com.google.android.material.tabs.TabLayout; @@ -27,11 +24,11 @@ import fr.free.nrw.commons.ViewPagerAdapter; import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.contributions.ContributionsFragment; -import fr.free.nrw.commons.contributions.ContributionsListFragment; import fr.free.nrw.commons.explore.ParentViewPager; import fr.free.nrw.commons.profile.achievements.AchievementsFragment; import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment; import fr.free.nrw.commons.theme.BaseActivity; +import fr.free.nrw.commons.utils.DialogUtil; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -196,17 +193,21 @@ public boolean onOptionsItemSelected(final MenuItem item) { * @param screenshot screenshot of the present screen */ public void showAlert(final Bitmap screenshot) { - final AlertDialog.Builder alert = new AlertDialog.Builder(this); final LayoutInflater factory = LayoutInflater.from(this); final View view = factory.inflate(R.layout.image_alert_layout, null); final ImageView screenShotImage = view.findViewById(R.id.alert_image); screenShotImage.setImageBitmap(screenshot); final TextView shareMessage = view.findViewById(R.id.alert_text); shareMessage.setText(R.string.achievements_share_message); - alert.setView(view); - alert.setPositiveButton(R.string.about_translate_proceed, (dialog, which) -> shareScreen(screenshot)); - alert.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel()); - alert.show(); + DialogUtil.showAlertDialog(this, + null, + null, + getString(R.string.about_translate_proceed), + getString(R.string.cancel), + () -> shareScreen(screenshot), + () -> {}, + view, + true); } /** diff --git a/app/src/main/java/fr/free/nrw/commons/profile/achievements/AchievementsFragment.java b/app/src/main/java/fr/free/nrw/commons/profile/achievements/AchievementsFragment.java index 51371da449..8befed29b2 100644 --- a/app/src/main/java/fr/free/nrw/commons/profile/achievements/AchievementsFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/profile/achievements/AchievementsFragment.java @@ -1,8 +1,6 @@ package fr.free.nrw.commons.profile.achievements; import android.accounts.Account; -import android.app.AlertDialog; -import android.app.AlertDialog.Builder; import android.content.Context; import android.net.Uri; import android.os.Bundle; @@ -32,6 +30,7 @@ import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; import fr.free.nrw.commons.utils.ConfigUtils; +import fr.free.nrw.commons.utils.DialogUtil; import fr.free.nrw.commons.utils.ViewUtil; import fr.free.nrw.commons.profile.ProfileActivity; import io.reactivex.android.schedulers.AndroidSchedulers; @@ -373,16 +372,15 @@ private void setUploadProgress(int uploadCount){ } private void setZeroAchievements() { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) - .setMessage( - !Objects.equals(sessionManager.getUserName(), userName) ? - getString(R.string.no_achievements_yet, userName) : - getString(R.string.you_have_no_achievements_yet) - ) - .setPositiveButton(getString(R.string.ok), (dialog, which) -> { - }); - AlertDialog dialog = builder.create(); - dialog.show(); + String message = !Objects.equals(sessionManager.getUserName(), userName) ? + getString(R.string.no_achievements_yet, userName) : + getString(R.string.you_have_no_achievements_yet); + DialogUtil.showAlertDialog(getActivity(), + null, + message, + getString(R.string.ok), + () -> {}, + true); imagesUploadedProgressbar.setVisibility(View.INVISIBLE); imageRevertsProgressbar.setVisibility(View.INVISIBLE); imagesUsedByWikiProgressBar.setVisibility(View.INVISIBLE); @@ -391,7 +389,6 @@ private void setZeroAchievements() { imageRevertedText.setText(R.string.no_image_reverted); imageUploadedText.setText(R.string.no_image_uploaded); imageView.setVisibility(View.INVISIBLE); - } /** @@ -507,29 +504,27 @@ public void showQualityImagesInfo() { * @param message */ private void launchAlert(String title, String message){ - new AlertDialog.Builder(getActivity()) - .setTitle(title) - .setMessage(message) - .setCancelable(true) - .setPositiveButton(android.R.string.ok, (dialog, id) -> dialog.cancel()) - .create() - .show(); + DialogUtil.showAlertDialog(getActivity(), + title, + message, + getString(R.string.ok), + () -> {}, + true); } /** * Launch Alert with a READ MORE button and clicking it open a custom webpage */ - private void launchAlertWithHelpLink(String title, String message, String helpLinkUrl){ - new Builder(getActivity()) - .setTitle(title) - .setMessage(message) - .setCancelable(true) - .setPositiveButton(android.R.string.ok, (dialog, id) -> dialog.cancel()) - .setNegativeButton(R.string.read_help_link, (dialog ,id) ->{ - Utils.handleWebUrl(requireContext(), Uri.parse(helpLinkUrl));; - }) - .create() - .show(); + private void launchAlertWithHelpLink(String title, String message, String helpLinkUrl) { + DialogUtil.showAlertDialog(getActivity(), + title, + message, + getString(R.string.ok), + getString(R.string.read_help_link), + () -> {}, + () -> Utils.handleWebUrl(requireContext(), Uri.parse(helpLinkUrl)), + null, + true); } /** 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 2016e40bf0..88e9a690f7 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 @@ -34,8 +34,8 @@ object DialogUtil { @JvmStatic fun showAlertDialog( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, onPositiveBtnClick: Runnable?, onNegativeBtnClick: Runnable? ) { @@ -53,8 +53,8 @@ object DialogUtil { @JvmStatic fun showAlertDialog( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, positiveButtonText: String?, negativeButtonText: String?, onPositiveBtnClick: Runnable?, @@ -74,8 +74,8 @@ object DialogUtil { @JvmStatic fun showAlertDialog( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, onPositiveBtnClick: Runnable?, onNegativeBtnClick: Runnable?, customView: View?, @@ -97,8 +97,8 @@ object DialogUtil { @JvmStatic fun showAlertDialog( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, positiveButtonText: String?, negativeButtonText: String?, onPositiveBtnClick: Runnable?, @@ -122,8 +122,8 @@ object DialogUtil { @JvmStatic fun showAlertDialog( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, positiveButtonText: String?, onPositiveBtnClick: Runnable?, cancelable: Boolean @@ -152,8 +152,8 @@ object DialogUtil { */ private fun createAndShowDialogSafely( activity: Activity, - title: String, - message: String, + title: String?, + message: String?, positiveButtonText: String? = null, negativeButtonText: String? = null, onPositiveBtnClick: Runnable? = null, @@ -171,8 +171,8 @@ object DialogUtil { } showSafely(activity, AlertDialog.Builder(activity).apply { - setTitle(title) - setMessage(message) + title?.also{setTitle(title)} + title?.also{setMessage(message)} setView(customView) setCancelable(cancelable) positiveButtonText?.let {