2023-10-30 18:25:16 +08:00

233 lines
8.2 KiB
Java

package com.shayu.onetoone.activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSONObject;
import com.makeramen.roundedimageview.RoundedImageView;
import com.shayu.onetoone.R;
import com.shayu.onetoone.activity.message.ChatActivity;
import com.shayu.onetoone.bean.MatchingInfoBean;
import com.shayu.onetoone.bean.MatchingInfoUserBean;
import com.shayu.onetoone.bean.SendConsumeBean;
import com.shayu.onetoone.bean.UserBean;
import com.shayu.onetoone.dialog.TipsDialog;
import com.shayu.onetoone.listener.OnDialogClickListener;
import com.shayu.onetoone.listener.OnSendMessageListener;
import com.shayu.onetoone.manager.OTONetManager;
import com.shayu.onetoone.manager.RouteManager;
import com.shayu.onetoone.manager.SendMessageManager;
import com.shayu.onetoone.utils.ConversationUtils;
import com.yunbao.common.glide.ImgLoader;
import com.yunbao.common.http.base.HttpCallback;
import com.yunbao.common.utils.ToastUtil;
import com.yunbao.common.utils.WordUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
@Route(path = RouteManager.ACTIVITY_MATCHING)
public class MatchingActivity extends AbsOTOActivity {
private Task task;
private Handler handler;
private RoundedImageView user1, user2, user3, user4, user5;
private TextView num;
private ImageView back;
List<RoundedImageView> imageViewList = new ArrayList<>();
@Override
protected int getLayoutId() {
return R.layout.activity_matching;
}
@Override
protected void main(Bundle savedInstanceState) {
handler = new Handler(Looper.getMainLooper());
if (initView()) {
task = new Task();
new Timer().schedule(task, 10000, 10000);
}
initData();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (task != null) {
task.cancel();
task = null;
}
handler = null;
}
private boolean initView() {
user1 = findViewById(R.id.user1);
user2 = findViewById(R.id.user2);
user3 = findViewById(R.id.user3);
user4 = findViewById(R.id.user4);
user5 = findViewById(R.id.user5);
num = findViewById(R.id.num);
back = findViewById(R.id.back);
imageViewList.add(user1);
imageViewList.add(user2);
imageViewList.add(user3);
imageViewList.add(user4);
imageViewList.add(user5);
back.setOnClickListener(v -> {
onBack();
});
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String data = bundle.getString("data");
bundle.putInt("type", ChatActivity.CALL_CHAT_TYPE_MATCH);
if (data != null) {
bean = JSONObject.parseObject(data, SendConsumeBean.class);
if (bean != null) {
MatchingActivity.this.bundle=bundle;
new Handler(Looper.getMainLooper()).postDelayed(toCallRunnable, 1000);
return false;
}
}
}
return true;
}
private void onBack() {
new TipsDialog(mContext)
.setTitle(WordUtil.getNewString(R.string.matching_quit_title))
.setApplyText(WordUtil.getNewString(R.string.matching_quit_cancel))
.setCancelText(WordUtil.getNewString(R.string.matching_quit_apply))
.setOnDialogClickListener(new OnDialogClickListener() {
@Override
public void onCancel(Dialog dialog) {
super.onCancel(dialog);
bean = null;
handler.removeCallbacks(toCallRunnable);
if (task != null) {
task.cancel();
task = null;
}
MatchingActivity.this.finish();
}
}).showDialog();
}
private void initData() {
OTONetManager.getInstance(mContext)
.getMatchingInfo(new HttpCallback<MatchingInfoBean>() {
@Override
public void onSuccess(MatchingInfoBean data) {
List<MatchingInfoUserBean> list = data.getList();
for (int i = 0; i < 5; i++) {
ImgLoader.display(mContext, list.get(i).getAvatar(), imageViewList.get(i));
}
num.setText(data.getPeople() + "");
}
@Override
public void onError(String error) {
}
});
}
private Bundle bundle;
private Runnable toCallRunnable = new Runnable() {
@Override
public void run() {
if (bean == null) {
return;
}
ConversationUtils.startConversation(mContext, bean.getMateUser().getUser().getId() + "", bundle);
MatchingActivity.this.finish();
}
};
SendConsumeBean bean;
private void call() {
handler.post(() -> ToastUtil.showDebug("匹配"));
SendMessageManager.matching(new OnSendMessageListener() {
@Override
public void onSuccess(String token, SendConsumeBean b) {
super.onSuccess(token, b);
bean = b;
task.cancel();
Bundle bundle = new Bundle();
bundle.putInt("type", ChatActivity.CALL_CHAT_TYPE_MATCH);
bundle.putString("data", JSONObject.toJSONString(bean));
MatchingActivity.this.bundle = bundle;
handler.postDelayed(toCallRunnable, 1000);
}
@Override
public void onError(int status, String msg, SendConsumeBean bean) {
super.onError(status, msg, bean);
if (bean.getCode() == 503) {
handler.post(() -> ToastUtil.showDebug("没人,继续"));
} else if (status == OnSendMessageListener.STATUS_NOT_PRICE) {
task.cancel();
handler.post(() -> ToastUtil.showDebug("价格不足"));
MatchingActivity.this.finish();
}
}
});
}
private class Task extends TimerTask {
final int MAX = 5;
int index = MAX;
@Override
public void run() {
if (index != 0) {
call();
} else {
handler.post(() -> {
new TipsDialog(mContext)
.setTitle(WordUtil.getNewString(R.string.matching_wait_title))
.setApplyText(WordUtil.getNewString(R.string.matching_wait_apply))
.setCancelText(WordUtil.getNewString(R.string.matching_wait_cancel))
.setOnDialogClickListener(new OnDialogClickListener() {
@Override
public void onCancel(Dialog dialog) {
super.onCancel(dialog);
index = MAX;
}
@Override
public void onApply(Dialog dialog) {
super.onApply(dialog);
bean = null;
handler.removeCallbacks(toCallRunnable);
task.cancel();
task = null;
MatchingActivity.this.finish();
}
}).showDialog();
});
}
index--;
}
@Override
public boolean cancel() {
handler.post(() -> ToastUtil.showDebug("取消"));
return super.cancel();
}
}
@Override
public void onBackPressed() {
onBack();
}
}