MsgViewHolderOrderAlreadyDone.java 9.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package com.yidianling.im.session.viewholder;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

11 12 13
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MsgService;
14
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
15 16
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
17 18
import com.ydl.webview.H5Params;
import com.ydl.webview.NewH5Activity;
19
import com.ydl.ydlcommon.data.http.RxUtils;
刘鹏 committed
20
import com.yidianling.common.tools.LogUtil;
21
import com.yidianling.common.tools.ToastUtil;
22
import com.yidianling.im.R;
23
import com.yidianling.im.config.constants.ImConstants;
24
import com.yidianling.im.helper.IMChatUtil;
25
import com.yidianling.im.http.ImRetrofitApi;
26
import com.yidianling.im.session.extension.CustomAttachmentConfirmOrder;
刘鹏 committed
27
import com.yidianling.im.session.extension.CustomAttachmentOrderAlreadyDone;
28 29 30
import com.yidianling.nimbase.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.yidianling.uikit.business.session.viewholder.MsgViewHolderBase;

31 32 33 34 35
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
刘鹏 committed
36
import io.reactivex.disposables.Disposable;
37 38
import io.reactivex.schedulers.Schedulers;

刘鹏 committed
39
/**
40
 * 确认订单 消息类型39
刘鹏 committed
41
 */
42 43
public class MsgViewHolderOrderAlreadyDone extends MsgViewHolderBase {

44
    private TextView tv_title, tv_date, tv_type, tv_submit;
45 46 47
    private View lin_root;
    private ImageView img_icon;

48 49 50 51 52 53
    /***跳转url*/
    public String url;
    /***订单id*/
    public String orderId;
    /***0 未确认 1已确认*/
    public int flag;
刘鹏 committed
54 55
    public boolean isSureed = false;//是否已确认

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    public MsgViewHolderOrderAlreadyDone(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
        return R.layout.im_ui_message_custom_order_alreadydone;
    }

    @Override
    protected void inflateContentView() {
        tv_title = view.findViewById(R.id.tv_title);
        tv_date = view.findViewById(R.id.tv_date);
        tv_type = view.findViewById(R.id.tv_type);
        lin_root = view.findViewById(R.id.lin_root);
        img_icon = view.findViewById(R.id.img_icon);
73
        tv_submit = view.findViewById(R.id.tv_submit);
74

75
        if (isReceivedMessage()) {
76 77
            setAvatarRightInVisibity();
            lin_root.setBackgroundResource(R.drawable.im_bg_radius_no_topleft_white_8);
78
        } else {
79 80 81 82 83 84 85 86
            setAvatarLeftInVisibity();
            lin_root.setBackgroundResource(R.drawable.im_bg_radius_no_topright_white_8);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindContentView() {
87
        if (isReceivedMessage()) {
88
            setAvatarRightInVisibity();
89
        } else {
90 91 92 93
            setAvatarLeftInVisibity();
        }

        MsgAttachment attachment = message.getAttachment();
刘鹏 committed
94 95
        if (attachment instanceof CustomAttachmentOrderAlreadyDone) {
            CustomAttachmentOrderAlreadyDone customAttachment = (CustomAttachmentOrderAlreadyDone) attachment;
96
            tv_title.setText(customAttachment.title);
刘鹏 committed
97 98
            tv_date.setText(customAttachment.consultDuration == null ? "" : customAttachment.consultDuration);
            tv_type.setText(customAttachment.serviceType == null ? "" : customAttachment.serviceType);
99 100 101 102 103 104 105 106 107 108 109 110
            url = customAttachment.url;
            orderId = customAttachment.orderId;
            flag = customAttachment.status;


            if (flag == 1) {
                // 1代表已确认,0代表待确认
                tv_submit.setText("已确认");
                tv_submit.setEnabled(false);
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_gray_linear_12));
                img_icon.setVisibility(View.VISIBLE);
                tv_submit.setVisibility(View.GONE);
111
                updateLocalMsg();
112 113 114 115 116 117 118 119 120 121 122 123 124
            } else {
                tv_submit.setText("确认");
                tv_submit.setEnabled(true);
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
                img_icon.setVisibility(View.GONE);
                tv_submit.setVisibility(View.VISIBLE);
            }

            tv_submit.setOnClickListener(v -> {
                //确定按钮给服务端发消息发送
                //确认接口
                ToastUtil.toastShort(context, "发送中...");

刘鹏 committed
125
                Disposable subscribe = ImRetrofitApi.Companion.getImRetrofitApi().affirmComplete(orderId)
刘鹏 committed
126
                        .compose(RxUtils.INSTANCE.resultJavaData())
127 128 129
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(it -> {
刘鹏 committed
130 131 132
                                    //确认完成,按钮置灰,更新本地消息
                                    updateLocalMsg();
                                }, throwable ->
133
                                        ToastUtil.toastShort(throwable.getMessage())
刘鹏 committed
134
                        );
135 136

            });
137
        }
刘鹏 committed
138 139

        try {
140 141
            Map<String, Object> map = message.getLocalExtension();
            if (map != null) {
刘鹏 committed
142 143
                Object obj = map.get("isSure");
                Object exp = map.get("isExpired");
144
                if (obj != null) {
刘鹏 committed
145
                    boolean b = (boolean) obj;
146
                    if (flag == 1 || b) {
刘鹏 committed
147 148
                        updateSureView("已确认");
                    }
149
                } else if (exp != null) {
刘鹏 committed
150
                    boolean expb = (boolean) exp;
151
                    if (expb) {
刘鹏 committed
152 153
                        updateSureView("已确认");
                    }
154
                } else {
刘鹏 committed
155 156 157
                    isSureed = false;
                    tv_submit.setText("确认");
                    tv_submit.setEnabled(true);
158
                    tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
刘鹏 committed
159 160
                }

161
            } else {
刘鹏 committed
162 163 164
                isSureed = false;
                tv_submit.setText("确认");
                tv_submit.setEnabled(true);
165
                tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_green_linear_12));
刘鹏 committed
166
            }
167 168
        } catch (Exception e) {
        }
