xm
2020-04-29 07466c19110693e3e439a7d7c8ad0bc21d9b3287
ZigbeeApp/Shared/Phone/UserCenter/Residence/ResidenceListForm.cs
@@ -16,9 +16,13 @@
        /// </summary>
        private VerticalListControl listView = null;
        /// <summary>
        /// 住宅数据
        /// 住宅数据(有网时使用)
        /// </summary>
        private Dictionary<string, Common.ResponseEntity.ResidenceObj> dicResidenceData = null;
        /// <summary>
        /// 住宅数据(无网时使用)
        /// </summary>
        private List<House> listLocalHouse = null;
        /// <summary>
        /// 住宅顺序
        /// </summary>
@@ -36,16 +40,20 @@
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.ResidenceManagement));
            //右上添加按钮
            var btnTopIcon = new MostRightIconControl(69, 69);
            btnTopIcon.UnSelectedImagePath = "Item/Add.png";
            topFrameLayout.AddChidren(btnTopIcon);
            btnTopIcon.InitControl();
            btnTopIcon.ButtonClickEvent += (sender, e) =>
            //虚拟住宅无法新建住宅
            if (Common.Config.Instance.Home.IsVirtually == false)
            {
                //显示添加住宅名称的画面
                this.ShowAddNameForm();
            };
                //右上添加按钮
                var btnTopIcon = new MostRightIconControl(69, 69);
                btnTopIcon.UnSelectedImagePath = "Item/Add.png";
                topFrameLayout.AddChidren(btnTopIcon);
                btnTopIcon.InitControl();
                btnTopIcon.ButtonClickEvent += (sender, e) =>
                {
                    //显示添加住宅名称的画面
                    this.ShowAddNameForm();
                };
            }
            //初始化中部控件
            this.InitMiddleFrame();
@@ -64,7 +72,6 @@
            if (strData != null)
            {
                listResidenceSort = Newtonsoft.Json.JsonConvert.DeserializeObject<List<string>>(strData);
                strData = null;
            }
            listView = new VerticalListControl(29);
