CrabtreeOn,印度客户定制APP,迁移2.0平台版本
wxr
2023-01-09 0fafbe4b132d03dc8f1f99de585bfcb5e63ab60a
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
using System;
using System.Collections.Generic;
 
namespace Shared.SimpleControl.Phone
{
    /// <summary>
    /// 住宅列表页面
    /// </summary>
    public class GuideAddResidence : FrameLayout
    {
        public bool IsHideBack = false;
 
        bool IsNeedGoToGuideAddGatewayView = false;
 
        public override void RemoveFromParent ()
        {
            base.RemoveFromParent ();
            if (!IsHideBack) {
                //判断是否需要隐藏或者显示功能菜单按钮
                UserSettingView.CheckIfRefreshView ();
            }
        }
 
        public GuideAddResidence ()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        //bool isInitViewEnd = false;
        VerticalScrolViewLayout VerticalScrolViewMiddle;
 
        public void ShowHomeView (bool showDialog = false) {
            #region 标题
            var topView = new FrameLayout () {
                Y = Application.GetRealHeight (36),
                Height = Application.GetRealHeight (90),
            };
            AddChidren (topView);
 
            var title = new Button () {
                TextAlignment = TextAlignment.Center,
                Text = "Zones/Residences",
                TextSize = 19,
                TextColor = SkinStyle.Current.TextColor1
            };
            topView.AddChidren (title);
 
            Button ItemButton = new Button () {
                Width = Application.GetRealWidth (55),
                Height = Application.GetRealHeight (55),
                UnSelectedImagePath = "Item/+.png",
                SelectedImagePath = "Item/+.png",
                Y = Application.GetRealHeight (17),
                X = Application.GetRealWidth (640 - 80),
            };
            topView.AddChidren (ItemButton);
            ItemButton.MouseUpEventHandler += (sender1, e1) => {
                //GuideAddHomeDialog (new RegionInfoRes () { Id = "",  Name = "" }, false);
                GuideAddHomeDialog (new RegionInfoRes () { Id = "", Name = "" }, false);
            };
 
            //if (!IsHideBack) {
            var back = new Button () {
                Height = Application.GetRealHeight (90),
                Width = Application.GetRealWidth (85),
                UnSelectedImagePath = "Item/Back.png",
                SelectedImagePath = "Item/BackSelected.png",
            };
            topView.AddChidren (back);
            back.MouseUpEventHandler += (sender, e) => {
                if (IsHideBack) {
                    ShowIfLogoutAlter ();
                } else {
                    (Parent as PageLayout).PageIndex -= 1;
                }
 
            };
 
            //}
 
 
 
            #endregion
 
            var BodyView = new FrameLayout () {
                Width = LayoutParams.MatchParent,
                Height = Application.GetRealHeight (1136 - 126),
                Y = topView.Bottom,
                BackgroundColor = SkinStyle.Current.ViewColor,
            };
            AddChidren (BodyView);
 
            VerticalScrolViewMiddle = new VerticalScrolViewLayout ();
            BodyView.AddChidren (VerticalScrolViewMiddle);
 
            GetHomeList (showDialog);
 
        }
 
