fix: cache channel data

This commit is contained in:
ThetaDev 2022-10-25 09:01:11 +02:00 committed by Stypox
parent 16cd47fa2e
commit 2c98d079de
No known key found for this signature in database
GPG Key ID: 4BDF1B40A49FDD23
2 changed files with 41 additions and 15 deletions

View File

@ -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<ChannelInfo> {
public class ChannelFragment extends BaseStateFragment<ChannelInfo>
implements StateSaver.WriteRead {
@State
protected int serviceId = Constants.NO_SERVICE_ID;
@State
@ -97,12 +100,6 @@ public class ChannelFragment extends BaseStateFragment<ChannelInfo> {
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<ChannelInfo> {
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<ChannelInfo> {
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<ChannelInfo> {
}
}
/*//////////////////////////////////////////////////////////////////////////
// State Saving
//////////////////////////////////////////////////////////////////////////*/
@Override
public String generateSuffix() {
return null;
}
@Override
public void writeTo(final Queue<Object> objectsToSave) {
objectsToSave.add(currentInfo);
if (binding != null) {
objectsToSave.add(binding.tabLayout.getSelectedTabPosition());
} else {
objectsToSave.add(0);
}
}
@Override
public void readFrom(@NonNull final Queue<Object> 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);

View File

@ -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)));
}