From ef284ba51d05586fe5a85168bd484d4dc3acb0a9 Mon Sep 17 00:00:00 2001
From: Kai DeLorenzo <git@seaoflaurels.com>
Date: Wed, 5 Jun 2024 13:44:05 -0500
Subject: [PATCH] fixed tab changing when adding the playlist tab

---
 .../mainactivity/main/ChannelFragment.kt       | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ChannelFragment.kt b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ChannelFragment.kt
index f6126c58..8015574e 100644
--- a/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ChannelFragment.kt
+++ b/app/src/main/java/com/futo/platformplayer/fragment/mainactivity/main/ChannelFragment.kt
@@ -459,17 +459,31 @@ class ChannelFragment : MainFragment() {
 
             val supportsPlaylists =
                 StatePlatform.instance.getChannelClient(channel.url).capabilities.hasGetChannelPlaylists
+            val playlistPosition = 2
             if (supportsPlaylists && !(_viewPager.adapter as ChannelViewPagerAdapter).containsItem(
                     ChannelTab.PLAYLISTS.ordinal.toLong()
                 )
             ) {
-                (_viewPager.adapter as ChannelViewPagerAdapter).insert(2, ChannelTab.PLAYLISTS)
+                // keep the current tab selected
+                if (_viewPager.currentItem >= playlistPosition) {
+                    _viewPager.setCurrentItem(_viewPager.currentItem + 1, false)
+                }
+
+                (_viewPager.adapter as ChannelViewPagerAdapter).insert(
+                    playlistPosition,
+                    ChannelTab.PLAYLISTS
+                )
             }
             if (!supportsPlaylists && (_viewPager.adapter as ChannelViewPagerAdapter).containsItem(
                     ChannelTab.PLAYLISTS.ordinal.toLong()
                 )
             ) {
-                (_viewPager.adapter as ChannelViewPagerAdapter).remove(2)
+                // keep the current tab selected
+                if (_viewPager.currentItem >= playlistPosition) {
+                    _viewPager.setCurrentItem(_viewPager.currentItem - 1, false)
+                }
+
+                (_viewPager.adapter as ChannelViewPagerAdapter).remove(playlistPosition)
             }
 
             // sets the channel for each tab
-- 
GitLab