From 2c98d079dec6abf42bdf5fe7c9b4e033332e3f21 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Tue, 25 Oct 2022 09:01:11 +0200 Subject: [PATCH] fix: cache channel data --- .../list/channel/ChannelFragment.java | 53 ++++++++++++++----- .../schabi/newpipe/util/ExtractorHelper.java | 3 +- 2 files changed, 41 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java index 51625d202..d7955eb9d 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelFragment.java @@ -35,9 +35,11 @@ import org.schabi.newpipe.util.ChannelTabs; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.NavigationHelper; +import org.schabi.newpipe.util.StateSaver; import org.schabi.newpipe.util.external_communication.ShareUtils; import java.util.List; +import java.util.Queue; import icepick.State; import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; @@ -47,7 +49,8 @@ import io.reactivex.rxjava3.disposables.Disposable; import io.reactivex.rxjava3.functions.Consumer; import io.reactivex.rxjava3.schedulers.Schedulers; -public class ChannelFragment extends BaseStateFragment { +public class ChannelFragment extends BaseStateFragment + implements StateSaver.WriteRead { @State protected int serviceId = Constants.NO_SERVICE_ID; @State @@ -97,12 +100,6 @@ public class ChannelFragment extends BaseStateFragment { public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); - - if (savedInstanceState != null) { - lastTab = savedInstanceState.getInt("LastTab"); - } else { - lastTab = 0; - } } @Override @@ -128,12 +125,6 @@ public class ChannelFragment extends BaseStateFragment { binding.tabLayout.setupWithViewPager(binding.viewPager); } - @Override - public void onSaveInstanceState(final @NonNull Bundle outState) { - super.onSaveInstanceState(outState); - outState.putInt("LastTab", binding.tabLayout.getSelectedTabPosition()); - } - @Override public void onDestroy() { super.onDestroy(); @@ -301,7 +292,7 @@ public class ChannelFragment extends BaseStateFragment { final String description = currentInfo.getDescription(); if (description != null && !description.isEmpty() && ChannelTabs.showChannelTab( - context, preferences, R.string.show_channel_tabs_info)) { + context, preferences, R.string.show_channel_tabs_info)) { tabAdapter.addFragment( ChannelInfoFragment.getInstance(currentInfo), "Info"); } @@ -321,6 +312,40 @@ public class ChannelFragment extends BaseStateFragment { } } + /*////////////////////////////////////////////////////////////////////////// + // State Saving + //////////////////////////////////////////////////////////////////////////*/ + + @Override + public String generateSuffix() { + return null; + } + + @Override + public void writeTo(final Queue objectsToSave) { + objectsToSave.add(currentInfo); + if (binding != null) { + objectsToSave.add(binding.tabLayout.getSelectedTabPosition()); + } else { + objectsToSave.add(0); + } + } + + @Override + public void readFrom(@NonNull final Queue savedObjects) { + currentInfo = (ChannelInfo) savedObjects.poll(); + lastTab = (Integer) savedObjects.poll(); + } + + @Override + protected void doInitialLoadLogic() { + if (currentInfo == null) { + startLoading(false); + } else { + handleResult(currentInfo); + } + } + @Override public void startLoading(final boolean forceLoad) { super.startLoading(forceLoad); diff --git a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java index b4648c79b..bf99ae3d3 100644 --- a/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java +++ b/app/src/main/java/org/schabi/newpipe/util/ExtractorHelper.java @@ -158,7 +158,8 @@ public final class ExtractorHelper { final boolean forceLoad) { checkServiceId(serviceId); return checkCache(forceLoad, serviceId, - tabHandler.getUrl() + tabHandler.getTab().name(), InfoItem.InfoType.CHANNEL, + tabHandler.getUrl() + "/" + + tabHandler.getTab().name(), InfoItem.InfoType.CHANNEL, Single.fromCallable(() -> ChannelTabInfo.getInfo(NewPipe.getService(serviceId), tabHandler))); }