PopWindowPresneter.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. package com.edufound.reader.presenter;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.FrameLayout;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.TextView;
  13. import com.edufound.reader.R;
  14. import com.edufound.reader.activity.MainActivity;
  15. import com.edufound.reader.adapter.PopupMyOrderAdapter;
  16. import com.edufound.reader.apiserver.UserApiServerImpl;
  17. import com.edufound.reader.application.EApplication;
  18. import com.edufound.reader.bean.HttpResultBean;
  19. import com.edufound.reader.bean.MyInfoBean;
  20. import com.edufound.reader.bean.RecordResultBean;
  21. import com.edufound.reader.bean.UserEventEnum;
  22. import com.edufound.reader.bean.UserRecordBean;
  23. import com.edufound.reader.listener.PopupRecordStatusListener;
  24. import com.edufound.reader.model.PopWindowModelImpl;
  25. import com.edufound.reader.popwindow.PopWindowUtil;
  26. import com.edufound.reader.util.Consts;
  27. import com.edufound.reader.util.QRCodeUtil;
  28. import com.edufound.reader.util.SizeUtils;
  29. import com.google.gson.Gson;
  30. import com.google.gson.reflect.TypeToken;
  31. import com.okhttplib.HttpInfo;
  32. import com.okhttplib.callback.Callback;
  33. import com.okhttplib.callback.ProgressCallback;
  34. import com.orhanobut.logger.Logger;
  35. import java.io.IOException;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import androidx.annotation.NonNull;
  41. import androidx.recyclerview.widget.LinearLayoutManager;
  42. import androidx.recyclerview.widget.RecyclerView;
  43. public class PopWindowPresneter {
  44. PopWindowModelImpl mModel;
  45. ProgressCallback mUploadProgressBack;
  46. Gson mGson;
  47. UserApiServerImpl userApi;
  48. Activity mActivity;
  49. public PopWindowPresneter() {
  50. userApi = new UserApiServerImpl();
  51. mModel = new PopWindowModelImpl();
  52. mGson = new Gson();
  53. }
  54. public void initRecordStatusWindow(Activity activity, View dialog_view, RecordResultBean bean, UserRecordBean.UserRead userread, PopupRecordStatusListener listener) {
  55. mActivity = activity;
  56. FrameLayout mOverFrame = dialog_view.findViewById(R.id.popupwindow_record_status_over_frame);
  57. FrameLayout mUploadingFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_frame);
  58. FrameLayout mUploadingSuccessFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_success_frame);
  59. FrameLayout mUploadingErrorFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_error_frame);
  60. LinearLayout startLayout = mOverFrame.findViewById(R.id.popupwindow_record_status_over_start_layout);
  61. FrameLayout mRePlay = mOverFrame.findViewById(R.id.popupwindow_record_status_over_replay);
  62. FrameLayout mUpload = mOverFrame.findViewById(R.id.popupwindow_record_status_over_upload);
  63. // float a = bean.getOverall() / 100f;//百分制
  64. int integrity = bean.getIntegrity();
  65. int accuracy = bean.getAccuracy();
  66. int speedd = bean.getSpeed();
  67. int tone = bean.getTone();
  68. int score = (int) ((integrity * 0.5) + (bean.getOverall() * 0.3) + (speedd * 0.1) + (tone * 0.1));
  69. int startCount = 0;
  70. if (score > 0 && score <= 20) {
  71. //1个星星
  72. startCount = 1;
  73. } else if (score > 20 && score <= 40) {
  74. //2个星星
  75. startCount = 2;
  76. } else if (score > 40 && score <= 60) {
  77. //3个星星
  78. startCount = 3;
  79. } else if (score > 60 && score <= 80) {
  80. //4个星星
  81. startCount = 4;
  82. } else if (score > 80 && score <= 100) {
  83. //5个星星
  84. startCount = 5;
  85. }
  86. //先显示评测报告,处理测评报告逻辑
  87. {
  88. mOverFrame.setVisibility(View.VISIBLE);
  89. startLayout.removeAllViews();
  90. for (int i = 0; i < 5; i++) {
  91. ImageView start = new ImageView(activity);
  92. if (i < startCount) {
  93. start.setImageResource(R.drawable.popup_record_status_over_stars_true);
  94. } else {
  95. start.setImageResource(R.drawable.popup_record_status_over_stars_false);
  96. }
  97. start.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1));
  98. startLayout.addView(start);
  99. }
  100. TextView completionText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_completion_text);
  101. TextView correctrateText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_correctrate_text);
  102. TextView speedText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_speed_text);
  103. TextView intonationText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_intonation_text);
  104. completionText.post(new Runnable() {
  105. @Override
  106. public void run() {
  107. for (int completionPoint = 0; completionPoint < integrity; completionPoint++) {
  108. Message msg = new Message();
  109. msg.obj = completionText;
  110. msg.arg1 = completionPoint + 1;
  111. msg.what = 0x0131;
  112. handler.sendMessageDelayed(msg, 50 * completionPoint);
  113. }
  114. }
  115. });
  116. correctrateText.post(new Runnable() {
  117. @Override
  118. public void run() {
  119. for (int correctrate = 0; correctrate < accuracy; correctrate++) {
  120. Message msg = new Message();
  121. msg.obj = correctrateText;
  122. msg.arg1 = correctrate + 1;
  123. msg.what = 0x0131;
  124. handler.sendMessageDelayed(msg, 50 * correctrate);
  125. }
  126. }
  127. });
  128. speedText.post(new Runnable() {
  129. @Override
  130. public void run() {
  131. for (int speed = 0; speed < speedd; speed++) {
  132. Message msg = new Message();
  133. msg.obj = speedText;
  134. msg.arg1 = speed + 1;
  135. msg.what = 0x0131;
  136. handler.sendMessageDelayed(msg, 50 * speed);
  137. }
  138. }
  139. });
  140. intonationText.post(new Runnable() {
  141. @Override
  142. public void run() {
  143. for (int intonation = 0; intonation < tone; intonation++) {
  144. Message msg = new Message();
  145. msg.obj = intonationText;
  146. msg.arg1 = intonation + 1;
  147. msg.what = 0x0131;
  148. handler.sendMessageDelayed(msg, 50 * intonation);
  149. }
  150. }
  151. });
  152. mUploadProgressBack = new ProgressCallback() {
  153. @Override
  154. public void onResponseMain(String filePath, HttpInfo info) {
  155. super.onResponseMain(filePath, info);
  156. Logger.e("onResponseMain:" + info.getRetDetail());
  157. Logger.e("onResponseMain--netCode:" + info.getNetCode());
  158. if (info.getNetCode() == 200) {
  159. //上传完成了
  160. //上传成功逻辑
  161. {
  162. HttpResultBean<String> bean = info.getRetDetail(new TypeToken<HttpResultBean<String>>() {
  163. }.getType());
  164. Logger.e("filePath:" + bean.getData());
  165. //请求发布接口
  166. Map<String, String> postUserRead = new HashMap<>();
  167. postUserRead.put("audioPath", bean.getData());
  168. postUserRead.put("coverImg", userread.getCoverImg());
  169. postUserRead.put("exampleId", userread.getExampleId());
  170. postUserRead.put("originVideo", userread.getOriginVideo());
  171. postUserRead.put("shareImg", userread.getShareImg());
  172. postUserRead.put("title", userread.getTitle());
  173. postUserRead.put("type", "READ");
  174. postUserRead.put("uid", Consts.getUID());
  175. mModel.postUserRead(activity, mGson.toJson(postUserRead), new Callback() {
  176. @Override
  177. public void onSuccess(HttpInfo info) throws IOException {
  178. //发布朗读成功了,成功之后求情评测数据接口
  179. Logger.e("发布朗读成功了:" + info.getRetDetail());
  180. HttpResultBean<UserRecordBean.UserRead> read = info.getRetDetail(new TypeToken<HttpResultBean<UserRecordBean.UserRead>>() {
  181. }.getType());
  182. Map<String, String> postAssessment = new HashMap<>();
  183. postAssessment.put("complete", String.valueOf(integrity));
  184. postAssessment.put("speed", String.valueOf(speedd));
  185. postAssessment.put("intonation", String.valueOf(tone));
  186. postAssessment.put("score", String.valueOf(score));
  187. postAssessment.put("accuracy", String.valueOf(accuracy));
  188. postAssessment.put("userReadId", read.getData().getId());
  189. mModel.postAssessment(activity, mGson.toJson(postAssessment), new Callback() {
  190. @Override
  191. public void onSuccess(HttpInfo info) throws IOException {
  192. Logger.e("请求评测成功:" + info.getRetDetail());
  193. mUploadingSuccessFrame.setVisibility(View.VISIBLE);
  194. FrameLayout toLisMySelf = mUploadingSuccessFrame.findViewById(R.id.popup_record_status_upload_success_btn);
  195. toLisMySelf.setOnClickListener(new View.OnClickListener() {
  196. @Override
  197. public void onClick(View view) {
  198. PopWindowUtil.hidePopupWindow();
  199. // Toast.makeText(context, "去听听(首页->我的tab)", Toast.LENGTH_SHORT).show();
  200. for (int i = 0; i < EApplication.mActivityList.size(); i++) {
  201. EApplication.mActivityList.get(i).finish();
  202. }
  203. Intent intent = new Intent(activity, MainActivity.class);
  204. intent.putExtra("defaultIndex", 0);
  205. activity.startActivity(intent);
  206. }
  207. });
  208. }
  209. @Override
  210. public void onFailure(HttpInfo info) throws IOException {
  211. Logger.e("请求评测失败:" + info.getRetDetail());
  212. }
  213. });
  214. }
  215. @Override
  216. public void onFailure(HttpInfo info) throws IOException {
  217. Logger.e("发布朗读失败了:" + info.getRetDetail());
  218. }
  219. });
  220. }
  221. } else {
  222. //失败逻辑
  223. {
  224. mUploadingErrorFrame.setVisibility(View.VISIBLE);
  225. FrameLayout mCancelUpload = mUploadingErrorFrame.findViewById(R.id.popupwindow_record_status_uploading_error_cancel_upload);
  226. FrameLayout mReUpload = mUploadingErrorFrame.findViewById(R.id.popupwindow_record_status_uploading_error_re_upload);
  227. mCancelUpload.setOnClickListener(new View.OnClickListener() {
  228. @Override
  229. public void onClick(View view) {
  230. PopWindowUtil.hidePopupWindow();
  231. }
  232. });
  233. mReUpload.setOnClickListener(new View.OnClickListener() {
  234. @Override
  235. public void onClick(View view) {
  236. // Toast.makeText(context, "重新上传", Toast.LENGTH_SHORT).show();
  237. mModel.upLoadRecord(activity, Consts.getRecordUpLoadFileUrl(), mUploadProgressBack);
  238. }
  239. });
  240. }
  241. }
  242. }
  243. @Override
  244. public void onProgressAsync(int percent, long bytesWritten, long contentLength, boolean done) {
  245. super.onProgressAsync(percent, bytesWritten, contentLength, done);
  246. }
  247. @Override
  248. public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) {
  249. super.onProgressMain(percent, bytesWritten, contentLength, done);
  250. }
  251. };
  252. mUpload.setOnClickListener(new View.OnClickListener() {
  253. @Override
  254. public void onClick(View view) {
  255. //上传录音
  256. mOverFrame.setVisibility(View.GONE);
  257. mUploadingFrame.setVisibility(View.VISIBLE);
  258. userApi.postUserEvent(activity, UserEventEnum.EVENT_UPLOAD.getEvent());
  259. mModel.upLoadRecord(activity, Consts.getRecordUpLoadFileUrl(), mUploadProgressBack);
  260. }
  261. });
  262. mRePlay.setOnClickListener(new View.OnClickListener() {
  263. @Override
  264. public void onClick(View view) {
  265. listener.clickReRecord();
  266. }
  267. });
  268. }
  269. }
  270. public void myOrderWindowInit(Context context, View view) {
  271. List<Object> mDataList = new ArrayList<>();
  272. // for (int i = 0; i < 30; i++) {
  273. // mDataList.add("i=" + i);
  274. // }
  275. if (mDataList != null && mDataList.size() > 0) {
  276. RecyclerView rv = view.findViewById(R.id.popupwindow_myorder_recyclerview);
  277. PopupMyOrderAdapter popupMyOrderAdapter = new PopupMyOrderAdapter(context, mDataList);
  278. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
  279. linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  280. rv.setLayoutManager(linearLayoutManager);
  281. rv.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
  282. @Override
  283. public void onChildViewAttachedToWindow(@androidx.annotation.NonNull View view) {
  284. }
  285. @Override
  286. public void onChildViewDetachedFromWindow(@androidx.annotation.NonNull View view) {
  287. }
  288. });
  289. rv.setAdapter(popupMyOrderAdapter);
  290. } else {
  291. view.findViewById(R.id.popupwindow_myorder_noitem).setVisibility(View.VISIBLE);
  292. }
  293. }
  294. public void getBindWeChatPath(Activity activity, ImageView imageView) {
  295. mActivity = activity;
  296. userApi.getBindWeChatPath(activity, new Callback() {
  297. @Override
  298. public void onSuccess(HttpInfo info) throws IOException {
  299. Logger.e("获取绑定微信成功:" + info.getRetDetail());
  300. HttpResultBean<String> bean = info.getRetDetail(new TypeToken<HttpResultBean<String>>() {
  301. }.getType());
  302. imageView.setImageBitmap(QRCodeUtil.createQRCode(bean.getData(), SizeUtils.dp2px(activity, 900)));
  303. }
  304. @Override
  305. public void onFailure(HttpInfo info) throws IOException {
  306. Logger.e("获取绑定微信失败:" + info.getRetDetail());
  307. }
  308. });
  309. }
  310. public void getMyInfo(Activity activity, TextView textView) {
  311. mActivity = activity;
  312. userApi.getMyInfo(activity, new Callback() {
  313. @Override
  314. public void onSuccess(HttpInfo info) throws IOException {
  315. HttpResultBean<MyInfoBean> bean = info.getRetDetail(new TypeToken<HttpResultBean<MyInfoBean>>() {
  316. }.getType());
  317. if (bean.getData().getUser().getOpenId() == null || bean.getData().getUser().getOpenId().equals("")) {
  318. textView.setText("");
  319. } else {
  320. textView.setText("微信昵称:" + bean.getData().getUser().getWechatName());
  321. textView.setVisibility(View.VISIBLE);
  322. }
  323. }
  324. @Override
  325. public void onFailure(HttpInfo info) throws IOException {
  326. }
  327. });
  328. Message message = new Message();
  329. message.obj = textView;
  330. message.what = 0x1312;
  331. handler.sendMessageDelayed(message, 3000);
  332. }
  333. public Handler getPopHandler() {
  334. return handler;
  335. }
  336. private Handler handler = new Handler(new Handler.Callback() {
  337. @Override
  338. public boolean handleMessage(@NonNull Message message) {
  339. switch (message.what) {
  340. case 0x0131:
  341. ((TextView) message.obj).setText(message.arg1 + "/100");
  342. break;
  343. case 0x1312:
  344. getMyInfo(mActivity, (TextView) message.obj);
  345. break;
  346. }
  347. return false;
  348. }
  349. });
  350. }