diff --git a/app/src/main/java/org/schabi/newpipe/ktx/Context.kt b/app/src/main/java/org/schabi/newpipe/ktx/Context.kt new file mode 100644 index 000000000..f2f4e9613 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/ktx/Context.kt @@ -0,0 +1,13 @@ +package org.schabi.newpipe.ktx + +import android.content.Context +import android.content.ContextWrapper +import androidx.fragment.app.FragmentActivity + +tailrec fun Context.findFragmentActivity(): FragmentActivity { + return when (this) { + is FragmentActivity -> this + is ContextWrapper -> baseContext.findFragmentActivity() + else -> throw IllegalStateException("Unable to find FragmentActivity") + } +} diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt b/app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt index 4f58e3e32..237a57667 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt @@ -15,7 +15,6 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.rememberNestedScrollInteropConnection import androidx.compose.ui.res.stringResource -import androidx.fragment.app.FragmentActivity import androidx.preference.PreferenceManager import androidx.window.core.layout.WindowWidthSizeClass import my.nanihadesuka.compose.LazyColumnScrollbar @@ -25,6 +24,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.ui.components.items.playlist.PlaylistListItem import org.schabi.newpipe.ui.components.items.stream.StreamListItem import org.schabi.newpipe.ui.theme.md_theme_dark_primary @@ -40,7 +40,7 @@ fun ItemList( val context = LocalContext.current val onClick = remember { { item: InfoItem -> - val fragmentManager = (context as FragmentActivity).supportFragmentManager + val fragmentManager = context.findFragmentActivity().supportFragmentManager if (item is StreamInfoItem) { NavigationHelper.openVideoDetailFragment( context, fragmentManager, item.serviceId, item.url, item.name, null, false diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/items/stream/StreamMenu.kt b/app/src/main/java/org/schabi/newpipe/ui/components/items/stream/StreamMenu.kt index 2bb143db8..2902aa660 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/items/stream/StreamMenu.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/items/stream/StreamMenu.kt @@ -8,12 +8,12 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource -import androidx.fragment.app.FragmentActivity import androidx.lifecycle.viewmodel.compose.viewModel import org.schabi.newpipe.R import org.schabi.newpipe.database.stream.model.StreamEntity import org.schabi.newpipe.download.DownloadDialog import org.schabi.newpipe.extractor.stream.StreamInfoItem +import org.schabi.newpipe.ktx.findFragmentActivity import org.schabi.newpipe.local.dialog.PlaylistAppendDialog import org.schabi.newpipe.local.dialog.PlaylistDialog import org.schabi.newpipe.player.helper.PlayerHolder @@ -84,7 +84,7 @@ fun StreamMenu( ) { info -> // TODO: Use an AlertDialog composable instead. val downloadDialog = DownloadDialog(context, info) - val fragmentManager = (context as FragmentActivity).supportFragmentManager + val fragmentManager = context.findFragmentActivity().supportFragmentManager downloadDialog.show(fragmentManager, "downloadDialog") } } @@ -97,7 +97,7 @@ fun StreamMenu( PlaylistDialog.createCorrespondingDialog(context, list) { dialog -> val tag = if (dialog is PlaylistAppendDialog) "append" else "create" dialog.show( - (context as FragmentActivity).supportFragmentManager, + context.findFragmentActivity().supportFragmentManager, "StreamDialogEntry@${tag}_playlist" ) } @@ -131,7 +131,8 @@ fun StreamMenu( SparseItemUtil.fetchUploaderUrlIfSparse( context, stream.serviceId, stream.url, stream.uploaderUrl ) { url -> - NavigationHelper.openChannelFragment(context as FragmentActivity, stream, url) + val activity = context.findFragmentActivity() + NavigationHelper.openChannelFragment(activity, stream, url) } } ) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt index 59a451392..baf0c7106 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt @@ -38,7 +38,6 @@ import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import androidx.compose.ui.tooling.preview.PreviewParameterProvider import androidx.compose.ui.unit.dp -import androidx.fragment.app.FragmentActivity import coil.compose.AsyncImage import org.schabi.newpipe.R import org.schabi.newpipe.extractor.Page @@ -84,9 +83,7 @@ fun Comment(comment: CommentsInfoItem) { .size(42.dp) .clip(CircleShape) .clickable { - NavigationHelper.openCommentAuthorIfPresent( - context as FragmentActivity, comment - ) + NavigationHelper.openCommentAuthorIfPresent(context, comment) } ) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesHeader.kt b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesHeader.kt index 64d391b08..3a10fe021 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesHeader.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesHeader.kt @@ -22,7 +22,6 @@ import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.fragment.app.FragmentActivity import coil.compose.AsyncImage import org.schabi.newpipe.R import org.schabi.newpipe.extractor.comments.CommentsInfoItem @@ -45,8 +44,7 @@ fun CommentRepliesHeader(comment: CommentsInfoItem) { ) { Row( modifier = Modifier.clickable { - val activity = context as FragmentActivity - NavigationHelper.openCommentAuthorIfPresent(activity, comment) + NavigationHelper.openCommentAuthorIfPresent(context, comment) }, horizontalArrangement = Arrangement.spacedBy(8.dp), verticalAlignment = Alignment.CenterVertically diff --git a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java index 2f8952afa..139381867 100644 --- a/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java @@ -48,6 +48,7 @@ import org.schabi.newpipe.fragments.list.channel.ChannelFragment; import org.schabi.newpipe.fragments.list.kiosk.KioskFragment; import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment; +import org.schabi.newpipe.ktx.ContextKt; import org.schabi.newpipe.local.bookmark.BookmarkFragment; import org.schabi.newpipe.local.feed.FeedFragment; import org.schabi.newpipe.local.history.StatisticsPlaylistFragment; @@ -483,19 +484,20 @@ public final class NavigationHelper { * Opens the comment author channel fragment, if the {@link CommentsInfoItem#getUploaderUrl()} * of {@code comment} is non-null. Shows a UI-error snackbar if something goes wrong. * - * @param activity the activity with the fragment manager and in which to show the snackbar + * @param context the context to use for opening the fragment * @param comment the comment whose uploader/author will be opened */ - public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity, + public static void openCommentAuthorIfPresent(@NonNull final Context context, @NonNull final CommentsInfoItem comment) { if (isEmpty(comment.getUploaderUrl())) { return; } try { + final var activity = ContextKt.findFragmentActivity(context); openChannelFragment(activity.getSupportFragmentManager(), comment.getServiceId(), comment.getUploaderUrl(), comment.getUploaderName()); } catch (final Exception e) { - ErrorUtil.showUiErrorSnackbar(activity, "Opening channel fragment", e); + ErrorUtil.showUiErrorSnackbar(context, "Opening channel fragment", e); } }