@@ -72,37 +79,68 @@
            listView.BackgroundColor = UserCenterColor.Current.White;
            bodyFrameLayout.AddChidren(listView);
            HdlThreadLogic.Current.RunThread(() =>
            //设置住宅信息
            this.SetResidenceInfo(1);
        }
        /// <summary>
        /// 设置住宅信息(1:先读取云端,无法联网,再读取本地  2:只读云端  3:只读本地)
        /// </summary>
        /// <param name="div">1:先读取云端,无法联网,再读取本地  2:只读云端  3:只读本地</param>
        private void SetResidenceInfo(int div)
        {
            HdlThreadLogic.Current.RunThread(async () =>
            {
                //设置住宅信息
                this.SetResidenceInfo();
                //虚拟住宅的话,只读本地
                if (div == 3 || Config.Instance.Home.IsVirtually == true)
                {
                    //只读本地
                    this.SetResidenceInfoByOffline();
                }
                else
                {
                    //开启进度条
                    this.ShowProgressBar();
                    //设置住宅信息(有网情况)
                    var result = await this.SetResidenceInfoByOnline();
                    if (result == true)
                    {
                        //云端读取成功
                        this.CloseProgressBar();
                        return;
                    }
                    if (div == 2)
                    {
                        //如果只读云端 关闭进度条
                        this.CloseProgressBar(ShowReLoadMode.YES);
                        return;
                    }
                    //如果在没网的情况下,从文件夹路径中获取全部住宅,然后显示
                    this.SetResidenceInfoByOffline();
                    //关闭进度条
                    this.CloseProgressBar();
                }
            });
        }
        #endregion
        #region ■ 设置住宅信息_______________________
        #region ■ 设置住宅信息(有网情况)_____________
        /// <summary>
        /// 设置住宅信息
        /// 设置住宅信息(有网情况)
        /// </summary>
        private async void SetResidenceInfo()
        private async System.Threading.Tasks.Task<bool> SetResidenceInfoByOnline()
        {
            //开启进度条
            this.ShowProgressBar();
            //获取住宅信息
            var Pra = new GetResidenceInfoPra();
            string resultData = await UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomePager", false, Pra);
            var listNotCheck = new List<string>() { "NotSetAgain" };
            string resultData = await UserCenterLogic.GetResponseDataByRequestHttps("App/GetHomePager", false, Pra, listNotCheck);
            if (resultData == null)
            {
                //关闭进度条
                this.CloseProgressBar(ShowReLoadMode.YES);
                return;
                return false;
            }
            //关闭进度条
            this.CloseProgressBar();
            var pageData = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.ResponseEntity.ResidenceRes>(resultData);
            //暂时存储数据
@@ -135,7 +173,7 @@
                {
                    return;
                }
                listView.Height = bodyFrameLayout.Height + Application.GetRealHeight(6);
                listView.Height = bodyFrameLayout.Height;
                this.dicResidenceData = new Dictionary<string, Common.ResponseEntity.ResidenceObj>();
                for (int i = 0; i < listResidenceSort.Count; i++)
@@ -148,22 +186,20 @@
                    //收集该账号住宅的数据
                    this.dicResidenceData[info.Id] = info;
                    this.AddRowLayout(info, i != listResidenceSort.Count - 1);
                    //添加住宅的行(有网情况)
                    this.AddRowLayoutByOnline(info, i != listResidenceSort.Count - 1);
                }
                //调整列表控件的高度
                this.listView.AdjustRealHeight(Application.GetRealHeight(23));
            });
            return true;
        }
        #endregion
        #region ■ 添加住宅的行_______________________
        /// <summary>
        /// 添加住宅的行
        /// 添加住宅的行(有网情况)
        /// </summary>
        /// <param name="info"></param>
        private void AddRowLayout(Common.ResponseEntity.ResidenceObj info, bool addLine)
        private void AddRowLayoutByOnline(Common.ResponseEntity.ResidenceObj info, bool addLine)
        {
            var rowLayout = new RowLayoutControl(listView.rowSpace / 2);
            this.listView.AddChidren(rowLayout);
@@ -216,6 +252,7 @@
                    {
                        HdlThreadLogic.Current.RunThread(async () =>
                        {
                            //开启进度条
                            this.ShowProgressBar();
                            //检测本地有没有这个住宅
@@ -228,13 +265,13 @@
                            //刷新个人中心的内存及线程
                            await UserCenterLogic.InitUserCenterMenmoryAndThread(false);
                            HdlRoomLogic.Current.InitAllRoom();
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                //重新刷新列表
                                this.listView.RemoveAll();
                                this.SetResidenceInfo();
                                this.SetResidenceInfo(2);
                                //关闭进度条
                                this.CloseProgressBar();
                            });
                        });
                    });
