Skip to content

Commit

Permalink
Merge pull request #10223 from TacoTheDank/cleanAlertDialogs
Browse files Browse the repository at this point in the history
Clean up AlertDialogs
  • Loading branch information
Stypox authored Jul 12, 2023
2 parents e33bb67 + c1f0a94 commit 5c7a9a5
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,6 @@ private void showFailedDialog(@StringRes final int msg) {
.setTitle(R.string.general_error)
.setMessage(msg)
.setNegativeButton(getString(R.string.ok), null)
.create()
.show();
}

Expand Down Expand Up @@ -983,7 +982,7 @@ private void checkSelectedDownload(final StoredDirectoryHelper mainStorage,
break;
}

askDialog.create().show();
askDialog.show();
return;
}

Expand Down Expand Up @@ -1027,7 +1026,7 @@ private void checkSelectedDownload(final StoredDirectoryHelper mainStorage,
}
});

askDialog.create().show();
askDialog.show();
}

private void continueSelectedDownload(@NonNull final StoredFileHelper storage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ private void openPrivacyPolicyDialog(final Context context, final String action)
ShareUtils.openUrlInApp(this, ERROR_GITHUB_ISSUE_URL);
}
})
.setNegativeButton(R.string.decline, (dialog, which) -> {
// do nothing
})
.setNegativeButton(R.string.decline, null)
.show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,8 @@ private void showClearingQueueConfirmation(final Runnable onAllow) {
.setPositiveButton(R.string.ok, (dialog, which) -> {
onAllow.run();
dialog.dismiss();
}).show();
})
.show();
}

private void showExternalVideoPlaybackDialog() {
Expand Down Expand Up @@ -2155,25 +2156,24 @@ private void showExternalAudioPlaybackDialog() {
} else if (audioTracks.size() == 1) {
startOnExternalPlayer(activity, currentInfo, audioTracks.get(0));
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.select_audio_track_external_players);
builder.setNeutralButton(R.string.open_in_browser, (dialog, i) ->
ShareUtils.openUrlInBrowser(requireActivity(), url));

final int selectedAudioStream =
ListHelper.getDefaultAudioFormat(activity, audioTracks);
final CharSequence[] trackNames = audioTracks.stream()
.map(audioStream -> Localization.audioTrackName(activity, audioStream))
.toArray(CharSequence[]::new);

builder.setSingleChoiceItems(trackNames, selectedAudioStream, null);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ok, (dialog, i) -> {
final int index = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
startOnExternalPlayer(activity, currentInfo,
audioTracks.get(index));
});
builder.show();
new AlertDialog.Builder(activity)
.setTitle(R.string.select_audio_track_external_players)
.setNeutralButton(R.string.open_in_browser, (dialog, i) ->
ShareUtils.openUrlInBrowser(requireActivity(), url))
.setSingleChoiceItems(trackNames, selectedAudioStream, null)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.ok, (dialog, i) -> {
final int index = ((AlertDialog) dialog).getListView()
.getCheckedItemPosition();
startOnExternalPlayer(activity, currentInfo, audioTracks.get(index));
})
.show();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,6 @@ private void showLocalDialog(final PlaylistMetadataEntry selectedItem) {
final boolean isThumbnailPermanent = localPlaylistManager
.getIsPlaylistThumbnailPermanent(selectedItem.uid);

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);

final ArrayList<String> items = new ArrayList<>();
items.add(rename);
items.add(delete);
Expand All @@ -289,7 +287,9 @@ private void showLocalDialog(final PlaylistMetadataEntry selectedItem) {
}
};

builder.setItems(items.toArray(new String[0]), action).create().show();
new AlertDialog.Builder(activity)
.setItems(items.toArray(new String[0]), action)
.show();
}

