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

3
import android.os.Build;
4
import android.view.View;
5 6
import android.widget.TextView;

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

10
import com.google.gson.Gson;
11 12 13 14 15 16
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;
17
import com.ydl.ydlcommon.data.http.RxUtils;
18
import com.yidianling.common.tools.LogUtil;
19
import com.yidianling.im.R;
20
import com.yidianling.im.event.MultipleAnswerBean;
21
import com.yidianling.im.event.MultipleSelectedEvent;
22
import com.yidianling.im.event.OthersState;
23 24
import com.yidianling.im.http.ImRetrofitApi;
import com.yidianling.im.router.ImIn;
25
import com.yidianling.im.session.extension.CustomAttachLingxiAnswerQuestion;
26
import com.yidianling.im.session.extension.CustomAttachLingxiWhichQuestion;
27
import com.yidianling.im.session.viewholder.adapter.AdapterLingxiMultipleChoice;
28 29 30
import com.yidianling.im.session.viewholder.bean.AnswerBean;
import com.yidianling.im.session.viewholder.bean.AnswersItem;
import com.yidianling.im.session.viewholder.bean.LingxiAnswerBean;
31
import com.yidianling.im.session.viewholder.bean.QuestionItemBean;
32 33 34 35 36 37
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;
38 39 40
import java.util.HashMap;
import java.util.List;
import java.util.Map;
41

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

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

54 55
    private AdapterLingxiMultipleChoice adapterLingxiMultipleChoice;

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

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

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

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

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

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

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

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

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

            @Override
            public void onException(Throwable exception) {
145
                LogUtil.e("获取消息exception:" + exception.getLocalizedMessage());
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
    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
175
                            if (questionList.get(i).getName().equals(multipleAnswers.get(j))) {
176 177 178 179
                                questionList.get(i).setSelected(true);
                            }
                        }
                    }
霍志良 committed
180 181 182 183 184 185 186 187 188 189 190 191 192
                    if (multipleAnswers.size()>0){
                        confirm_multiple_choice.setEnabled(true);
                        confirm_multiple_choice.setText(String.format("确定(%d)", multipleAnswers.size()));
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            confirm_multiple_choice.setBackground(context.getResources().getDrawable(R.drawable.im_background_18dp_1da1f2));
                        }
                    }else{
                        confirm_multiple_choice.setEnabled(false);
                        confirm_multiple_choice.setText("确定");
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                            confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
                        }
                    }
193 194 195 196
                }

            } else {
                //可点击
霍志良 committed
197 198 199 200 201 202 203
                if (!singleOrMultipletype.contains("radio")){
                    confirm_multiple_choice.setEnabled(false);
                    confirm_multiple_choice.setText("确定");
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
                    }
                }
204 205 206 207 208 209
            }
        } catch (Exception e) {
            LogUtil.e("aaaaa" + e.getLocalizedMessage());
        }
    }

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

241
    }
242

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

267

268 269 270
    /*
     * 用户回答问题接口
     * */
271
    private void answerLingxiQuestion(String examID, String questionPaperId, String uid, String questionId, ArrayList<AnswersItem> answersItems) {
霍志良 committed
272
        String str = new Gson().toJson(new LingxiAnswerBean(new AnswerBean(examID, questionPaperId, uid, questionId, answersItems), uid, "56", message.getFromAccount()));
273 274 275 276 277 278
        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 -> {
279
                            LogUtil.e("answerQuestion" + it.toString());
280 281 282 283
                        }, throwable ->
                                LogUtil.e(throwable.getMessage())
                );
    }
284 285


286
    @Override
287
    public void onMultipleItemClick(@NotNull QuestionItemBean string, int position) {
288 289 290 291 292 293 294 295 296 297 298 299
        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());
300
                            break;
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                        }
                    }
                } 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);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    confirm_multiple_choice.setBackground(context.getResources().getDrawable(R.drawable.im_background_18dp_1da1f2));
                }
            } else {
                confirm_multiple_choice.setEnabled(false);
                confirm_multiple_choice.setText("确定");
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    confirm_multiple_choice.setBackground(context.getDrawable(R.drawable.im_background_18dp_ebebeb));
                }
            }
霍志良 committed
378
            stringBuffer.delete(3, stringBuffer.length());
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
            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);
        }
394
    }
395

396
    private void updateLocalMsg(Integer position, ArrayList<String> selectedPosition) {
397 398
        //更新本地消息
        Map<String, Object> map = message.getLocalExtension();
399
        map = new HashMap();
400
        map.put("isSure", true);
401
        if (position != null) {
霍志良 committed
402 403
            map.put("position", position);
        }
404
        if (selectedPosition != null) {
霍志良 committed
405 406
            map.put("multiple", selectedPosition);
        }
407
        message.setLocalExtension(null);
408 409
        message.setLocalExtension(map);
        NIMClient.getService(MsgService.class).updateIMMessage(message);
霍志良 committed
410 411
        //通知ui刷新
        getMsgAdapter().updateItemAtLocalExtension(message);
412
    }
413
}