@@ -259,7 +296,132 @@
            btnChanged.ButtonClickEvent += (sender, e) =>
            {
                //显示编辑名称界面
                this.ShowEditorNameForm(info, btnHome);
                this.ShowEditorNameForm(info, null, btnHome);
            };
        }
        #endregion
        #region ■ 设置住宅信息(无网情况)_____________
        /// <summary>
        /// 设置住宅信息(无网情况)
        /// </summary>
        private void SetResidenceInfoByOffline()
        {
            //从文件夹中获取全部的住宅对象
            this.listLocalHouse = HdlResidenceLogic.Current.GetAllLocalResidenceListByDirectory();
            if (listLocalHouse.Count == 0)
            {
                return;
            }
            HdlThreadLogic.Current.RunMain(() =>
            {
                listView.Height = bodyFrameLayout.Height;
                for (int i = 0; i < listLocalHouse.Count; i++)
                {
                    //添加住宅的行(无网情况)
                    this.AddRowLayoutByOffline(listLocalHouse[i], i != listLocalHouse.Count - 1);
                }
                //调整列表控件的高度
                this.listView.AdjustRealHeight(Application.GetRealHeight(23));
            });
        }
        /// <summary>
        /// 添加住宅的行(无网情况)
        /// </summary>
        /// <param name="i_home"></param>
        private void AddRowLayoutByOffline(House i_home, bool addLine)
        {
            var rowLayout = new RowLayoutControl(listView.rowSpace / 2);
            this.listView.AddChidren(rowLayout);
            //图标
            var btnIcon = rowLayout.frameTable.AddLeftIcon(81);
            btnIcon.UnSelectedImagePath = "Item/ResidenceIcon.png";
            //当前住宅
            NormalViewControl btnHome = null;
            if (i_home.Id == Config.Instance.Home.Id)
            {
                //住宅名称
                btnHome = rowLayout.frameTable.AddLeftCaption(i_home.Name, 800, 60, true);
                btnHome.TextSize = 15;
                btnHome.Y = Application.GetRealHeight(12) + rowLayout.chidrenYaxis;
                rowLayout.frameTable.AddChidren(btnHome, ChidrenBindMode.BindEventOnly);
                //当前住宅
                var btnNowView = rowLayout.frameTable.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowResidence), 800, 49, true);
                btnNowView.Y = Application.GetRealHeight(72) + rowLayout.chidrenYaxis;
                btnNowView.TextSize = 12;
                btnNowView.TextColor = UserCenterColor.Current.TextGrayColor1;
                rowLayout.frameTable.AddChidren(btnNowView, ChidrenBindMode.BindEventOnly);
            }
            else
            {
                //住宅名称
                btnHome = rowLayout.frameTable.AddLeftCaption(i_home.Name, 800, true);
                btnHome.TextSize = 15;
            }
            //右箭头
            rowLayout.frameTable.AddRightArrow();
            if (addLine == true)
            {
                //底线
                rowLayout.frameTable.AddBottomLine();
            }
            rowLayout.frameTable.ButtonClickEvent += (sender, e) =>
            {
                if (i_home.Id != Config.Instance.Home.Id)
                {
                    //确认切换当前住宅到「」?
                    string msg = Language.StringByID(R.MyInternationalizationString.uSwitchResidenceMsg);
                    if (msg.Contains("{0}") == true)
                    {
                        msg = string.Format(msg, i_home.Name);
                    }
                    this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                    {
                        HdlThreadLogic.Current.RunThread(async () =>
                        {
                            this.ShowProgressBar();
                            Config.Instance.HomeId = i_home.Id;
                            //重新初始化住宅对象
                            Config.Instance.Home = House.GetHouseByHouseId(i_home.Id);
                            Config.Instance.Save();
                            //刷新个人中心的内存及线程
                            await UserCenterLogic.InitUserCenterMenmoryAndThread(false);
                            HdlThreadLogic.Current.RunMain(() =>
                            {
                                //重新刷新列表
                                this.listView.RemoveAll();
                                this.SetResidenceInfo(3);
                                //关闭进度条
                                this.CloseProgressBar();
                            });
                        });
                    });
                }
                else
                {
                    //检测能否删除住宅
                    bool flage = this.CheckIsCanDeleteResidence(i_home);
                    var form = new ResidenceManagementForm();
                    form.AddForm(flage);
                }
            };
            //更改
            var btnChanged = rowLayout.AddEditorControl();
            btnChanged.TextID = R.MyInternationalizationString.uChanged1;
            btnChanged.ButtonClickEvent += (sender, e) =>
            {
                //显示编辑名称界面
                this.ShowEditorNameForm(null, i_home, btnHome);
            };
        }
