admin
2021-06-05 ddff7888bf7e754d12fb5fc85a58f3012f456490
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
58
59
60
61
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
89
90
91
92
93
94
95
96
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package com.tejia.lijin.app.ui.main;
 
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.tejia.lijin.app.ShoppingApplication;
import com.wpc.library.content.ConnectivityChangeHelper;
import com.wpc.library.recyclerviewhelper.DividerItemDecoration;
import com.wpc.library.util.NetUtils;
import com.wpc.library.util.cache.DiskLruCache;
import com.wpc.library.util.common.DimenUtils;
import com.wpc.library.util.common.StorageUtils;
import com.wpc.library.widget.MyListView;
import com.tejia.lijin.app.BasicTextHttpResponseHandler;
import com.tejia.lijin.app.R;
import com.tejia.lijin.app.ShoppingApi;
import com.tejia.lijin.app.callBack.MessagePush;
import com.tejia.lijin.app.contract.MessgeContract;
import com.tejia.lijin.app.entity.CommonList;
import com.tejia.lijin.app.entity.MainMessage;
import com.tejia.lijin.app.entity.UserMessage;
import com.tejia.lijin.app.presenter.MessgePresenter;
import com.tejia.lijin.app.ui.dialog.RedPacketHintDialog;
import com.tejia.lijin.app.ui.message.AppMailActivity;
import com.tejia.lijin.app.ui.message.IDeleteMessageListener;
import com.tejia.lijin.app.ui.message.MainMessageAdapter;
import com.tejia.lijin.app.ui.message.SystemMessageActivity;
import com.tejia.lijin.app.ui.message.UserMessageActivity;
import com.tejia.lijin.app.ui.recommend.MessagelistAdapter;
import com.tejia.lijin.app.util.umengCustomEvent.MessageCustomEvent;
import com.tejia.lijin.app.util.user.LoginAndInviteStatusUtil;
import com.tejia.lijin.app.util.user.UserUtil;
import com.tejia.lijin.app.view.BaseFragment;
 
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
 
/**
 * 消息页面
 */
public class MessageFragment extends BaseFragment<MessgeContract.MessgeView, MessgePresenter> implements MessgeContract.MessgeView, View.OnClickListener, MessagePush {
 
    private TextView tv_middle, tv_top_bar_right, tv_no_data_hint;
    private ImageView iv_top_bar_clear;
    private SwipeRefreshLayout srl_message;
    private RecyclerView rv_message;
 
    private View bottom;
    private ImageView iv_loading, iv_no_data;
    private TextView tv_loading;
 
    /*
    消息分类
    */
    private FrameLayout fl_order_notice;
    private FrameLayout fl_capital_notice;
    private FrameLayout fl_account_notice;
    private FrameLayout fl_invite_notice;
    private FrameLayout fl_other_msg;
    private LinearLayout ll_order_notice_num;
    private LinearLayout ll_capital_notice_num;
    private LinearLayout ll_account_notice_num;
    private LinearLayout ll_invite_notice_num;
    private LinearLayout ll_other_msg_num;
    private TextView tv_order_notice_num;
    private TextView tv_capital_notice_num;
    private TextView tv_account_notice_num;
    private TextView tv_invite_notice_num;
    private TextView tv_other_msg_num;
    private LinearLayout ll_critical_notice;//系统消息布局
    private ImageView iv_critical_msg_img;
    private ImageView iv_critical_msg_grade;
    private TextView tv_critical_msg_title;
    private TextView tv_critical_msg_time;
    private TextView tv_critical_msg_content;
    private TextView iv_red_dot;//系统消息红点
    //公告
    private LinearLayoutCompat ll_critical_announce;//公告布局
    private TextView ll_critical_announce_title;//标题
    private TextView ll_critical_announce_time;//时间
    private TextView ll_critical_announce_content;//公告内容
    private ImageView ll_critical_announce_reddot;//红点
    private ImageView ll_critical_announce_img;//公告图片
    //Topview listview
    private MyListView ll_critical_list;//listview
 
 
    private boolean isLoad = false;
    public int mPage = 1;
    public int count = 0;
    List<MainMessage> mList;
    MainMessageAdapter adapter;
 
    private ConnectivityChangeHelper mChangeHelper;
    private LinearLayout ll_no_net, ll_no_data, ll_request_failture;
 