刘鹏 committed
169

170 171
        hideItemBg();
    }
172

173 174
    @Override
    protected void onItemClick() {
刘鹏 committed
175
        if (TextUtils.isEmpty(url) && orderId == null) {
176 177
            return;
        }
刘鹏 committed
178
        if (orderId == null || orderId.isEmpty()) {
179 180 181 182
            NewH5Activity.start(context, new H5Params(url, null));
        } else {
            //跳转新订单详情页
            H5Params orderParams = new H5Params(
刘鹏 committed
183
                    ImConstants.Companion.getORDER_DETAIL() + (orderId.isEmpty() ? "" : orderId), null);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
            NewH5Activity.start(context, orderParams);
        }
    }

    private void updateLocalMsg() {
        updateSureView("已确认");
        //更新本地消息
        Map<String, Object> map = message.getLocalExtension();
        if (map == null) {
            map = new HashMap();
        }
        map.put("isSure", true);
        message.setLocalExtension(map);
        NIMClient.getService(MsgService.class).updateIMMessage(message);
        //同时更新历史同订单未确认的消息状态
199
        NIMClient.getService(MsgService.class).queryMessageList(message.getSessionId(), SessionTypeEnum.P2P, 0, 100).setCallback(new RequestCallback<List<IMMessage>>() {
200 201 202 203 204 205 206 207 208 209
            @Override
            public void onSuccess(List<IMMessage> param) {
                if (param != null && param.size() > 0) {
                    for (IMMessage msg : param) {
                        MsgAttachment attachment = msg.getAttachment();
                        if (attachment instanceof CustomAttachmentConfirmOrder) {
                            CustomAttachmentConfirmOrder customAttachConsultSubScript = (CustomAttachmentConfirmOrder) attachment;
                            if (customAttachConsultSubScript.orderId.equals(orderId)) {
                                //修改历史消息状态为已失效
                                Map map1 = msg.getLocalExtension();
210
                                if (map1 == null) {
211
                                    map1 = new HashMap();
212
                                    map1.put("isExpired", true);
213 214
                                    msg.setLocalExtension(map1);
                                    NIMClient.getService(MsgService.class).updateIMMessage(msg);
215 216
                                    //通知ui刷新
                                    getMsgAdapter().updateItemAtLocalExtension(msg);
217
                                } else {
218
                                    map1 = new HashMap();
219
                                    map1.put("isExpired", true);
220 221 222 223
                                    msg.setLocalExtension(map1);
                                    NIMClient.getService(MsgService.class).updateIMMessage(msg);
                                    //通知ui刷新
                                    getMsgAdapter().updateItemAtLocalExtension(msg);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
                                }
                            }
                        }
                    }
                }
            }

            @Override
            public void onFailed(int code) {

            }

            @Override
            public void onException(Throwable exception) {

            }
        });
    }

    private void updateSureView(String btnName) {
        tv_submit.setText(btnName);
        tv_submit.setEnabled(false);
        tv_submit.setBackground(ContextCompat.getDrawable(context, R.drawable.bg_radius_gray_linear_12));
247 248
    }
}