MsgViewHolderLingxiWhichQuestion.java 19 KB
Newer Older
1 2
package com.yidianling.im.session.viewholder;

3
import android.view.View;
4 5
import android.widget.TextView;

6 7 8
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

9
import com.google.gson.Gson;
10 11 12 13 14 15
import com.netease.nimlib.sdk.NIMClient;
import com.netease.nimlib.sdk.RequestCallback;
import com.netease.nimlib.sdk.msg.MsgService;
import com.netease.nimlib.sdk.msg.attachment.MsgAttachment;
import com.netease.nimlib.sdk.msg.constant.SessionTypeEnum;
import com.netease.nimlib.sdk.msg.model.IMMessage;
16
import com.ydl.ydlcommon.data.http.RxUtils;
17
import com.yidianling.common.tools.LogUtil;
18
import com.yidianling.im.R;
19
import com.yidianling.im.event.MultipleAnswerBean;
20
import com.yidianling.im.event.MultipleSelectedEvent;
21
import com.yidianling.im.event.OthersState;
22 23
import com.yidianling.im.http.ImRetrofitApi;
import com.yidianling.im.router.ImIn;
24
import com.yidianling.im.session.extension.CustomAttachLingxiAnswerQuestion;
25
import com.yidianling.im.session.extension.CustomAttachLingxiWhichQuestion;
26
import com.yidianling.im.session.viewholder.adapter.AdapterLingxiMultipleChoice;
27 28 29
import com.yidianling.im.session.viewholder.bean.AnswerBean;
import com.yidianling.im.session.viewholder.bean.AnswersItem;
import com.yidianling.im.session.viewholder.bean.LingxiAnswerBean;
30
import com.yidianling.im.session.viewholder.bean.QuestionItemBean;
31 32 33 34 35 36
import com.yidianling.nimbase.common.ui.recyclerview.adapter.BaseMultiItemFetchLoadAdapter;
import com.yidianling.uikit.business.session.viewholder.MsgViewHolderBase;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
37 38 39
import java.util.HashMap;
import java.util.List;
import java.util.Map;
40

41
import de.greenrobot.event.EventBus;
42 43 44 45 46
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
47

48
public class MsgViewHolderLingxiWhichQuestion extends MsgViewHolderBase implements AdapterLingxiMultipleChoice.OnMultiplelItemClickListener {
49
    private RecyclerView recycleviewMultipleChoice;
50
    private TextView confirm_multiple_choice;
51
    private TextView multiple_choice_title;
52

53 54
    private AdapterLingxiMultipleChoice adapterLingxiMultipleChoice;

55
    ArrayList<QuestionItemBean> questionList = new ArrayList<QuestionItemBean>();
56
    Boolean isSingleChoice = false;
57
    CustomAttachLingxiWhichQuestion customAttachLingxiWhichQuestion;
58 59
    private StringBuffer stringBuffer = new StringBuffer("已选:");
    ArrayList<String> selectedStr = new ArrayList<String>();
60
    private Boolean isOthers;
61 62 63 64
    //回答试卷接口
    private String examId;
    private String questionPaperId;
    private String questionId;
65
    private String singleOrMultipletype;
66
    private ArrayList<AnswersItem> answersItems = new ArrayList<>();
67
    Map<String, Object> mapMessageFromCustom;
霍志良 committed
68
    Map map1 = new HashMap();
69 70 71
    private ArrayList<String> multipleAnswers = new ArrayList<>();
    private ArrayList<String> answersItemsMap = new ArrayList<>();

72 73 74 75 76 77
    public MsgViewHolderLingxiWhichQuestion(BaseMultiItemFetchLoadAdapter adapter) {
        super(adapter);
    }

    @Override
    protected int getContentResId() {
78
        customAttachLingxiWhichQuestion = ((CustomAttachLingxiWhichQuestion) message.getAttachment());
79
        singleOrMultipletype = customAttachLingxiWhichQuestion.type;
80
        if (customAttachLingxiWhichQuestion.type != null) {
霍志良 committed
81
            return R.layout.im_nim_lingxi_multiple_choice;
82
        } else {
83
            return R.layout.im_nim_lingxi_multiple_choice;
84
        }
85 86 87 88
    }

    @Override
    protected void inflateContentView() {
89
        recycleviewMultipleChoice = findViewById(R.id.multiple_question_rv);
90
        confirm_multiple_choice = findViewById(R.id.confirm_multiple_choice);
91
        multiple_choice_title = findViewById(R.id.multiple_choice_title);
92 93 94 95
    }

