OthersRecordPresenter.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package com.edufound.reader.presenter;
  2. import android.content.Intent;
  3. import android.os.Build;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.LayoutInflater;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.widget.FrameLayout;
  11. import android.widget.GridLayout;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16. import com.edufound.reader.R;
  17. import com.edufound.reader.activity.OthersRecordActivity;
  18. import com.edufound.reader.activity.SmallFullVideoActivity;
  19. import com.edufound.reader.apiserver.UserApiServerImpl;
  20. import com.edufound.reader.base.BaseActivity;
  21. import com.edufound.reader.base.BasePresenter;
  22. import com.edufound.reader.bean.FansBean;
  23. import com.edufound.reader.bean.HttpResultBean;
  24. import com.edufound.reader.bean.HttpResultDataListBean;
  25. import com.edufound.reader.bean.MyInfoBean;
  26. import com.edufound.reader.bean.UserRecordBean;
  27. import com.edufound.reader.contract.OthersRecordContract;
  28. import com.edufound.reader.model.CharacterFragmentModel;
  29. import com.edufound.reader.model.MyFollowModel;
  30. import com.edufound.reader.model.OthersRecordModel;
  31. import com.edufound.reader.receiver.BackPageReceiver;
  32. import com.edufound.reader.util.EfunboxUtil;
  33. import com.edufound.reader.util.GlideUtils;
  34. import com.edufound.reader.util.MMKVUtil;
  35. import com.edufound.reader.util.SizeUtils;
  36. import com.google.gson.reflect.TypeToken;
  37. import com.okhttplib.HttpInfo;
  38. import com.okhttplib.callback.Callback;
  39. import com.orhanobut.logger.Logger;
  40. import java.io.IOException;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. import androidx.annotation.RequiresApi;
  45. public class OthersRecordPresenter extends BasePresenter<OthersRecordContract.View> implements OthersRecordContract.Presenter {
  46. OthersRecordModel mModel;
  47. UserApiServerImpl mUserApi;
  48. List<UserRecordBean> mDataList;
  49. int onceLoadCount = 10;
  50. int mListStart = 0;
  51. int mListEnd = onceLoadCount;
  52. String backRecordId = null;
  53. public OthersRecordPresenter() {
  54. mModel = new OthersRecordModel();
  55. mUserApi = new UserApiServerImpl();
  56. }
  57. @Override
  58. public void initGridLayout(GridLayout gridLayout, FrameLayout norecordlayout) {
  59. gridLayout.removeAllViews();
  60. gridLayout.setRowCount(2);
  61. onceLoadCount = 10;
  62. mListStart = 0;
  63. mListEnd = onceLoadCount;
  64. setGridData(gridLayout, norecordlayout);
  65. }
  66. private void setGridItemView(GridLayout gridLayout, int index) {
  67. if (mView == null) {
  68. return;
  69. }
  70. View view = LayoutInflater.from(mView.getActivity()).inflate(R.layout.adapter_item_record_user, null);
  71. UserRecordBean.UserRead reader = mDataList.get(index).getUserRead();
  72. ImageView imgIcon = view.findViewById(R.id.adapter_item_record_user_icon);
  73. TextView name = view.findViewById(R.id.adapter_item_record_user_name);
  74. ImageView imgDel = view.findViewById(R.id.adapter_item_record_user_del);
  75. ImageView imgPriv = view.findViewById(R.id.adapter_item_record_user_priv);
  76. TextView lookAmount = view.findViewById(R.id.adapter_item_record_user_look_num);
  77. TextView likeAmount = view.findViewById(R.id.adapter_item_record_user_thumb_num);
  78. TextView commentAmount = view.findViewById(R.id.adapter_item_record_user_comment_num);
  79. TextView favoritesAmount = view.findViewById(R.id.adapter_item_record_user_start_num);
  80. FrameLayout checkLayout = view.findViewById(R.id.adapter_item_record_user_examine_layout);
  81. // GlideUtils.loadRoundCircleImage(mView.getActivity(), reader.getCoverImg(), imgIcon, SizeUtils.dp2px(mView.getActivity(), 20));
  82. GlideUtils.loadImage(mView.getActivity(), reader.getCoverImg(), imgIcon);
  83. name.setText(reader.getTitle());
  84. lookAmount.setText(EfunboxUtil.checkNum10000(reader.getPlayAmount()));
  85. likeAmount.setText(EfunboxUtil.checkNum10000(reader.getLikeAmount()));
  86. commentAmount.setText(EfunboxUtil.checkNum10000(reader.getCommentAmount()));
  87. favoritesAmount.setText(EfunboxUtil.checkNum10000(reader.getFavoritesAmount()));
  88. imgDel.setVisibility(View.GONE);
  89. imgPriv.setVisibility(View.GONE);
  90. checkLayout.setVisibility(View.GONE);
  91. mView.addUiClick(view, o -> {
  92. Bundle bundle = new Bundle();
  93. bundle.putString("index", index + "");
  94. bundle.putString("dataJson", "getmmkv");
  95. bundle.putString("intoPage", "OthersRecord");
  96. //数据太多了。直接传不过去,通过MMKV写一下再读一下
  97. try {
  98. System.gc();
  99. MMKVUtil.getInstance().removeKey("video_data");
  100. String json = ((BaseActivity) mView.getActivity()).getGson().toJson(mDataList);
  101. MMKVUtil.getInstance().encode("video_data", json);
  102. // bundle.putString("dataJson", ((BaseActivity) mView.getActivity()).getGson().toJson(mDataList));
  103. ((BaseActivity) mView.getActivity()).toNextActivity(SmallFullVideoActivity.class, bundle);
  104. } catch (Exception e) {
  105. Logger.e("报错数据:" + mDataList);
  106. e.printStackTrace();
  107. }
  108. });
  109. checkLayout.setOnTouchListener(new View.OnTouchListener() {
  110. @Override
  111. public boolean onTouch(View view, MotionEvent motionEvent) {
  112. return true;
  113. }
  114. });
  115. GridLayout.LayoutParams params = new GridLayout.LayoutParams(new FrameLayout.LayoutParams(SizeUtils.dp2px(mView.getActivity(), 415), SizeUtils.dp2px(mView.getActivity(), 290)));
  116. params.setMargins(SizeUtils.dp2px(mView.getActivity(), 10), 0, SizeUtils.dp2px(mView.getActivity(), 20), SizeUtils.dp2px(mView.getActivity(), 30));
  117. view.setLayoutParams(params);
  118. gridLayout.setOrientation(GridLayout.VERTICAL);
  119. gridLayout.addView(view);
  120. }
  121. public void setGridData(GridLayout gridLayout, FrameLayout norecordlayout) {
  122. if (mDataList == null || mDataList.size() <= 0) {
  123. norecordlayout.setVisibility(View.VISIBLE);
  124. gridLayout.setVisibility(View.GONE);
  125. return;
  126. }
  127. if (mListEnd > mDataList.size()) {
  128. mListEnd = mDataList.size();
  129. }
  130. int timeCount = 0;
  131. for (int j = mListStart; j < mListEnd; j++) {
  132. timeCount++;
  133. Message message = new Message();
  134. message.obj = gridLayout;
  135. message.what = OthersRecordModel.LOAD_GRID;
  136. message.arg1 = j;
  137. otherRecordHandler.sendMessageDelayed(message, 50 * timeCount);
  138. }
  139. mListStart = mListEnd;
  140. if (mListEnd + onceLoadCount > mDataList.size()) {
  141. mListEnd = mDataList.size();
  142. } else {
  143. mListEnd = mListEnd + onceLoadCount;
  144. }
  145. }
  146. @Override
  147. public void getOtherRecordList(String uid) {
  148. mModel.getOtherRecordList(mView.getActivity(), uid, new Callback() {
  149. @Override
  150. public void onSuccess(HttpInfo info) throws IOException {
  151. HttpResultBean<HttpResultDataListBean<UserRecordBean>> bean = info.getRetDetail(new TypeToken<HttpResultBean<HttpResultDataListBean<UserRecordBean>>>() {
  152. }.getType());
  153. mDataList = bean.getData().getList();
  154. mView.getOtherRecordSuccess();
  155. // mView.loadMoreItem();
  156. }
  157. @Override
  158. public void onFailure(HttpInfo info) throws IOException {
  159. }
  160. });
  161. }
  162. @Override
  163. public void doFollow(String uid) {
  164. Map<String, String> map = new HashMap<>();
  165. map.put("uid", uid);
  166. String json = ((BaseActivity) mView.getActivity()).getGson().toJson(map);
  167. mModel.doFollow(mView.getActivity(), json, new Callback() {
  168. @RequiresApi(api = Build.VERSION_CODES.N)
  169. @Override
  170. public void onSuccess(HttpInfo info) throws IOException {
  171. HttpResultBean<FansBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<FansBean>>() {
  172. }.getType());
  173. if (bean.getData().getStatus().equals("NORMAL")) {
  174. //已关注状态
  175. mView.doFollowSuccess(true);
  176. setFans(true);
  177. } else if (bean.getData().getStatus().equals("DEL")) {
  178. //未关注状态
  179. mView.doFollowSuccess(false);
  180. setFans(false);
  181. }
  182. getOtherUserInfo(uid);
  183. }
  184. @Override
  185. public void onFailure(HttpInfo info) throws IOException {
  186. }
  187. });
  188. }
  189. public void setFans(boolean fans) {
  190. for (int i = 0; i < mDataList.size(); i++) {
  191. mDataList.get(i).setFans(fans);
  192. }
  193. }
  194. @Override
  195. public void sendBackReceiver(String action) {
  196. Intent intent = new Intent(action);
  197. intent.putExtra("recordId", backRecordId);
  198. mView.getActivity().sendBroadcast(intent);
  199. }
  200. @Override
  201. public void setBackRecordId(String id) {
  202. backRecordId = id;
  203. }
  204. @Override
  205. public void onDestory(GridLayout layout) {
  206. otherRecordHandler.removeMessages(CharacterFragmentModel.LOAD_GRID);
  207. otherRecordHandler = null;
  208. layout.removeAllViews();
  209. layout = null;
  210. mView = null;
  211. }
  212. @Override
  213. public void getOtherUserInfo(String uid) {
  214. mUserApi.getOtherUserInfo(mView.getActivity(), uid, new Callback() {
  215. @Override
  216. public void onSuccess(HttpInfo info) throws IOException {
  217. HttpResultBean<MyInfoBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<MyInfoBean>>() {
  218. }.getType());
  219. mView.setLeftUserInfo(bean.getData());
  220. }
  221. @Override
  222. public void onFailure(HttpInfo info) throws IOException {
  223. }
  224. });
  225. }
  226. Handler otherRecordHandler = new Handler(new Handler.Callback() {
  227. @Override
  228. public boolean handleMessage(Message msg) {
  229. switch (msg.what) {
  230. case OthersRecordModel.LOAD_GRID:
  231. setGridItemView((GridLayout) msg.obj, msg.arg1);
  232. break;
  233. default:
  234. throw new IllegalStateException("Unexpected value: " + msg.what);
  235. }
  236. return false;
  237. }
  238. });
  239. }