1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-09 17:00:32 +00:00

Avoid issues if context is a ContextWrapper

This commit is contained in:
Isira Seneviratne 2024-09-11 22:10:25 +05:30
parent 0d12cfc983
commit 1f4298b6c2
4 changed files with 22 additions and 11 deletions

View File

@ -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")
}
}

View File

@ -14,7 +14,6 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.fragment.app.FragmentActivity
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.window.core.layout.WindowWidthSizeClass import androidx.window.core.layout.WindowWidthSizeClass
import my.nanihadesuka.compose.LazyColumnScrollbar 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.playlist.PlaylistInfoItem
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.info_list.ItemViewMode 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.playlist.PlaylistListItem
import org.schabi.newpipe.ui.components.items.stream.StreamListItem import org.schabi.newpipe.ui.components.items.stream.StreamListItem
import org.schabi.newpipe.util.DependentPreferenceHelper import org.schabi.newpipe.util.DependentPreferenceHelper
@ -37,7 +37,7 @@ fun ItemList(
val context = LocalContext.current val context = LocalContext.current
val onClick = remember { val onClick = remember {
{ item: InfoItem -> { item: InfoItem ->
val fragmentManager = (context as FragmentActivity).supportFragmentManager val fragmentManager = context.findFragmentActivity().supportFragmentManager
if (item is StreamInfoItem) { if (item is StreamInfoItem) {
NavigationHelper.openVideoDetailFragment( NavigationHelper.openVideoDetailFragment(
context, fragmentManager, item.serviceId, item.url, item.name, null, false context, fragmentManager, item.serviceId, item.url, item.name, null, false

View File

@ -8,12 +8,12 @@ import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.stream.model.StreamEntity import org.schabi.newpipe.database.stream.model.StreamEntity
import org.schabi.newpipe.download.DownloadDialog import org.schabi.newpipe.download.DownloadDialog
import org.schabi.newpipe.extractor.stream.StreamInfoItem 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.PlaylistAppendDialog
import org.schabi.newpipe.local.dialog.PlaylistDialog import org.schabi.newpipe.local.dialog.PlaylistDialog
import org.schabi.newpipe.player.helper.PlayerHolder import org.schabi.newpipe.player.helper.PlayerHolder
@ -84,7 +84,7 @@ fun StreamMenu(
) { info -> ) { info ->
// TODO: Use an AlertDialog composable instead. // TODO: Use an AlertDialog composable instead.
val downloadDialog = DownloadDialog(context, info) val downloadDialog = DownloadDialog(context, info)
val fragmentManager = (context as FragmentActivity).supportFragmentManager val fragmentManager = context.findFragmentActivity().supportFragmentManager
downloadDialog.show(fragmentManager, "downloadDialog") downloadDialog.show(fragmentManager, "downloadDialog")
} }
} }
@ -96,10 +96,8 @@ fun StreamMenu(
val list = listOf(StreamEntity(stream)) val list = listOf(StreamEntity(stream))
PlaylistDialog.createCorrespondingDialog(context, list) { dialog -> PlaylistDialog.createCorrespondingDialog(context, list) { dialog ->
val tag = if (dialog is PlaylistAppendDialog) "append" else "create" val tag = if (dialog is PlaylistAppendDialog) "append" else "create"
dialog.show( val fragmentManager = context.findFragmentActivity().supportFragmentManager
(context as FragmentActivity).supportFragmentManager, dialog.show(fragmentManager, "StreamDialogEntry@${tag}_playlist")
"StreamDialogEntry@${tag}_playlist"
)
} }
} }
) )
@ -131,7 +129,7 @@ fun StreamMenu(
SparseItemUtil.fetchUploaderUrlIfSparse( SparseItemUtil.fetchUploaderUrlIfSparse(
context, stream.serviceId, stream.url, stream.uploaderUrl context, stream.serviceId, stream.url, stream.uploaderUrl
) { url -> ) { url ->
NavigationHelper.openChannelFragment(context as FragmentActivity, stream, url) NavigationHelper.openChannelFragment(context.findFragmentActivity(), stream, url)
} }
} }
) )

View File

@ -20,8 +20,8 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.ktx.findFragmentActivity
import org.schabi.newpipe.ui.theme.AppTheme import org.schabi.newpipe.ui.theme.AppTheme
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
@ -44,7 +44,7 @@ fun TagsSection(serviceId: Int, tags: List<String>) {
ElevatedSuggestionChip( ElevatedSuggestionChip(
onClick = { onClick = {
NavigationHelper.openSearchFragment( NavigationHelper.openSearchFragment(
(context as FragmentActivity).supportFragmentManager, serviceId, tag context.findFragmentActivity().supportFragmentManager, serviceId, tag
) )
}, },
label = { Text(text = tag) } label = { Text(text = tag) }