Add Context.findFragmentManager() extension function

This commit is contained in:
Stypox 2026-02-09 15:10:35 +01:00
parent 9c558d97e9
commit d075539a8a
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
4 changed files with 12 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package org.schabi.newpipe.ktx
import android.content.Context
import android.content.ContextWrapper
import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentManager
tailrec fun Context.findFragmentActivity(): FragmentActivity {
return when (this) {
@ -11,3 +12,7 @@ tailrec fun Context.findFragmentActivity(): FragmentActivity {
else -> throw IllegalStateException("Unable to find FragmentActivity")
}
}
fun Context.findFragmentManager(): FragmentManager {
return findFragmentActivity().supportFragmentManager
}

View File

@ -18,7 +18,7 @@ import org.schabi.newpipe.extractor.InfoItem
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.info_list.ItemViewMode
import org.schabi.newpipe.ktx.findFragmentActivity
import org.schabi.newpipe.ktx.findFragmentManager
import org.schabi.newpipe.player.playqueue.PlayQueue
import org.schabi.newpipe.ui.components.common.LazyColumnThemedScrollbar
import org.schabi.newpipe.ui.components.items.playlist.PlaylistListItem
@ -41,11 +41,10 @@ fun ItemList(
val context = LocalContext.current
val onClick = remember {
{ item: InfoItem ->
val fragmentManager = context.findFragmentActivity().supportFragmentManager
if (item is StreamInfoItem) {
NavigationHelper.openVideoDetailFragment(
context,
fragmentManager,
context.findFragmentManager(),
item.serviceId,
item.url,
item.name,
@ -54,7 +53,7 @@ fun ItemList(
)
} else if (item is PlaylistInfoItem) {
NavigationHelper.openPlaylistFragment(
fragmentManager,
context.findFragmentManager(),
item.serviceId,
item.url,
item.name

View File

@ -50,7 +50,7 @@ import org.schabi.newpipe.extractor.channel.ChannelInfoItem
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.ktx.findFragmentActivity
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog
import org.schabi.newpipe.ktx.findFragmentManager
import org.schabi.newpipe.local.dialog.PlaylistDialog
import org.schabi.newpipe.local.history.HistoryRecordManager
import org.schabi.newpipe.local.playlist.LocalPlaylistManager
@ -240,9 +240,7 @@ data class LongPressAction(
addAction(Type.Download) { context ->
val info = fetchStreamInfoAndSaveToDatabase(context, item.serviceId, item.url)
val downloadDialog = DownloadDialog(context, info)
val fragmentManager = context.findFragmentActivity()
.supportFragmentManager
downloadDialog.show(fragmentManager, "downloadDialog")
downloadDialog.show(context.findFragmentManager(), "downloadDialog")
}
addAction(Type.AddToPlaylist) { context ->
LocalPlaylistManager(NewPipeDatabase.getInstance(context))
@ -251,11 +249,7 @@ data class LongPressAction(
PlaylistDialog.createCorrespondingDialog(context, listOf(StreamEntity(item)))
.awaitSingle()
}
val tag = if (dialog is PlaylistAppendDialog) "append" else "create"
dialog.show(
context.findFragmentActivity().supportFragmentManager,
"StreamDialogEntry@${tag}_playlist"
)
dialog.show(context.findFragmentManager(), "addToPlaylistDialog")
}
addAction(Type.ShowChannelDetails) { context ->
val uploaderUrl = fetchUploaderUrlIfSparse(

View File

@ -493,8 +493,7 @@ public final class NavigationHelper {
return;
}
try {
final var activity = ContextKt.findFragmentActivity(context);
openChannelFragment(activity.getSupportFragmentManager(), comment.getServiceId(),
openChannelFragment(ContextKt.findFragmentManager(context), comment.getServiceId(),
comment.getUploaderUrl(), comment.getUploaderName());
} catch (final Exception e) {
ErrorUtil.showUiErrorSnackbar(context, "Opening channel fragment", e);