Save and restore state in CommentRepliesFragment

This commit is contained in:
Stypox 2023-04-11 23:07:58 +02:00
parent f9494a294f
commit 78cf9aaa7d
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23

View file

@ -16,19 +16,30 @@ import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
import org.schabi.newpipe.fragments.list.BaseListInfoFragment; import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
import org.schabi.newpipe.util.ExtractorHelper; import org.schabi.newpipe.util.ExtractorHelper;
import java.util.Queue;
import io.reactivex.rxjava3.core.Single; import io.reactivex.rxjava3.core.Single;
public final class CommentRepliesFragment public final class CommentRepliesFragment
extends BaseListInfoFragment<CommentsInfoItem, CommentRepliesInfo> { extends BaseListInfoFragment<CommentsInfoItem, CommentRepliesInfo> {
// has the same content as super.currentInfo, except that it's never null // has the same content as super.currentInfo, except that it's never null
private final CommentRepliesInfo currentInfo; private CommentRepliesInfo currentInfo;
// the original comments info loaded alongside stream // the original comments info loaded alongside stream
private final CommentsInfo commentsInfo; private CommentsInfo commentsInfo;
/*//////////////////////////////////////////////////////////////////////////
// Constructors and lifecycle
//////////////////////////////////////////////////////////////////////////*/
public CommentRepliesFragment() {
super(UserAction.REQUESTED_COMMENT_REPLIES);
}
public CommentRepliesFragment(final CommentsInfo commentsInfo, public CommentRepliesFragment(final CommentsInfo commentsInfo,
final CommentsInfoItem commentsInfoItem) { final CommentsInfoItem commentsInfoItem) {
super(UserAction.REQUESTED_COMMENT_REPLIES); this();
this.currentInfo = CommentRepliesInfo.getInfo(commentsInfoItem); this.currentInfo = CommentRepliesInfo.getInfo(commentsInfoItem);
this.commentsInfo = commentsInfo; this.commentsInfo = commentsInfo;
setInitialData(commentsInfo.getServiceId(), commentsInfo.getUrl(), commentsInfo.getName()); setInitialData(commentsInfo.getServiceId(), commentsInfo.getUrl(), commentsInfo.getName());
@ -42,6 +53,30 @@ public final class CommentRepliesFragment
return inflater.inflate(R.layout.fragment_comments, container, false); return inflater.inflate(R.layout.fragment_comments, container, false);
} }
/*//////////////////////////////////////////////////////////////////////////
// State Saving
//////////////////////////////////////////////////////////////////////////*/
@Override
public void writeTo(final Queue<Object> objectsToSave) {
super.writeTo(objectsToSave);
objectsToSave.add(currentInfo);
objectsToSave.add(commentsInfo);
}
@Override
public void readFrom(@NonNull final Queue<Object> savedObjects) throws Exception {
super.readFrom(savedObjects);
currentInfo = (CommentRepliesInfo) savedObjects.poll();
commentsInfo = (CommentsInfo) savedObjects.poll();
}
/*//////////////////////////////////////////////////////////////////////////
// Data loading
//////////////////////////////////////////////////////////////////////////*/
@Override @Override
protected Single<CommentRepliesInfo> loadResult(final boolean forceLoad) { protected Single<CommentRepliesInfo> loadResult(final boolean forceLoad) {
return Single.just(this.currentInfo); return Single.just(this.currentInfo);