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
This commit is contained in:
Chan Jun Da 2023-02-24 17:00:58 +08:00 committed by GitHub
parent 968911db32
commit 20100c4960
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 65 deletions

View file

@ -1,7 +1,6 @@
package fr.free.nrw.commons; package fr.free.nrw.commons;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -16,6 +15,7 @@ import androidx.annotation.NonNull;
import fr.free.nrw.commons.databinding.ActivityAboutBinding; import fr.free.nrw.commons.databinding.ActivityAboutBinding;
import fr.free.nrw.commons.theme.BaseActivity; import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.ConfigUtils; import fr.free.nrw.commons.utils.ConfigUtils;
import fr.free.nrw.commons.utils.DialogUtil;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -161,17 +161,20 @@ public class AboutActivity extends BaseActivity {
spinner.setAdapter(languageAdapter); spinner.setAdapter(languageAdapter);
spinner.setGravity(17); spinner.setGravity(17);
spinner.setPadding(50,0,0,0); 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);
} }
} }

View file

@ -1,6 +1,5 @@
package fr.free.nrw.commons.profile; package fr.free.nrw.commons.profile;
import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
@ -14,11 +13,9 @@ import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager; import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import com.google.android.material.tabs.TabLayout; import com.google.android.material.tabs.TabLayout;
@ -27,11 +24,11 @@ import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.ViewPagerAdapter; import fr.free.nrw.commons.ViewPagerAdapter;
import fr.free.nrw.commons.auth.SessionManager; import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.contributions.ContributionsFragment; 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.explore.ParentViewPager;
import fr.free.nrw.commons.profile.achievements.AchievementsFragment; import fr.free.nrw.commons.profile.achievements.AchievementsFragment;
import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment; import fr.free.nrw.commons.profile.leaderboard.LeaderboardFragment;
import fr.free.nrw.commons.theme.BaseActivity; import fr.free.nrw.commons.theme.BaseActivity;
import fr.free.nrw.commons.utils.DialogUtil;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -196,17 +193,21 @@ public class ProfileActivity extends BaseActivity {
* @param screenshot screenshot of the present screen * @param screenshot screenshot of the present screen
*/ */
public void showAlert(final Bitmap screenshot) { public void showAlert(final Bitmap screenshot) {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
final LayoutInflater factory = LayoutInflater.from(this); final LayoutInflater factory = LayoutInflater.from(this);
final View view = factory.inflate(R.layout.image_alert_layout, null); final View view = factory.inflate(R.layout.image_alert_layout, null);
final ImageView screenShotImage = view.findViewById(R.id.alert_image); final ImageView screenShotImage = view.findViewById(R.id.alert_image);
screenShotImage.setImageBitmap(screenshot); screenShotImage.setImageBitmap(screenshot);
final TextView shareMessage = view.findViewById(R.id.alert_text); final TextView shareMessage = view.findViewById(R.id.alert_text);
shareMessage.setText(R.string.achievements_share_message); shareMessage.setText(R.string.achievements_share_message);
alert.setView(view); DialogUtil.showAlertDialog(this,
alert.setPositiveButton(R.string.about_translate_proceed, (dialog, which) -> shareScreen(screenshot)); null,
alert.setNegativeButton(android.R.string.cancel, (dialog, which) -> dialog.cancel()); null,
alert.show(); getString(R.string.about_translate_proceed),
getString(R.string.cancel),
() -> shareScreen(screenshot),
() -> {},
view,
true);
} }
/** /**

View file

@ -1,8 +1,6 @@
package fr.free.nrw.commons.profile.achievements; package fr.free.nrw.commons.profile.achievements;
import android.accounts.Account; import android.accounts.Account;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -32,6 +30,7 @@ import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment; import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import fr.free.nrw.commons.utils.ConfigUtils; 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.utils.ViewUtil;
import fr.free.nrw.commons.profile.ProfileActivity; import fr.free.nrw.commons.profile.ProfileActivity;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
@ -373,16 +372,15 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
} }
private void setZeroAchievements() { private void setZeroAchievements() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) String message = !Objects.equals(sessionManager.getUserName(), userName) ?
.setMessage( getString(R.string.no_achievements_yet, userName) :
!Objects.equals(sessionManager.getUserName(), userName) ? getString(R.string.you_have_no_achievements_yet);
getString(R.string.no_achievements_yet, userName) : DialogUtil.showAlertDialog(getActivity(),
getString(R.string.you_have_no_achievements_yet) null,
) message,
.setPositiveButton(getString(R.string.ok), (dialog, which) -> { getString(R.string.ok),
}); () -> {},
AlertDialog dialog = builder.create(); true);
dialog.show();
imagesUploadedProgressbar.setVisibility(View.INVISIBLE); imagesUploadedProgressbar.setVisibility(View.INVISIBLE);
imageRevertsProgressbar.setVisibility(View.INVISIBLE); imageRevertsProgressbar.setVisibility(View.INVISIBLE);
imagesUsedByWikiProgressBar.setVisibility(View.INVISIBLE); imagesUsedByWikiProgressBar.setVisibility(View.INVISIBLE);
@ -391,7 +389,6 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
imageRevertedText.setText(R.string.no_image_reverted); imageRevertedText.setText(R.string.no_image_reverted);
imageUploadedText.setText(R.string.no_image_uploaded); imageUploadedText.setText(R.string.no_image_uploaded);
imageView.setVisibility(View.INVISIBLE); imageView.setVisibility(View.INVISIBLE);
} }
/** /**
@ -507,29 +504,27 @@ public class AchievementsFragment extends CommonsDaggerSupportFragment {
* @param message * @param message
*/ */
private void launchAlert(String title, String message){ private void launchAlert(String title, String message){
new AlertDialog.Builder(getActivity()) DialogUtil.showAlertDialog(getActivity(),
.setTitle(title) title,
.setMessage(message) message,
.setCancelable(true) getString(R.string.ok),
.setPositiveButton(android.R.string.ok, (dialog, id) -> dialog.cancel()) () -> {},
.create() true);
.show();
} }
/** /**
* Launch Alert with a READ MORE button and clicking it open a custom webpage * Launch Alert with a READ MORE button and clicking it open a custom webpage
*/ */
private void launchAlertWithHelpLink(String title, String message, String helpLinkUrl){ private void launchAlertWithHelpLink(String title, String message, String helpLinkUrl) {
new Builder(getActivity()) DialogUtil.showAlertDialog(getActivity(),
.setTitle(title) title,
.setMessage(message) message,
.setCancelable(true) getString(R.string.ok),
.setPositiveButton(android.R.string.ok, (dialog, id) -> dialog.cancel()) getString(R.string.read_help_link),
.setNegativeButton(R.string.read_help_link, (dialog ,id) ->{ () -> {},
Utils.handleWebUrl(requireContext(), Uri.parse(helpLinkUrl));; () -> Utils.handleWebUrl(requireContext(), Uri.parse(helpLinkUrl)),
}) null,
.create() true);
.show();
} }
/** /**

View file

@ -34,8 +34,8 @@ object DialogUtil {
@JvmStatic @JvmStatic
fun showAlertDialog( fun showAlertDialog(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
onNegativeBtnClick: Runnable? onNegativeBtnClick: Runnable?
) { ) {
@ -53,8 +53,8 @@ object DialogUtil {
@JvmStatic @JvmStatic
fun showAlertDialog( fun showAlertDialog(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
positiveButtonText: String?, positiveButtonText: String?,
negativeButtonText: String?, negativeButtonText: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
@ -74,8 +74,8 @@ object DialogUtil {
@JvmStatic @JvmStatic
fun showAlertDialog( fun showAlertDialog(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
onNegativeBtnClick: Runnable?, onNegativeBtnClick: Runnable?,
customView: View?, customView: View?,
@ -97,8 +97,8 @@ object DialogUtil {
@JvmStatic @JvmStatic
fun showAlertDialog( fun showAlertDialog(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
positiveButtonText: String?, positiveButtonText: String?,
negativeButtonText: String?, negativeButtonText: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
@ -122,8 +122,8 @@ object DialogUtil {
@JvmStatic @JvmStatic
fun showAlertDialog( fun showAlertDialog(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
positiveButtonText: String?, positiveButtonText: String?,
onPositiveBtnClick: Runnable?, onPositiveBtnClick: Runnable?,
cancelable: Boolean cancelable: Boolean
@ -152,8 +152,8 @@ object DialogUtil {
*/ */
private fun createAndShowDialogSafely( private fun createAndShowDialogSafely(
activity: Activity, activity: Activity,
title: String, title: String?,
message: String, message: String?,
positiveButtonText: String? = null, positiveButtonText: String? = null,
negativeButtonText: String? = null, negativeButtonText: String? = null,
onPositiveBtnClick: Runnable? = null, onPositiveBtnClick: Runnable? = null,
@ -171,8 +171,8 @@ object DialogUtil {
} }
showSafely(activity, AlertDialog.Builder(activity).apply { showSafely(activity, AlertDialog.Builder(activity).apply {
setTitle(title) title?.also{setTitle(title)}
setMessage(message) title?.also{setMessage(message)}
setView(customView) setView(customView)
setCancelable(cancelable) setCancelable(cancelable)
positiveButtonText?.let { positiveButtonText?.let {