    private SharedPreferences sp;
    private SharedPreferences spkefu;
    private boolean isChoice = false;//表示已经点击过按钮
    private int iscategory = 0;//表示 选择的按钮类型
    private MessagelistAdapter messagelistAdapter;
    private DiskLruCache cache;
 
 
    @Override
    protected int initFragmentView() {
        return R.layout.fragment_message;
    }
 
    @Override
    protected void initFragmentChildView(View view) {
        try {
            cache = DiskLruCache.open(
                    new File(StorageUtils.getCacheDirectory(getContext())
                            .toString(), "http"), getVersionNum(getContext()),
                    1, 1024 * 1024);
        } catch (IOException e) {
            e.printStackTrace();
        }
        initView(view);
        addListener();
        spkefu = getActivity().getSharedPreferences("kefurecord", Context.MODE_PRIVATE);//是否打开过客服  记录
        if (UserUtil.isLogin(getContext())) {//登陆显示消息中心红包弹窗
            getMSGConfig();//消息中心红包
        }
    }
 
    @Override
    protected void initFragmentData(Bundle savedInstanceState) {
 
    }
 
    @Override
    protected MessgePresenter createPresenter() {
        return new MessgePresenter(getActivity());
    }
 
 
    private int getVersionNum(Context context) {
        try {
            PackageInfo pi = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return 1;
        }
    }
 
