Skip to content

Commit 3514c85

Browse files
committed
Add Context.findFragmentManager() extension function
1 parent 2cb0785 commit 3514c85

4 files changed

Lines changed: 12 additions & 15 deletions

File tree

app/src/main/java/org/schabi/newpipe/ktx/Context.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.schabi.newpipe.ktx
33
import android.content.Context
44
import android.content.ContextWrapper
55
import androidx.fragment.app.FragmentActivity
6+
import androidx.fragment.app.FragmentManager
67

78
tailrec fun Context.findFragmentActivity(): FragmentActivity {
89
return when (this) {
@@ -11,3 +12,7 @@ tailrec fun Context.findFragmentActivity(): FragmentActivity {
1112
else -> throw IllegalStateException("Unable to find FragmentActivity")
1213
}
1314
}
15+
16+
fun Context.findFragmentManager(): FragmentManager {
17+
return findFragmentActivity().supportFragmentManager
18+
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import org.schabi.newpipe.extractor.InfoItem
1818
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
1919
import org.schabi.newpipe.extractor.stream.StreamInfoItem
2020
import org.schabi.newpipe.info_list.ItemViewMode
21-
import org.schabi.newpipe.ktx.findFragmentActivity
21+
import org.schabi.newpipe.ktx.findFragmentManager
2222
import org.schabi.newpipe.player.playqueue.PlayQueue
2323
import org.schabi.newpipe.ui.components.common.LazyColumnThemedScrollbar
2424
import org.schabi.newpipe.ui.components.items.playlist.PlaylistListItem
@@ -41,11 +41,10 @@ fun ItemList(
4141
val context = LocalContext.current
4242
val onClick = remember {
4343
{ item: InfoItem ->
44-
val fragmentManager = context.findFragmentActivity().supportFragmentManager
4544
if (item is StreamInfoItem) {
4645
NavigationHelper.openVideoDetailFragment(
4746
context,
48-
fragmentManager,
47+
context.findFragmentManager(),
4948
item.serviceId,
5049
item.url,
5150
item.name,
@@ -54,7 +53,7 @@ fun ItemList(
5453
)
5554
} else if (item is PlaylistInfoItem) {
5655
NavigationHelper.openPlaylistFragment(
57-
fragmentManager,
56+
context.findFragmentManager(),
5857
item.serviceId,
5958
item.url,
6059
item.name

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressAction.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ import org.schabi.newpipe.extractor.channel.ChannelInfoItem
5050
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem
5151
import org.schabi.newpipe.extractor.stream.StreamInfoItem
5252
import org.schabi.newpipe.ktx.findFragmentActivity
53-
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog
53+
import org.schabi.newpipe.ktx.findFragmentManager
5454
import org.schabi.newpipe.local.dialog.PlaylistDialog
5555
import org.schabi.newpipe.local.history.HistoryRecordManager
5656
import org.schabi.newpipe.local.playlist.LocalPlaylistManager
@@ -240,9 +240,7 @@ data class LongPressAction(
240240
addAction(Type.Download) { context ->
241241
val info = fetchStreamInfoAndSaveToDatabase(context, item.serviceId, item.url)
242242
val downloadDialog = DownloadDialog(context, info)
243-
val fragmentManager = context.findFragmentActivity()
244-
.supportFragmentManager
245-
downloadDialog.show(fragmentManager, "downloadDialog")
243+
downloadDialog.show(context.findFragmentManager(), "downloadDialog")
246244
}
247245
addAction(Type.AddToPlaylist) { context ->
248246
LocalPlaylistManager(NewPipeDatabase.getInstance(context))
@@ -251,11 +249,7 @@ data class LongPressAction(
251249
PlaylistDialog.createCorrespondingDialog(context, listOf(StreamEntity(item)))
252250
.awaitSingle()
253251
}
254-
val tag = if (dialog is PlaylistAppendDialog) "append" else "create"
255-
dialog.show(
256-
context.findFragmentActivity().supportFragmentManager,
257-
"StreamDialogEntry@${tag}_playlist"
258-
)
252+
dialog.show(context.findFragmentManager(), "addToPlaylistDialog")
259253
}
260254
addAction(Type.ShowChannelDetails) { context ->
261255
val uploaderUrl = fetchUploaderUrlIfSparse(

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,7 @@ public static void openCommentAuthorIfPresent(@NonNull final Context context,
493493
return;
494494
}
495495
try {
496-
final var activity = ContextKt.findFragmentActivity(context);
497-
openChannelFragment(activity.getSupportFragmentManager(), comment.getServiceId(),
496+
openChannelFragment(ContextKt.findFragmentManager(context), comment.getServiceId(),
498497
comment.getUploaderUrl(), comment.getUploaderName());
499498
} catch (final Exception e) {
500499
ErrorUtil.showUiErrorSnackbar(context, "Opening channel fragment", e);

0 commit comments

Comments
 (0)