@@ -270,16 +432,18 @@
        /// <summary>
        /// 显示编辑住宅名称界面
        /// </summary>
        /// <param name="info"></param>
        private void ShowEditorNameForm(Common.ResponseEntity.ResidenceObj info, NormalViewControl btnHome)
        /// <param name="info">有网时使用</param>
        /// <param name="i_house">无网时适用</param>
        private void ShowEditorNameForm(Common.ResponseEntity.ResidenceObj info, House i_house, NormalViewControl btnHome)
        {
            string oldName = info != null ? info.Name : i_house.Name;
            //生成一个弹窗画面
            var dialogForm = new DialogInputControl();
            //编辑住宅
            dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uChangedName));
            //请输入住宅名称
            dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInputResidenceName));
            dialogForm.Text = info.Name;
            dialogForm.Text = oldName;
            //按下确认按钮
            dialogForm.ComfirmClickEvent += ((textValue) =>
@@ -293,11 +457,25 @@
                }
                //画面关闭
                dialogForm.CloseDialog();
                if (info.Name != textValue)
                if (oldName != textValue)
                {
                    //编辑云端的住宅名称
                    this.EditorResidenceName(textValue, info);
                    if (info != null)
                    {
                        //编辑云端的住宅名称
                        this.EditorResidenceName(textValue, info, btnHome);
                    }
                    else
                    {
                        //直接编辑住宅缓存
                        if (this.CheckIsCanSaveResidence(i_house.Id, textValue, true) == false)
                        {
                            return;
                        }
                        //刷新内存的住宅名
                        House.EditorHouseByHouseId(i_house.Id, textValue);
                        //更改显示的名字
                        btnHome.Text = textValue;
                    }
                }
            });
        }
@@ -308,7 +486,7 @@
        /// <param name="ResidenceName"></param>
        /// <param name="info"></param>
        /// <returns></returns>
        private async void EditorResidenceName(string ResidenceName, Common.ResponseEntity.ResidenceObj info)
        private async void EditorResidenceName(string ResidenceName, Common.ResponseEntity.ResidenceObj info, NormalViewControl btnHome)
        {
            //检测能否保存住宅
            if (this.CheckIsCanSaveResidence(info.Id, ResidenceName, true) == false)
@@ -321,23 +499,21 @@
            var Pra = new EditorResidencePra();
            Pra.HomeId = info.Id;
            Pra.Name = ResidenceName;
            if (info.IsOthreShare == true)
            {
                Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
            }
            Pra.IsOtherAccountCtrl = false;
            Pra.LoginAccessToken = Config.Instance.Token;
            //编辑住宅
            bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", info.IsOthreShare, Pra);
            if (flage == false)
            bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", false, Pra);
            //关闭进度条
            this.CloseProgressBar();
            if (flage == true)
            {
                //关闭进度条
                this.CloseProgressBar();
                return;
                //刷新内存的住宅名
                House.EditorHouseByHouseId(info.Id, ResidenceName);
                //更改显示的名字
                btnHome.Text = ResidenceName;
            }
            //刷新内存的住宅名
            House.EditorHouseByHouseId(info.Id, ResidenceName);
            //重新刷新列表
            this.listView.RemoveAll();
            this.SetResidenceInfo();
        }
        #endregion
@@ -404,7 +580,7 @@
            House.AddHouse(newInfo[0].Id, ResidenceName, 0);
            //重新刷新列表
            this.listView.RemoveAll();
            this.SetResidenceInfo();
            this.SetResidenceInfo(2);
        }
        #endregion
@@ -442,7 +618,7 @@
        }
        /// <summary>
        /// 检测能否删除住宅
        /// 检测能否删除住宅(有网使用)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