private void showRenameDialog(final PlaylistMetadataEntry selectedItem) {
Expand All @@ -299,14 +299,13 @@ private void showRenameDialog(final PlaylistMetadataEntry selectedItem) {
dialogBinding.dialogEditText.setInputType(InputType.TYPE_CLASS_TEXT);
dialogBinding.dialogEditText.setText(selectedItem.name);

final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setView(dialogBinding.getRoot())
new AlertDialog.Builder(activity)
.setView(dialogBinding.getRoot())
.setPositiveButton(R.string.rename_playlist, (dialog, which) ->
changeLocalPlaylistName(
selectedItem.uid,
dialogBinding.dialogEditText.getText().toString()))
.setNegativeButton(R.string.cancel, null)
.create()
.show();
}

Expand Down
44 changes: 19 additions & 25 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ class FeedFragment : BaseStateFragment<FeedState>() {
}
}
.setPositiveButton(resources.getString(R.string.ok), null)
.create()
.show()
return true
} else if (item.itemId == R.id.menu_item_feed_toggle_played_items) {
Expand All @@ -254,22 +253,18 @@ class FeedFragment : BaseStateFragment<FeedState>() {
viewModel.getShowFutureItemsFromPreferences()
)

val builder = AlertDialog.Builder(context!!)
builder.setTitle(R.string.feed_hide_streams_title)
builder.setMultiChoiceItems(dialogItems, checkedDialogItems) { _, which, isChecked ->
checkedDialogItems[which] = isChecked
}

builder.setPositiveButton(R.string.ok) { _, _ ->
viewModel.setSaveShowPlayedItems(checkedDialogItems[0])

viewModel.setSaveShowPartiallyPlayedItems(checkedDialogItems[1])

viewModel.setSaveShowFutureItems(checkedDialogItems[2])
}
builder.setNegativeButton(R.string.cancel, null)

builder.create().show()
AlertDialog.Builder(context!!)
.setTitle(R.string.feed_hide_streams_title)
.setMultiChoiceItems(dialogItems, checkedDialogItems) { _, which, isChecked ->
checkedDialogItems[which] = isChecked
}
.setPositiveButton(R.string.ok) { _, _ ->
viewModel.setSaveShowPlayedItems(checkedDialogItems[0])
viewModel.setSaveShowPartiallyPlayedItems(checkedDialogItems[1])
viewModel.setSaveShowFutureItems(checkedDialogItems[2])
}
.setNegativeButton(R.string.cancel, null)
.show()
}

override fun onDestroyOptionsMenu() {
Expand Down Expand Up @@ -490,15 +485,13 @@ class FeedFragment : BaseStateFragment<FeedState>() {

val builder = AlertDialog.Builder(requireContext())
.setTitle(R.string.feed_load_error)
.setPositiveButton(
R.string.unsubscribe
) { _, _ ->
SubscriptionManager(requireContext()).deleteSubscription(
subscriptionEntity.serviceId, subscriptionEntity.url
).subscribe()
.setPositiveButton(R.string.unsubscribe) { _, _ ->
SubscriptionManager(requireContext())
.deleteSubscription(subscriptionEntity.serviceId, subscriptionEntity.url)
.subscribe()
handleItemsErrors(nextItemsErrors)
}
.setNegativeButton(R.string.cancel) { _, _ -> }
.setNegativeButton(R.string.cancel, null)

var message = getString(R.string.feed_load_error_account_info, subscriptionEntity.name)
if (cause is AccountTerminatedException) {
Expand All @@ -515,7 +508,8 @@ class FeedFragment : BaseStateFragment<FeedState>() {
message += "\n" + cause.message
}
}
builder.setMessage(message).create().show()
builder.setMessage(message)
.show()
}

private fun updateRelativeTimeViews() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Parcelable;
import android.text.InputType;
Expand Down Expand Up @@ -358,14 +357,13 @@ public boolean onOptionsItemSelected(final MenuItem item) {
new AlertDialog.Builder(requireContext())
.setMessage(R.string.remove_watched_popup_warning)
.setTitle(R.string.remove_watched_popup_title)
.setPositiveButton(R.string.ok,
(DialogInterface d, int id) -> removeWatchedStreams(false))
.setPositiveButton(R.string.ok, (d, id) ->
removeWatchedStreams(false))
.setNeutralButton(
R.string.remove_watched_popup_yes_and_partially_watched_videos,
(DialogInterface d, int id) -> removeWatchedStreams(true))
(d, id) -> removeWatchedStreams(true))
.setNegativeButton(R.string.cancel,
(DialogInterface d, int id) -> d.cancel())
.create()
(d, id) -> d.cancel())
.show();
}
} else if (item.getItemId() == R.id.menu_item_remove_duplicates) {
Expand Down Expand Up @@ -560,15 +558,14 @@ private void createRenameDialog() {
dialogBinding.dialogEditText.setSelection(dialogBinding.dialogEditText.getText().length());
dialogBinding.dialogEditText.setText(name);

final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getContext())
new AlertDialog.Builder(getContext())
.setTitle(R.string.rename_playlist)
.setView(dialogBinding.getRoot())
.setCancelable(true)
.setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.rename, (dialogInterface, i) ->
changePlaylistName(dialogBinding.dialogEditText.getText().toString()));

dialogBuilder.show();
changePlaylistName(dialogBinding.dialogEditText.getText().toString()))
.show();
}

private void changePlaylistName(final String title) {
Expand Down Expand Up @@ -634,15 +631,13 @@ private void updateThumbnailUrl() {
}

private void openRemoveDuplicatesDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());

builder.setTitle(R.string.remove_duplicates_title)
new AlertDialog.Builder(this.getActivity())
.setTitle(R.string.remove_duplicates_title)
.setMessage(R.string.remove_duplicates_message)
.setPositiveButton(R.string.ok,
(dialog, i) -> removeDuplicatesInPlaylist())
.setNeutralButton(R.string.cancel, null);

builder.create().show();
.setPositiveButton(R.string.ok, (dialog, i) ->
removeDuplicatesInPlaylist())
.setNeutralButton(R.string.cancel, null)
.show();
}

