黄学彪
2020-11-20 99253403b560f761d5f9af8b4f5140496996433a
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
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    public class AreaCodeSelectForm : DialogCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 完成选择的事件(地区码)
        /// </summary>
        public Action<string> FinishSelectEvent = null;
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 前回选择的控件
        /// </summary>
        private FrameRowControl oldFrameRowControl = null;
        /// <summary>
        /// 锁
        /// </summary>
        private object objLock = new object();
        /// <summary>
        /// 这个东西是个静态(它是个特殊的)
        /// </summary>
        private static List<AreaCodeInfo> listAreaCode = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            var frameBack = new FrameLayout();
            frameBack.Width = Application.GetRealWidth(850);
            frameBack.Height = Application.GetRealHeight(1342);
            frameBack.Radius = (uint)Application.GetRealHeight(17);
            frameBack.BackgroundColor = UserCenterColor.Current.White;
            frameBack.Gravity = Gravity.CenterHorizontal;
            frameBack.Y = Application.GetRealHeight(328);
            bodyFrameLayout.AddChidren(frameBack);
 
            this.listView = new VerticalListControl(3);
            listView.Y = Application.GetRealHeight(164 - 11);
            listView.Height = frameBack.Height - Application.GetRealHeight(164 - 11);
            listView.Radius = (uint)Application.GetRealHeight(17);
            frameBack.AddChidren(listView);
 
            //请选择区号
            var btnTitle = new NormalViewControl(270, 60, true);
            btnTitle.X = HdlControlResourse.XXLeft;
            btnTitle.Y = Application.GetRealHeight(69);
            btnTitle.TextID = R.MyInternationalizationString.uPleaseSelectAreaCode;
            btnTitle.TextSize = 15;
            btnTitle.TextColor = UserCenterColor.Current.TextColor2;
            frameBack.AddChidren(btnTitle);
 
            //搜索
            var btnSearch = new MySearchControl(Language.StringByID(R.MyInternationalizationString.uSearch));
            btnSearch.X = Application.GetRealWidth(334);
            btnSearch.Y = Application.GetRealHeight(46);
            frameBack.AddChidren(btnSearch);
            btnSearch.BindEvent(this.SearchEvent);
 
            //让这个按钮这个区域不能点击菜单
            var frameTemp = new FrameLayout();
            frameTemp.Y = Application.GetRealHeight(1146);
            frameTemp.Height = frameBack.Height - Application.GetRealHeight(1146);
            frameBack.AddChidren(frameTemp);
 
            var btnOk = new BottomClickButton(688);
            btnOk.Y = Application.GetRealHeight(1146);
            btnOk.TextID = R.MyInternationalizationString.uConfirm1;
            frameBack.AddChidren(btnOk);
            btnOk.ButtonClickEvent += (sender, e) =>
            {
                if (oldFrameRowControl != null)
                {
                    this.FinishSelectEvent?.Invoke(oldFrameRowControl.MainKeys);
                }
                this.CloseForm();
            };
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //初始化地区列表
                this.InitAreaList();
            });
        }
 
        #endregion
 
        #region ■ 初始化地区列表_____________________
 
        /// <summary>
        /// 初始化地区列表
        /// </summary>
        private void InitAreaList()
        {
            if (listAreaCode == null)
            {
                //第一次获取
                //开启进度条
                ProgressBar.Show();
 
                //☆マーク☆ 地区代码列表
                //获取全部的地区代码列表
                //var Pra = new { Common.CommonPage.RequestVersion };
                //string result = HdlUserCenterLogic.GetResponseDataByRequestHttps("ZigbeeUsers/GetAreaCode", false, Pra);
                string result = string.Empty;
                //关闭进度条
                ProgressBar.Close();
                if (string.IsNullOrEmpty(result) == true)
                {
                    return;
                }
                listAreaCode = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AreaCodeInfo>>(result);
                for (int i = 0; i < listAreaCode.Count; i++)
                {
                    //获取首字母
                    listAreaCode[i].First = this.GetCharSpellCode(listAreaCode[i].Name.Substring(0, 1));
                }
                //排序
                listAreaCode.Sort((obj1, obj2) =>
                {
                    return obj1.First.CompareTo(obj2.First);
                });
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                //添加全部明细行
                this.AddAllDetailRow(listAreaCode);
            });   
        }
 
        /// <summary>
        /// 添加全部明细行
        /// </summary>
        /// <param name="listData"></param>
        private void AddAllDetailRow(List<AreaCodeInfo> listData)
        {
            listView.RemoveAll();
            string oldFirst = string.Empty;
            int realHeight = 0;
            for (int i = 0; i < listData.Count; i++)
            {
                if (oldFirst != listData[i].First)
                {
                    oldFirst = listData[i].First;
                    //添加字母标题
                    var frame1 = new FrameRowControl(listView.rowSpace / 2);
                    frame1.UseClickStatu = false;
                    //列表控件只帮扩大3,而它的间距需要11
                    frame1.Height = Application.GetRealHeight(58 + 8);
                    frame1.Width = Application.GetRealWidth(850);
                    listView.AddChidren(frame1);
                    realHeight += frame1.Height;
 
                    var btnView1 = frame1.AddLeftCaption(listData[i].First, 300);
                    btnView1.TextColor = UserCenterColor.Current.TextGrayColor2;
                    if (listData[i].First == "ZZ")
                    {
                        //其他
                        btnView1.TextID = R.MyInternationalizationString.uOther;
                    }
                }
                //添加明细行
                var frameDetail = new FrameRowControl(listView.rowSpace / 2);
                frameDetail.MainKeys = listData[i].Code;
                frameDetail.UseClickStatu = false;
                frameDetail.Width = Application.GetRealWidth(850);
                frameDetail.Height = Application.GetRealHeight(118);
                listView.AddChidren(frameDetail);
                realHeight += frameDetail.Height;
                //地区名
                var btnName = frameDetail.AddLeftCaption(listData[i].Name, 600);
                btnName.TextColor = UserCenterColor.Current.TextGrayColor3;
                //地区码
                var btnCode = frameDetail.AddMostRightView("+" + listData[i].Code, 300);
                btnCode.TextColor = UserCenterColor.Current.TextGrayColor3;
                frameDetail.ButtonClickEvent += (sender, e) =>
                {
                    if (oldFrameRowControl != null)
                    {
                        if (oldFrameRowControl.MainKeys == frameDetail.MainKeys)
                        {
                            //同一个东西
                            return;
                        }
                        //取消上一次的选择
                        oldFrameRowControl.BackgroundColor = UserCenterColor.Current.Transparent;
                        ((NormalViewControl)oldFrameRowControl.GetChildren(0)).TextColor = UserCenterColor.Current.TextGrayColor3;
                        ((NormalViewControl)oldFrameRowControl.GetChildren(1)).TextColor = UserCenterColor.Current.TextGrayColor3;
                    }
 
                    //选中状态
                    frameDetail.BackgroundColor = 0x0ffc744b;
                    btnName.TextColor = UserCenterColor.Current.TextColor1;
                    btnCode.TextColor = UserCenterColor.Current.TextColor1;
                    oldFrameRowControl = frameDetail;
                };
            }
 
            if (realHeight > listView.Height - Application.GetRealHeight(196))
            {
                //促使被挡住的菜单能够向上滑动
                var frameTemp = new FrameLayout();
                frameTemp.Height = Application.GetRealHeight(196 + 34);//间距34
                listView.AddChidren(frameTemp);
            }
        }
 
        #endregion
 
        #region ■ 搜索事件___________________________
 
        /// <summary>
        /// 搜索事件
        /// </summary>
        /// <param name="changdValue"></param>
        private void SearchEvent(string changdValue)
        {
            lock (objLock)
            {
                //如果只输入一位的话,可以判断首字母
                string first = changdValue.Length == 1 ? changdValue.ToUpper() : string.Empty;
 
                var listData = new List<AreaCodeInfo>();
                for (int i = 0; i < listAreaCode.Count; i++)
                {
                    //如果只输入一位的话,可以判断首字母
                    if (listAreaCode[i].First == first)
                    {
                        listData.Add(listAreaCode[i]);
                    }
                    //判断名字和地区码
                    else if (listAreaCode[i].Code.Contains(changdValue) == true || listAreaCode[i].Name.Contains(changdValue) == true)
                    {
                        listData.Add(listAreaCode[i]);
                    }
                }
                HdlThreadLogic.Current.RunMain(() =>
                {
                    //添加全部明细行
                    this.AddAllDetailRow(listData);
                });
            }
        }
 
        #endregion
 
        #region ■ 搜索控件___________________________
 
        /// <summary>
        /// 制作一个根据键值进行搜索的控件
        /// 添加控件后,请调用LoadEvent()函数进行回调函数的绑定
        /// </summary>
        public class MySearchControl : FrameLayout
        {
            #region ■ 变量声明__________________________
            
            /// <summary>
            /// 搜索控件
            /// </summary>
            private TextInputControl txtSearch = null;
            /// <summary>
            /// 回调函数
            /// </summary>
            private Action<string> actionMethod = null;
            /// <summary>
            /// 没有输入键值的显示文本
            /// </summary>
            private string tipText = string.Empty;
 
            #endregion
 
            #region ■ 初始化____________________________
            /// <summary>
            /// 制作一个根据键值进行搜索的控件
            /// 添加控件到父控件后,请调用BindEvent()函数进行回调函数的绑定
            /// </summary>
            /// <param name="i_tipText">没有输入键值的显示文本</param>
            public MySearchControl(string i_tipText)
            {
                this.tipText = i_tipText;
                //桌布控件
                this.Width = Application.GetRealWidth(458);
                this.Height = Application.GetRealHeight(104);
                this.Radius = (uint)Application.GetRealHeight(17);
                this.BorderColor = 0xffbfc1c2;
                this.BorderWidth = 1;
            }
 
            /// <summary>
            /// 控件初始化
            /// </summary>
            private void InitControl()
            {
                //输入文本
                int textWidth = this.Width - Application.GetRealWidth(35 * 2);
                txtSearch = new TextInputControl(textWidth, Application.GetRealHeight(58), false);
                txtSearch.X = Application.GetRealWidth(35);
                txtSearch.PlaceholderText = this.tipText;
                txtSearch.Gravity = Gravity.CenterVertical;
                this.AddChidren(txtSearch);
 
                string oldText = string.Empty;
                //值改变事件
                txtSearch.TextChangeEventHandler += (sender, e) =>
                {
                    this.nextSearchKeys = txtSearch.Text.Trim();
                    if (oldText == this.nextSearchKeys)
                    {
                        //输入的值一样
                        return;
                    }
                    oldText = this.nextSearchKeys;
                    //允许执行回调函数
                    this.isCanSearch = true;
                };
            }
            #endregion
 
            #region ■ 绑定事件__________________________
 
            /// <summary>
            /// 绑定事件
            /// </summary>
            /// <param name="action">回调函数:值是输入的关键字</param>
            public void BindEvent(Action<string> action)
            {
                this.actionMethod = action;
 
                //控件初始化
                this.InitControl();
 
                //打开搜索键值的线程
                this.StartSearchKeysThead();
            }
 
            #endregion
 
            #region ■ 开启线程__________________________
 
            /// <summary>
            /// 能否执行搜索
            /// </summary>
            private bool isCanSearch = false;
            /// <summary>
            /// 下一个搜索键值
            /// </summary>
            private string nextSearchKeys = string.Empty;
            /// <summary>
            /// 打开搜索键值的线程
            /// </summary>
            private void StartSearchKeysThead()
            {
                HdlThreadLogic.Current.RunThread(() =>
                {
                    while (this.Parent != null)
                    {
                        //根据逻辑,不能够搜索的话,等待
                        if (this.isCanSearch == false)
                        {
                            System.Threading.Thread.Sleep(1000);
                            continue;
                        }
                        //获取当前瞬间的键值
                        string nowSearchKeys = nextSearchKeys;
 
                        //根据搜索键值,调用回调函数
                        this.actionMethod(nowSearchKeys);
 
                        //处理完毕后,如果当前的检索键值与下一个检索键值一样的话
                        //说明用户已经不再输入键值了,这个时候,停止搜索
                        if (nowSearchKeys == nextSearchKeys)
                        {
                            this.isCanSearch = false;
                        }
 
                        //如果当前的检索键值与下一个检索键值不一样的话
                        //那就说明,用户再次输入了键值,这个时候,让线程继续跑下去
                    }
                });
            }
            #endregion
 
            #region ■ 控件摧毁__________________________
 
            /// <summary>
            /// 控件摧毁
            /// </summary>
            public override void RemoveFromParent()
            {
                actionMethod = null;
                base.RemoveFromParent();
            }
 
            #endregion
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 获取汉字首字母
        /// </summary>
        /// <param name="textValue"></param>
        /// <returns></returns>
        private string GetCharSpellCode(string textValue)
        {
            long iCnChar;
 
            byte[] ZW = Encoding.GetEncoding("gb2312").GetBytes(textValue);
 
            //如果是字母,则直接返回 
            if (ZW.Length == 1)
            {
                return textValue.ToUpper();
            }
            else
            {
                // get the array of byte from the single char 
                int i1 = (short)(ZW[0]);
                int i2 = (short)(ZW[1]);
                iCnChar = i1 * 256 + i2;
            }
 
            //expresstion 
            //table of the constant list 
            // 'A'; //45217..45252 
            // 'B'; //45253..45760 
            // 'C'; //45761..46317 
            // 'D'; //46318..46825 
            // 'E'; //46826..47009 
            // 'F'; //47010..47296 
            // 'G'; //47297..47613 
 
            // 'H'; //47614..48118 
            // 'J'; //48119..49061 
            // 'K'; //49062..49323 
            // 'L'; //49324..49895 
            // 'M'; //49896..50370 
            // 'N'; //50371..50613 
            // 'O'; //50614..50621 
            // 'P'; //50622..50905 
            // 'Q'; //50906..51386 
 
            // 'R'; //51387..51445 
            // 'S'; //51446..52217 
            // 'T'; //52218..52697 
            //没有U,V 
            // 'W'; //52698..52979 
            // 'X'; //52980..53640 
            // 'Y'; //53689..54480 
            // 'Z'; //54481..55289 
 
            // iCnChar match the constant 
            if ((iCnChar >= 45217) && (iCnChar <= 45252))
            {
                return "A";
            }
            else if ((iCnChar >= 45253) && (iCnChar <= 45760))
            {
                return "B";
            }
            else if ((iCnChar >= 45761) && (iCnChar <= 46317))
            {
                return "C";
            }
            else if ((iCnChar >= 46318) && (iCnChar <= 46825))
            {
                return "D";
            }
            else if ((iCnChar >= 46826) && (iCnChar <= 47009))
            {
                return "E";
            }
            else if ((iCnChar >= 47010) && (iCnChar <= 47296))
            {
                return "F";
            }
            else if ((iCnChar >= 47297) && (iCnChar <= 47613))
            {
                return "G";
            }
            else if ((iCnChar >= 47614) && (iCnChar <= 48118))
            {
                return "H";
            }
            else if ((iCnChar >= 48119) && (iCnChar <= 49061))
            {
                return "J";
            }
            else if ((iCnChar >= 49062) && (iCnChar <= 49323))
            {
                return "K";
            }
            else if ((iCnChar >= 49324) && (iCnChar <= 49895))
            {
                return "L";
            }
            else if ((iCnChar >= 49896) && (iCnChar <= 50370))
            {
                return "M";
            }
 
            else if ((iCnChar >= 50371) && (iCnChar <= 50613))
            {
                return "N";
            }
            else if ((iCnChar >= 50614) && (iCnChar <= 50621))
            {
                return "O";
            }
            else if ((iCnChar >= 50622) && (iCnChar <= 50905))
            {
                return "P";
            }
            else if ((iCnChar >= 50906) && (iCnChar <= 51386))
            {
                return "Q";
            }
            else if ((iCnChar >= 51387) && (iCnChar <= 51445))
            {
                return "R";
            }
            else if ((iCnChar >= 51446) && (iCnChar <= 52217))
            {
                return "S";
            }
            else if ((iCnChar >= 52218) && (iCnChar <= 52697))
            {
                return "T";
            }
            else if ((iCnChar >= 52698) && (iCnChar <= 52979))
            {
                return "W";
            }
            else if ((iCnChar >= 52980) && (iCnChar <= 53640))
            {
                return "X";
            }
            else if ((iCnChar >= 53689) && (iCnChar <= 54480))
            {
                return "Y";
            }
            else if ((iCnChar >= 54481) && (iCnChar <= 55289))
            {
                return "Z";
            }
            else return ("ZZ");
        }
 
        #endregion
 
        #region ■ 结构体_____________________________
 
        /// <summary>
        /// 地区码
        /// </summary>
        private class AreaCodeInfo
        {
            /// <summary>
            /// 首字母
            /// </summary>
            public string First = string.Empty;
            /// <summary>
            /// 名字
            /// </summary>
            public string Name = string.Empty;
            /// <summary>
            /// 地区码
            /// </summary>
            public string Code = string.Empty;
            /// <summary>
            /// Id
            /// </summary>
            public string Id = string.Empty;
        }
 
        #endregion
    }
}