黄学彪
2019-11-18 16604a593202f2f87adf71abd57d036fe7da3b52
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
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;
 
        #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.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.RunMain(() =>
            {
                //初始化中部控件
                this.InitMiddleFrame(dicData);
            });
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        /// <param name="dicData"></param>
        private void InitMiddleFrame(Dictionary<string, List<MessageRecordInfo>> dicData)
        {
            foreach (var keys in dicData.Keys)
            {
                var listData = dicData[keys];
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //容器
                    var framBack = new FrameListControl(23);
                    framBack.Name = keys;
                    framBack.Height = Application.GetRealHeight(100);
                    framBack.BackgroundColor = UserCenterColor.Current.White;
 
                    //日期
                    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);
                    }
                });
            }
        }
 
        #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), () =>
            {
            });
            //全部删除
            menuContr.AddMenu(Language.StringByID(R.MyInternationalizationString.uAllDelete), () =>
            {
            });
        }
 
        #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);
           
 
            //图标(现在测试)
            var btnIcon = rowControl.frameTable.AddLeftIcon(81);
            btnIcon.UnSelectedImagePath = "Item/Satety.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(6) + 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.uDeleteRoomMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, async () =>
                {
                    await this.DeleteMsg(recordInfo.RegId, frameList, rowControl);
                });
            };
 
            //点击
            rowControl.frameTable.ButtonClickEvent += (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();
                //标记已读(不管它成功不成功)
                this.SetTickIsRead(recordInfo.RegId);
            };
        }
 
        #endregion
 
        #region ■ 标记已读___________________________
 
        /// <summary>
        /// 标记已读
        /// </summary>
        /// <param name="strId"></param>
        private async void SetTickIsRead(string strId)
        {
            var pra = new { Id = strId };
            //await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/TickIsRead", true, pra);
        }
 
        #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(() =>
                {
                    rowContr.RemoveFromParent();
                    //调整控件位置
                    this.AdjustControlLocation(frameList);
                });
            }
            //return result;
            return true;
        }
 
        #endregion
 
        #region ■ 调整控件位置_______________________
 
        /// <summary>
        /// 调整控件位置
        /// </summary>
        /// <param name="frameList"></param>
        private void AdjustControlLocation(FrameListControl frameList)
        {
            int nowHeight = frameList.Height;
            int changedHeight = 0;
            if (frameList.ChildrenCount <= 1)
            {
                //已经删完了,只剩下标题日期了
                frameList.RemoveFromParent();
                changedHeight = nowHeight;
            }
            else
            {
                frameList.Height = frameList.GetChildren(frameList.ChildrenCount - 1).Bottom;
                changedHeight = nowHeight - frameList.Height;
            }
            bool canChangedLoaction = false;
            for (int i = 0; i < this.listView.ChildrenCount; i++)
            {
                var myView = this.listView.GetChildren(i);
                if (myView.Name == frameList.Name)
                {
                    //它之后的控件全部往上移动
                    canChangedLoaction = true;
                    continue;
                }
                if (canChangedLoaction == true)
                {
                    myView.Y -= 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[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (dicData.ContainsKey(myArry[1]) == false)
                    {
                        dicData[myArry[1]] = new List<MessageRecordInfo>();
                    }
                    dicData[myArry[1]].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
    }
}