        /// <summary>
        /// 查询所有住宅列表
        /// </summary>
        /// <param name="showDialog"></param>
        void GetHomeList (bool showDialog = false)
        {
            
            new System.Threading.Thread (() => {
                bool needHide = true;
                try {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Start ("Please wait...");
                        new System.Threading.Thread (() => {
                            System.Threading.Thread.Sleep (10000);
                            if (needHide) {
                                MainPage.Loading.MouseUpEventHandler += (sd, dd) => {
                                    Application.RunOnMainThread (() => {
                                        this.RemoveFromParent ();
                                        MainPage.Loading.Hide ();
                                        needHide = false;
                                    });
                                };
                            }
                        }) { IsBackground = true }.Start ();
                    });
 
                    //获取住宅列表
                    var revertObjCode = HttpServerRequest.Current.GetHomePager ();
                    if (revertObjCode == StateCode.SUCCESS) {
                        var mHomeLists = UserConfig.Instance.HomeLists;
                        if (mHomeLists != null && mHomeLists.Count > 0) {
                            if (string.IsNullOrEmpty (UserConfig.Instance.HomeLists [0].Name)) {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Hide ();
                                    this.RemoveFromParent ();
                                    return;
                                });
                            }
                            UserConfig.Instance.SaveUserConfig ();
                            //2020-01-16
                            var mHome = mHomeLists.Find ((obj) => obj.Id == UserConfig.Instance.CurrentRegion.Id);
                            if (mHome != null) {
                                UserConfig.Instance.CurrentRegion = mHome;
                            } else {
                                UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0];
                            }
                        } else {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                                this.RemoveFromParent ();
                            });
                            return;
                        }
                    } else {
                        IMessageCommon.Current.ShowErrorInfoAlter (revertObjCode);
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                            this.RemoveFromParent ();
                            return;
                        });
                    }
                    //加载住宅界面
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        ShowHomeListView (showDialog);
                    });
                } catch (Exception ex) {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                        this.RemoveFromParent ();
                        needHide = false;
                    });
                    Utlis.WriteLine (ex.Message);
                } finally {
                    needHide = false;
                }
            }) { IsBackground = true }.Start ();
        }
 
        /// <summary>
        /// 加载刷新所有住宅
        /// </summary>
        /// <param name="ifNeedShowDialog"></param>
        void ShowHomeListView (bool ifNeedShowDialog = false)
        {
            try {
                VerticalScrolViewMiddle.RemoveAll ();
                foreach (var tempHome in UserConfig.Instance.HomeLists) {
                    AddResidenceListRow (tempHome, VerticalScrolViewMiddle);
                }
            } catch { }
 
 
            if (!UserConfig.Instance.CheckHomeGatewaysNotEmpty ()) {
                if (ifNeedShowDialog) {
                    GuideAddHomeDialog (UserConfig.Instance.HomeLists [0], true);
                    UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0];
                }
            }
            try {
                if (IsNeedGoToGuideAddGatewayView) {
                    IsNeedGoToGuideAddGatewayView = false;
                    if (UserConfig.Instance.CurrentRegion.IsOthreShare) {
                        //提示没有分享住宅的权限
                        new Alert ("", ErrorCode.InsufficientAuthority, "Close").Show ();
                        return;
                    }
                    GotoGuideAddGatewayView (UserConfig.Instance.CurrentRegion);
                }
            } catch { }
 
        }
 
        /// <summary>
        /// 提示用户是否退出登录
        /// </summary>
        void ShowIfLogoutAlter ()
        {
            var logoutAlert = new Alert ("", "Log out?", "No", "Yes");
            logoutAlert.Show ();
            logoutAlert.ResultEventHandler += (sender1, e1) => {
                if (e1) {
                    AccountLogout ();
                }
            };
 
        }
 
        /// <summary>
        /// 退出登录
        /// </summary>
        void AccountLogout ()
        {
            try {
                this.RemoveFromParent ();
                MainPage.LoginUser.LastTime = DateTime.Now.AddDays (-30);
                MainPage.LoginUser.SaveUserInfo ();
                new AccountLogin ().Show ();
            } catch {
 
            }
 
        }
 
        /// <summary>
        /// 加载界面
        /// </summary>
        /// <param name="home">Home.</param>
        /// <param name="mVerticalScrolViewMiddle">Vertical scrol view middle.</param>
        void AddResidenceListRow (RegionInfoRes home, VerticalScrolViewLayout mVerticalScrolViewMiddle)
        {
            var rowView = new RowLayout () {
                Height = Application.GetRealHeight (110),
            };
            mVerticalScrolViewMiddle.AddChidren (rowView);
 
            Button btnRowLine = new Button () {
                Y = Application.GetRealHeight (107),
                Height = Application.GetRealHeight (3),
                BackgroundColor = SkinStyle.Current.Black50Transparent
            };
            rowView.AddChidren (btnRowLine);
 
            var btnPoint = new Button () {
                Width = Application.GetRealWidth (10),
                Height = Application.GetRealWidth (10),
                X = Application.GetRealWidth (50),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Point.png",
                SelectedImagePath = "Item/Point.png",
                Visible = home.Id != UserConfig.Instance.CurrentRegion.Id
            };
            rowView.AddChidren (btnPoint);
 
            var btnName = new Button () {
                Width = Application.GetRealWidth (390),
                Text = home.Name,
                TextAlignment = TextAlignment.CenterLeft,
                X = Application.GetRealWidth (90),
                TextColor = SkinStyle.Current.TextColor1,
            };
            rowView.AddChidren (btnName);
 
            //显示分享按钮
            if (home.IsOthreShare) {
                Button FromSharingButton = new Button () {
                    X = Application.GetRealWidth (480),
                    Width = Application.GetRealWidth (30),
                    Height = Application.GetRealWidth (30),
                    Gravity = Gravity.CenterVertical,
                    UnSelectedImagePath = "CrabtreeAdd/FromSharing.png",
                    SelectedImagePath = "CrabtreeAdd/FromSharing.png",
                };
                rowView.AddChidren (FromSharingButton);
            }
 
            var goNextButton = new Button () {
                X = Application.GetRealWidth (500),
                Width = Application.GetRealWidth (120),
            };
            rowView.AddChidren (goNextButton);
 
            Button rightButton1 = new Button () {
                X = Application.GetRealWidth (550),
                Width = Application.GetRealWidth (28),
                Height = Application.GetRealHeight (40),
                Gravity = Gravity.CenterVertical,
                UnSelectedImagePath = "Item/Right.png",
                SelectedImagePath = "Item/RightSelected.png",
            };
            rowView.AddChidren (rightButton1);
 
            EventHandler<MouseEventArgs> eHandler = (sender, e) => {
                if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
                    if (home.IsOthreShare) {
                        //提示没有分享住宅的权限
                        new Alert ("", ErrorCode.InsufficientAuthority, "Close").Show ();
                        return;
                    }
 
                    GotoGuideAddGatewayView (home);
                } else {
                    ChangeRegion (home);
                }
            };
            rightButton1.MouseUpEventHandler += eHandler;
            goNextButton.MouseUpEventHandler += eHandler;
            btnName.MouseUpEventHandler += (sender, e) => {
                if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
                    if (home.IsOthreShare) {
                        //提示没有分享住宅的权限
                        new Alert ("", ErrorCode.InsufficientAuthority, "Close").Show ();
                        return;
                    }
                    GuideAddHomeDialog (home, false, true, btnName);
                } else {
                    ChangeRegion (home);
                }
            };
 
            if (home.Id == UserConfig.Instance.CurrentRegion.Id) {
                Button btnCheck = new Button () {
                    X = Application.GetRealWidth (20),
                    Y = Application.GetRealHeight (25),
                    Width = Application.GetRealWidth (72),
                    Height = Application.GetRealHeight (61),
                    UnSelectedImagePath = "Skin/Check.png",
                };
                rowView.AddChidren (btnCheck);
            }
            if (UserConfig.Instance.CurrentRegion.Id != home.Id) {
                Button btnDel = new Button () {
                    TextID = R.MyInternationalizationString.Del,
                    BackgroundColor = SkinStyle.Current.DelColor,
                };
                rowView.AddRightView (btnDel);
                btnDel.MouseUpEventHandler += (sd, ee) => {
                    if (home.IsOthreShare) {
                        //提示没有分享住宅的权限
                        new Alert ("", ErrorCode.InsufficientAuthority, "Close").Show ();
                        return;
                    }
 
                    Dialog dialog = new Dialog ();
 
                    FrameLayout dialogBody = new FrameLayout () {
                        Width = Application.GetRealWidth (500),
                        Height = Application.GetRealHeight (400),
                        Radius = 5,
                        BorderColor = SkinStyle.Current.Transparent,
                        BorderWidth = 0,
                        BackgroundColor = SkinStyle.Current.DialogColor,
                        Gravity = Gravity.Center,
                    };
                    dialog.AddChidren (dialogBody);
 
                    Button btnTitle = new Button () {
                        Height = Application.GetRealHeight (80),
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                        TextAlignment = TextAlignment.Center,
                        TextID = R.MyInternationalizationString.DeleteTheArea,
                        TextColor = SkinStyle.Current.DialogTextColor,
                    };
                    dialogBody.AddChidren (btnTitle);
                    TextView btnTip = new TextView () {
                        X = Application.GetRealWidth (50),
                        Y = btnTitle.Bottom,
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (120),
                        TextColor = SkinStyle.Current.TextColor,
                        TextID = R.MyInternationalizationString.TipDelArea,
                        Enable = false,
                        TextSize = 14,
                        IsMoreLines = true,
                    };
                    dialogBody.AddChidren (btnTip);
                    btnTip.IsMoreLines = true;
                    EditText etPw = new EditText () {
                        X = Application.GetRealWidth (50),
                        Y = btnTip.Bottom + Application.GetRealHeight (10),
                        Width = Application.GetRealWidth (400),
                        Height = Application.GetRealHeight (70),
                        TextColor = SkinStyle.Current.TextColor,
                        Radius = 5,
                        BorderColor = SkinStyle.Current.BorderColor,
                        BorderWidth = 1,
                        SecureTextEntry = true,
                    };
                    dialogBody.AddChidren (etPw);
                    etPw.EditorEnterAction += (sender) => {
                        Application.HideSoftInput ();
                    };
 
                    FrameLayout bottomView = new FrameLayout () {
                        Y = Application.GetRealHeight (400 - 88),
                        Height = Application.GetRealHeight (90),
                        BackgroundColor = SkinStyle.Current.Black50Transparent,
                    };
                    dialogBody.AddChidren (bottomView);
 
                    Button btnClose = new Button () {
                        Width = Application.GetRealWidth (250),
                        TextAlignment = TextAlignment.Center,
                        TextColor = SkinStyle.Current.DialogTextColor,
                        TextID = R.MyInternationalizationString.Close,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                    };
                    bottomView.AddChidren (btnClose);
                    btnClose.MouseUpEventHandler += (asdf, eell) => {
                        dialog.Close ();
                    };
                    Button btnConfirm = new Button () {
                        X = Application.GetRealWidth (252),
                        Width = Application.GetRealWidth (250),
                        TextColor = SkinStyle.Current.DialogTextColor,
                        TextID = R.MyInternationalizationString.confirm,
                        BackgroundColor = SkinStyle.Current.DialogTitle,
                        TextAlignment = TextAlignment.Center,
                    };
                    bottomView.AddChidren (btnConfirm);
                    btnConfirm.MouseUpEventHandler += (dou, ni) => {
                        string password = etPw.Text.Trim ();
                        if (string.IsNullOrEmpty (password)) {
                            Utlis.ShowAlertOnMainThread (Language.StringByID (R.MyInternationalizationString.PasswordIsEmpty));
                            return;
                        }
                        System.Threading.Tasks.Task.Run (() => {
                            try {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Start ("Please wait...");
                                });
                               
                                var revertObj = HttpServerRequest.Current.LoginByPassword (MainPage.LoginUser.AccountString, password);
                                if (revertObj.Code == StateCode.SUCCESS) {
                                    //删除住宅
                                    var revertObj2 = HttpServerRequest.Current.DeleteHome (home.Id);
                                    if (revertObj2.Code == StateCode.SUCCESS) {
                                        Application.RunOnMainThread (() => {
                                            GetHomeList ();
                                        });
                                    } else {
                                        IMessageCommon.Current.ShowErrorInfoAlter (revertObj2.Code);
                                    }
                                } else {
                                    Application.RunOnMainThread (() => {
                                        new Alert ("", "Password error, deletion failed.", "Close").Show ();
                                    });
                                }
                            } catch {
                                MainPage.FailureToServer ();
                            } finally {
                                Application.RunOnMainThread (() => {
                                    MainPage.Loading.Hide ();
                                    dialog.Close ();
                                });
                            }
                        });
                    };
 
                    dialog.Show ();
                };
            }
 
        }
 
     
 
        /// <summary>
        /// 启动本地模式
        /// </summary>
        void EnableLocalMode () {
            MainPage.AddTip ("Enable local mode.");
            Utlis.ShowAppLinkStatus (AppLinkStatus.WiFiOffline);
            CommonPage.IsRemote = false;
            SmartHome.MqttCommon.DisConnectRemote ("EnableLocalMode", false);
        }
 
        /// <summary>
        /// 启动远程模式
        /// </summary>
        void EnableRemoteMode ()
        {
            MainPage.AddTip ("Restore remote mode.");
            Utlis.ShowAppLinkStatus (AppLinkStatus.CloudUnlink);
            CommonPage.IsRemote = true;
        }
 
        /// <summary>
        ///  跳转网关搜索页面
        /// </summary>
        /// <param name="home"></param>
        void GotoGuideAddGatewayView (RegionInfoRes home) {
            try {
                //远程连接的时候禁止 搜索网关和添加设备
                if (CommonPage.IsRemote) {
                    ////当前处于远程连接模式,无法进入搜索网关页面。请先切换回本地连接模式。
                    //new Alert ("", "Currently in remote connection mode, unable to enter the search gateway page. Please switch back to local connection mode first.", Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    //return;
 
                    //2020-06-29 启动本地通信模式
                    EnableLocalMode ();
                }
 
 
                var addGatewayDialog = new GuideAddGateway ();
                (Parent as PageLayout).AddChidren (addGatewayDialog);
                addGatewayDialog.ShowPage (home);
                (Parent as PageLayout).PageIndex = (Parent as PageLayout).ChildrenCount - 1;
            } catch {
 
                Utlis.WriteLine ("============>GotoGuideAddGatewayView 跳转失败 ");
            }
            
        }
 
        /// <summary>
        /// 更换本地区域
        /// </summary>
        /// <param name="home">Home.</param>
        /// <param name="gatewayMAC">Gateway mac.</param>
        ///
        void ChangeRegion (RegionInfoRes home, bool goHomePage = true, bool bNeedCheckAddRoom = false, bool goToGuideAddGatewayView = false)
        {
            if (home == null || string.IsNullOrEmpty (home.Id)) return;
            Application.RunOnMainThread (() => {
                MainPage.Loading.Start ("Please wait...");
            });
            System.Threading.Tasks.Task.Run (() => {
                try {
                    var backuplist = IO.FileUtils.ReadFiles ();
                    int index = 0;
                    if (string.IsNullOrEmpty (UserConfig.Instance.CurrentRegion.Id)) {
                        UserConfig.Instance.CurrentRegion = UserConfig.Instance.HomeLists [0];
                    }
                    string oldRegionRootPath = IO.FileUtils.CreateRegionBackup (UserConfig.Instance.CurrentRegion.Id);
                    IO.FileUtils.DeleteRegionFiles (oldRegionRootPath);
                    foreach (var fileName in backuplist) {
                        System.IO.FileInfo fileInfo = new System.IO.FileInfo (IO.FileUtils.RootPath + fileName);
                        if (fileInfo.Exists) {
                            fileInfo.MoveTo (oldRegionRootPath + fileName);
                            Utlis.WriteLine ("move file : " + fileName);
                        }
                        index++;
                        Application.RunOnMainThread (() => {
                            int pro = (int)(index * 1.0 / backuplist.Count * 50);
                            MainPage.Loading.Text = pro.ToString () + "%";
                        });
                    }
                    IO.FileUtils.DeleteAllFile ();
                    string newRegionRootPath = IO.FileUtils.CreateRegionBackup (home.Id);
                    IO.FileUtils.RestoreRegionFiles (newRegionRootPath);
                    UserConfig.Instance.CurrentRegion = home;
                    UserConfig.Instance.SaveUserConfig ();
                    Room.InitAllRoom ();
                    Application.RunOnMainThread (() => {
                        if (goToGuideAddGatewayView) {
                            IsNeedGoToGuideAddGatewayView = true;
                        }
                        GetHomeList (false);
 
                        if (goHomePage) {
                            UserConfig.Instance.RefreshUserConfig ();
                            new Alert ("", Language.StringByID (R.MyInternationalizationString.SwitchRegionSuccessfully),
                                      Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                            //this.RemoveFromParent ();
                            //CommonPage.IsRemote = false;
                            EquipmentPublicClass.CheckLinkRemote (UserConfig.Instance.internetStatus);
                        }
                        if (bNeedCheckAddRoom) {
                            CheckIfNeedAddRoom ();
                        }
                    });
                    MainPage.LoginUser.SaveUserInfo ();
                } catch (Exception ex) {
                    Application.RunOnMainThread (() => {
                        new Alert ("", Language.StringByID (R.MyInternationalizationString.CheckInternet),
                                   Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    });
                    Utlis.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
        }
 
        /// <summary>
        /// 引导添加住宅
        /// </summary>
        void GuideAddHomeDialog (RegionInfoRes home, bool guide, bool isChangeName = false, Button button = null)
        {
            Dialog dialog = new Dialog ();
 
            var dialogView = new FrameLayout ();
            dialogView.MouseUpEventHandler += (sn, dd) => {
                if (guide)
                    return;
                Application.HideSoftInput ();
            };
            dialog.AddChidren (dialogView);
            var bodyView = new FrameLayout () {
                Gravity = Gravity.Center,
                Width = Application.GetRealWidth (500),
                Height = Application.GetRealHeight (400),
                Radius = 5,
                BorderColor = SkinStyle.Current.Transparent,
                BorderWidth = 0,
                BackgroundColor = SkinStyle.Current.DialogColor,
            };
            dialogView.AddChidren (bodyView);
 
            var btnTitle = new Button () {
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.Center,
                TextID = R.MyInternationalizationString.HonseManagement,
                TextColor = SkinStyle.Current.DialogTextColor,
                BackgroundColor = SkinStyle.Current.DialogTitle,
            };
            bodyView.AddChidren (btnTitle);
 
            var btnZoneName = new Button () {
                X = Application.GetRealWidth (50),
                Y = btnTitle.Bottom + Application.GetRealHeight (10),
                Width = Application.GetRealWidth (400),
                Height = Application.GetRealHeight (80),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = SkinStyle.Current.TextColor,
                TextID = R.MyInternationalizationString.ResidentialName
            };
            bodyView.AddChidren (btnZoneName);
 
            var etZoneName = new EditText () {
                X = Application.GetRealWidth (50),
                Y = btnZoneName.Bottom + Application.GetRealHeight (10),
                Width = Application.GetRealWidth (400),
                Height = Application.GetRealHeight (80),
                TextColor = SkinStyle.Current.TextColor,
                TextAlignment = TextAlignment.Center,
                Radius = 5,
                BorderColor = SkinStyle.Current.BorderColor,
                BorderWidth = 2,
                Text = home.Name,
            };
            bodyView.AddChidren (etZoneName);
 
            etZoneName.EditorEnterAction += (obj) => {
                Application.HideSoftInput ();
            };
            //if (MainPage.LoginUser.AccountType == 1) {
            //    etZoneName.Enable = false;
            //    etZoneName.BorderWidth = 0;
            //}
 
            var BottomView = new FrameLayout () {
                Y = Application.GetRealHeight (400 - 88),
                Height = Application.GetRealHeight (90),
                BackgroundColor = SkinStyle.Current.Black50Transparent,
            };
            bodyView.AddChidren (BottomView);
 
            var btnOption = new Button () {
                TextID = R.MyInternationalizationString.OK,
                TextColor = SkinStyle.Current.DialogTextColor,
                BackgroundColor = SkinStyle.Current.DialogTitle,
                TextAlignment = TextAlignment.Center,
            };
            btnOption.MouseUpEventHandler += (sdf, fds) => {
                dialog.Close ();
                var homeName = etZoneName.Text.Trim ();
                if (homeName == "") {
                    new Alert ("", Language.StringByID (R.MyInternationalizationString.PleaseEnterZoneName), Language.StringByID (R.MyInternationalizationString.Close)).Show ();
                    return;
                }
 
                //2020-04-26
                MainPage.Loading.Start ("Please wait...");
                System.Threading.Tasks.Task.Run (() => {
                    try {
                        if (isChangeName) {
                            var revertObj2 = HttpServerRequest.Current.EditResidenceInfo (0, homeName);
                            if (revertObj2.Code != StateCode.SUCCESS) {
                                IMessageCommon.Current.ShowErrorInfoAlter (revertObj2.Code);
                                return;
                            }
                            Application.RunOnMainThread (() => {
                                home.Name = button.Text = homeName;
                            });
                            //IO.FileUtils.ReNameFoler (UserConfig.Instance.CurrentRegion.Name, home.Name);
                            return;
                        }
 
                        EditResidence (home, homeName, guide);
 
                    } catch {
                        //MainPage.FailureToServer ();
                    } finally {
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                        });
                    }
                });
 
            };
 
            if(!guide)
            {
                var btnClose = new Button () { 
                    Width = Application.GetRealWidth(250),
                    TextID = R.MyInternationalizationString.cancel,
                    TextColor = SkinStyle.Current.DialogTextColor,
                    BackgroundColor = SkinStyle.Current.DialogTitle,
                    TextAlignment = TextAlignment.Center,
                };
                BottomView.AddChidren (btnClose);
                btnClose.MouseUpEventHandler += (bb, cc) => {
                    dialog.Close ();
                };
 
                btnOption.X = btnClose.Right + 1;
                btnOption.Width = Application.GetRealWidth (250);
 
            }
            BottomView.AddChidren (btnOption);
 
            dialog.Show ();
 
        }
 
        /// <summary>
        /// 添加住宅,完成之后进入搜索网关界面
        /// </summary>
        /// <param name="home">Home.</param>
        /// <param name="residenceName">Et zone name string.</param>
        bool EditResidence (RegionInfoRes home, string residenceName, bool goToGuideAddGatewayView = false)
        {
            if (home.Name != residenceName) {
               
                if (string.IsNullOrEmpty(home.Id)) {
                    var revertObj21 = HttpServerRequest.Current.AddHome (residenceName);
                    if (revertObj21.Code != StateCode.SUCCESS) {
                        IMessageCommon.Current.ShowErrorInfoAlter (revertObj21.Code);
                        return false;
                    }
 
                    home.Name = residenceName;
                    //创建住宅成功,切换到该住宅
                    var newHome = Newtonsoft.Json.JsonConvert.DeserializeObject<RegionInfoResNew> (revertObj21.Data.ToString ());
                    if (newHome != null && !string.IsNullOrEmpty(newHome.Id)) {
                        home = HttpServerRequest.Current.RegionInfoResNewToHome (newHome);
                        if (UserConfig.Instance.HomeLists == null) {
                            UserConfig.Instance.HomeLists = new List<RegionInfoRes> ();
                        }
                        UserConfig.Instance.HomeLists.Add (home);
                        UserConfig.Instance.SaveUserConfig ();
                        ChangeRegion (home, true, true, goToGuideAddGatewayView);
                    } 
                } else {
                    var revertObj2 = HttpServerRequest.Current.EditResidenceInfo (0, residenceName);
                    if (revertObj2.Code != StateCode.SUCCESS) {
                        IMessageCommon.Current.ShowErrorInfoAlter (revertObj2.Code);
                        return false;
                    }
                    home.Name = residenceName;
                    //IO.FileUtils.ReNameFoler (UserConfig.Instance.CurrentRegion.Name, home.Name);
                    ChangeRegion (home, false, true, goToGuideAddGatewayView);
                    //CheckIfNeedAddRoom ();
                }
            }else {
                ChangeRegion (home, false, true, goToGuideAddGatewayView);
            }
            return true;
        
        }
 
        /// <summary>
        /// 检测是否默认创建房间
        /// </summary>
        void CheckIfNeedAddRoom () {
            if (Room.Lists.FindAll ((obj) => obj.Name == "Living Room" || obj.Name == "Bedroom").Count == 0) {
                Room room = new Room ();
                string newRoomFilePath = typeof (Room).Name + "_Living Room";
                room.Name = "Living Room";
                room.BackGroundImage = "Room/r1.png";
                room.Add (newRoomFilePath);
                room.Save (newRoomFilePath);
                Room.Lists.Add (room);
 
                room = new Room ();
                newRoomFilePath = typeof (Room).Name + "_Bedroom";
                room.Name = "Bedroom";
                room.BackGroundImage = "Room/r5.png";
                room.Add (newRoomFilePath);
                room.Save (newRoomFilePath);
                Room.Lists.Add (room);
 
                Room.InitAllRoom ();
            }
        }
 
 
        
 
     
    }
}