123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- package com.edufound.reader.presenter;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Outline;
- import android.text.TextUtils;
- import android.view.View;
- import android.view.ViewOutlineProvider;
- import android.widget.Toast;
- import com.edufound.reader.R;
- import com.edufound.reader.adapter.SlideVideoAdapter;
- import com.edufound.reader.base.BaseActivity;
- import com.edufound.reader.base.BasePresenter;
- import com.edufound.reader.bean.FavoritesBean;
- import com.edufound.reader.bean.HttpResultBean;
- import com.edufound.reader.bean.HttpResultDataListBean;
- import com.edufound.reader.bean.UserBean;
- import com.edufound.reader.bean.UserRecordBean;
- import com.edufound.reader.contract.RecommendFragmentContract;
- import com.edufound.reader.cusview.RvListJzvdStd;
- import com.edufound.reader.model.RecommendFragmentModel;
- import com.edufound.reader.receiver.BackPageReceiver;
- import com.edufound.reader.util.Consts;
- import com.edufound.reader.util.EfunboxUtil;
- import com.edufound.reader.util.MMKVEncodeKey;
- import com.edufound.reader.util.MMKVUtil;
- import com.edufound.reader.util.SizeUtils;
- import com.edufound.reader.videoutil.OnViewPagerListener;
- import com.edufound.reader.videoutil.ViewPagerLayoutManager;
- import com.google.gson.reflect.TypeToken;
- import com.okhttplib.HttpInfo;
- import com.okhttplib.callback.Callback;
- import com.orhanobut.logger.Logger;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import androidx.recyclerview.widget.LinearLayoutManager;
- import androidx.recyclerview.widget.OrientationHelper;
- import androidx.recyclerview.widget.RecyclerView;
- import cn.jzvd.JZUtils;
- import cn.jzvd.Jzvd;
- public class RecommendFragmentPresenter extends BasePresenter<RecommendFragmentContract.View> implements RecommendFragmentContract.Presenter {
- Context mContext;
- RecommendFragmentModel mModel;
- private ViewPagerLayoutManager mViewPagerLayoutManager;
- private int mCurrentPosition = 0;
- List<UserRecordBean> mDataList;
- SlideVideoAdapter mAdapter;
- public RecommendFragmentPresenter(Context context) {
- mModel = new RecommendFragmentModel();
- mContext = context;
- BackPageReceiver.registerReceiver(context);
- BackPageReceiver.registerBackPageReceiverListener(backListener);
- }
- @Override
- public void initVideoList(RecyclerView rv, int type, int startPosition) {
- rv.setOutlineProvider(new ViewOutlineProvider() {
- @Override
- public void getOutline(View view, Outline outline) {
- outline.setRoundRect(SizeUtils.dp2px(mContext, 5),
- 0,
- view.getWidth() - SizeUtils.dp2px(mContext, 5),
- view.getHeight(),
- SizeUtils.dp2px(mContext, 40));
- }
- });
- rv.setClipToOutline(true);
- if (type == 0) {
- //推荐的接口
- initVideoDate(rv, startPosition);
- getRecommendDataList();
- } else if (type == 1) {
- //关注的接口
- } else if (type == 2) {
- //官方的
- } else if (type == 3) {
- //关注的人的
- }
- }
- public void initVideoDate(RecyclerView rv, int startPosition) {
- rv.setLayoutManager(new LinearLayoutManager(mView.getActivity()));
- rv.setNestedScrollingEnabled(false);
- mViewPagerLayoutManager = new ViewPagerLayoutManager(mView.getActivity(), OrientationHelper.VERTICAL);
- rv.setLayoutManager(mViewPagerLayoutManager);
- mDataList = new ArrayList<>();
- mAdapter = new SlideVideoAdapter(mView.getActivity(), mDataList);
- rv.setAdapter(mAdapter);
- mViewPagerLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
- @Override
- public void onInitComplete() {
- //自动播放第一条
- autoPlayVideo(rv);
- }
- @Override
- public void onPageRelease(boolean isNext, int position) {
- if (mCurrentPosition == position) {
- Jzvd.releaseAllVideos();
- }
- }
- @Override
- public void onPageSelected(int position, boolean isBottom) {
- // if (mCurrentPosition == position) {
- // Logger.e("mCurrentPosition == position..return了");
- // return;
- // }
- autoPlayVideo(rv);
- mCurrentPosition = position;
- if (mCurrentPosition == 0) {
- Toast.makeText(mContext, "已经是第一个了", Toast.LENGTH_SHORT).show();
- }
- }
- });
- rv.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
- @Override
- public void onChildViewAttachedToWindow(View view) {
- scrollToNextFunction(rv, false);
- }
- @Override
- public void onChildViewDetachedFromWindow(View view) {
- Jzvd jzvd = view.findViewById(R.id.slidevideo_video);
- if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource != null &&
- jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
- if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
- Jzvd.releaseAllVideos();
- }
- }
- }
- });
- if (startPosition > 0) {
- rv.scrollToPosition(startPosition);
- }
- }
- @Override
- public Object getCurPlayVideo() {
- return mCurrentPosition;
- }
- @Override
- public void fragmentPause(RecyclerView rv) {
- if (rv.getChildCount() > 0) {
- RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
- player.pauseVideo();
- }
- }
- @Override
- public void fragmentResume(RecyclerView rv) {
- if (rv.getChildCount() > 0) {
- RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
- player.startVideoPauseEnd();
- }
- }
- @Override
- public void getRecommendDataList() {
- if (TextUtils.isEmpty(MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE))) {
- //如果是空的,就是没选择过年纪。然后给个默认的一年级
- //PRESCHOOL,FIRST_GRADE,SECOND_GRADE,THIRD_GRADE,FOURTH_GRADE
- // MMKVUtil.getInstance().encode(MMKVEncodeKey.USER_GRADE, "FIRST_GRADE");
- }
- String grade = MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE);
- if (TextUtils.isEmpty(grade)) {
- grade = EfunboxUtil.getEnumByCNGrade("一年级");
- }
- if (Consts.getmConstsUserBean() == null) {
- grade = EfunboxUtil.getEnumByCNGrade("一年级");
- }
- mModel.getRecommendList(mView.getActivity(), grade, (mCurrentPosition / 100) + 1, new Callback() {
- @Override
- public void onSuccess(HttpInfo info) throws IOException {
- HttpResultBean<HttpResultDataListBean<UserRecordBean>> bean = info.getRetDetail(new TypeToken<HttpResultBean<HttpResultDataListBean<UserRecordBean>>>() {
- }.getType());
- if (mDataList == null) {
- mDataList = bean.getData().getList();
- } else {
- mDataList.addAll(bean.getData().getList());
- mAdapter.addMored(mDataList);
- }
- mView.getRootView().post(new Runnable() {
- @Override
- public void run() {
- mAdapter.notifyDataSetChanged();
- }
- });
- }
- @Override
- public void onFailure(HttpInfo info) throws IOException {
- Logger.e("获取推荐列表失败:" + info.getRetDetail());
- }
- });
- }
- @Override
- public void doFavorites(UserRecordBean.UserRead userRead) {
- Map<String, String> map = new HashMap<>();
- map.put("exampleId", userRead.getExampleId());
- map.put("targetCode", userRead.getExampleId());
- String json = ((BaseActivity) mView.getActivity()).getGson().toJson(map);
- mModel.doFavorites(mView.getActivity(), json, new Callback() {
- @Override
- public void onSuccess(HttpInfo info) throws IOException {
- HttpResultBean<FavoritesBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<FavoritesBean>>() {
- }.getType());
- if (bean.getData().getStatus().equals("DEL")) {
- mView.setFavorites(false);
- } else if (bean.getData().getStatus().equals("NORMAL")) {
- mView.setFavorites(true);
- }
- }
- @Override
- public void onFailure(HttpInfo info) throws IOException {
- Logger.e("收藏/取消收藏接口失败:" + info.getRetDetail());
- }
- });
- }
- @Override
- public void doLike(UserRecordBean.UserRead userRead) {
- mModel.doLick(mView.getActivity(), userRead.getId(), new Callback() {
- @Override
- public void onSuccess(HttpInfo info) throws IOException {
- HttpResultBean<String> bean = info.getRetDetail(new TypeToken<HttpResultBean<String>>() {
- }.getType());
- mView.setLikes(bean.getData());
- }
- @Override
- public void onFailure(HttpInfo info) throws IOException {
- Logger.e("failure---info.getRetDetail:" + info.getRetDetail());
- }
- });
- }
- @Override
- public void destory() {
- Jzvd.releaseAllVideos();
- Jzvd.backPress();
- }
- @Override
- public void getRecordInfoById(String id) {
- mModel.getRecordInfoById(mView.getActivity(), id, new Callback() {
- @Override
- public void onSuccess(HttpInfo info) throws IOException {
- HttpResultBean<UserRecordBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<UserRecordBean>>() {
- }.getType());
- mDataList.set(mCurrentPosition, bean.getData());
- mView.getRootView().post(new Runnable() {
- @Override
- public void run() {
- mAdapter.notifyDataSetChanged();
- }
- });
- }
- @Override
- public void onFailure(HttpInfo info) throws IOException {
- Logger.e("获取单独的失敗了:" + info.getRetDetail());
- }
- });
- }
- private void autoPlayVideo(RecyclerView rv) {
- if (rv == null || rv.getChildAt(0) == null) {
- return;
- }
- //获取当前显示的View 的数据
- int childCount = rv.getChildCount();
- //获取最后一具 Item 对应的View
- View childAt = rv.getChildAt(childCount - 1);
- //获取当前军舰中显示的最后一个 Item 的位置 Postion
- int childLayoutPosition = rv.getChildLayoutPosition(childAt);
- mView.setTopStatus(mDataList.get(childLayoutPosition));
- RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
- if (player != null) {
- if (player.state == Jzvd.STATE_PLAYING) {
- //正在播放
- } else {
- player.startVideoAfterPreloading();
- player.setVideoCallBack(new RvListJzvdStd.VideoCallBack() {
- @Override
- public void onComplete() {
- scrollToNextFunction(rv, true);
- }
- @Override
- public void onError(int what, int extra) {
- }
- @Override
- public void onInfo(int what, int extra) {
- }
- });
- }
- }
- }
- public void scrollToNextFunction(RecyclerView rv, boolean needScroll) {
- //获取当前显示的View 的数据
- int childCount = rv.getChildCount();
- //获取最后一具 Item 对应的View
- View childAt = rv.getChildAt(childCount - 1);
- //获取当前军舰中显示的最后一个 Item 的位置 Postion
- int childLayoutPosition = rv.getChildLayoutPosition(childAt);
- //如果不是最后一个就向下滑动
- if (childLayoutPosition < rv.getAdapter().getItemCount() - 2) {
- if (needScroll) {
- rv.smoothScrollToPosition(childLayoutPosition + 1);
- }
- } else {
- // Toast.makeText(this, "已滑动到底部了", Toast.LENGTH_LONG).show();
- // CToast.makeText(mView.getActivity(), "滑动到最后一个啦", 3000).show();
- getRecommendDataList();
- }
- }
- public void scrollToUpFunction(RecyclerView rv) {
- //获取当前显示的View 的数据
- Logger.e("mCurrentPosition:" + mCurrentPosition);
- if (mCurrentPosition > 0) {
- rv.smoothScrollToPosition(mCurrentPosition - 1);
- mCurrentPosition = mCurrentPosition - 1;
- } else if (mCurrentPosition == 0) {
- Toast.makeText(mContext, "已经是第一个了", Toast.LENGTH_SHORT).show();
- }
- }
- BackPageReceiver.onBackPage backListener = new BackPageReceiver.onBackPage() {
- @Override
- public void pageBack(Intent intent) {
- if (intent.getAction().equals(BackPageReceiver.BACK_HOME_REF_VIDEO_ITEM)) {
- //从其他页面回来了。查询一下单独的item数据,然后赋值
- Logger.e("收到了回来的广播了");
- getRecordInfoById(intent.getStringExtra("recordId"));
- }
- }
- };
- }
|