    @Override
    protected void bindContentView() {
96
        avatarRight.setVisibility(View.INVISIBLE);
霍志良 committed
97
        ImIn.INSTANCE.setImMessage(message);
98
        customAttachLingxiWhichQuestion = ((CustomAttachLingxiWhichQuestion) message.getAttachment());
99 100 101
        examId = customAttachLingxiWhichQuestion.examId;
        questionPaperId = customAttachLingxiWhichQuestion.questionPaperId;
        questionId = customAttachLingxiWhichQuestion.id;
102

103
        //您想要咨询下列哪类的问题?(单选)
104 105
        if (customAttachLingxiWhichQuestion.options != null) {
            questionList = (ArrayList<QuestionItemBean>) customAttachLingxiWhichQuestion.options;
106
        }
107
        for (int i = 0; i < questionList.size(); i++) {
108
            questionList.get(i).setSelected(false);
霍志良 committed
109
            if (questionList.get(i).getName().length() > 5) {
110
                isSingleChoice = true;
111 112
            }
        }
113
        if (customAttachLingxiWhichQuestion.type != null) {
114 115
            singleOrMultipletype = customAttachLingxiWhichQuestion.type;
            UpdateFromLocal();
霍志良 committed
116
            initMultipleChoice();
117
        }
118

119
        //进入消息列表时候,保存当type为56的时候的,将ID作为key保存
120
        NIMClient.getService(MsgService.class).queryMessageList(message.getSessionId(), SessionTypeEnum.P2P, 0, 100).setCallback(new RequestCallback<List<IMMessage>>() {
121 122 123
            @Override
            public void onSuccess(List<IMMessage> param) {
                if (param != null && param.size() > 0) {
霍志良 committed
124

125 126 127 128
                    for (IMMessage msg : param) {
                        MsgAttachment attachment = msg.getAttachment();
                        if (attachment instanceof CustomAttachLingxiAnswerQuestion) {
                            CustomAttachLingxiAnswerQuestion customattachlingxianswerquestion = (CustomAttachLingxiAnswerQuestion) attachment;
129 130 131
                            map1.put(customattachlingxianswerquestion.questionId + customattachlingxianswerquestion.examId, true);
                            mapMessageFromCustom = map1;
                            UpdateFromLocalExtention();
132 133 134 135 136 137 138
                        }
                    }
                }
            }

            @Override
            public void onFailed(int code) {
139
                LogUtil.e("获取消息onFailed:" + code);
140 141 142 143
            }

            @Override
            public void onException(Throwable exception) {
144
                LogUtil.e("获取消息exception:" + exception.getLocalizedMessage());
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
    private void UpdateFromLocal() {
        try {
            Map<String, Object> map = message.getLocalExtension();
            if (map != null) {
                Object obj = map.get("isSure");
                Object position = map.get("position");
                Object multiple = map.get("multiple");

                if (obj != null) {
                    boolean b = (boolean) obj;
                    Integer posi = (Integer) position;
                    if (b) {
                        //不可点击
                        if (posi != null) {
                            questionList.get(posi).setSelected(true);
                        }
                    }
                } else {
                    //可点击
                }
                if (multiple != null) {
                    multipleAnswers = (ArrayList<String>) multiple;
                    for (int i = 0; i < questionList.size(); i++) {
                        for (int j = 0; j < multipleAnswers.size(); j++) {
霍志良 committed
174
                            if (questionList.get(i).getName().equals(multipleAnswers.get(j))) {
175 176 177 178
                                questionList.get(i).setSelected(true);
                            }
                        }
                    }
霍志良 committed
179 180 181
                    if (multipleAnswers.size()>0){
                        confirm_multiple_choice.setEnabled(true);
                        confirm_multiple_choice.setText(String.format("确定(%d)", multipleAnswers.size()));
182
                        confirm_multiple_choice.setBackground(context.getResources().getDrawable(R.drawable.im_background_18dp_1da1f2));
霍志良 committed
183 184 185
                    }else{
                        confirm_multiple_choice.setEnabled(false);
                        confirm_multiple_choice.setText("确定");
186
                        confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
霍志良 committed
187
                    }
188 189 190 191
                }

            } else {
                //可点击
霍志良 committed
192 193 194
                if (!singleOrMultipletype.contains("radio")){
                    confirm_multiple_choice.setEnabled(false);
                    confirm_multiple_choice.setText("确定");
195
                    confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
霍志良 committed
196
                }
197 198 199 200 201 202
            }
        } catch (Exception e) {
            LogUtil.e("aaaaa" + e.getLocalizedMessage());
        }
    }

203
    private void UpdateFromLocalExtention() {
204
        try {
霍志良 committed
205
            Map<String, Object> mapMessage = new HashMap<>();
206 207
            if (mapMessageFromCustom != null) {
                mapMessage = mapMessageFromCustom;
208 209
            }
            if (mapMessage != null) {
210
                Object questionPaperId = mapMessage.get(customAttachLingxiWhichQuestion.id + customAttachLingxiWhichQuestion.examId);
霍志良 committed
211 212 213 214
                if (questionPaperId != null) {
                    boolean b = (boolean) questionPaperId;
                    if (b) {
                        //隐藏回答的信息
霍志良 committed
215 216
                        recycleviewMultipleChoice.setVisibility(View.GONE);
                        confirm_multiple_choice.setVisibility(View.GONE);
217
                    } else {
霍志良 committed
218 219 220
                        recycleviewMultipleChoice.setVisibility(View.VISIBLE);
                        confirm_multiple_choice.setVisibility(View.VISIBLE);
                    }
221 222
                } else {
                    if (singleOrMultipletype.contains("radio")) {
霍志良 committed
223
                        recycleviewMultipleChoice.setVisibility(View.VISIBLE);
224
                    } else {
霍志良 committed
225 226
                        recycleviewMultipleChoice.setVisibility(View.VISIBLE);
                        confirm_multiple_choice.setVisibility(View.VISIBLE);
霍志良 committed
227 228
                    }
                }
229 230
            }
        } catch (Exception e) {
231
            LogUtil.e(e.getLocalizedMessage());
232
        }
霍志良 committed
233

234
    }
235

236
    private void initMultipleChoice() {
237
        if (singleOrMultipletype.contains("radio")) {
霍志良 committed
238
            confirm_multiple_choice.setVisibility(View.GONE);
239
        } else {
霍志良 committed
240 241
            confirm_multiple_choice.setVisibility(View.VISIBLE);
        }
242
        confirm_multiple_choice.setOnClickListener(view -> {
243 244 245
            //请求接口发送所选题目,通过发通知到YDLMessageFragment统一回答问题。
            EventBus.getDefault().post(new MultipleSelectedEvent(null, null,
                    new MultipleAnswerBean(examId, questionPaperId, ImIn.INSTANCE.getUserInfo().getUid(),
246
                            questionId, answersItems, message.getFromAccount()), false, false));
247
        });
248 249 250 251 252 253
        multiple_choice_title.setText(customAttachLingxiWhichQuestion.name);
        adapterLingxiMultipleChoice = new AdapterLingxiMultipleChoice();
        adapterLingxiMultipleChoice.setTagData(questionList);
        adapterLingxiMultipleChoice.setItemListener(this);
        GridLayoutManager multipleChoiceManager = new GridLayoutManager(context, 2);
        //当list中字段大于6个显示单行,否则显示双行
254
        multipleChoiceManager.setSpanCount(isSingleChoice ? 1 : 2);
255 256 257 258
        multipleChoiceManager.setOrientation(GridLayoutManager.VERTICAL);
        recycleviewMultipleChoice.setLayoutManager(multipleChoiceManager);
        recycleviewMultipleChoice.setAdapter(adapterLingxiMultipleChoice);
    }
259

260

261 262 263
    /*
     * 用户回答问题接口
     * */
264
    private void answerLingxiQuestion(String examID, String questionPaperId, String uid, String questionId, ArrayList<AnswersItem> answersItems) {
霍志良 committed
265
        String str = new Gson().toJson(new LingxiAnswerBean(new AnswerBean(examID, questionPaperId, uid, questionId, answersItems), uid, "56", message.getFromAccount()));
266 267 268 269 270 271
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), str);
        Disposable subscribe = ImRetrofitApi.Companion.getImRetrofitApi().lingxiAnswerQuestion(body)
                .compose(RxUtils.INSTANCE.resultJavaData())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(it -> {
272
                            LogUtil.e("answerQuestion" + it.toString());
273 274 275 276
                        }, throwable ->
                                LogUtil.e(throwable.getMessage())
                );
    }
277 278


279
    @Override
280
    public void onMultipleItemClick(@NotNull QuestionItemBean string, int position) {
281 282 283 284 285 286 287 288 289 290 291 292
        if (singleOrMultipletype.contains("radio")) {
            /*
             * 单选题:分其他选项和非其他选项
             * 其他选项,唤起输入框
             * 点击非其他选项直接发送答案
             * */
            for (int i = 0; i < questionList.size(); i++) {
                if (questionList.get(i).isSelected()) {
                    if (i == position) {
                        if (questionList.get(i).getName().contains("其他")) {
                            questionList.get(i).setSelected(false);
                            selectedStr.remove(questionList.get(i).getName());
293
                            break;
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
                        }
                    }
                } else {
                    if (i == position) {
                        questionList.get(i).setSelected(true);
                        selectedStr.add(questionList.get(i).getName());
                    } else {
                        questionList.get(i).setSelected(false);
                        if (questionList.get(i).getName().contains("其他")) {
                            selectedStr.remove(questionList.get(i).getName());
                        }
                    }
                }
                questionList.get(i).setSelected(i == position);
            }
            adapterLingxiMultipleChoice.setTagData(questionList);
            for (int i = 0; i < questionList.size(); i++) {
                if (questionList.get(i).isSelected()) {
                    answersItems.clear();
                    answersItems.add(new AnswersItem(questionList.get(position).getId(),
                            questionList.get(position).getName(), "", questionList.get(position).getRelates()));
                    break;
                }
            }
            stringBuffer.delete(2, stringBuffer.length());
            for (int i = 0; i < selectedStr.size(); i++) {
                stringBuffer.append("、").append(selectedStr.get(i));
            }
            isOthers = questionList.get(position).getName().contains("其他");
            if (!isOthers) {
                //请求接口发送所选题目
                answerLingxiQuestion(examId, questionPaperId, ImIn.INSTANCE.getUserInfo().getUid().toString(), questionId, answersItems);
                //更新本地消息
                updateLocalMsg(position, null);
                //发通知取消隐藏输入框表情等符号
                EventBus.getDefault().post(new MultipleSelectedEvent(stringBuffer.toString(), new OthersState(isOthers, questionList.get(position).isSelected()), new MultipleAnswerBean(examId, questionPaperId, ImIn.INSTANCE.getUserInfo().getUid(),
                        questionId, answersItems, message.getFromAccount()), false, true));
            } else {
                EventBus.getDefault().post(new MultipleSelectedEvent(stringBuffer.toString(), new OthersState(isOthers, questionList.get(position).isSelected()), new MultipleAnswerBean(examId, questionPaperId, ImIn.INSTANCE.getUserInfo().getUid(),
                        questionId, answersItems, message.getFromAccount()), false, false));
            }
        } else {
            //多选发送消息
            for (int i = 0; i < questionList.size(); i++) {
                if (questionList.get(i).isSelected()) {
                    if (i == position) {
                        questionList.get(i).setSelected(false);
                        selectedStr.remove(questionList.get(i).getName());
                        for (int j = 0; j < answersItems.size(); j++) {
                            if (questionList.get(i).getName() == answersItems.get(j).getName()) {
                                answersItems.remove(j);
                                break;
                            }
                        }
                    }
                } else {
                    if (i == position) {
                        questionList.get(i).setSelected(true);
                        selectedStr.add(questionList.get(i).getName());
                        answersItems.add(new AnswersItem(questionList.get(position).getId(),
                                questionList.get(position).getName(), "", questionList.get(position).getRelates()));
                    }
                }
            }
            confirm_multiple_choice.setText(String.format("确定(%d)", selectedStr.size()));
            if (selectedStr.size() > 0) {
                confirm_multiple_choice.setEnabled(true);
361
                confirm_multiple_choice.setBackground(context.getResources().getDrawable(R.drawable.im_background_18dp_1da1f2));
362 363 364
            } else {
                confirm_multiple_choice.setEnabled(false);
                confirm_multiple_choice.setText("确定");
365
                confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
366
            }
霍志良 committed
367
            stringBuffer.delete(3, stringBuffer.length());
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
            for (int i = 0; i < selectedStr.size(); i++) {
                stringBuffer.append("、").append(selectedStr.get(i));
            }
            isOthers = questionList.get(position).getName().contains("其他");
            EventBus.getDefault().post(new MultipleSelectedEvent(stringBuffer.toString(), new OthersState(isOthers, questionList.get(position).isSelected()), new MultipleAnswerBean(examId, questionPaperId, ImIn.INSTANCE.getUserInfo().getUid(),
                    questionId, answersItems, message.getFromAccount()), false, false));
            adapterLingxiMultipleChoice.setTagData(questionList);
            answersItemsMap.clear();
            for (int i = 0; i < answersItems.size(); i++) {
                if (!answersItemsMap.contains(answersItems.get(i).getName())) {
                    answersItemsMap.add(i, answersItems.get(i).getName());
                }
            }
            updateLocalMsg(null, answersItemsMap);
        }
383
    }
384

385
    private void updateLocalMsg(Integer position, ArrayList<String> selectedPosition) {
386 387
        //更新本地消息
        Map<String, Object> map = message.getLocalExtension();
388
        map = new HashMap();
389
        map.put("isSure", true);
390
        if (position != null) {
霍志良 committed
391 392
            map.put("position", position);
        }
393
        if (selectedPosition != null) {
霍志良 committed
394 395
            map.put("multiple", selectedPosition);
        }
396
        message.setLocalExtension(null);
397 398
        message.setLocalExtension(map);
        NIMClient.getService(MsgService.class).updateIMMessage(message);
霍志良 committed
399 400
        //通知ui刷新
        getMsgAdapter().updateItemAtLocalExtension(message);
401
    }
402
}