RecommendFragmentPresenter.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package com.edufound.reader.presenter;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.graphics.Outline;
  5. import android.text.TextUtils;
  6. import android.view.View;
  7. import android.view.ViewOutlineProvider;
  8. import android.widget.Toast;
  9. import com.edufound.reader.R;
  10. import com.edufound.reader.adapter.SlideVideoAdapter;
  11. import com.edufound.reader.base.BaseActivity;
  12. import com.edufound.reader.base.BasePresenter;
  13. import com.edufound.reader.bean.FavoritesBean;
  14. import com.edufound.reader.bean.HttpResultBean;
  15. import com.edufound.reader.bean.HttpResultDataListBean;
  16. import com.edufound.reader.bean.UserBean;
  17. import com.edufound.reader.bean.UserRecordBean;
  18. import com.edufound.reader.contract.RecommendFragmentContract;
  19. import com.edufound.reader.cusview.RvListJzvdStd;
  20. import com.edufound.reader.model.RecommendFragmentModel;
  21. import com.edufound.reader.receiver.BackPageReceiver;
  22. import com.edufound.reader.util.Consts;
  23. import com.edufound.reader.util.EfunboxUtil;
  24. import com.edufound.reader.util.MMKVEncodeKey;
  25. import com.edufound.reader.util.MMKVUtil;
  26. import com.edufound.reader.util.SizeUtils;
  27. import com.edufound.reader.videoutil.OnViewPagerListener;
  28. import com.edufound.reader.videoutil.ViewPagerLayoutManager;
  29. import com.google.gson.reflect.TypeToken;
  30. import com.okhttplib.HttpInfo;
  31. import com.okhttplib.callback.Callback;
  32. import com.orhanobut.logger.Logger;
  33. import java.io.IOException;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. import java.util.Map;
  38. import androidx.recyclerview.widget.LinearLayoutManager;
  39. import androidx.recyclerview.widget.OrientationHelper;
  40. import androidx.recyclerview.widget.RecyclerView;
  41. import cn.jzvd.JZUtils;
  42. import cn.jzvd.Jzvd;
  43. public class RecommendFragmentPresenter extends BasePresenter<RecommendFragmentContract.View> implements RecommendFragmentContract.Presenter {
  44. Context mContext;
  45. RecommendFragmentModel mModel;
  46. private ViewPagerLayoutManager mViewPagerLayoutManager;
  47. private int mCurrentPosition = 0;
  48. List<UserRecordBean> mDataList;
  49. SlideVideoAdapter mAdapter;
  50. public RecommendFragmentPresenter(Context context) {
  51. mModel = new RecommendFragmentModel();
  52. mContext = context;
  53. BackPageReceiver.registerReceiver(context);
  54. BackPageReceiver.registerBackPageReceiverListener(backListener);
  55. }
  56. @Override
  57. public void initVideoList(RecyclerView rv, int type, int startPosition) {
  58. rv.setOutlineProvider(new ViewOutlineProvider() {
  59. @Override
  60. public void getOutline(View view, Outline outline) {
  61. outline.setRoundRect(SizeUtils.dp2px(mContext, 5),
  62. 0,
  63. view.getWidth() - SizeUtils.dp2px(mContext, 5),
  64. view.getHeight(),
  65. SizeUtils.dp2px(mContext, 40));
  66. }
  67. });
  68. rv.setClipToOutline(true);
  69. if (type == 0) {
  70. //推荐的接口
  71. initVideoDate(rv, startPosition);
  72. getRecommendDataList();
  73. } else if (type == 1) {
  74. //关注的接口
  75. } else if (type == 2) {
  76. //官方的
  77. } else if (type == 3) {
  78. //关注的人的
  79. }
  80. }
  81. public void initVideoDate(RecyclerView rv, int startPosition) {
  82. rv.setLayoutManager(new LinearLayoutManager(mView.getActivity()));
  83. rv.setNestedScrollingEnabled(false);
  84. mViewPagerLayoutManager = new ViewPagerLayoutManager(mView.getActivity(), OrientationHelper.VERTICAL);
  85. rv.setLayoutManager(mViewPagerLayoutManager);
  86. mDataList = new ArrayList<>();
  87. mAdapter = new SlideVideoAdapter(mView.getActivity(), mDataList);
  88. rv.setAdapter(mAdapter);
  89. mViewPagerLayoutManager.setOnViewPagerListener(new OnViewPagerListener() {
  90. @Override
  91. public void onInitComplete() {
  92. //自动播放第一条
  93. autoPlayVideo(rv);
  94. }
  95. @Override
  96. public void onPageRelease(boolean isNext, int position) {
  97. if (mCurrentPosition == position) {
  98. Jzvd.releaseAllVideos();
  99. }
  100. }
  101. @Override
  102. public void onPageSelected(int position, boolean isBottom) {
  103. // if (mCurrentPosition == position) {
  104. // Logger.e("mCurrentPosition == position..return了");
  105. // return;
  106. // }
  107. autoPlayVideo(rv);
  108. mCurrentPosition = position;
  109. if (mCurrentPosition == 0) {
  110. Toast.makeText(mContext, "已经是第一个了", Toast.LENGTH_SHORT).show();
  111. }
  112. }
  113. });
  114. rv.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
  115. @Override
  116. public void onChildViewAttachedToWindow(View view) {
  117. scrollToNextFunction(rv, false);
  118. }
  119. @Override
  120. public void onChildViewDetachedFromWindow(View view) {
  121. Jzvd jzvd = view.findViewById(R.id.slidevideo_video);
  122. if (jzvd != null && Jzvd.CURRENT_JZVD != null && jzvd.jzDataSource != null &&
  123. jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.getCurrentUrl())) {
  124. if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
  125. Jzvd.releaseAllVideos();
  126. }
  127. }
  128. }
  129. });
  130. if (startPosition > 0) {
  131. rv.scrollToPosition(startPosition);
  132. }
  133. }
  134. @Override
  135. public Object getCurPlayVideo() {
  136. return mCurrentPosition;
  137. }
  138. @Override
  139. public void fragmentPause(RecyclerView rv) {
  140. if (rv.getChildCount() > 0) {
  141. RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
  142. player.pauseVideo();
  143. }
  144. }
  145. @Override
  146. public void fragmentResume(RecyclerView rv) {
  147. if (rv.getChildCount() > 0) {
  148. RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
  149. player.startVideoPauseEnd();
  150. }
  151. }
  152. @Override
  153. public void getRecommendDataList() {
  154. if (TextUtils.isEmpty(MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE))) {
  155. //如果是空的,就是没选择过年纪。然后给个默认的一年级
  156. //PRESCHOOL,FIRST_GRADE,SECOND_GRADE,THIRD_GRADE,FOURTH_GRADE
  157. // MMKVUtil.getInstance().encode(MMKVEncodeKey.USER_GRADE, "FIRST_GRADE");
  158. }
  159. String grade = MMKVUtil.getInstance().decodeString(MMKVEncodeKey.USER_GRADE);
  160. if (TextUtils.isEmpty(grade)) {
  161. grade = EfunboxUtil.getEnumByCNGrade("一年级");
  162. }
  163. if (Consts.getmConstsUserBean() == null) {
  164. grade = EfunboxUtil.getEnumByCNGrade("一年级");
  165. }
  166. mModel.getRecommendList(mView.getActivity(), grade, (mCurrentPosition / 100) + 1, new Callback() {
  167. @Override
  168. public void onSuccess(HttpInfo info) throws IOException {
  169. HttpResultBean<HttpResultDataListBean<UserRecordBean>> bean = info.getRetDetail(new TypeToken<HttpResultBean<HttpResultDataListBean<UserRecordBean>>>() {
  170. }.getType());
  171. if (mDataList == null) {
  172. mDataList = bean.getData().getList();
  173. } else {
  174. mDataList.addAll(bean.getData().getList());
  175. mAdapter.addMored(mDataList);
  176. }
  177. mView.getRootView().post(new Runnable() {
  178. @Override
  179. public void run() {
  180. mAdapter.notifyDataSetChanged();
  181. }
  182. });
  183. }
  184. @Override
  185. public void onFailure(HttpInfo info) throws IOException {
  186. Logger.e("获取推荐列表失败:" + info.getRetDetail());
  187. }
  188. });
  189. }
  190. @Override
  191. public void doFavorites(UserRecordBean.UserRead userRead) {
  192. Map<String, String> map = new HashMap<>();
  193. map.put("exampleId", userRead.getExampleId());
  194. map.put("targetCode", userRead.getExampleId());
  195. String json = ((BaseActivity) mView.getActivity()).getGson().toJson(map);
  196. mModel.doFavorites(mView.getActivity(), json, new Callback() {
  197. @Override
  198. public void onSuccess(HttpInfo info) throws IOException {
  199. HttpResultBean<FavoritesBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<FavoritesBean>>() {
  200. }.getType());
  201. if (bean.getData().getStatus().equals("DEL")) {
  202. mView.setFavorites(false);
  203. } else if (bean.getData().getStatus().equals("NORMAL")) {
  204. mView.setFavorites(true);
  205. }
  206. }
  207. @Override
  208. public void onFailure(HttpInfo info) throws IOException {
  209. Logger.e("收藏/取消收藏接口失败:" + info.getRetDetail());
  210. }
  211. });
  212. }
  213. @Override
  214. public void doLike(UserRecordBean.UserRead userRead) {
  215. mModel.doLick(mView.getActivity(), userRead.getId(), new Callback() {
  216. @Override
  217. public void onSuccess(HttpInfo info) throws IOException {
  218. HttpResultBean<String> bean = info.getRetDetail(new TypeToken<HttpResultBean<String>>() {
  219. }.getType());
  220. mView.setLikes(bean.getData());
  221. }
  222. @Override
  223. public void onFailure(HttpInfo info) throws IOException {
  224. Logger.e("failure---info.getRetDetail:" + info.getRetDetail());
  225. }
  226. });
  227. }
  228. @Override
  229. public void destory() {
  230. Jzvd.releaseAllVideos();
  231. Jzvd.backPress();
  232. }
  233. @Override
  234. public void getRecordInfoById(String id) {
  235. mModel.getRecordInfoById(mView.getActivity(), id, new Callback() {
  236. @Override
  237. public void onSuccess(HttpInfo info) throws IOException {
  238. HttpResultBean<UserRecordBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<UserRecordBean>>() {
  239. }.getType());
  240. mDataList.set(mCurrentPosition, bean.getData());
  241. mView.getRootView().post(new Runnable() {
  242. @Override
  243. public void run() {
  244. mAdapter.notifyDataSetChanged();
  245. }
  246. });
  247. }
  248. @Override
  249. public void onFailure(HttpInfo info) throws IOException {
  250. Logger.e("获取单独的失敗了:" + info.getRetDetail());
  251. }
  252. });
  253. }
  254. private void autoPlayVideo(RecyclerView rv) {
  255. if (rv == null || rv.getChildAt(0) == null) {
  256. return;
  257. }
  258. //获取当前显示的View 的数据
  259. int childCount = rv.getChildCount();
  260. //获取最后一具 Item 对应的View
  261. View childAt = rv.getChildAt(childCount - 1);
  262. //获取当前军舰中显示的最后一个 Item 的位置 Postion
  263. int childLayoutPosition = rv.getChildLayoutPosition(childAt);
  264. mView.setTopStatus(mDataList.get(childLayoutPosition));
  265. RvListJzvdStd player = rv.getChildAt(0).findViewById(R.id.slidevideo_video);
  266. if (player != null) {
  267. if (player.state == Jzvd.STATE_PLAYING) {
  268. //正在播放
  269. } else {
  270. player.startVideoAfterPreloading();
  271. player.setVideoCallBack(new RvListJzvdStd.VideoCallBack() {
  272. @Override
  273. public void onComplete() {
  274. scrollToNextFunction(rv, true);
  275. }
  276. @Override
  277. public void onError(int what, int extra) {
  278. }
  279. @Override
  280. public void onInfo(int what, int extra) {
  281. }
  282. });
  283. }
  284. }
  285. }
  286. public void scrollToNextFunction(RecyclerView rv, boolean needScroll) {
  287. //获取当前显示的View 的数据
  288. int childCount = rv.getChildCount();
  289. //获取最后一具 Item 对应的View
  290. View childAt = rv.getChildAt(childCount - 1);
  291. //获取当前军舰中显示的最后一个 Item 的位置 Postion
  292. int childLayoutPosition = rv.getChildLayoutPosition(childAt);
  293. //如果不是最后一个就向下滑动
  294. if (childLayoutPosition < rv.getAdapter().getItemCount() - 2) {
  295. if (needScroll) {
  296. rv.smoothScrollToPosition(childLayoutPosition + 1);
  297. }
  298. } else {
  299. // Toast.makeText(this, "已滑动到底部了", Toast.LENGTH_LONG).show();
  300. // CToast.makeText(mView.getActivity(), "滑动到最后一个啦", 3000).show();
  301. getRecommendDataList();
  302. }
  303. }
  304. public void scrollToUpFunction(RecyclerView rv) {
  305. //获取当前显示的View 的数据
  306. Logger.e("mCurrentPosition:" + mCurrentPosition);
  307. if (mCurrentPosition > 0) {
  308. rv.smoothScrollToPosition(mCurrentPosition - 1);
  309. mCurrentPosition = mCurrentPosition - 1;
  310. } else if (mCurrentPosition == 0) {
  311. Toast.makeText(mContext, "已经是第一个了", Toast.LENGTH_SHORT).show();
  312. }
  313. }
  314. BackPageReceiver.onBackPage backListener = new BackPageReceiver.onBackPage() {
  315. @Override
  316. public void pageBack(Intent intent) {
  317. if (intent.getAction().equals(BackPageReceiver.BACK_HOME_REF_VIDEO_ITEM)) {
  318. //从其他页面回来了。查询一下单独的item数据,然后赋值
  319. Logger.e("收到了回来的广播了");
  320. getRecordInfoById(intent.getStringExtra("recordId"));
  321. }
  322. }
  323. };
  324. }