wei
2021-07-01 2835fe50d3e194c21b16fec1c53ff905dd3a3ceb
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
using System;
using Shared;
using HDL_ON.UI.CSS;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
#if DEBUG
using Xamarin.Essentials;
#endif
namespace HDL_ON.UI
{
    /// <summary>
    /// 信息中心页面
    /// </summary>
    public class MessageCenterPage : FrameLayout
    {
        FrameLayout bodyView;
        /// <summary>
        /// 当前
        /// </summary>
        VerticalScrolViewLayout bodyScrolView;
        /// <summary>
        /// 内容为空提示View
        /// </summary>
        FrameLayout emptyTipView;
 
        /// <summary>
        /// menuView
        /// </summary>
        FrameLayout menuView;
        MenuButton allMenuButton;
        MenuButton shareMenuButton;
        MenuButton alarmMenuButton;
        MenuButton systemMenuButton;
        FrameLayout messageView;
 
        /// <summary>
        /// 显示的消息类型
        /// 0全部 1分享与功能 2报警类 3系统信息
        /// </summary>
        int showMesType = 0;
        /// <summary>
        /// 推送消息列表
        /// </summary>
        List<PushMessageInfo> PushMessageInfoList = new List<PushMessageInfo>();
 
        /// <summary>
        /// 页面关闭时间
        /// </summary>
        Action backAction;
 
        public override void RemoveFromParent()
        {
            UnregisterGetPushMessageAction();
            base.RemoveFromParent();
            backAction?.Invoke();
        }
 
