Skip to content

Commit 3177ca6

Browse files
Avoid issues if context is a ContextWrapper
1 parent 5017f4f commit 3177ca6

6 files changed

Lines changed: 27 additions & 16 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
@@ -15,7 +15,6 @@ import androidx.compose.ui.input.nestedscroll.nestedScroll
1515
import androidx.compose.ui.platform.LocalContext
1616
import androidx.compose.ui.platform.rememberNestedScrollInteropConnection
1717
import androidx.compose.ui.res.stringResource
18-
import androidx.fragment.app.FragmentActivity
1918
import androidx.preference.PreferenceManager
2019
import androidx.window.core.layout.WindowWidthSizeClass
2120
import my.nanihadesuka.compose.LazyColumnScrollbar
@@ -25,6 +24,7 @@ import org.schabi.newpipe.extractor.InfoItem
2524
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
2625
import org.schabi.newpipe.extractor.stream.StreamInfoItem
2726
import org.schabi.newpipe.info_list.ItemViewMode
27+
import org.schabi.newpipe.ktx.findFragmentActivity
2828
import org.schabi.newpipe.ui.components.items.playlist.PlaylistListItem
2929
import org.schabi.newpipe.ui.components.items.stream.StreamListItem
3030
import org.schabi.newpipe.ui.theme.md_theme_dark_primary
@@ -40,7 +40,7 @@ fun ItemList(
4040
val context = LocalContext.current
4141
val onClick = remember {
4242
{ item: InfoItem ->
43-
val fragmentManager = (context as FragmentActivity).supportFragmentManager
43+
val fragmentManager = context.findFragmentActivity().supportFragmentManager
4444
if (item is StreamInfoItem) {
4545
NavigationHelper.openVideoDetailFragment(
4646
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 & 4 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
}
@@ -97,7 +97,7 @@ fun StreamMenu(
9797
PlaylistDialog.createCorrespondingDialog(context, list) { dialog ->
9898
val tag = if (dialog is PlaylistAppendDialog) "append" else "create"
9999
dialog.show(
100-
(context as FragmentActivity).supportFragmentManager,
100+
context.findFragmentActivity().supportFragmentManager,
101101
"StreamDialogEntry@${tag}_playlist"
102102
)
103103
}
@@ -131,7 +131,8 @@ fun StreamMenu(
131131
SparseItemUtil.fetchUploaderUrlIfSparse(
132132
context, stream.serviceId, stream.url, stream.uploaderUrl
133133
) { url ->
134-
NavigationHelper.openChannelFragment(context as FragmentActivity, stream, url)
134+
val activity = context.findFragmentActivity()
135+
NavigationHelper.openChannelFragment(activity, stream, url)
135136
}
136137
}
137138
)

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/Comment.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import androidx.compose.ui.tooling.preview.Preview
3838
import androidx.compose.ui.tooling.preview.PreviewParameter
3939
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
4040
import androidx.compose.ui.unit.dp
41-
import androidx.fragment.app.FragmentActivity
4241
import coil.compose.AsyncImage
4342
import org.schabi.newpipe.R
4443
import org.schabi.newpipe.extractor.Page
@@ -84,9 +83,7 @@ fun Comment(comment: CommentsInfoItem) {
8483
.size(42.dp)
8584
.clip(CircleShape)
8685
.clickable {
87-
NavigationHelper.openCommentAuthorIfPresent(
88-
context as FragmentActivity, comment
89-
)
86+
NavigationHelper.openCommentAuthorIfPresent(context, comment)
9087
}
9188
)
9289

app/src/main/java/org/schabi/newpipe/ui/components/video/comment/CommentRepliesHeader.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import androidx.compose.ui.res.painterResource
2222
import androidx.compose.ui.res.stringResource
2323
import androidx.compose.ui.tooling.preview.Preview
2424
import androidx.compose.ui.unit.dp
25-
import androidx.fragment.app.FragmentActivity
2625
import coil.compose.AsyncImage
2726
import org.schabi.newpipe.R
2827
import org.schabi.newpipe.extractor.comments.CommentsInfoItem
@@ -45,8 +44,7 @@ fun CommentRepliesHeader(comment: CommentsInfoItem) {
4544
) {
4645
Row(
4746
modifier = Modifier.clickable {
48-
val activity = context as FragmentActivity
49-
NavigationHelper.openCommentAuthorIfPresent(activity, comment)
47+
NavigationHelper.openCommentAuthorIfPresent(context, comment)
5048
},
5149
horizontalArrangement = Arrangement.spacedBy(8.dp),
5250
verticalAlignment = Alignment.CenterVertically

app/src/main/java/org/schabi/newpipe/util/NavigationHelper.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.schabi.newpipe.fragments.list.kiosk.KioskFragment;
4949
import org.schabi.newpipe.fragments.list.playlist.PlaylistFragment;
5050
import org.schabi.newpipe.fragments.list.search.SearchFragment;
51+
import org.schabi.newpipe.ktx.ContextKt;
5152
import org.schabi.newpipe.local.bookmark.BookmarkFragment;
5253
import org.schabi.newpipe.local.feed.FeedFragment;
5354
import org.schabi.newpipe.local.history.StatisticsPlaylistFragment;
@@ -483,19 +484,20 @@ public static void openChannelFragment(@NonNull final FragmentActivity activity,
483484
* Opens the comment author channel fragment, if the {@link CommentsInfoItem#getUploaderUrl()}
484485
* of {@code comment} is non-null. Shows a UI-error snackbar if something goes wrong.
485486
*
486-
* @param activity the activity with the fragment manager and in which to show the snackbar
487+
* @param context the context to use for opening the fragment
487488
* @param comment the comment whose uploader/author will be opened
488489
*/
489-
public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity,
490+
public static void openCommentAuthorIfPresent(@NonNull final Context context,
490491
@NonNull final CommentsInfoItem comment) {
491492
if (isEmpty(comment.getUploaderUrl())) {
492493
return;
493494
}
494495
try {
496+
final var activity = ContextKt.findFragmentActivity(context);
495497
openChannelFragment(activity.getSupportFragmentManager(), comment.getServiceId(),
496498
comment.getUploaderUrl(), comment.getUploaderName());
497499
} catch (final Exception e) {
498-
ErrorUtil.showUiErrorSnackbar(activity, "Opening channel fragment", e);
500+
ErrorUtil.showUiErrorSnackbar(context, "Opening channel fragment", e);
499501
}
500502
}
501503

0 commit comments

Comments
 (0)