@@ -468,6 +644,36 @@
        }
        /// <summary>
        /// 检测能否删除住宅(无网使用)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private bool CheckIsCanDeleteResidence(House i_house)
        {
            if (i_house.IsVirtually == false)
            {
                //非虚拟住宅都不允许删除,只有在能联网的时候,才能删除
                return false;
            }
            int myCount = 0;
            foreach (var data in this.listLocalHouse)
            {
                if (data.IsVirtually == true)
                {
                    myCount++;
                }
            }
            //如果此账号下只剩下唯一一个虚拟的住宅,则不能再删除
            if (myCount == 1)
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 检测能否保存住宅
        /// </summary>
        /// <param name="residenceId">住宅ID</param>
@@ -476,21 +682,43 @@
        /// <returns></returns>
        private bool CheckIsCanSaveResidence(string residenceId, string residenceName, bool isEditor)
        {
            //住宅名字重复检测
            foreach (string strId in this.dicResidenceData.Keys)
            if (this.dicResidenceData != null)
            {
                if (this.dicResidenceData[strId].IsOthreShare == true)
                //住宅名字重复检测
                foreach (string strId in this.dicResidenceData.Keys)
                {
                    //分享的住宅不考虑
                    continue;
                    if (this.dicResidenceData[strId].IsOthreShare == true)
                    {
                        //分享的住宅不考虑
                        continue;
                    }
                    if (residenceId != strId && residenceName == this.dicResidenceData[strId].Name)
                    {
                        //住宅名字已经存在
                        int msgId = isEditor == true ? R.MyInternationalizationString.EditZigbeeHome_Exist : R.MyInternationalizationString.AddZigbeeHome_Exist;
                        string msg = Language.StringByID(msgId);
                        this.ShowMassage(ShowMsgType.Error, msg);
                        return false;
                    }
                }
                if (residenceId != strId && residenceName == this.dicResidenceData[strId].Name)
            }
            else
            {
                foreach (var myHouse in this.listLocalHouse)
                {
                    //住宅名字已经存在
                    int msgId = isEditor == true ? R.MyInternationalizationString.EditZigbeeHome_Exist : R.MyInternationalizationString.AddZigbeeHome_Exist;
                    string msg = Language.StringByID(msgId);
                    this.ShowMassage(ShowMsgType.Error, msg);
                    return false;
                    if (myHouse.IsOthreShare == true)
                    {
                        //分享的住宅不考虑
                        continue;
                    }
                    if (residenceId != myHouse.Id && residenceName == myHouse.Name)
                    {
                        //住宅名字已经存在
                        int msgId = isEditor == true ? R.MyInternationalizationString.EditZigbeeHome_Exist : R.MyInternationalizationString.AddZigbeeHome_Exist;
                        string msg = Language.StringByID(msgId);
                        this.ShowMassage(ShowMsgType.Error, msg);
                        return false;
                    }
                }
            }
            return true;
@@ -505,22 +733,38 @@
        /// </summary>
        public override int FormActionAgainEvent()
        {
            foreach (string keys in dicResidenceData.Keys)
            if (dicResidenceData != null)
            {
                if (Config.Instance.HomeFilePathList.Contains($"House_{keys}.json") == false)
                foreach (string keys in dicResidenceData.Keys)
                {
                    //保存顺序
                    listResidenceSort.Remove(keys);
                    HdlRoomLogic.Current.SaveFloorSort(listResidenceSort);
                    this.listView.RemoveAll();
                    //数据已经变更
                    HdlThreadLogic.Current.RunThread(() =>
                    if (Config.Instance.HomeFilePathList.Contains($"House_{keys}.json") == false)
                    {
                        //重新设置住宅信息
                        this.SetResidenceInfo();
                    });
                    break;
                        //保存顺序
                        listResidenceSort.Remove(keys);
                        HdlRoomLogic.Current.SaveFloorSort(listResidenceSort);
                        this.listView.RemoveAll();
                        //数据已经变更 重新设置住宅信息
                        this.SetResidenceInfo(2);
                        break;
                    }
                }
            }
            else
            {
                //检测还有这个住宅文件吗
                var strPath = System.IO.Path.Combine(Shared.IO.FileUtils.RootPath, Common.Config.Instance.Guid);
                foreach (var myHouse in this.listLocalHouse)
                {
                    string fullName = System.IO.Path.Combine(strPath, myHouse.FileName);
                    //如果不存在了的话
                    if (System.IO.File.Exists(fullName) == false)
                    {
                        //数据已经变更 重新设置住宅信息
                        this.listView.RemoveAll();
                        this.SetResidenceInfo(3);
                        return 1;
                    }
                }
            }
            return 1;