Pass FragementActivity instead of Fragment to StreamDialogEntryAction
This is necessary if it is not clear which fragment is available.
This commit is contained in:
parent
df3996c246
commit
a0616c74ce
@ -13,6 +13,7 @@ import android.widget.TextView;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import org.schabi.newpipe.App;
|
||||
@ -73,7 +74,7 @@ public final class InfoItemDialog {
|
||||
|
||||
// Call an entry's action / onClick method when the entry is selected.
|
||||
final DialogInterface.OnClickListener action = (d, index) ->
|
||||
entries.get(index).action.onClick(fragment, info);
|
||||
entries.get(index).action.onClick((FragmentActivity) activity, info);
|
||||
|
||||
dialog = new AlertDialog.Builder(activity)
|
||||
.setCustomTitle(bannerView)
|
||||
|
||||
@ -9,6 +9,7 @@ import android.net.Uri;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
|
||||
import org.schabi.newpipe.R;
|
||||
import org.schabi.newpipe.database.stream.model.StreamEntity;
|
||||
@ -44,44 +45,43 @@ import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
* </p>
|
||||
*/
|
||||
public enum StreamDialogDefaultEntry {
|
||||
SHOW_CHANNEL_DETAILS(R.string.show_channel_details, (fragment, item) -> {
|
||||
final var activity = fragment.requireActivity();
|
||||
fetchUploaderUrlIfSparse(activity, item.getServiceId(), item.getUrl(),
|
||||
item.getUploaderUrl(), url -> openChannelFragment(activity, item, url));
|
||||
}),
|
||||
SHOW_CHANNEL_DETAILS(R.string.show_channel_details, (fragmentActivity, item) ->
|
||||
fetchUploaderUrlIfSparse(fragmentActivity, item.getServiceId(), item.getUrl(),
|
||||
item.getUploaderUrl(), url -> openChannelFragment(fragmentActivity, item, url))
|
||||
),
|
||||
|
||||
/**
|
||||
* Enqueues the stream automatically to the current PlayerType.
|
||||
*/
|
||||
ENQUEUE(R.string.enqueue_stream, (fragment, item) ->
|
||||
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
|
||||
NavigationHelper.enqueueOnPlayer(fragment.getContext(), singlePlayQueue))
|
||||
ENQUEUE(R.string.enqueue_stream, (fragmentActivity, item) ->
|
||||
fetchItemInfoIfSparse(fragmentActivity, item, singlePlayQueue ->
|
||||
NavigationHelper.enqueueOnPlayer(fragmentActivity, singlePlayQueue))
|
||||
),
|
||||
|
||||
/**
|
||||
* Enqueues the stream automatically to the current PlayerType
|
||||
* after the currently playing stream.
|
||||
*/
|
||||
ENQUEUE_NEXT(R.string.enqueue_next_stream, (fragment, item) ->
|
||||
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
|
||||
NavigationHelper.enqueueNextOnPlayer(fragment.getContext(), singlePlayQueue))
|
||||
ENQUEUE_NEXT(R.string.enqueue_next_stream, (fragmentActivity, item) ->
|
||||
fetchItemInfoIfSparse(fragmentActivity, item, singlePlayQueue ->
|
||||
NavigationHelper.enqueueNextOnPlayer(fragmentActivity, singlePlayQueue))
|
||||
),
|
||||
|
||||
START_HERE_ON_BACKGROUND(R.string.start_here_on_background, (fragment, item) ->
|
||||
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
|
||||
START_HERE_ON_BACKGROUND(R.string.start_here_on_background, (fragmentActivity, item) ->
|
||||
fetchItemInfoIfSparse(fragmentActivity, item, singlePlayQueue ->
|
||||
NavigationHelper.playOnBackgroundPlayer(
|
||||
fragment.getContext(), singlePlayQueue, true))),
|
||||
fragmentActivity, singlePlayQueue, true))),
|
||||
|
||||
START_HERE_ON_POPUP(R.string.start_here_on_popup, (fragment, item) ->
|
||||
fetchItemInfoIfSparse(fragment.requireContext(), item, singlePlayQueue ->
|
||||
NavigationHelper.playOnPopupPlayer(fragment.getContext(), singlePlayQueue, true))),
|
||||
START_HERE_ON_POPUP(R.string.start_here_on_popup, (fragmentActivity, item) ->
|
||||
fetchItemInfoIfSparse(fragmentActivity, item, singlePlayQueue ->
|
||||
NavigationHelper.playOnPopupPlayer(fragmentActivity, singlePlayQueue, true))),
|
||||
|
||||
SET_AS_PLAYLIST_THUMBNAIL(R.string.set_as_playlist_thumbnail, (fragment, item) -> {
|
||||
SET_AS_PLAYLIST_THUMBNAIL(R.string.set_as_playlist_thumbnail, (fragmentActivity, item) -> {
|
||||
throw new UnsupportedOperationException("This needs to be implemented manually "
|
||||
+ "by using InfoItemDialog.Builder.setAction()");
|
||||
}),
|
||||
|
||||
DELETE(R.string.delete, (fragment, item) -> {
|
||||
DELETE(R.string.delete, (fragmentActivity, item) -> {
|
||||
throw new UnsupportedOperationException("This needs to be implemented manually "
|
||||
+ "by using InfoItemDialog.Builder.setAction()");
|
||||
}),
|
||||
@ -90,12 +90,12 @@ public enum StreamDialogDefaultEntry {
|
||||
* Opens a {@link PlaylistDialog} to either append the stream to a playlist
|
||||
* or create a new playlist if there are no local playlists.
|
||||
*/
|
||||
APPEND_PLAYLIST(R.string.add_to_playlist, (fragment, item) ->
|
||||
APPEND_PLAYLIST(R.string.add_to_playlist, (fragmentActivity, item) ->
|
||||
PlaylistDialog.createCorrespondingDialog(
|
||||
fragment.getContext(),
|
||||
fragmentActivity,
|
||||
List.of(new StreamEntity(item)),
|
||||
dialog -> dialog.show(
|
||||
fragment.getParentFragmentManager(),
|
||||
fragmentActivity.getSupportFragmentManager(),
|
||||
"StreamDialogEntry@"
|
||||
+ (dialog instanceof PlaylistAppendDialog ? "append" : "create")
|
||||
+ "_playlist"
|
||||
@ -103,49 +103,50 @@ public enum StreamDialogDefaultEntry {
|
||||
)
|
||||
),
|
||||
|
||||
PLAY_WITH_KODI(R.string.play_with_kodi_title, (fragment, item) ->
|
||||
KoreUtils.playWithKore(fragment.requireContext(), Uri.parse(item.getUrl()))),
|
||||
PLAY_WITH_KODI(R.string.play_with_kodi_title, (fragmentActivity, item) ->
|
||||
KoreUtils.playWithKore(fragmentActivity, Uri.parse(item.getUrl()))),
|
||||
|
||||
SHARE(R.string.share, (fragment, item) ->
|
||||
ShareUtils.shareText(fragment.requireContext(), item.getName(), item.getUrl(),
|
||||
SHARE(R.string.share, (fragmentActivity, item) ->
|
||||
ShareUtils.shareText(fragmentActivity, item.getName(), item.getUrl(),
|
||||
item.getThumbnails())),
|
||||
|
||||
/**
|
||||
* Opens a {@link DownloadDialog} after fetching some stream info.
|
||||
* If the user quits the current fragment, it will not open a DownloadDialog.
|
||||
* If the user quits the current fragmentActivity, it will not open a DownloadDialog.
|
||||
*/
|
||||
DOWNLOAD(R.string.download, (fragment, item) ->
|
||||
fetchStreamInfoAndSaveToDatabase(fragment.requireContext(), item.getServiceId(),
|
||||
DOWNLOAD(R.string.download, (fragmentActivity, item) ->
|
||||
fetchStreamInfoAndSaveToDatabase(fragmentActivity, item.getServiceId(),
|
||||
item.getUrl(), info -> {
|
||||
// Ensure the fragment is attached and its state hasn't been saved to avoid
|
||||
// Ensure the fragment in the activity is attached
|
||||
// and its state hasn't been saved to avoid
|
||||
// showing dialog during lifecycle changes or when the activity is paused,
|
||||
// e.g. by selecting the download option and opening a different fragment.
|
||||
if (fragment.isAdded() && !fragment.isStateSaved()) {
|
||||
final FragmentManager fm = fragmentActivity.getSupportFragmentManager();
|
||||
if (!fm.isStateSaved()) {
|
||||
final DownloadDialog downloadDialog =
|
||||
new DownloadDialog(fragment.requireContext(), info);
|
||||
downloadDialog.show(fragment.getChildFragmentManager(),
|
||||
"downloadDialog");
|
||||
new DownloadDialog(fragmentActivity, info);
|
||||
downloadDialog.show(fm, "downloadDialog");
|
||||
}
|
||||
})
|
||||
),
|
||||
|
||||
OPEN_IN_BROWSER(R.string.open_in_browser, (fragment, item) ->
|
||||
ShareUtils.openUrlInBrowser(fragment.requireContext(), item.getUrl())),
|
||||
OPEN_IN_BROWSER(R.string.open_in_browser, (fragmentActivity, item) ->
|
||||
ShareUtils.openUrlInBrowser(fragmentActivity, item.getUrl())),
|
||||
|
||||
|
||||
MARK_AS_WATCHED(R.string.mark_as_watched, (fragment, item) ->
|
||||
new HistoryRecordManager(fragment.getContext())
|
||||
MARK_AS_WATCHED(R.string.mark_as_watched, (fragmentActivity, item) ->
|
||||
new HistoryRecordManager(fragmentActivity)
|
||||
.markAsWatched(item)
|
||||
.doOnError(error -> {
|
||||
.doOnError(error ->
|
||||
ErrorUtil.showSnackbar(
|
||||
fragment.requireContext(),
|
||||
fragmentActivity,
|
||||
new ErrorInfo(
|
||||
error,
|
||||
UserAction.OPEN_INFO_ITEM_DIALOG,
|
||||
"Got an error when trying to mark as watched"
|
||||
)
|
||||
);
|
||||
})
|
||||
)
|
||||
)
|
||||
.onErrorComplete()
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe()
|
||||
|
||||
@ -4,7 +4,7 @@ import android.content.Context;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
||||
|
||||
@ -26,6 +26,6 @@ public class StreamDialogEntry {
|
||||
}
|
||||
|
||||
public interface StreamDialogEntryAction {
|
||||
void onClick(Fragment fragment, StreamInfoItem infoItem);
|
||||
void onClick(FragmentActivity fragmentActivity, StreamInfoItem infoItem);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user