private void removeDuplicatesInPlaylist() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ class SubscriptionFragment : BaseStateFragment<SubscriptionState>() {
AlertDialog.Builder(requireContext())
.setCustomTitle(dialogTitleBinding.root)
.setItems(commands, actions)
.create()
.show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ private void requestImportPathResult(final ActivityResult result) {
importDatabase(file, lastImportDataUri))
.setNegativeButton(R.string.cancel, (d, id) ->
d.cancel())
.create()
.show();
}
}
Expand Down Expand Up @@ -223,20 +222,19 @@ private void importDatabase(final StoredFileHelper file, final Uri importDataUri

// if settings file exist, ask if it should be imported.
if (manager.extractSettings(file)) {
final AlertDialog.Builder alert = new AlertDialog.Builder(requireContext());
alert.setTitle(R.string.import_settings);

alert.setNegativeButton(R.string.cancel, (dialog, which) -> {
dialog.dismiss();
finishImport(importDataUri);
});
alert.setPositiveButton(R.string.ok, (dialog, which) -> {
dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext()));
finishImport(importDataUri);
});
alert.show();
new AlertDialog.Builder(requireContext())
.setTitle(R.string.import_settings)
.setNegativeButton(R.string.cancel, (dialog, which) -> {
dialog.dismiss();
finishImport(importDataUri);
})
.setPositiveButton(R.string.ok, (dialog, which) -> {
dialog.dismiss();
manager.loadSharedPreferences(PreferenceManager
.getDefaultSharedPreferences(requireContext()));
finishImport(importDataUri);
})
.show();
} else {
finishImport(importDataUri);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ private void forgetSAFTree(final Context context, final String oldPath) {
}

private void showMessageDialog(@StringRes final int title, @StringRes final int message) {
final AlertDialog.Builder msg = new AlertDialog.Builder(ctx);
msg.setTitle(title);
msg.setMessage(message);
msg.setPositiveButton(getString(R.string.ok), null);
msg.show();
new AlertDialog.Builder(ctx)
.setTitle(title)
.setMessage(message)
.setPositiveButton(getString(R.string.ok), null)
.show();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ public static void openDeleteWatchHistoryDialog(@NonNull final Context context,
disposables.add(getWholeStreamHistoryDisposable(context, recordManager));
disposables.add(getRemoveOrphanedRecordsDisposable(context, recordManager));
}))
.create()
.show();
}

Expand All @@ -144,7 +143,6 @@ public static void openDeletePlaybackStatesDialog(@NonNull final Context context
.setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss()))
.setPositiveButton(R.string.delete, ((dialog, which) ->
disposables.add(getDeletePlaybackStatesDisposable(context, recordManager))))
.create()
.show();
}

Expand All @@ -156,7 +154,6 @@ public static void openDeleteSearchHistoryDialog(@NonNull final Context context,
.setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.dismiss()))
.setPositiveButton(R.string.delete, ((dialog, which) ->
disposables.add(getDeleteSearchHistoryDisposable(context, recordManager))))
.create()
.show();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ public static void resolveActivityOrAskToInstall(@NonNull final Context context,
if (context instanceof Activity) {
new AlertDialog.Builder(context)
.setMessage(R.string.no_player_found)
.setPositiveButton(R.string.install,
(dialog, which) -> ShareUtils.installApp(context,
.setPositiveButton(R.string.install, (dialog, which) ->
ShareUtils.installApp(context,
context.getString(R.string.vlc_package)))
.setNegativeButton(R.string.cancel, (dialog, which)
-> Log.i("NavigationHelper", "You unlocked a secret unicorn."))
.setNegativeButton(R.string.cancel, (dialog, which) ->
Log.i("NavigationHelper", "You unlocked a secret unicorn."))
.show();
} else {
Toast.makeText(context, R.string.no_player_found_toast, Toast.LENGTH_LONG).show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ public static void playWithKore(final Context context, final Uri streamUrl) {
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (!tryOpenIntentInApp(context, intent)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(R.string.kore_not_found)
.setPositiveButton(R.string.install, (dialog, which) -> installKore(context))
.setNegativeButton(R.string.cancel, (dialog, which) -> { });
builder.create().show();
new AlertDialog.Builder(context)
.setMessage(R.string.kore_not_found)
.setPositiveButton(R.string.install, (dialog, which) ->
installKore(context))
.setNegativeButton(R.string.cancel, null)
.show();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static boolean playOnPopup(final Context context,
.setTitle(R.string.player_stream_failure)
.setMessage(
ErrorPanelHelper.Companion.getExceptionDescription(throwable))
.setPositiveButton(R.string.ok, (v, b) -> { })
.setPositiveButton(R.string.ok, null)
.show();
}));
return true;
Expand Down
Loading

0 comments on commit 5c7a9a5

Please sign in to comment.