        /// <summary>
        /// 
        /// </summary>
        public MessageCenterPage()
        {
            bodyView = this;
            BackgroundColor = CSS_Color.BackgroundColor;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="backAction"></param>
        public void LoadPage(Action backAction)
        {
            this.backAction = backAction;
            new TopViewDiv(bodyView, Language.StringByID(StringId.MessageCenter)).LoadTopView();
 
            AddTopMenuView();
            AddMessageView();
            MenuButtonSelect(showMesType);
            RegisterGetPushMessageAction();
            //测试代码
            //AddEmptyTipView();
            //TestLoad();
        }
 
        /// <summary>
        /// 顶部菜单选项
        /// </summary>
        void AddTopMenuView()
        {
 
            menuView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealWidth(117),
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            bodyView.AddChidren(menuView);
 
 
            #region 全部按钮
            allMenuButton = new MenuButton()
            {
                X = Application.GetRealWidth(15),
                Y = Application.GetRealWidth(16),
            };
            menuView.AddChidren(allMenuButton);
            allMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/AllOn.png";
            allMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/All.png";
            allMenuButton.TextButton.TextID = StringId.All;
            Action allAction = () =>
            {
                MenuButtonSelect(0);
            };
            allMenuButton.SelectAction = allAction;
            //测试提前当前推送ID
            allMenuButton.ImageButton.MouseLongEventHandler = (sender, e) =>
            {
#if DEBUG
 
                CopyToClipboard("推送ID:" + OnAppConfig.Instance.PushId);
#else
                //检测是否获取推送ID成功,提示推送注册是否正常
                if (string.IsNullOrEmpty(OnAppConfig.Instance.PushId))
                {
                    Utlis.ShowTip(Language.StringByID(StringId.PushException));
                }
                else
                {
                    Utlis.ShowTip(Language.StringByID(StringId.PushNormal));
                }
#endif
 
            };
#endregion
 
            #region 分享与功能
            shareMenuButton = new MenuButton()
            {
                X = allMenuButton.Right,
                Y = Application.GetRealWidth(16),
            };
            menuView.AddChidren(shareMenuButton);
            shareMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/ShareOn.png";
            shareMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/Share.png";
            shareMenuButton.TextButton.TextID = StringId.SharingAndFunctionality;
            shareMenuButton.TextButton.IsMoreLines = true;
            Action shateAction = () =>
            {
                MenuButtonSelect(1);
            };
            shareMenuButton.SelectAction = shateAction;
            #endregion
 
#region 报警类
            alarmMenuButton = new MenuButton()
            {
                X = shareMenuButton.Right,
                Y = Application.GetRealWidth(16),
            };
            menuView.AddChidren(alarmMenuButton);
            alarmMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/AlarmOn.png";
            alarmMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/Alarm.png";
            alarmMenuButton.TextButton.TextID = StringId.AlarmType;
            Action alarmAction = () =>
            {
                MenuButtonSelect(2);
            };
            alarmMenuButton.SelectAction = alarmAction;
#endregion
 
#region 系统信息
            systemMenuButton = new MenuButton()
            {
                X = alarmMenuButton.Right,
                Y = Application.GetRealWidth(16),
            };
            menuView.AddChidren(systemMenuButton);
            systemMenuButton.ImageButton.SelectedImagePath = "Collection/MesCenter/SystemOn.png";
            systemMenuButton.ImageButton.UnSelectedImagePath = "Collection/MesCenter/System.png";
            systemMenuButton.TextButton.TextID = StringId.SystemMessage;
            Action systemAction = () =>
            {
                MenuButtonSelect(3);
            };
            systemMenuButton.SelectAction = systemAction;
#endregion
 
        }
 
        /// <summary>
        /// 中间信息内容
        /// </summary>
        void AddMessageView()
        {
            int messageViewY = menuView.Bottom + Application.GetRealWidth(8);
            int messageViewHeight = bodyView.Height - messageViewY;
            messageView = new FrameLayout()
            {
                Y = messageViewY,
                Height = messageViewHeight,
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            bodyView.AddChidren(messageView);
 
            bodyScrolView = new VerticalScrolViewLayout()
            {
                Height = messageViewHeight,
            };
            messageView.AddChidren(bodyScrolView);
 
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="selectId">0全部 1分享与功能 2报警类 3系统信息</param>
        void MenuButtonSelect(int selectId)
        {
            showMesType = selectId;
 
            if (selectId == 0)
            {
                allMenuButton.IsSelected = true;
                shareMenuButton.IsSelected = true;
                alarmMenuButton.IsSelected = true;
                systemMenuButton.IsSelected = true;
 
            }
            else if (selectId == 1)
            {
                allMenuButton.IsSelected = false;
                shareMenuButton.IsSelected = true;
                alarmMenuButton.IsSelected = false;
                systemMenuButton.IsSelected = false;
 
            }
            else if (selectId == 2)
            {
                allMenuButton.IsSelected = false;
                shareMenuButton.IsSelected = false;
                alarmMenuButton.IsSelected = true;
                systemMenuButton.IsSelected = false;
            }
            else if (selectId == 3)
            {
                allMenuButton.IsSelected = false;
                shareMenuButton.IsSelected = false;
                alarmMenuButton.IsSelected = false;
                systemMenuButton.IsSelected = true;
 
            }
 
            GetPushMessageList(showMesType);
        }
 
 
 
        /// <summary>
        /// 添加内容为空提示页面
        /// </summary>
        void AddEmptyTipView()
        {
            emptyTipView = new FrameLayout()
            {
                Height = bodyScrolView.Height,
                Width = bodyScrolView.Width,
            };
            bodyScrolView.AddChidren(emptyTipView);
 
            var tipView = new EmptyTipView()
            {
                Gravity = Gravity.Center
            };
            emptyTipView.AddChidren(tipView);
        }
 
        /// <summary>
        /// 年份标记
        /// </summary>
        string YearMark = "";
        /// <summary>
        /// 加载报警信息RowView
        /// </summary>
        /// <param name="VerticalScrolViewMiddle"></param>
        /// <param name="pushMessageInfo"></param>
        void AddRowView(VerticalScrolViewLayout VerticalScrolViewMiddle, PushMessageInfo pushMessageInfo)
        {
            var rowView = new RowLayout()
            {
                Height = Application.GetRealHeight(65),
                LineColor = CSS_Color.MainBackgroundColor,
            };
            VerticalScrolViewMiddle.AddChidren(rowView);
 
            //标题
            var btnTilte = new TextButton()
            {
                X = Application.GetRealWidth(16),
                Y = Application.GetRealHeight(12),
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(20),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                Text = pushMessageInfo.messageContent,
                //IsMoreLines = true,
            };
            rowView.AddChidren(btnTilte);
            //
            var needHeight = btnTilte.GetMoreLineNeedHeight();
            btnTilte.IsMoreLines = true;
            btnTilte.Height = needHeight;
            rowView.Height = Application.GetRealHeight(45) + needHeight;
 
            //副标题
            var btnSubtitle = new TextButton()
            {
                X = Application.GetRealWidth(16),
                Y = btnTilte.Bottom + Application.GetRealHeight(4),
                Width = Application.GetRealWidth(240),
                Height = Application.GetRealHeight(17),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                Text = pushMessageInfo.messageTitle
            };
            rowView.AddChidren(btnSubtitle);
 
            //日期
            var btnDatetime = new Button()
            {
                X = Application.GetRealWidth(259),
                Y = btnTilte.Bottom + Application.GetRealHeight(4),
                Width = Application.GetRealWidth(100),
                Height = Application.GetRealHeight(17),
                TextAlignment = TextAlignment.CenterRight,
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
            };
            rowView.AddChidren(btnDatetime);
 
            var lineView = new LineView(rowView.Height - Application.GetRealHeight(1));
            rowView.AddChidren(lineView);
 
            //删除按钮
            var deleteBtn = new Button()
            {
                TextID = StringId.Del,
                BackgroundColor = CSS_Color.AuxiliaryColor2,
                TextColor = CSS_Color.MainBackgroundColor,
                TextSize = CSS_FontSize.TextFontSize,
            };
            rowView.AddRightView(deleteBtn);
            deleteBtn.MouseUpEventHandler = (sender, e) =>
            {
                DeleteThePush(pushMessageInfo, rowView);
            };
 
 
            btnDatetime.Text = GetUnixToDateTime(pushMessageInfo.createTime);
            //2020-12-23 去掉点击弹窗事件
            //EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            //{
            //    new Alert(pushMessageInfo.messageTitle, pushMessageInfo.messageContent, Language.StringByID(StringId.Close)).Show();
            //};
            //rowView.MouseUpEventHandler = eventHandler;
            //btnTilte.MouseUpEventHandler = eventHandler;
            //btnSubtitle.MouseUpEventHandler = eventHandler;
 
           
        }
 
        /// <summary>
        /// 时间格式转换处理
        /// </summary>
        /// <param name="time">毫秒时间戳</param>
        /// <returns></returns>
        string GetUnixToDateTime(long time)
        {
            try
            {
                return Utlis.UnixToDateTimeWithFormatMS(time, "MM/dd HH:mm");
            }
            catch (Exception ex)
            {
                return "";
            }
        }
 
        /// <summary>
        /// 删除推送消息
        /// <param name="pushMessageInfo"></param>
        /// <param name="rowView"></param>
        void DeleteThePush(PushMessageInfo pushMessageInfo, RowLayout rowView)
        {
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            new Thread(() =>
            {
                try
                {
                    var result = new HttpServerRequest().PushSerivceDeleteMessage(pushMessageInfo.id);
                    if (result)
                    {
                        PushMessageInfoList.Remove(pushMessageInfo);
                        //删除成功
                        Application.RunOnMainThread(() =>
                        {
                            rowView.RemoveFromParent();
                        });
 
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 加载年份标题
        /// </summary>
        /// <param name="VerticalScrolViewMiddle"></param>
        /// <param name="yearStr"></param>
        void AddYearRowView(VerticalScrolViewLayout VerticalScrolViewMiddle, string yearStr)
        {
            var bgView = new FrameLayout()
            {
                Height = Application.GetRealHeight(57),
            };
            VerticalScrolViewMiddle.AddChidren(bgView);
 
            var yearBtn = new TextButton()
            {
                X = Application.GetRealWidth(16),
                Y = Application.GetRealHeight(16),
                Width = Application.GetRealWidth(320),
                Height = Application.GetRealHeight(33),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.EmphasisFontSize_FirstLevel,
                IsBold = true,
            };
            bgView.AddChidren(yearBtn);
            yearBtn.Text = yearStr + Language.StringByID(StringId.Years);
        }
 
        /// <summary>
        /// 查询推送记录
        /// </summary>
        /// <param name="queryType"></param>
        void GetPushMessageList(int queryType = 0)
        {
            bodyScrolView.RemoveAll();
            if(PushMessageInfoList == null)
            {
                PushMessageInfoList = new List<PushMessageInfo>();
            }
            else
            {
                PushMessageInfoList.Clear();
            }
          
            YearMark = "";//标记清空
            if (string.IsNullOrEmpty(OnAppConfig.Instance.PushId))
            {
                AddEmptyTipView();
                return;
            }
 
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            new Thread(() =>
            {
                try
                {
                    var result = new HttpServerRequest().PushSerivceGetPushmessagelist(queryType);
                    if (result.Code == StateCode.SUCCESS)
                    {
                        PushMessageInfoList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PushMessageInfo>>(result.Data.ToString());
                        Application.RunOnMainThread(() =>
                        {
                            if (PushMessageInfoList != null && PushMessageInfoList.Count > 0)
                            {
 
                                ParseAndDisplayPushList();
                                PushSerivceMarkAllMessageRead();
                            }
                            else
                            {
                                AddEmptyTipView();
                            }
                        });
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            AddEmptyTipView();
                        });
                        IMessageCommon.Current.ShowErrorInfoAlter(result.Code);
                    }
                }
                catch (Exception ex)
                {
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 解析和显示推送消息
        /// </summary>
        void ParseAndDisplayPushList()
        {
            foreach (var info in PushMessageInfoList)
            {
                //年份解析 判断是否需要添加年份标题
                var mesYear = Utlis.UnixToDateTimeYearMS(info.createTime);
                if (string.IsNullOrEmpty(YearMark))
                {
                    //刚开始YearMark标记为空,添加第一个年份标题
                    YearMark = mesYear;
                    AddYearRowView(bodyScrolView, YearMark);
                }
                else if (YearMark != mesYear)
                {
                    //当前信息年份和之前的不一样,重新添加一个年份标题
                    YearMark = mesYear;
                    AddYearRowView(bodyScrolView, YearMark);
                }
 
                AddRowView(bodyScrolView, info);
            }
        }
 
#region 测试
#if DEBUG
        /// <summary>
        /// 
        /// </summary>
        void TestLoad()
        {
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息",
                messageContent = "门锁被打开",
                createTime = 1606900275,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息1",
                messageContent = "门锁被打开",
                createTime = 1606895454,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2",
                messageContent = "门锁被打开",
                createTime = 1606895454,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2019",
                messageContent = "门锁被打开",
                createTime = 1575277874,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2019",
                messageContent = "门锁被打开",
                createTime = 1572685874,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2018",
                messageContent = "门锁被打开",
                createTime = 1541149874,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2017",
                messageContent = "门锁被打开",
                createTime = 1509613874,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2017",
                messageContent = "门锁被打开",
                createTime = 1509613874,
            });
 
            PushMessageInfoList.Add(new PushMessageInfo()
            {
                messageTitle = "报警信息2017",
                messageContent = "门锁被打开",
                createTime = 1506935474,
            });
 
            ParseAndDisplayPushList();
        }
 
#endif
 
#endregion
 
        /// <summary>
        /// 标记所有推送消息已读
        /// </summary>
        void PushSerivceMarkAllMessageRead()
        {
            new Thread(() =>
            {
                var result = new HttpServerRequest().PushSerivceMarkAllMessageRead();
                if (result)
                {
                    Utlis.WriteLine("标记成功");
                }
                else
                {
                    Utlis.WriteLine("标记失败");
                }
 
 
            })
            { IsBackground = true }.Start();
        }
 
        /// <summary>
        /// 注册收到推送监听
        /// </summary>
        void RegisterGetPushMessageAction()
        {
            HDLCommon.GetPushMessageAction = () =>
            {
                if (bodyView != null)
                {
                    //Utlis.WriteLine("GetPushMessageAction M收到推送");
                    GetPushMessageList(showMesType);
                }
            };
        }
 
        /// <summary>
        /// 取消收到推送监听
        /// </summary>
        void UnregisterGetPushMessageAction()
        {
            HDLCommon.GetPushMessageAction = null;
        }
 
#if DEBUG
 
        /// <summary>
        /// 复制到剪切板
        /// </summary>
        /// <param name="text"></param>
        void CopyToClipboard(string text)
        {
            Clipboard.SetTextAsync(text);
            Utlis.ShowTip(text + "\n" + Language.StringByID(StringId.CopySuccess));
        }
#endif
 
    }
 
    /// <summary>
    ///  推送消息类型详情
    /// </summary>
    [System.Serializable]
    public class PushMessageInfo
    {
        /// <summary>
        /// 记录id
        /// </summary>
        public string id;
        /// <summary>
        /// 推送Token记录Id
        /// </summary>
        public string pushId;
        /// <summary>
        /// 云端定义 推送类型;0:默认;1:报警;2:提示;3:新闻;4:推荐;
        ///
        /// Default:分享与功能;Alarm:报警类;Prompt:系统消息
        /// </summary>
        public string pushType;
        /// <summary>
        /// 消息主题
        /// </summary>
        public string messageTitle;
        /// <summary>
        /// 消息内容
        /// </summary>
        public string messageContent;
        ///// <summary>
        ///// 扩展数据
        ///// </summary>
        //public string messageExpand;
        ///// <summary>
        ///// 消息类型;0:提示;1:报警;2:丰林可是对讲呼叫;3:强制下线推送
        ///// </summary>
        //public string messageType;
        /// <summary>
        /// 是否已读
        /// </summary>
        public bool isRead;
        /// <summary>
        /// 推送时间
        /// </summary>
        public long createTime;
    }
 
}