Skip to content

Commit 6ab95a9

Browse files
committed
Fix tab sync: use title-hash IDs, live chat as 2nd tab, auto-select on live
1 parent 0f59bc4 commit 6ab95a9

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/detail/TabAdapter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ public TabAdapter(final FragmentManager fm) {
2424
this.fragmentManager = fm;
2525
}
2626

27+
@Override
28+
public long getItemId(final int position) {
29+
// Use the fragment title hash as ID instead of position.
30+
// This allows inserting fragments in the middle without breaking
31+
// FragmentPagerAdapter's internal fragment cache.
32+
return mFragmentTitleList.get(position).hashCode();
33+
}
34+
2735
@NonNull
2836
@Override
2937
public Fragment getItem(final int position) {

app/src/main/java/org/schabi/newpipe/fragments/detail/VideoDetailFragment.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -928,17 +928,26 @@ class VideoDetailFragment :
928928
return
929929
}
930930

931-
// Append live chat tab at the end to avoid FragmentPagerAdapter
932-
// position synchronization issues when inserting in the middle
931+
val commentsPosition = pageAdapter.getItemPositionByTitle(COMMENTS_TAB_TAG)
932+
val insertPosition = if (commentsPosition >= 0) commentsPosition + 1 else pageAdapter.count
933+
933934
pageAdapter.addFragment(
934935
LiveChatFragment.getInstance(serviceId, url, continuation),
935-
LIVE_CHAT_TAB_TAG
936+
LIVE_CHAT_TAB_TAG,
937+
insertPosition
936938
)
937-
tabIcons.add(R.drawable.ic_live_tv)
938-
tabContentDescriptions.add(R.string.live_chat_tab_description)
939+
tabIcons.add(insertPosition, R.drawable.ic_live_tv)
940+
tabContentDescriptions.add(insertPosition, R.string.live_chat_tab_description)
939941
pageAdapter.notifyDataSetUpdate()
940942
updateTabIconsAndContentDescriptions()
941943
updateTabLayoutVisibility()
944+
945+
// Select live chat tab by default for live streams
946+
if (streamInfo.streamType == StreamType.LIVE_STREAM ||
947+
streamInfo.streamType == StreamType.AUDIO_LIVE_STREAM
948+
) {
949+
binding.viewPager.setCurrentItem(insertPosition, false)
950+
}
942951
}
943952

944953
fun updateTabLayoutVisibility() {

0 commit comments

Comments
 (0)