Skip to content

Commit 1f4298b

Browse files
Avoid issues if context is a ContextWrapper
1 parent 0d12cfc commit 1f4298b

4 files changed

Lines changed: 22 additions & 11 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.schabi.newpipe.ktx
2+
3+
import android.content.Context
4+
import android.content.ContextWrapper
5+
import androidx.fragment.app.FragmentActivity
6+
7+
tailrec fun Context.findFragmentActivity(): FragmentActivity {
8+
return when (this) {
9+
is FragmentActivity -> this
10+
is ContextWrapper -> baseContext.findFragmentActivity()
11+
else -> throw IllegalStateException("Unable to find FragmentActivity")
12+
}
13+
}

app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
1414
import androidx.compose.ui.platform.LocalContext
1515
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
1616
import androidx.compose.ui.res.stringResource
17-
import androidx.fragment.app.FragmentActivity
1817
import androidx.preference.PreferenceManager
1918
import androidx.window.core.layout.WindowWidthSizeClass
2019
import my.nanihadesuka.compose.LazyColumnScrollbar
@@ -23,6 +22,7 @@ import org.schabi.newpipe.extractor.InfoItem
2322
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
2423
import org.schabi.newpipe.extractor.stream.StreamInfoItem
2524
import org.schabi.newpipe.info_list.ItemViewMode
25+
import org.schabi.newpipe.ktx.findFragmentActivity
2626
import org.schabi.newpipe.ui.components.items.playlist.PlaylistListItem
2727
import org.schabi.newpipe.ui.components.items.stream.StreamListItem
2828
import org.schabi.newpipe.util.DependentPreferenceHelper
@@ -37,7 +37,7 @@ fun ItemList(
3737
val context = LocalContext.current
3838
val onClick = remember {
3939
{ item: InfoItem ->
40-
val fragmentManager = (context as FragmentActivity).supportFragmentManager
40+
val fragmentManager = context.findFragmentActivity().supportFragmentManager
4141
if (item is StreamInfoItem) {
4242
NavigationHelper.openVideoDetailFragment(
4343
context, fragmentManager, item.serviceId, item.url, item.name, null, false

app/src/main/java/org/schabi/newpipe/ui/components/items/stream/StreamMenu.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import androidx.compose.material3.Text
88
import androidx.compose.runtime.Composable
99
import androidx.compose.ui.platform.LocalContext
1010
import androidx.compose.ui.res.stringResource
11-
import androidx.fragment.app.FragmentActivity
1211
import androidx.lifecycle.viewmodel.compose.viewModel
1312
import org.schabi.newpipe.R
1413
import org.schabi.newpipe.database.stream.model.StreamEntity
1514
import org.schabi.newpipe.download.DownloadDialog
1615
import org.schabi.newpipe.extractor.stream.StreamInfoItem
16+
import org.schabi.newpipe.ktx.findFragmentActivity
1717
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog
1818
import org.schabi.newpipe.local.dialog.PlaylistDialog
1919
import org.schabi.newpipe.player.helper.PlayerHolder
@@ -84,7 +84,7 @@ fun StreamMenu(
8484
) { info ->
8585
// TODO: Use an AlertDialog composable instead.
8686
val downloadDialog = DownloadDialog(context, info)
87-
val fragmentManager = (context as FragmentActivity).supportFragmentManager
87+
val fragmentManager = context.findFragmentActivity().supportFragmentManager
8888
downloadDialog.show(fragmentManager, "downloadDialog")
8989
}
9090
}
@@ -96,10 +96,8 @@ fun StreamMenu(
9696
val list = listOf(StreamEntity(stream))
9797
PlaylistDialog.createCorrespondingDialog(context, list) { dialog ->
9898
val tag = if (dialog is PlaylistAppendDialog) "append" else "create"
99-
dialog.show(
100-
(context as FragmentActivity).supportFragmentManager,
101-
"StreamDialogEntry@${tag}_playlist"
102-
)
99+
val fragmentManager = context.findFragmentActivity().supportFragmentManager
100+
dialog.show(fragmentManager, "StreamDialogEntry@${tag}_playlist")
103101
}
104102
}
105103
)
@@ -131,7 +129,7 @@ fun StreamMenu(
131129
SparseItemUtil.fetchUploaderUrlIfSparse(
132130
context, stream.serviceId, stream.url, stream.uploaderUrl
133131
) { url ->
134-
NavigationHelper.openChannelFragment(context as FragmentActivity, stream, url)
132+
NavigationHelper.openChannelFragment(context.findFragmentActivity(), stream, url)
135133
}
136134
}
137135
)

app/src/main/java/org/schabi/newpipe/ui/components/metadata/TagsSection.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import androidx.compose.ui.text.font.FontWeight
2020
import androidx.compose.ui.text.style.TextAlign
2121
import androidx.compose.ui.tooling.preview.Preview
2222
import androidx.compose.ui.unit.dp
23-
import androidx.fragment.app.FragmentActivity
2423
import org.schabi.newpipe.R
24+
import org.schabi.newpipe.ktx.findFragmentActivity
2525
import org.schabi.newpipe.ui.theme.AppTheme
2626
import org.schabi.newpipe.util.NavigationHelper
2727

@@ -44,7 +44,7 @@ fun TagsSection(serviceId: Int, tags: List<String>) {
4444
ElevatedSuggestionChip(
4545
onClick = {
4646
NavigationHelper.openSearchFragment(
47-
(context as FragmentActivity).supportFragmentManager, serviceId, tag
47+
context.findFragmentActivity().supportFragmentManager, serviceId, tag
4848
)
4949
},
5050
label = { Text(text = tag) }

0 commit comments

Comments
 (0)