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 506687d12..2491a6c1c 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 @@ -14,7 +14,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 @@ -23,6 +22,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.util.DependentPreferenceHelper @@ -37,7 +37,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..7aa0b032c 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") } } @@ -96,10 +96,8 @@ fun StreamMenu( val list = listOf(StreamEntity(stream)) PlaylistDialog.createCorrespondingDialog(context, list) { dialog -> val tag = if (dialog is PlaylistAppendDialog) "append" else "create" - dialog.show( - (context as FragmentActivity).supportFragmentManager, - "StreamDialogEntry@${tag}_playlist" - ) + val fragmentManager = context.findFragmentActivity().supportFragmentManager + dialog.show(fragmentManager, "StreamDialogEntry@${tag}_playlist") } } ) @@ -131,7 +129,7 @@ fun StreamMenu( SparseItemUtil.fetchUploaderUrlIfSparse( context, stream.serviceId, stream.url, stream.uploaderUrl ) { url -> - NavigationHelper.openChannelFragment(context as FragmentActivity, stream, url) + NavigationHelper.openChannelFragment(context.findFragmentActivity(), stream, url) } } ) diff --git a/app/src/main/java/org/schabi/newpipe/ui/components/metadata/TagsSection.kt b/app/src/main/java/org/schabi/newpipe/ui/components/metadata/TagsSection.kt index 2357c6981..6254e9c51 100644 --- a/app/src/main/java/org/schabi/newpipe/ui/components/metadata/TagsSection.kt +++ b/app/src/main/java/org/schabi/newpipe/ui/components/metadata/TagsSection.kt @@ -20,8 +20,8 @@ import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp -import androidx.fragment.app.FragmentActivity import org.schabi.newpipe.R +import org.schabi.newpipe.ktx.findFragmentActivity import org.schabi.newpipe.ui.theme.AppTheme import org.schabi.newpipe.util.NavigationHelper @@ -44,7 +44,7 @@ fun TagsSection(serviceId: Int, tags: List) { ElevatedSuggestionChip( onClick = { NavigationHelper.openSearchFragment( - (context as FragmentActivity).supportFragmentManager, serviceId, tag + context.findFragmentActivity().supportFragmentManager, serviceId, tag ) }, label = { Text(text = tag) }