ConfideOrderInfoView.java 7.32 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package com.yidianling.uikit.custom.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MessageBuilder;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
import com.yidianling.common.tools.RxImageTool;
import com.yidianling.common.tools.ToastUtil;
import com.yidianling.im.R;
import com.yidianling.uikit.business.session.helper.MessageListPanelHelper;
import com.yidianling.uikit.custom.bridge.ActionHandlerStorage;
import com.yidianling.uikit.custom.bridge.IP2PCustomActionHandler;
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
import com.ydl.ydlcommon.router.YdlCommonRouterManager;
import com.ydl.ydlcommon.utils.StringUtils;
import com.ydl.ydlcommon.utils.YDLCacheUtils;

/**
 * Created by haorui on 2019/5/22.
 * Des:
 */
public class ConfideOrderInfoView extends RelativeLayout {
    private Context mContext = null;
    private String mSessionId;
    private IP2PCustomActionHandler.DocInfo info = null;
    private TextView tv_status = null;
    private TextView tv_action = null;
    private TextView tv_order_info = null;
    private ImageView iv_call = null;

    public ConfideOrderInfoView(Context context, String sessionId) {
        super(context);
        this.mContext = context;
        this.mSessionId= sessionId;
        initView();
    }

    public ConfideOrderInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void initView() {
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = RxImageTool.dp2px(8f);
        params.rightMargin = RxImageTool.dp2px(8f);
58
        params.topMargin = RxImageTool.dp2px(10f);
konghaorui committed
59
        setLayoutParams(params);
konghaorui committed
60 61
        View.inflate(mContext, R.layout.im_nim_chat_confide_order_info_view, this);
        setBackground(getResources().getDrawable(R.drawable.im_bg_im_confide_action));
konghaorui committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
        tv_status = findViewById(R.id.tv_status);
        tv_action = findViewById(R.id.tv_action);
        iv_call = findViewById(R.id.iv_call);
        tv_order_info = findViewById(R.id.tv_order_info);
    }


    public void setData(IP2PCustomActionHandler.DocInfo info) {
        this.info = info;
        updateUI();
        setClick();
    }

    private void updateUI() {
        //有未完成订单
        if (info.hasAvailableListenOrder == 2) {
            tv_status.setText("剩余时间:" + coverTime(Integer.valueOf(info.listenOrderRemainTime)));


            if (info.is_online == 3) { //通话中
                tv_action.setText("通话中");
                tv_action.setTextColor(getResources().getColor(R.color.platform_color_FF6633));
            } else if (info.is_online == 4 || info.is_online == 1) { //继续拨打
                tv_action.setVisibility(View.GONE);
                iv_call.setVisibility(View.VISIBLE);
            } else if (info.is_online == 2) { //离线
                tv_action.setText("喊TA上线");
konghaorui committed
89
                tv_action.setBackground(getResources().getDrawable(R.drawable.im_background_chat_confide_order_call_online));
konghaorui committed
90 91 92 93 94
                tv_action.setTextColor(getResources().getColor(R.color.platform_color_1DA1F2));
            }

        } else if (info.listenOrderCommentStatus == 1) {//订单未评价
            tv_status.setText("已结束");
konghaorui committed
95
            tv_action.setBackground(getResources().getDrawable(R.drawable.im_background_chat_confide_order_action));
konghaorui committed
96
            tv_action.setTextColor(getResources().getColor(R.color.platform_but_text_color));
konghaorui committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            tv_action.setText("去评价");
        }
        tv_order_info.setText(info.listenOrderDesc);
    }

    void setClick() {
        iv_call.setOnClickListener(view -> {
            if (info.is_online == 1 || info.is_online == 4) {//通话中
                //跳转专家倾述主页
                ActionHandlerStorage.getL(mSessionId).confide();
            }
        });
        tv_action.setOnClickListener(view -> {
            if (info.is_online == 2) {//离线  喊ta上线
                //走发送留言逻辑:一天只能给同一个老师发送一次留言
                checkNeedSendMsg();
            }

            if (info.is_online == 1 || info.is_online == 3 || info.is_online == 4) {//通话中
                //跳转专家倾述主页
                ActionHandlerStorage.getL(mSessionId).confide();
            }

            if (info.listenOrderCommentStatus == 1) {//待评价订单
                if (TextUtils.isEmpty(info.listenOrderCommentUrl)) {
                    ToastUtil.toastShort("评价页面参数错误,请退出重试");
                    return;
                }
                H5Params h5Params = new H5Params(info.listenOrderCommentUrl, "评价");
                NewH5Activity.start(mContext, h5Params);
            }
        });

        //点击整个view,跳转订单详情页面
        setOnClickListener(view -> {
            if (!TextUtils.isEmpty(info.listenOrderUrl)) {//订单详情
                H5Params h5Params = new H5Params(info.listenOrderUrl, "订单详情");
                NewH5Activity.start(mContext, h5Params);
            }
        });
    }


    //时间转化
    private String coverTime(int time) {
        return String.valueOf((time / 60)) + ":" + time % 60;
    }

    /**
     * 判断是否需要给老师发送留言
     */
    private void checkNeedSendMsg() {
        String localTime = YDLCacheUtils.Companion.getSendMsgTime(YdlCommonRouterManager.INSTANCE.getYdlCommonRoute().getUid() + "", ActionHandlerStorage.getL(mSessionId).getInfo().toUid);
        //是否是同一天
        if (StringUtils.compareTime(localTime)) {//是同一天
            ToastUtil.toastShort("已留言");
            return;
        }
        String text = "老师好,我想找您倾诉,请尽快上线私聊我,我在等您";
        //以用户的身份发送消息给老师
        IMMessage message = MessageBuilder.createTextMessage(ActionHandlerStorage.getL(mSessionId).getInfo().toUid, SessionTypeEnum.P2P, text);
        NIMClient.getService(MsgService.class).sendMessage(message, false).setCallback(new RequestCallback<Void>() {
            @Override
            public void onSuccess(Void param) {
                YDLCacheUtils.Companion.saveSendMsgTime(YdlCommonRouterManager.INSTANCE.getYdlCommonRoute().getUid() + "", ActionHandlerStorage.getL(mSessionId).getInfo().toUid, System.currentTimeMillis() + "");
            }

            @Override
            public void onFailed(int code) {
                if (code == 7101) {
                    ToastUtil.toastShort("您已被对方拉黑!");
                } else if (code == 404) {

                } else {
                    //上传至后台
                    ActionHandlerStorage.getL(mSessionId).uploadSendMessageError(code);
                }
            }

            @Override
            public void onException(Throwable exception) {

            }
        });
        MessageListPanelHelper.getInstance().notifyAddMessage(message);
    }
}