fix: remember selected channel tab on screen rotation

This commit is contained in:
ThetaDev 2022-10-23 21:28:54 +02:00 committed by Stypox
parent bb062f07f9
commit c929f00456
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
5 changed files with 31 additions and 7 deletions

View file

@ -16,6 +16,8 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.google.android.material.tabs.TabLayout;
import org.schabi.newpipe.R; import org.schabi.newpipe.R;
import org.schabi.newpipe.database.subscription.NotificationMode; import org.schabi.newpipe.database.subscription.NotificationMode;
import org.schabi.newpipe.database.subscription.SubscriptionEntity; import org.schabi.newpipe.database.subscription.SubscriptionEntity;
@ -58,6 +60,7 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
private Disposable subscriptionMonitor; private Disposable subscriptionMonitor;
private final CompositeDisposable disposables = new CompositeDisposable(); private final CompositeDisposable disposables = new CompositeDisposable();
private SubscriptionManager subscriptionManager; private SubscriptionManager subscriptionManager;
private int lastTab;
private MenuItem menuRssButton; private MenuItem menuRssButton;
private MenuItem menuNotifyButton; private MenuItem menuNotifyButton;
@ -94,10 +97,16 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
public void onCreate(final Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(true); setHasOptionsMenu(true);
if (savedInstanceState != null) {
lastTab = savedInstanceState.getInt("LastTab");
} else {
lastTab = 0;
}
} }
@Override @Override
public void onAttach(@NonNull Context context) { public void onAttach(final @NonNull Context context) {
super.onAttach(context); super.onAttach(context);
subscriptionManager = new SubscriptionManager(activity); subscriptionManager = new SubscriptionManager(activity);
} }
@ -119,6 +128,12 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
binding.tabLayout.setupWithViewPager(binding.viewPager); binding.tabLayout.setupWithViewPager(binding.viewPager);
} }
@Override
public void onSaveInstanceState(final @NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("LastTab", binding.tabLayout.getSelectedTabPosition());
}
@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
@ -284,8 +299,8 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
} }
final String description = currentInfo.getDescription(); final String description = currentInfo.getDescription();
if (description != null && !description.isEmpty() && if (description != null && !description.isEmpty()
ChannelTabs.showChannelTab( && ChannelTabs.showChannelTab(
context, preferences, R.string.show_channel_tabs_info)) { context, preferences, R.string.show_channel_tabs_info)) {
tabAdapter.addFragment( tabAdapter.addFragment(
ChannelInfoFragment.getInstance(currentInfo), "Info"); ChannelInfoFragment.getInstance(currentInfo), "Info");
@ -298,6 +313,12 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
for (int i = 0; i < tabAdapter.getCount(); i++) { for (int i = 0; i < tabAdapter.getCount(); i++) {
binding.tabLayout.getTabAt(i).setText(tabAdapter.getItemTitle(i)); binding.tabLayout.getTabAt(i).setText(tabAdapter.getItemTitle(i));
} }
// Restore previously selected tab
final TabLayout.Tab ltab = binding.tabLayout.getTabAt(lastTab);
if (ltab != null) {
binding.tabLayout.selectTab(ltab);
}
} }
@Override @Override

View file

@ -79,7 +79,7 @@ public class ChannelInfoFragment extends BaseFragment {
private void setupMetadata(final LayoutInflater inflater, private void setupMetadata(final LayoutInflater inflater,
final LinearLayout layout) { final LinearLayout layout) {
Context context = getContext(); final Context context = getContext();
if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) { if (channelInfo.getSubscriberCount() != UNKNOWN_SUBSCRIBER_COUNT) {
addMetadataItem(inflater, layout, R.string.metadata_subscribers, addMetadataItem(inflater, layout, R.string.metadata_subscribers,

View file

@ -46,7 +46,7 @@ public class ChannelTabFragment extends BaseListInfoFragment<InfoItem, ChannelTa
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(false); setHasOptionsMenu(false);
} }

View file

@ -117,7 +117,7 @@ public class ChannelVideosFragment extends BaseListInfoFragment<StreamInfoItem,
//////////////////////////////////////////////////////////////////////////*/ //////////////////////////////////////////////////////////////////////////*/
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setHasOptionsMenu(false); setHasOptionsMenu(false);
} }

View file

@ -10,7 +10,10 @@ import org.schabi.newpipe.extractor.linkhandler.ChannelTabHandler.Tab;
import java.util.Set; import java.util.Set;
public class ChannelTabs { public final class ChannelTabs {
private ChannelTabs() {
}
@StringRes @StringRes
private static int getShowTabKey(final Tab tab) { private static int getShowTabKey(final Tab tab) {
switch (tab) { switch (tab) {