    private void initView(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getActivity().getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            int result = 0;
            int resourceId = getActivity().getResources().getIdentifier("status_bar_height",
                    "dimen", "android");
            if (resourceId > 0) {
                result = getActivity().getResources().getDimensionPixelSize(resourceId);
            }
            //设置状态栏文字颜色及图标为深色
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    result);
            view.findViewById(R.id.v_status_bar).setLayoutParams(params);
        } else {
            view.findViewById(R.id.v_status_bar).setVisibility(View.GONE);
        }
        tv_middle = view.findViewById(R.id.tv_top_bar_middle);
        iv_top_bar_clear = view.findViewById(R.id.iv_top_bar_clear);
        tv_top_bar_right = view.findViewById(R.id.tv_top_bar_right);
        srl_message = view.findViewById(R.id.srl_message);
        rv_message = view.findViewById(R.id.rv_message);
        srl_message.setColorSchemeColors(getResources().getColor(R.color.theme));
        rv_message.setLayoutManager(new LinearLayoutManager(rv_message.getContext()));
        DividerItemDecoration decoration = new DividerItemDecoration();
        decoration.setSize(DimenUtils.dip2px(rv_message.getContext(), 5));
        rv_message.addItemDecoration(decoration);
 
        mList = new ArrayList<>();
        adapter = new MainMessageAdapter(rv_message.getContext(), mList, new IDeleteMessageListener() {
            @Override
            public void delete(final UserMessage msg) {
                if (LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true)) {
                    ShoppingApi.deleteMsg(getContext(), UserUtil.getUid(getContext()) , msg.getType(), msg.getId(), new BasicTextHttpResponseHandler() {
                        @Override
                        public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws Exception {
                            super.onSuccessPerfect(statusCode, headers, jsonObject);
                            if (jsonObject.optInt("code") == 0) {
                                for (int i = 0; i < mList.size(); i++) {
                                    if (mList.get(i).getUserMsg() != null && msg.getId().equalsIgnoreCase(mList.get(i).getUserMsg().getId()) && msg.getType().equalsIgnoreCase(mList.get(i).getUserMsg().getType())) {
                                        mList.remove(i);
                                        adapter.notifyDataSetChanged();
                                        Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                                        break;
                                    }
 
                                }
 
                            } else {
                                Toast.makeText(getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
                            }
                        }
 
                        @Override
                        public void onFailure(int statusCode, Header[] headers, String jsonObject, Throwable e) {
                            super.onFailure(statusCode, headers, jsonObject, e);
                            Toast.makeText(getContext(), "网络连接出错", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
 
            }
        });
        rv_message.setAdapter(adapter);
 
        bottom = LayoutInflater.from(rv_message.getContext()).inflate(R.layout.item_recyclerview_bottom, null);
        tv_loading = bottom.findViewById(R.id.tv_loading);
        iv_loading = bottom.findViewById(R.id.iv_loading);
        AnimationDrawable animationDrawable = (AnimationDrawable) getResources().getDrawable(
                R.drawable.anim_list_loading);
        iv_loading.setImageDrawable(animationDrawable);
        animationDrawable.start();
//        Glide.with(this).load(R.drawable.gif_loading).into(iv_loading);
        bottom.setVisibility(View.GONE);
        View top = LayoutInflater.from(rv_message.getContext()).inflate(R.layout.item_message_top, null);
        fl_order_notice = top.findViewById(R.id.fl_order_notice);
        fl_capital_notice = top.findViewById(R.id.fl_capital_notice);
        fl_account_notice = top.findViewById(R.id.fl_account_notice);
        fl_invite_notice = top.findViewById(R.id.fl_invite_notice);
        fl_other_msg = top.findViewById(R.id.fl_other_msg);
        ll_order_notice_num = top.findViewById(R.id.ll_order_notice_num);
        ll_capital_notice_num = top.findViewById(R.id.ll_capital_notice_num);
        ll_account_notice_num = top.findViewById(R.id.ll_account_notice_num);
        ll_invite_notice_num = top.findViewById(R.id.ll_invite_notice_num);
        ll_other_msg_num = top.findViewById(R.id.ll_other_msg_num);
        tv_order_notice_num = top.findViewById(R.id.tv_order_notice_num);
        tv_capital_notice_num = top.findViewById(R.id.tv_capital_notice_num);
        tv_account_notice_num = top.findViewById(R.id.tv_account_notice_num);
        tv_invite_notice_num = top.findViewById(R.id.tv_invite_notice_num);
        tv_other_msg_num = top.findViewById(R.id.tv_other_msg_num);
        ll_critical_notice = top.findViewById(R.id.ll_critical_notice);
        iv_critical_msg_img = top.findViewById(R.id.iv_critical_msg_img);
        iv_critical_msg_grade = top.findViewById(R.id.iv_critical_msg_grade);
        tv_critical_msg_title = top.findViewById(R.id.tv_critical_msg_title);
        tv_critical_msg_time = top.findViewById(R.id.tv_critical_msg_time);
        tv_critical_msg_content = top.findViewById(R.id.tv_critical_msg_content);
        iv_red_dot = top.findViewById(R.id.iv_red_dot);
        ll_critical_notice.setVisibility(View.GONE);//系统消息
        ll_critical_list = top.findViewById(R.id.ll_critical_list);
        ll_critical_list.getFocusedChild();
        //公告
        ll_critical_announce = top.findViewById(R.id.ll_critical_announce);//公告布局
        ll_critical_announce_title = top.findViewById(R.id.ll_critical_announce_title);//标题
        ll_critical_announce_time = top.findViewById(R.id.ll_critical_announce_time);//时间
        ll_critical_announce_content = top.findViewById(R.id.ll_critical_announce_content);//公告内容
        ll_critical_announce_reddot = top.findViewById(R.id.ll_critical_announce_reddot);//红点
        ll_critical_announce_img = top.findViewById(R.id.ll_critical_announce_img);//公告图片
        ll_critical_announce.setOnClickListener(this);
        //------------------------------------------
        adapter.addHeaderView(top);
        adapter.addFooterView(bottom);
        //注册 eventbus
//        EventBus.getDefault().register(this);
        //注册 主页消息推送传播接口
        ((MainActivity) getActivity()).setMessagePush(this);
 
        sp = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        ll_no_net = view.findViewById(R.id.ll_no_net);
        ll_no_data = view.findViewById(R.id.ll_no_data);
        ll_request_failture = view.findViewById(R.id.ll_request_failture);
        tv_no_data_hint = view.findViewById(R.id.tv_no_data_hint);
        iv_no_data = view.findViewById(R.id.iv_no_data);
        view.findViewById(R.id.tv_net_setting).setOnClickListener(this);
        view.findViewById(R.id.tv_refresh).setOnClickListener(this);
 
        //
        messagelistAdapter = new MessagelistAdapter((MainActivity) getActivity(), commList);
        ll_critical_list.setAdapter(messagelistAdapter);
        ll_critical_list.setDividerHeight(0);
 
        tv_middle.setText("消息");
        tv_middle.setTextSize(22);
        tv_middle.getPaint().setFakeBoldText(true);
        tv_middle.setTextColor(getResources().getColor(R.color.theme));
        getCache();
    }
 
    /**
     * 获取缓存
     */
    private void getCache() {
        mPresenter.getCaChe(cache, mPage, mList, adapter, ll_critical_notice, tv_critical_msg_title,
                tv_critical_msg_time, tv_critical_msg_content, iv_red_dot, iv_critical_msg_img, iv_critical_msg_grade, new MessgeContract.onCommonListCallback() {
                    @Override
                    public void onSuccess(JSONArray mJsonArray) throws JSONException {
                        if (mJsonArray != null)
                            setMsgdeal(mJsonArray);//推送消息处理 客服/推荐记录/官宣/小助手
                    }
                });
    }
 
    private void addListener() {
        fl_order_notice.setOnClickListener(this);
        fl_capital_notice.setOnClickListener(this);
        fl_account_notice.setOnClickListener(this);
        fl_invite_notice.setOnClickListener(this);
        fl_other_msg.setOnClickListener(this);
        ll_critical_notice.setOnClickListener(this);
        tv_top_bar_right.setOnClickListener(this);
        iv_top_bar_clear.setOnClickListener(this);
 
        mChangeHelper = new ConnectivityChangeHelper(rv_message.getContext(),
                new ConnectivityChangeHelper.OnConnectivityChangeListener() {
 
                    @Override
                    public void onNetworkUnAvailable() {
                        if (mList.size() == 0)
                            requestState(3);
                    }
 
                    @Override
                    public void onNetworkAvailable() {
                        requestState(0);
                        if (mList.size() == 0) {
                            getMessage();
                            getUnReadMsgCount();
                        }
                    }
                });
        srl_message.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {//下拉刷新
            @Override
            public void onRefresh() {
                mPage = 1;
                ((MainActivity) getActivity()).setmPage(mPage);
                srl_message.setRefreshing(true);
                getMessage();
                getUnReadMsgCount();
            }
        });
 
        rv_message.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int last = manager.findLastVisibleItemPosition();
                int total = manager.getItemCount();
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (last == total - 1 && mList.size() < count && !isLoad) {
                        mPage++;
                        ((MainActivity) getActivity()).setmPage(mPage);
                        isLoad = true;
                        iv_loading.setVisibility(View.VISIBLE);
                        tv_loading.setText("正在加载更多是数据");
                        getMessage();
                    }
                }
            }
 
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, final int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
 
    @Override
    public void onResume() {
        super.onResume();
        mChangeHelper.registerReceiver();
        getMessage();
        getUnReadMsgCount();
        boolean isLogin = UserUtil.isLogin(getContext());
        if (!isLogin) { //判断未登陆  隐藏清空按钮和 去旧版
            if (iv_top_bar_clear.getVisibility() == View.VISIBLE | tv_top_bar_right.getVisibility() == View.VISIBLE) {
                iv_top_bar_clear.setVisibility(View.GONE);
                tv_top_bar_right.setVisibility(View.GONE);
            }
        } else { //登陆显示清空按钮和 去旧版
            if (iv_top_bar_clear.getVisibility() == View.GONE | tv_top_bar_right.getVisibility() == View.GONE) {
                iv_top_bar_clear.setVisibility(View.VISIBLE);
                tv_top_bar_right.setVisibility(View.INVISIBLE);
            }
            IsSkipActivity();//判断登陆跳转 是否是点击按钮后再登陆 如是则跳转到相应的页面
        }
        isChoice = false;
        iscategory = 0;
    }
 
    //判断是否点击 订单 资金 账号 邀请 其他按钮 点击后 则跳转到相应的页面
    private void IsSkipActivity() {
        if (isChoice) {
            if (iscategory == 1001) {//订单
                fl_order_notice.performClick();
            } else if (iscategory == 1002) {//资金
                fl_capital_notice.performClick();
            } else if (iscategory == 1003) {//账号
                fl_account_notice.performClick();
            } else if (iscategory == 1004) {//邀请
                fl_invite_notice.performClick();
            } else if (iscategory == 1005) {//其他
                fl_other_msg.performClick();
            }
        }
    }
 
    @Override
    public void onPause() {
        super.onPause();
        mChangeHelper.unregisterReceiver();
//        Log.e("mResult", "消息页面onPause");
    }
 
 
    /**
     * 获取主页消息列表
     */
    private void getMessage() {
        final boolean isLogin =UserUtil.isLogin(getContext());
        ((MainActivity) getActivity()).getMessageMain(new MainActivity.getHomeMsgListListener() {
            @Override
            public void onSuccessPerfect(int statusCode, Header[] headers, JSONObject jsonObject) throws JSONException, ParseException {
                if (jsonObject.optInt("code") == 0) {
                    Gson gson = new GsonBuilder().serializeNulls().create();
                    List<MainMessage> list = gson.fromJson(jsonObject.optJSONObject("data").optJSONArray("list").toString(), new TypeToken<List<MainMessage>>() {
                    }.getType());
                    if (mPage == 1) {
                        mList.clear();
                        setMsgdeal(jsonObject.optJSONObject("data").optJSONArray("commonList"));//处理 推送消息
                        setAnnounce(jsonObject);//处理公告消息
                    }
//                    adapter.notifyItemRangeChanged(adapter.getItemCount(), list.size());//倒序批量刷新列表
                    //处理系统消息-列表信息
                    mPresenter.setMessage(mPage, jsonObject, ll_critical_notice, isLogin, iv_critical_msg_img, iv_critical_msg_grade, tv_critical_msg_title, tv_critical_msg_time, tv_critical_msg_content, iv_red_dot);
                    count = Integer.parseInt(jsonObject.optJSONObject("data").optString("count"));
                    mList.addAll(list);
                    adapter.notifyDataSetChanged();
 
                    if (mList.size() == count) {
                        bottom.setVisibility(View.VISIBLE);
                        iv_loading.setVisibility(View.GONE);
                        tv_loading.setText(getResources().getString(R.string.list_no_more));
                    }
//                    requestState(mList.size() == 0 ? 1 : 0);
                } else {
                    if (mList.size() == 0)
                        requestState(2);
                }
            }
 
            @Override
            public void onFinish() {
                isLoad = false;
                srl_message.setRefreshing(false);
            }
 
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                if (mList.size() == 0) {
                    if (NetUtils.getNetworkState(rv_message.getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        requestState(3);
                    } else {
                        requestState(2);
                    }
                } else {
                    if (NetUtils.getNetworkState(rv_message.getContext()).equalsIgnoreCase(NetUtils.NETWORK_NONE)) {
                        Toast.makeText(rv_message.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(rv_message.getContext(), "网络连接异常,请检测网络设置", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }, true);//继续请求 消息列表
    }
 
    /**
     * 处理公告消息
     *
     * @param jsonObject
     */
    private void setAnnounce(JSONObject jsonObject) {
        final JSONObject notifyMsg = jsonObject.optJSONObject("data").optJSONObject("notifyMsg");
        if (notifyMsg != null) {
            ll_critical_announce.setVisibility(View.VISIBLE);//显示 公告布局
            //处理公告消息
            mPresenter.setAnnounce(notifyMsg, ll_critical_announce, ll_critical_announce_title, ll_critical_announce_time, ll_critical_announce_content
                    , ll_critical_announce_img, ll_critical_announce_reddot);
        } else {
            ll_critical_announce.setVisibility(View.GONE);//隐藏 公告布局
        }
    }
 
    private List<CommonList> commList = new ArrayList<>();
 
    /**
     * 推送消息处理 客服/推荐记录/官宣/小助手
     */
    public void setMsgdeal(JSONArray array) throws JSONException {
        if (array != null && array.length() > 0) {
            if (commList != null && commList.size() > 0) {
                commList.clear();
            }
            Gson gson = new Gson();
            for (int i = 0; i < array.length(); i++) {
                JSONObject object = (JSONObject) array.get(i);
                CommonList user = gson.fromJson(object.toString(), CommonList.class);
                if (user.getType().equals("kefu") && user.getLatestMsg().equals("")) {
                    user.setLatestMsg("[点击打开人工客服]");
                }
                commList.add(user);
            }
            messagelistAdapter.notifyDataSetChanged();
        }
 
    }
 
    /**
     * 获取消息未读数量
     */
    private void getUnReadMsgCount() {
        mPresenter.getUnReadMsgCount(UserUtil.getUid(ShoppingApplication.application));
    }
 
    /**
     * 一键清空未读消息
     */
    private void setAllMsgRead() {
        mPresenter.setAllMsgRead(UserUtil.getUid(ShoppingApplication.application));
    }
 
    @Override
    public void onClick(View v) {
        boolean isLogin =UserUtil.isLogin(getContext());
        switch (v.getId()) {
            case R.id.fl_order_notice: {
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
 
                if (!isLogin) {//判断登陆
                    isChoice = true;//代表点击
                    iscategory = 1001;
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
//                MessageCustomEvent.msgOrder(rv_message.getContext());
                Intent intent = new Intent(rv_message.getContext(), UserMessageActivity.class);
                intent.putExtra("title", "订单消息");
                intent.putExtra("type", "order");
                startActivity(intent);
                break;
            }
            case R.id.fl_capital_notice: {
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
                if (!isLogin) {//判断登陆
                    isChoice = true;//代表点击
                    iscategory = 1002;
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
                Intent intent = new Intent(rv_message.getContext(), UserMessageActivity.class);
                intent.putExtra("title", "资金消息");
                intent.putExtra("type", "money");
                startActivity(intent);
                break;
            }
            case R.id.fl_account_notice: {
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
                if (!isLogin) {//判断登陆
                    isChoice = true;//代表点击
                    iscategory = 1003;
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
                Intent intent = new Intent(rv_message.getContext(), UserMessageActivity.class);
                intent.putExtra("title", "账号消息");
                intent.putExtra("type", "account");
                startActivity(intent);
                break;
            }
            case R.id.fl_invite_notice: {
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
                if (!isLogin) {//判断登陆
                    isChoice = true;//代表点击
                    iscategory = 1004;
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
                Intent intent = new Intent(rv_message.getContext(), UserMessageActivity.class);
                intent.putExtra("title", "邀请消息");
                intent.putExtra("type", "invite");
                startActivity(intent);
                break;
            }
            case R.id.fl_other_msg: {
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
                if (!isLogin) {//判断登陆
                    isChoice = true;//代表点击
                    iscategory = 1005;
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
                Intent intent = new Intent(rv_message.getContext(), UserMessageActivity.class);
                intent.putExtra("title", "其它消息");
                intent.putExtra("type", "other");
                startActivity(intent);
                break;
            }
            case R.id.ll_critical_notice://系统消息
                if (!LoginAndInviteStatusUtil.acessNext(getContext(), UserUtil.getUid(getContext()) , true))
                    return;
                if (!isLogin) {//判断登陆
                    UserUtil.jumpLogin(fl_order_notice.getContext());
                    break;
                }
                MessageCustomEvent.msgItemClick(rv_message.getContext(), "系统消息");
                startActivity(new Intent(rv_message.getContext(), SystemMessageActivity.class));
                break;
            case R.id.tv_top_bar_right:
                MessageCustomEvent.msgOld(rv_message.getContext());
                startActivity(new Intent(rv_message.getContext(), AppMailActivity.class));
                break;
            case R.id.tv_net_setting:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.tv_refresh:
                startActivity(new Intent(Settings.ACTION_SETTINGS));
                break;
            case R.id.iv_top_bar_clear:
                MessageCustomEvent.msgClearNotify(rv_message.getContext());
                setAllMsgRead();
                break;
            //点击公告
            case R.id.ll_critical_announce:
 
                break;
 
            default:
                break;
        }
    }
 
    /**
     * 请求状态 0 数据正常展示;1 返回数据为空;2 网络请求失败;3 没有连接网络
     *
     * @param state
     */
    private void requestState(int state) {
        srl_message.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        rv_message.setVisibility(state == 0 ? View.VISIBLE : View.GONE);
        ll_no_data.setVisibility(state == 1 ? View.VISIBLE : View.GONE);
        ll_request_failture.setVisibility(state == 2 ? View.VISIBLE : View.GONE);
        ll_no_net.setVisibility(state == 3 ? View.VISIBLE : View.GONE);
    }
 
 
    @Override
    public void onDestroyView() {
        super.onDestroyView();
//        Log.e("mResult", "消息页面onDestroyView");
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
//        Log.e("mResult", "消息页面onDestroy");
//        messageReceiver = null;
//        EventBus.getDefault().unregister(this);
        //注册 主页消息推送传播接口
        ((MainActivity) getActivity()).setMessagePush(null);
    }
 
    private boolean accountLogin = false;
    private JSONObject floatImgDetail;
 
    /**
     * 消息中心红包
     */
    private void getMSGConfig() {
        mPresenter.getMSGConfig(UserUtil.getUid(ShoppingApplication.application));
    }
 
    /**
     * 展示红包等级福利提示Dialog
     */
    private void showHintDialog(RedPacketHintDialog.Builder builder, final JSONObject floatImgDetail) {
        final boolean isLogin = UserUtil.isLogin(getContext());
        mPresenter.showHintDialog(builder, floatImgDetail, accountLogin, isLogin);
    }
 
 
    /**
     * 获取未读消息条数
     *
     * @param jsonObject
     */
    @Override
    public void onUnReadMsgCount(JSONObject jsonObject) {
        srl_message.setRefreshing(false);
        if (jsonObject.optInt("code") == 0) {
            mPresenter.setUnReadMsgCount(jsonObject, tv_order_notice_num, tv_capital_notice_num, tv_account_notice_num, tv_invite_notice_num, tv_other_msg_num
                    , ll_order_notice_num, ll_capital_notice_num, ll_account_notice_num, ll_invite_notice_num, ll_other_msg_num, iv_red_dot);
            ((MainActivity) getActivity()).setUnReadMsgCount(jsonObject);
        }
    }
 
    /**
     * 删除所有未读消息条数
     *
     * @param jsonObject
     */
    @Override
    public void onAllMsgReadSuccess(JSONObject jsonObject) {
        if (jsonObject.optInt("code") == 0) {
            getUnReadMsgCount();
            Toast.makeText(rv_message.getContext(), "消息全部已读", Toast.LENGTH_SHORT).show();
        } else {
            if (!spkefu.getString("read", "false").equals("false")) {
                Toast.makeText(rv_message.getContext(), jsonObject.optString("msg"), Toast.LENGTH_SHORT).show();
            }
        }
        int i = 0;
        for (CommonList cmlist : commList) {
            if (cmlist.getType().equals("kefu")) {
                commList.get(i).setRead(true);
                break;
            }
            i++;
        }
        // 设置 客服未读消息已读
        SharedPreferences.Editor editor = spkefu.edit();
        editor.putString("read", "true");//是否已读
        editor.apply();
        messagelistAdapter.notifyDataSetChanged();
        ((MainActivity) getActivity()).setclear(true);
    }
 
    /**
     * 消息中心红包
     *
     * @param jsonObject
     */
    @Override
    public void onMSGConfigSuccess(JSONObject jsonObject) {
        if (jsonObject.optInt("code") == 0) {
            SharedPreferences sp = getContext().getSharedPreferences("user", Context.MODE_PRIVATE);
            floatImgDetail = jsonObject.optJSONObject("data").optJSONObject("floatImgDetail");
            //底部网页链接
            if (floatImgDetail != null) {
                accountLogin = floatImgDetail.optBoolean("accountLogin");
                String imgUrl = floatImgDetail.optString("img");
                JSONObject jumpDetail = floatImgDetail.optJSONObject("jumpDetail");
                RedPacketHintDialog.Builder builder = new RedPacketHintDialog.Builder(getActivity());
                builder.imgUrl(imgUrl);
                if (jumpDetail == null) {
                    mPresenter.setMSGConfig_jumpDetailnull(builder);
                } else {
                    String showTime = floatImgDetail.optString("showTime");
                    final boolean isLogin = UserUtil.isLogin(getContext());
                    //是否是当天当月当年
                    if (showTime.equalsIgnoreCase("everyday") && mPresenter.isSameDay(new Date(System.currentTimeMillis()), new Date(sp.getLong("redPacketHintmsg", 0l)))) {
                        return;
                    }
                    //记录打开时间
                    if (showTime.equalsIgnoreCase("everyday")) {
                        SharedPreferences.Editor editor1 = sp.edit();
                        editor1.putLong("redPacketHintmsg", System.currentTimeMillis());
                        editor1.commit();
                    }
                    //百川打开
                    if (jumpDetail.optString("type").equalsIgnoreCase("baichuan!")) {
                        mPresenter.setMSGConfig_baichuan(floatImgDetail, accountLogin, isLogin, imgUrl, builder);
                    } else {//跳转其他activity
                        showHintDialog(builder, floatImgDetail);
                    }
                }
            }
        }
    }
 
    /**
     * 推送消息  接受方法 从新请求一次消息未读数量
     */
    @Override
    public void MessagePush(boolean b) {
        if (b) {
            mPage = 1;
            ((MainActivity) getActivity()).setmPage(mPage);
            srl_message.setRefreshing(true);
            getMessage();
            getUnReadMsgCount();
        }
    }
 
 
}