黄学彪
2019-11-18 4ce5177289b1d34e467de9d8790836559cc5a09e
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter.UserMain
{
    /// <summary>
    /// 消息中心
    /// </summary>
    public class MessageManagementForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalFrameControl listView = null;
        /// <summary>
        /// 全部的行
        /// </summary>
        private Dictionary<string, RowLayoutControl> dicAllRow = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uMessageCenter));
 
            listView = new VerticalFrameControl(35);
            listView.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listView);
 
            //初始化右上角菜单
            this.InitTopRightMenu();
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化中部信息
                this.InitMiddleData();
            });
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private async void InitMiddleData()
        {
            this.dicAllRow = new Dictionary<string, RowLayoutControl>();
 
            //打开进度条
            this.ShowProgressBar();
            var result = await UserCenterLogic.GetResponseDataByRequestHttps("ZigbeeUsers/GetPushMessageRecord", false, "");
            if (result == null)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return;
            }
            var dataInfo = Newtonsoft.Json.JsonConvert.DeserializeObject<List<MessageRecordInfo>>(result);
            //合并数据
            var dicData = this.MergeMessageRecordData(dataInfo);
            //关闭进度条
            this.CloseProgressBar();
            
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                //初始化中部控件
                this.InitMiddleFrame(dicData);
            });
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        /// <param name="dicData"></param>
        private void InitMiddleFrame(Dictionary<string, List<MessageRecordInfo>> dicData)
        {
            int index = 0;
            foreach (var keys in dicData.Keys)
            {
                var listData = dicData[keys];
                HdlThreadLogic.Current.RunMain(() =>
                {
                    index++;
                    //容器
                    var framBack = new FrameListControl(23);
                    framBack.Name = index.ToString();
                    framBack.Height = Application.GetRealHeight(100);
                    framBack.BackgroundColor = UserCenterColor.Current.White;
                    listView.AddChidrenFrame(framBack);
 
                    //日期
                    string strMonth = Language.StringByID(R.MyInternationalizationString.Month);
                    string strDate = Language.StringByID(R.MyInternationalizationString.Day);
                    var frameDate = new FrameLayout();
                    frameDate.Height = Application.GetRealHeight(118);
                    framBack.AddChidren(frameDate);
                    var btnDate = new NormalViewControl(500, 60, true);
                    btnDate.TextSize = 15;
                    btnDate.X = ControlCommonResourse.XXLeft;
                    btnDate.Y = Application.GetRealHeight(35);
                    btnDate.Text = Convert.ToDateTime(listData[0].MsgTime).ToString("MM" + strMonth + "dd" + strDate);
                    frameDate.AddChidren(btnDate);
 
                    for (int i = 0; i < listData.Count; i++)
                    {
                        //添加信息行
                        this.AddMsgRowControl(framBack, listData[i], i != listData.Count - 1);
                    }
                    //调整高度
                    listView.AdjustChidrenFrameHeight(framBack, Application.GetRealHeight(23));
                    if (index == dicData.Count)
                    {
                        listView.AdjustTableHeight();
                    }
                });
            }
        }
 
        #endregion
 
        #region ■ 右上角菜单_________________________
 
        /// <summary>
        /// 初始化右上角菜单
        /// </summary>
        private void InitTopRightMenu()
        {
            var btnIcon = new MostRightIconControl(69, 69);
            btnIcon.UnSelectedImagePath = "Item/More.png";
            topFrameLayout.AddChidren(btnIcon);
            btnIcon.InitControl();
            btnIcon.ButtonClickEvent += ((sender, e) =>
            {
                //显示底部菜单界面
                this.ShowBottomMenu();
            });
        }
 
        /// <summary>
        /// 显示底部菜单界面
        /// </summary>
        private void ShowBottomMenu()
        {
            var menuContr = new BottomMenuSelectForm();
            menuContr.AddForm(2);
            //全部已读
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.uAllRead), () =>
            {
                //设置全部为已读
                this.SetAllTickIsRead();
            });
            //全部删除
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.uAllDelete), () =>
            {
                //删除全部的消息
                this.DeleteAllMsg();
            });
        }
 
        #endregion
 
        #region ■ 添加信息行_________________________
 
        /// <summary>
        /// 添加信息行
        /// </summary>
        /// <param name="frameList"></param>
        /// <param name="recordInfo"></param>
        /// <param name="addLine"></param>
        private void AddMsgRowControl(FrameListControl frameList, MessageRecordInfo recordInfo, bool addLine)
        {
            var rowControl = new RowLayoutControl(frameList.rowSpace / 2);
            frameList.AddChidren(rowControl);
            rowControl.Name = frameList.ChildrenCount.ToString();
            rowControl.MainKeys = recordInfo.RegId;
            this.dicAllRow[frameList.Name + "-" + rowControl.Name] = rowControl;
 
            //图标(现在测试)
            var btnIcon = rowControl.frameTable.AddLeftIcon(81);
            btnIcon.UnSelectedImagePath = "Item/Safety.png";
 
            //消息类型
            var btnMsgObject = rowControl.frameTable.AddLeftCaption("测试:消息类型", 600);
            PicViewControl btnNewTip = null;
            if (recordInfo.IsRead == 0)
            {
                btnNewTip = new PicViewControl(78, 55);
                btnNewTip.UnSelectedImagePath = "Item/NewVersion.png";
                btnNewTip.Y = Application.GetRealHeight(9) + rowControl.frameTable.chidrenYaxis;
                btnNewTip.X = btnMsgObject.X + btnMsgObject.GetRealWidthByText(14);
                rowControl.frameTable.AddChidren(btnNewTip, ChidrenBindMode.BindEventOnly);
            }
 
            //消息
            var btnMsg = rowControl.frameTable.AddMostRightView(recordInfo.MsgContent, 600, 60);
            btnMsg.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnMsg.Y = Application.GetRealHeight(5) + rowControl.frameTable.chidrenYaxis;
            rowControl.frameTable.AddChidren(btnMsg, ChidrenBindMode.BindEventOnly);
            //时间
            var btnTime = rowControl.frameTable.AddMostRightView(recordInfo.MsgContent, 200, 50);
            btnTime.TextColor = UserCenterColor.Current.TextGrayColor1;
            btnTime.Y = btnMsg.Bottom + Application.GetRealHeight(12);
            btnTime.Text = Convert.ToDateTime(recordInfo.MsgTime).ToString("HH:mm");
            rowControl.frameTable.AddChidren(btnTime, ChidrenBindMode.BindEventOnly);
            //底线
            if (addLine == true)
            {
                rowControl.frameTable.AddBottomLine();
            }
            //已读
            if (recordInfo.IsRead == 1)
            {
                rowControl.frameTable.UseClickStatu = false;
                //消息类型
                btnMsgObject.TextColor = UserCenterColor.Current.TextGrayColor1;
                //消息
                btnMsg.TextColor = UserCenterColor.Current.TextGrayColor1;
            }
            //删除
            var btnDelete = rowControl.AddDeleteControl();
            btnDelete.ButtonClickEvent += (sender, e) =>
            {
                //确认删除消息?
                string msg = Language.StringByID(R.MyInternationalizationString.uDeleteMessageMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
                {
                    await this.DeleteMsg(recordInfo.RegId, frameList, rowControl);
                });
            };
 
            //点击
            rowControl.frameTable.ButtonClickEvent += async (sender, e) =>
            {
                if (recordInfo.IsRead == 1)
                {
                    return;
                }
                recordInfo.IsRead = 1;
                rowControl.frameTable.UseClickStatu = false;
                //消息类型
                btnMsgObject.TextColor = UserCenterColor.Current.TextGrayColor1;
                //消息
                btnMsg.TextColor = UserCenterColor.Current.TextGrayColor1;
                btnNewTip.RemoveFromParent();
                if (sender != null)
                {
                    //标记已读(不管它成功不成功)
                    await this.SetTickIsRead(recordInfo.RegId);
                }
            };
        }
 
        #endregion
 
        #region ■ 标记已读___________________________
 
        /// <summary>
        /// 标记已读
        /// </summary>
        /// <param name="strId"></param>
        private async System.Threading.Tasks.Task<bool> SetTickIsRead(string strId)
        {
            var pra = new { Id = strId };
            await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/TickIsRead", true, pra);
            return true;
        }
 
        /// <summary>
        /// 设置全部为已读
        /// </summary>
        private void SetAllTickIsRead()
        {
            if (dicAllRow.Count == 0)
            {
                return;
            }
            //确认标记全部消息为已读?
            this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.uUnTipAllMessageMsg), () =>
            {
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    //打开进度条
                    this.ShowProgressBar();
                    foreach (var contr in this.dicAllRow.Values)
                    {
                        //标识全部为已读
                        var result = await this.SetTickIsRead(contr.MainKeys);
                        if (result == false)
                        {
                            //关闭进度条
                            this.CloseProgressBar();
                            return;
                        }
                    }
                    //关闭进度条
                    this.CloseProgressBar();
 
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //设置全部状态
                        foreach (var contr in this.dicAllRow.Values)
                        {
                            contr.frameTable.ButtonClickEvent?.Invoke(null, null);
                        }
                    });
                });
            });
        }
 
        #endregion
 
        #region ■ 删除信息___________________________
 
        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="strId"></param>
        /// <param name="frameList"></param>
        /// <param name="rowContr"></param>
        private async System.Threading.Tasks.Task<bool> DeleteMsg(string strId, FrameListControl frameList, RowLayoutControl rowContr)
        {
            var pra = new { Id = strId };
            var result = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/DeletePushMessage", true, pra);
            if (result == true && frameList != null)
            {
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //移除缓存
                    this.dicAllRow.Remove(frameList.Name + "-" + rowContr.Name);
                    //调整控件位置
                    this.AdjustControlLocation(frameList, rowContr);
                });
            }
            return result;
        }
 
        /// <summary>
        /// 删除全部的消息
        /// </summary>
        private void DeleteAllMsg()
        {
            if (dicAllRow.Count == 0)
            {
                return;
            }
            //确认删除全部消息?
            this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.uDeleteAllMessageMsg), () =>
            {
                HdlThreadLogic.Current.RunThread(async () =>
                {
                    //打开进度条
                    this.ShowProgressBar();
                    foreach (var contr in this.dicAllRow.Values)
                    {
                        //删除全部消息
                        var result = await this.DeleteMsg(contr.MainKeys, null, null);
                        if (result == false)
                        {
                            //关闭进度条
                            this.CloseProgressBar();
                            return;
                        }
                    }
                    //关闭进度条
                    this.CloseProgressBar();
 
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        bodyFrameLayout.RemoveAll();
                        listView = new VerticalFrameControl(35);
                        listView.Height = bodyFrameLayout.Height;
                        bodyFrameLayout.AddChidren(listView);
                        HdlThreadLogic.Current.RunThread(() =>
                        {
                            //初始化中部信息
                            this.InitMiddleData();
                        });
                    });
                });
            });
        }
 
        #endregion
 
        #region ■ 调整控件位置_______________________
 
        /// <summary>
        /// 调整控件位置
        /// </summary>
        /// <param name="frameList"></param>
        private void AdjustControlLocation(FrameListControl frameList, RowLayoutControl removeRow)
        {
            int changedHeight = frameList.GetChildren(frameList.ChildrenCount - 1).Height;
            if (frameList.ChildrenCount <= 2)
            {
                //已经删完了,只剩下标题日期了
                changedHeight = frameList.Height;
            }
            //先调整各自容器的Y轴坐标
            bool canChangedLoaction = false;
            for (int i = 0; i < this.listView.frameTable.ChildrenCount; i++)
            {
                var myView = this.listView.frameTable.GetChildren(i);
                if (myView.Name == frameList.Name)
                {
                    //它之后的控件全部往上移动
                    canChangedLoaction = true;
                    continue;
                }
                if (canChangedLoaction == true)
                {
                    myView.Y -= changedHeight;
                }
            }
            //调整父容器大小
            this.listView.AdjustTableHeight();
 
 
            if (frameList.ChildrenCount <= 2)
            {
                //已经删完了,只剩下标题日期了
                frameList.RemoveFromParent();
            }
            else
            {
                //这个容器里面被移除的行的下面的全部行,往上移动
                bool changedFlage = false;
                for (int i = 0; i < frameList.ChildrenCount; i++)
                {
                    var myView = frameList.GetChildren(i);
                    if (myView.Name == removeRow.Name)
                    {
                        //它之后的控件全部往上移动
                        changedFlage = true;
                        continue;
                    }
                    if (changedFlage == true)
                    {
                        myView.Y -= changedHeight;
                    }
                }
                //移除指定的行
                removeRow.RemoveFromParent();
                //容器大小减少
                frameList.Height -= changedHeight;
            }
        }
 
        #endregion
 
        #region ■ 合并数据___________________________
 
        /// <summary>
        /// 合并数据
        /// </summary>
        /// <param name="dataInfo"></param>
        /// <returns></returns>
        private Dictionary<string, List<MessageRecordInfo>> MergeMessageRecordData(List<MessageRecordInfo> dataInfo)
        {
            var dicData = new Dictionary<string, List<MessageRecordInfo>>();
 
            //首先先排序一下,时间大的放在前面
            var dicTemp = new Dictionary<string, List<MessageRecordInfo>>();
            var listSortKeys = new List<string>();
            foreach (var data in dataInfo)
            {
                if (dicTemp.ContainsKey(data.MsgTime) == false)
                {
                    dicTemp[data.MsgTime] = new List<MessageRecordInfo>();
                    listSortKeys.Add(data.MsgTime);
                }
                dicTemp[data.MsgTime].Add(data);
            }
            listSortKeys.Sort();
            for (int i = listSortKeys.Count - 1; i >= 0; i--)
            {
                var listData = dicTemp[listSortKeys[i]];
                foreach (var data2 in listData)
                {
                    string[] myArry = data2.MsgTime.Split(new string[] { "T" }, StringSplitOptions.RemoveEmptyEntries);
                    if (myArry.Length != 2)
                    {
                        continue;
                    }
                    if (dicData.ContainsKey(myArry[0]) == false)
                    {
                        dicData[myArry[0]] = new List<MessageRecordInfo>();
                    }
                    dicData[myArry[0]].Add(data2);
                }
            }
            return dicData;
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        /// 信息结果
        /// </summary>
        private class MessageRecordInfo
        {
            /// <summary>
            /// 主键
            /// </summary>
            public string RegId = string.Empty;
            /// <summary>
            /// 消息内容
            /// </summary>
            public string MsgContent = string.Empty;
            /// <summary>
            /// MsgType
            /// </summary>
            public string MsgType = string.Empty;
            /// <summary>
            /// 是否已读
            /// </summary>
            public int IsRead = 0;
            /// <summary>
            /// 时间 2019-09-10 13:11:55
            /// </summary>
            public string MsgTime = string.Empty;
        }
 
        #endregion
    }
}