NewPipe/app/src/main/java/org/schabi/newpipe/fragments/list/comments/CommentsFragment.java

141 lines
4.6 KiB
Java
Raw Normal View History

2018-09-23 03:32:19 +02:00
package org.schabi.newpipe.fragments.list.comments;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
2018-09-23 03:32:19 +02:00
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.comments.CommentsInfo;
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
import org.schabi.newpipe.report.UserAction;
2018-12-25 11:06:15 +01:00
import org.schabi.newpipe.util.AnimationUtils;
2018-09-23 03:32:19 +02:00
import org.schabi.newpipe.util.ExtractorHelper;
2020-10-31 21:55:45 +01:00
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
2018-09-23 03:32:19 +02:00
public class CommentsFragment extends BaseListInfoFragment<CommentsInfo> {
2020-11-18 23:50:00 +01:00
private final CompositeDisposable disposables = new CompositeDisposable();
2018-09-23 03:32:19 +02:00
public static CommentsFragment getInstance(final int serviceId, final String url,
final String name) {
2020-08-16 10:24:58 +02:00
final CommentsFragment instance = new CommentsFragment();
2018-09-23 03:32:19 +02:00
instance.setInitialData(serviceId, url, name);
return instance;
}
/*//////////////////////////////////////////////////////////////////////////
// LifeCycle
//////////////////////////////////////////////////////////////////////////*/
@Override
public void onAttach(final Context context) {
2018-09-23 03:32:19 +02:00
super.onAttach(context);
}
@Override
public View onCreateView(@NonNull final LayoutInflater inflater,
@Nullable final ViewGroup container,
@Nullable final Bundle savedInstanceState) {
2018-09-23 03:32:19 +02:00
return inflater.inflate(R.layout.fragment_comments, container, false);
}
@Override
public void onDestroy() {
super.onDestroy();
if (disposables != null) {
disposables.clear();
}
2018-09-23 03:32:19 +02:00
}
/*//////////////////////////////////////////////////////////////////////////
// Load and handle
//////////////////////////////////////////////////////////////////////////*/
@Override
protected Single<ListExtractor.InfoItemsPage> loadMoreItemsLogic() {
return ExtractorHelper.getMoreCommentItems(serviceId, currentInfo, currentNextPage);
2018-09-23 03:32:19 +02:00
}
@Override
protected Single<CommentsInfo> loadResult(final boolean forceLoad) {
2018-09-23 03:32:19 +02:00
return ExtractorHelper.getCommentsInfo(serviceId, url, forceLoad);
}
/*//////////////////////////////////////////////////////////////////////////
// Contract
//////////////////////////////////////////////////////////////////////////*/
@Override
public void showLoading() {
super.showLoading();
}
@Override
public void handleResult(@NonNull final CommentsInfo result) {
2018-09-23 03:32:19 +02:00
super.handleResult(result);
AnimationUtils.slideUp(requireView(), 120, 150, 0.06f);
2018-12-25 11:06:15 +01:00
2018-09-23 03:32:19 +02:00
if (!result.getErrors().isEmpty()) {
showSnackBarError(result.getErrors(), UserAction.REQUESTED_COMMENTS,
NewPipe.getNameOfService(result.getServiceId()), result.getUrl(), 0);
2018-09-23 03:32:19 +02:00
}
if (disposables != null) {
disposables.clear();
}
2018-09-23 03:32:19 +02:00
}
@Override
public void handleNextItems(final ListExtractor.InfoItemsPage result) {
2018-09-23 03:32:19 +02:00
super.handleNextItems(result);
if (!result.getErrors().isEmpty()) {
showSnackBarError(result.getErrors(), UserAction.REQUESTED_COMMENTS,
NewPipe.getNameOfService(serviceId), "Get next page of: " + url,
2018-09-23 03:32:19 +02:00
R.string.general_error);
}
}
/*//////////////////////////////////////////////////////////////////////////
// OnError
//////////////////////////////////////////////////////////////////////////*/
@Override
protected boolean onError(final Throwable exception) {
if (super.onError(exception)) {
return true;
}
2018-09-23 03:32:19 +02:00
2018-12-25 11:06:15 +01:00
hideLoading();
showSnackBarError(exception, UserAction.REQUESTED_COMMENTS,
NewPipe.getNameOfService(serviceId), url, R.string.error_unable_to_load_comments);
2018-09-23 03:32:19 +02:00
return true;
}
/*//////////////////////////////////////////////////////////////////////////
// Utils
//////////////////////////////////////////////////////////////////////////*/
@Override
public void setTitle(final String title) { }
@Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) { }
2018-12-08 22:51:55 +01:00
@Override
protected boolean isGridLayout() {
return false;
}
2018-09-23 03:32:19 +02:00
}