Skip to content

Commit

Permalink
Fix commons-app#5151: Use DialogUtil methods instead of AlertDialog.B…
Browse files Browse the repository at this point in the history
…uilder (commons-app#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
  • Loading branch information
chan-j-d authored Feb 24, 2023
1 parent 968911d commit 20100c4
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 65 deletions.
25 changes: 14 additions & 11 deletions app/src/main/java/fr/free/nrw/commons/AboutActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}

}
19 changes: 10 additions & 9 deletions app/src/main/java/fr/free/nrw/commons/profile/ProfileActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);

}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down
28 changes: 14 additions & 14 deletions app/src/main/java/fr/free/nrw/commons/utils/DialogUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ object DialogUtil {
@JvmStatic
fun showAlertDialog(
activity: Activity,
title: String,
message: String,
title: String?,
message: String?,
onPositiveBtnClick: Runnable?,
onNegativeBtnClick: Runnable?
) {
Expand All @@ -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?,
Expand All @@ -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?,
Expand All @@ -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?,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand Down

0 comments on commit 20100c4

Please sign in to comment.