package com.edufound.reader.presenter; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import com.edufound.reader.R; import com.edufound.reader.activity.MainActivity; import com.edufound.reader.adapter.PopupMyOrderAdapter; import com.edufound.reader.apiserver.UserApiServerImpl; import com.edufound.reader.application.EApplication; import com.edufound.reader.bean.HttpResultBean; import com.edufound.reader.bean.MyInfoBean; import com.edufound.reader.bean.RecordResultBean; import com.edufound.reader.bean.UserEventEnum; import com.edufound.reader.bean.UserRecordBean; import com.edufound.reader.listener.PopupRecordStatusListener; import com.edufound.reader.model.PopWindowModelImpl; import com.edufound.reader.popwindow.PopWindowUtil; import com.edufound.reader.util.Consts; import com.edufound.reader.util.QRCodeUtil; import com.edufound.reader.util.SizeUtils; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.okhttplib.HttpInfo; import com.okhttplib.callback.Callback; import com.okhttplib.callback.ProgressCallback; 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.annotation.NonNull; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; public class PopWindowPresneter { PopWindowModelImpl mModel; ProgressCallback mUploadProgressBack; Gson mGson; UserApiServerImpl userApi; Activity mActivity; public PopWindowPresneter() { userApi = new UserApiServerImpl(); mModel = new PopWindowModelImpl(); mGson = new Gson(); } public void initRecordStatusWindow(Activity activity, View dialog_view, RecordResultBean bean, UserRecordBean.UserRead userread, PopupRecordStatusListener listener) { mActivity = activity; FrameLayout mOverFrame = dialog_view.findViewById(R.id.popupwindow_record_status_over_frame); FrameLayout mUploadingFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_frame); FrameLayout mUploadingSuccessFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_success_frame); FrameLayout mUploadingErrorFrame = dialog_view.findViewById(R.id.popupwindow_record_status_uploading_error_frame); LinearLayout startLayout = mOverFrame.findViewById(R.id.popupwindow_record_status_over_start_layout); FrameLayout mRePlay = mOverFrame.findViewById(R.id.popupwindow_record_status_over_replay); FrameLayout mUpload = mOverFrame.findViewById(R.id.popupwindow_record_status_over_upload); // float a = bean.getOverall() / 100f;//百分制 int integrity = bean.getIntegrity(); int accuracy = bean.getAccuracy(); int speedd = bean.getSpeed(); int tone = bean.getTone(); int score = (int) ((integrity * 0.5) + (bean.getOverall() * 0.3) + (speedd * 0.1) + (tone * 0.1)); int startCount = 0; if (score > 0 && score <= 20) { //1个星星 startCount = 1; } else if (score > 20 && score <= 40) { //2个星星 startCount = 2; } else if (score > 40 && score <= 60) { //3个星星 startCount = 3; } else if (score > 60 && score <= 80) { //4个星星 startCount = 4; } else if (score > 80 && score <= 100) { //5个星星 startCount = 5; } //先显示评测报告,处理测评报告逻辑 { mOverFrame.setVisibility(View.VISIBLE); startLayout.removeAllViews(); for (int i = 0; i < 5; i++) { ImageView start = new ImageView(activity); if (i < startCount) { start.setImageResource(R.drawable.popup_record_status_over_stars_true); } else { start.setImageResource(R.drawable.popup_record_status_over_stars_false); } start.setLayoutParams(new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1)); startLayout.addView(start); } TextView completionText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_completion_text); TextView correctrateText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_correctrate_text); TextView speedText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_speed_text); TextView intonationText = mOverFrame.findViewById(R.id.popupwindow_record_status_over_intonation_text); completionText.post(new Runnable() { @Override public void run() { for (int completionPoint = 0; completionPoint < integrity; completionPoint++) { Message msg = new Message(); msg.obj = completionText; msg.arg1 = completionPoint + 1; msg.what = 0x0131; handler.sendMessageDelayed(msg, 50 * completionPoint); } } }); correctrateText.post(new Runnable() { @Override public void run() { for (int correctrate = 0; correctrate < accuracy; correctrate++) { Message msg = new Message(); msg.obj = correctrateText; msg.arg1 = correctrate + 1; msg.what = 0x0131; handler.sendMessageDelayed(msg, 50 * correctrate); } } }); speedText.post(new Runnable() { @Override public void run() { for (int speed = 0; speed < speedd; speed++) { Message msg = new Message(); msg.obj = speedText; msg.arg1 = speed + 1; msg.what = 0x0131; handler.sendMessageDelayed(msg, 50 * speed); } } }); intonationText.post(new Runnable() { @Override public void run() { for (int intonation = 0; intonation < tone; intonation++) { Message msg = new Message(); msg.obj = intonationText; msg.arg1 = intonation + 1; msg.what = 0x0131; handler.sendMessageDelayed(msg, 50 * intonation); } } }); mUploadProgressBack = new ProgressCallback() { @Override public void onResponseMain(String filePath, HttpInfo info) { super.onResponseMain(filePath, info); Logger.e("onResponseMain:" + info.getRetDetail()); Logger.e("onResponseMain--netCode:" + info.getNetCode()); if (info.getNetCode() == 200) { //上传完成了 //上传成功逻辑 { HttpResultBean bean = info.getRetDetail(new TypeToken>() { }.getType()); Logger.e("filePath:" + bean.getData()); //请求发布接口 Map postUserRead = new HashMap<>(); postUserRead.put("audioPath", bean.getData()); postUserRead.put("coverImg", userread.getCoverImg()); postUserRead.put("exampleId", userread.getExampleId()); postUserRead.put("originVideo", userread.getOriginVideo()); postUserRead.put("shareImg", userread.getShareImg()); postUserRead.put("title", userread.getTitle()); postUserRead.put("type", "READ"); postUserRead.put("uid", Consts.getUID()); mModel.postUserRead(activity, mGson.toJson(postUserRead), new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { //发布朗读成功了,成功之后求情评测数据接口 Logger.e("发布朗读成功了:" + info.getRetDetail()); HttpResultBean read = info.getRetDetail(new TypeToken>() { }.getType()); Map postAssessment = new HashMap<>(); postAssessment.put("complete", String.valueOf(integrity)); postAssessment.put("speed", String.valueOf(speedd)); postAssessment.put("intonation", String.valueOf(tone)); postAssessment.put("score", String.valueOf(score)); postAssessment.put("accuracy", String.valueOf(accuracy)); postAssessment.put("userReadId", read.getData().getId()); mModel.postAssessment(activity, mGson.toJson(postAssessment), new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { Logger.e("请求评测成功:" + info.getRetDetail()); mUploadingSuccessFrame.setVisibility(View.VISIBLE); FrameLayout toLisMySelf = mUploadingSuccessFrame.findViewById(R.id.popup_record_status_upload_success_btn); toLisMySelf.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { PopWindowUtil.hidePopupWindow(); // Toast.makeText(context, "去听听(首页->我的tab)", Toast.LENGTH_SHORT).show(); for (int i = 0; i < EApplication.mActivityList.size(); i++) { EApplication.mActivityList.get(i).finish(); } Intent intent = new Intent(activity, MainActivity.class); intent.putExtra("defaultIndex", 0); activity.startActivity(intent); } }); } @Override public void onFailure(HttpInfo info) throws IOException { Logger.e("请求评测失败:" + info.getRetDetail()); } }); } @Override public void onFailure(HttpInfo info) throws IOException { Logger.e("发布朗读失败了:" + info.getRetDetail()); } }); } } else { //失败逻辑 { mUploadingErrorFrame.setVisibility(View.VISIBLE); FrameLayout mCancelUpload = mUploadingErrorFrame.findViewById(R.id.popupwindow_record_status_uploading_error_cancel_upload); FrameLayout mReUpload = mUploadingErrorFrame.findViewById(R.id.popupwindow_record_status_uploading_error_re_upload); mCancelUpload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { PopWindowUtil.hidePopupWindow(); } }); mReUpload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Toast.makeText(context, "重新上传", Toast.LENGTH_SHORT).show(); mModel.upLoadRecord(activity, Consts.getRecordUpLoadFileUrl(), mUploadProgressBack); } }); } } } @Override public void onProgressAsync(int percent, long bytesWritten, long contentLength, boolean done) { super.onProgressAsync(percent, bytesWritten, contentLength, done); } @Override public void onProgressMain(int percent, long bytesWritten, long contentLength, boolean done) { super.onProgressMain(percent, bytesWritten, contentLength, done); } }; mUpload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //上传录音 mOverFrame.setVisibility(View.GONE); mUploadingFrame.setVisibility(View.VISIBLE); userApi.postUserEvent(activity, UserEventEnum.EVENT_UPLOAD.getEvent()); mModel.upLoadRecord(activity, Consts.getRecordUpLoadFileUrl(), mUploadProgressBack); } }); mRePlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.clickReRecord(); } }); } } public void myOrderWindowInit(Context context, View view) { List mDataList = new ArrayList<>(); // for (int i = 0; i < 30; i++) { // mDataList.add("i=" + i); // } if (mDataList != null && mDataList.size() > 0) { RecyclerView rv = view.findViewById(R.id.popupwindow_myorder_recyclerview); PopupMyOrderAdapter popupMyOrderAdapter = new PopupMyOrderAdapter(context, mDataList); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context); linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); rv.setLayoutManager(linearLayoutManager); rv.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() { @Override public void onChildViewAttachedToWindow(@androidx.annotation.NonNull View view) { } @Override public void onChildViewDetachedFromWindow(@androidx.annotation.NonNull View view) { } }); rv.setAdapter(popupMyOrderAdapter); } else { view.findViewById(R.id.popupwindow_myorder_noitem).setVisibility(View.VISIBLE); } } public void getBindWeChatPath(Activity activity, ImageView imageView) { mActivity = activity; userApi.getBindWeChatPath(activity, new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { Logger.e("获取绑定微信成功:" + info.getRetDetail()); HttpResultBean bean = info.getRetDetail(new TypeToken>() { }.getType()); imageView.setImageBitmap(QRCodeUtil.createQRCode(bean.getData(), SizeUtils.dp2px(activity, 900))); } @Override public void onFailure(HttpInfo info) throws IOException { Logger.e("获取绑定微信失败:" + info.getRetDetail()); } }); } public void getMyInfo(Activity activity, TextView textView) { mActivity = activity; userApi.getMyInfo(activity, new Callback() { @Override public void onSuccess(HttpInfo info) throws IOException { HttpResultBean bean = info.getRetDetail(new TypeToken>() { }.getType()); if (bean.getData().getUser().getOpenId() == null || bean.getData().getUser().getOpenId().equals("")) { textView.setText(""); } else { textView.setText("微信昵称:" + bean.getData().getUser().getWechatName()); textView.setVisibility(View.VISIBLE); } } @Override public void onFailure(HttpInfo info) throws IOException { } }); Message message = new Message(); message.obj = textView; message.what = 0x1312; handler.sendMessageDelayed(message, 3000); } public Handler getPopHandler() { return handler; } private Handler handler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(@NonNull Message message) { switch (message.what) { case 0x0131: ((TextView) message.obj).setText(message.arg1 + "/100"); break; case 0x1312: getMyInfo(mActivity, (TextView) message.obj); break; } return false; } }); }