using System; using System.Collections.Generic; using System.Text; namespace Shared.Phone.UserCenter.Residence { /// /// 住宅管理界面 /// public class ResidenceManagementForm : EditorCommonForm { #region ■ 变量声明___________________________ /// /// 桌布控件 /// private NormalFrameLayout frameTable = null; /// /// 列表控件(白色背景) /// private FrameListControl listView = null; /// /// 整个界面的上下滑动控件 /// private VerticalFrameControl listBackContr = null; /// /// 楼层顺序 /// private List listFloorSort = new List(); /// /// 房间顺序 /// private List listRoomSort = new List(); /// /// 当前APP的住宅模式 0:还没有设置有模式 1:楼层模式 2:没有楼层模式 /// private int AppResidenceMode = 0; #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// /// 能否删除当前住宅 public void ShowForm(bool i_CanDeleteResidence) { //设置头部信息 base.SetTitleText(Common.Config.Instance.Home.Name); //主人或者管理员才会出现这个菜单 if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2) { //初始化右上角菜单 this.InitTopRightMenu(); } //初始化中部信息 this.InitMiddleFrame(i_CanDeleteResidence); } /// /// 初始化中部信息 /// /// 能否删除当前住宅 private void InitMiddleFrame(bool i_CanDeleteResidence) { //清空bodyFrame this.ClearBodyFrame(); //整个界面的上下滑动控件 this.listBackContr = new VerticalFrameControl(); listBackContr.Height = bodyFrameLayout.Height; bodyFrameLayout.AddChidren(listBackContr); //住宅行 var rowHome = new RowLayoutControl(); rowHome.Height = Application.GetRealHeight(173); rowHome.BackgroundColor = UserCenterColor.Current.White; listBackContr.frameTable.AddChidren(rowHome); rowHome.frameTable.UseClickStatu = false; var btnAdreeIcon = rowHome.frameTable.AddLeftIcon(81); btnAdreeIcon.UnSelectedImagePath = "Item/FixedPositionSelected.png"; //住宅名称 var btnHomeName = rowHome.frameTable.AddLeftCaption(Common.Config.Instance.Home.Name, 700, 60); btnHomeName.TextSize = 15; btnHomeName.Y = Application.GetRealHeight(35); rowHome.frameTable.AddChidren(btnHomeName, ChidrenBindMode.BindEvent); //当前住宅 var btnNowView = rowHome.frameTable.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uNowResidence), 700, 49); btnNowView.TextSize = 12; btnNowView.Y = Application.GetRealHeight(95); btnNowView.TextColor = UserCenterColor.Current.TextGrayColor1; rowHome.frameTable.AddChidren(btnNowView, ChidrenBindMode.BindEvent); //主人或者管理员才会出现这个菜单 if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2) { //右箭头 rowHome.frameTable.AddRightArrow(); } //经纬度 var btnLocation = rowHome.frameTable.AddMostRightView(string.Empty, 500); btnLocation.IsMoreLines = true; btnLocation.TextSize = 10; btnLocation.Text = Common.Config.Instance.Home.ResidenceAddressName; //主人或者管理员才会出现这个菜单 if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2) { rowHome.frameTable.UseClickStatu = true; rowHome.frameTable.ButtonClickEvent += (sender, e) => { //默认值 23.134421,113.267189 double latitude = Common.Config.Instance.Home.Latitude; double longitude = Common.Config.Instance.Home.Longitude; //地图 GDMapKit.Show((mLatitude, mLongitude, mRadius, name) => { HdlThreadLogic.Current.RunThread(() => { //保存住宅地址 this.SaveResidenceAdrress(btnLocation, mLatitude, mLongitude, name); }); }, Language.StringByID(R.MyInternationalizationString.uSelectAdrress), Language.StringByID(R.MyInternationalizationString.uSave), true, latitude, longitude, 100); }; } if (i_CanDeleteResidence == true) { //删除 var btnDelete = new NormalViewControl(Application.GetRealWidth(184), rowHome.Height, false); btnDelete.BackgroundColor = 0xfff75858; btnDelete.TextAlignment = TextAlignment.Center; btnDelete.TextColor = UserCenterColor.Current.White; btnDelete.TextSize = 12; btnDelete.TextID = R.MyInternationalizationString.uDelete; rowHome.AddRightView(btnDelete); btnDelete.ButtonClickEvent += (sender, e) => { //确认删除住宅及所有楼层、房间? string msg = Language.StringByID(R.MyInternationalizationString.uDeleteResidenceMsg); this.ShowMassage(ShowMsgType.Confirm, msg, () => { HdlThreadLogic.Current.RunThread(() => { this.DeleteResidence(); }); }); }; } //初始化桌布 this.frameTable = new NormalFrameLayout(); frameTable.Y = rowHome.Bottom + Application.GetRealHeight(23); frameTable.Height = listBackContr.frameTable.Height - rowHome.Bottom - Application.GetRealHeight(23); listBackContr.frameTable.AddChidren(frameTable); //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); } /// /// 根据指定模式,初始化桌布控件 /// private void InitFrameTableByMode() { this.frameTable.RemoveAll(); //0:还没有设置有模式 1:楼层模式 2:没有楼层模式 AppResidenceMode = this.CheckAppResidenceMode(); if (AppResidenceMode == 1) { //初始化楼层模式 this.InitControlByFloorMode(); } else if (AppResidenceMode == 2) { //初始化无楼层模式 this.InitControlByNotFloorMode(); } } #endregion #region ■ 右上角菜单_________________________ /// /// 初始化右上角菜单 /// private void InitTopRightMenu() { var btnIcon = new MostRightIconControl(69, 69); btnIcon.UnSelectedImagePath = "Item/Add.png"; topFrameLayout.AddChidren(btnIcon); btnIcon.InitControl(); btnIcon.ButtonClickEvent += ((sender, e) => { //0:还没有设置有模式 1:楼层模式 2:没有楼层模式 AppResidenceMode = this.CheckAppResidenceMode(); //没有设置过模式 if (AppResidenceMode == 0) { var frame = new TopRightMenuControl(2, 2); //创建楼层 frame.AddRowMenu(Language.StringByID(R.MyInternationalizationString.uCreatFloor), "Item/CreatFloor.png", "Item/CreatFloorSelected.png", () => { //创建楼层 this.CreatOrEditorFloor(null, null, string.Empty, string.Empty); }); //创建房间 frame.AddRowMenu(Language.StringByID(R.MyInternationalizationString.uCreatRoom), "Item/RoomIcon.png", "Item/RoomIconSelected.png", () => { var form = new AddNewRoomForm(); form.AddForm(string.Empty); form.FinishEditorEvent += () => { //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); }; }); } //楼层模式 else if (AppResidenceMode == 1) { //创建楼层 this.CreatOrEditorFloor(null, null, string.Empty, string.Empty); } //无楼层模式 else if (AppResidenceMode == 2) { var form = new AddNewRoomForm(); form.AddForm(string.Empty); form.FinishEditorEvent += () => { //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); }; } }); } #endregion #region ■ 初始化楼层模式_____________________ /// /// 初始化楼层模式 /// private void InitControlByFloorMode() { var frameFloor = new FrameRowControl(); frameFloor.UseClickStatu = false; frameFloor.Height = Application.GetRealHeight(115); frameFloor.BackgroundColor = UserCenterColor.Current.White; this.frameTable.AddChidren(frameFloor); //楼层 var btnFloor = frameFloor.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uFloor), 200, 60); btnFloor.Y = Application.GetRealHeight(49); btnFloor.TextColor = UserCenterColor.Current.TextColor2; btnFloor.TextSize = 15; frameFloor.AddChidren(btnFloor, ChidrenBindMode.NotBind); listView = new FrameListControl(29); listView.BackgroundColor = UserCenterColor.Current.White; listView.Y = frameFloor.Bottom; listView.Height = frameTable.Height - frameFloor.Bottom; this.frameTable.AddChidren(listView); //初始化楼层行 this.InitFloorRow(); } /// /// 初始化楼层行 /// private void InitFloorRow() { //获取楼层 var dicFloor = HdlRoomLogic.Current.GetFloorSortList(); this.listFloorSort.Clear(); foreach (string keys in dicFloor.Keys) { listFloorSort.Add(keys); } if (Common.Config.Instance.Home.FloorDics.Count == 0) { return; } HdlThreadLogic.Current.RunMainInThread(() => { int count = listFloorSort.Count - 1; for (int i = 0; i < listFloorSort.Count; i++) { string keys = listFloorSort[i]; this.AddFloorRow(keys, dicFloor[keys], i != count); } //调整控件高度 this.AdjustContrlTableHeight(); }); } #endregion #region ■ 添加楼层行_________________________ /// /// 添加楼层行 /// /// /// /// private void AddFloorRow(string keys, string floorName, bool addLine) { var frameRow = new RowLayoutControl(listView.rowSpace / 2); frameRow.MainKeys = keys; this.listView.AddChidren(frameRow); //图标 var btnIcon = frameRow.frameTable.AddLeftIcon(81); btnIcon.UnSelectedImagePath = "Floor/FloorSelected.png"; //楼层名称 var btnFloor = frameRow.frameTable.AddLeftCaption(floorName, 500); btnFloor.TextSize = 15; //向右箭头 frameRow.frameTable.AddRightArrow(); if (addLine == true) { frameRow.frameTable.AddBottomLine(); } frameRow.frameTable.ButtonClickEvent += (sender, e) => { var form = new FloorListRoomForm(); form.AddForm(keys); }; if (UserCenterResourse.UserInfo.AuthorityNo == 1 || UserCenterResourse.UserInfo.AuthorityNo == 2) { //更改 var btnChanged = frameRow.AddEditorControl(); btnChanged.TextID = R.MyInternationalizationString.uChanged1; btnChanged.ButtonClickEvent += (sender, e) => { //创建或者编辑楼层 this.CreatOrEditorFloor(frameRow, btnFloor, keys, floorName); }; //删除 var btnDelete = new NormalViewControl(Application.GetRealWidth(184), frameRow.Height, false); btnDelete.BackgroundColor = 0xfff75858; btnDelete.TextSize = 12; btnDelete.TextColor = UserCenterColor.Current.White; btnDelete.TextAlignment = TextAlignment.Center; btnDelete.TextID = R.MyInternationalizationString.uDelete; frameRow.AddRightView(btnDelete); btnDelete.ButtonClickEvent += (sender, e) => { //确认删除楼层及其所有房间? this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.uDeleteFloorMsg), () => { //移除楼层 Common.Config.Instance.Home.FloorDics.Remove(keys); if (Common.Config.Instance.Home.CurrentFloorId == keys) { //如果删除的是当前楼层的话 Common.Config.Instance.Home.CurrentFloorId = string.Empty; foreach (string floorId in Common.Config.Instance.Home.FloorDics.Keys) { //把第一个楼层ID给它 Common.Config.Instance.Home.CurrentFloorId = floorId; break; } //删除当前楼层的话,需要去刷新主页左边的房间列表 HdlRoomLogic.Current.RefreshRoomListView(); } var listDeleteId = new List(); var listAllRoom = HdlRoomLogic.Current.GetAllListRooms(); for (int i = 0; i < listAllRoom.Count; i++) { if (listAllRoom[i].FloorId == keys) { listDeleteId.Add(listAllRoom[i].Id); } } //删除房间 foreach (string roomId in listDeleteId) { HdlRoomLogic.Current.RemoveRoom(roomId); } Common.Config.Instance.Home.Save(); //保存顺序 listFloorSort.Remove(keys); HdlRoomLogic.Current.SaveFloorSort(listFloorSort); if (Common.Config.Instance.Home.FloorDics.Count == 0) { //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); } else { bool canMove = false; for (int i = 0; i < listView.ChildrenCount; i++) { var myRow = listView.GetChildren(i) as RowLayoutControl; if (myRow != null && myRow.MainKeys == keys) { canMove = true; continue; } if (canMove == true) { //它之后的行,全部往上移 myRow.Y -= frameRow.Height; } } frameRow.RemoveFromParent(); //调整桌布高度 this.AdjustContrlTableHeight(); } //删除楼层的话,主页需要重新刷新 UserView.UserPage.Instance.RefreshForm = true; }); }; } } /// /// 创建或者编辑楼层 /// /// /// /// private void CreatOrEditorFloor(RowLayoutControl frameRow, NormalViewControl btnFloor, string keys, string floorName) { //生成一个弹窗画面 var dialogForm = new DialogInputControl(); //创建楼层 dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uCreatFloor)); if (keys != string.Empty) { //更改名称 dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uChangedName)); } //请输入楼层名称 dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInputFloorName)); if (Common.Config.Instance.Home.FloorDics.ContainsKey(keys) == true) { dialogForm.Text = Common.Config.Instance.Home.FloorDics[keys]; floorName = Common.Config.Instance.Home.FloorDics[keys]; } else { dialogForm.Text = floorName; } //按下确认按钮 dialogForm.ComfirmClickEvent += ((textValue) => { //还原左右菜单 frameRow?.HideMenu(); if (this.CheckFloorName(keys, textValue) == false) { //楼层名称检测 return; } //画面关闭 dialogForm.CloseDialog(); if (floorName != textValue) { //编辑或者创建楼层名称 if (keys == string.Empty) { keys = Guid.NewGuid().ToString(); } Common.Config.Instance.Home.FloorDics[keys] = textValue; Common.Config.Instance.Home.Save(); if (Common.Config.Instance.Home.CurrentFloorId == string.Empty) { Common.Config.Instance.Home.CurrentFloorId = keys; //创建第一个新的楼层的话,需要去刷新主页左边的房间列表 HdlRoomLogic.Current.RefreshRoomListView(); } if (btnFloor != null) { btnFloor.Text = textValue; } else { //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); } //创建或者修改楼层的话,主页需要重新刷新 UserView.UserPage.Instance.RefreshForm = true; } }); } #endregion #region ■ 初始化无楼层模式___________________ /// /// 初始化无楼层模式 /// private void InitControlByNotFloorMode() { var frameRoom = new FrameRowControl(); frameRoom.UseClickStatu = false; frameRoom.Height = Application.GetRealHeight(115); frameRoom.BackgroundColor = UserCenterColor.Current.White; frameTable.AddChidren(frameRoom); //房间列表 var btnRoomList = frameRoom.AddLeftCaption(Language.StringByID(R.MyInternationalizationString.uRoomList), 200, 60); btnRoomList.Y = Application.GetRealHeight(50); btnRoomList.TextColor = UserCenterColor.Current.TextColor2; btnRoomList.TextSize = 15; frameRoom.AddChidren(btnRoomList, ChidrenBindMode.NotBind); listView = new FrameListControl(29); listView.BackgroundColor = UserCenterColor.Current.White; listView.Y = frameRoom.Bottom; listView.Height = frameTable.Height - frameRoom.Bottom; frameTable.AddChidren(listView); //初始化房间列表 this.InitRoomListRow(); } /// /// 初始化房间列表 /// private void InitRoomListRow() { //获取楼层的房间 var listRoom = HdlRoomLogic.Current.GetFloorSortRoom(string.Empty); this.listRoomSort.Clear(); for (int i = 0; i < listRoom.Count; i++) { this.listRoomSort.Add(listRoom[i].Id); } for (int i = 0; i < listRoom.Count; i++) { //添加房间行 this.AddRoomRow(listRoom[i], i != listRoom.Count - 1); } //调整桌布高度 this.AdjustContrlTableHeight(); } #endregion #region ■ 添加房间行_________________________ /// /// 添加房间行 /// /// /// private void AddRoomRow(Common.Room room, bool addLine) { var frameRow = new RowLayoutControl(listView.rowSpace / 2); frameRow.MainKeys = room.Id; listView.AddChidren(frameRow); //图标 var btnIcon = frameRow.frameTable.AddLeftIcon(81); btnIcon.UnSelectedImagePath = "Item/RoomIconSelected.png"; //名称 var btnName = frameRow.frameTable.AddLeftCaption(room.Name, 600); btnName.TextSize = 15; //右箭头 frameRow.frameTable.AddRightArrow(); if (addLine == true) { //底线 frameRow.frameTable.AddBottomLine(); } frameRow.frameTable.ButtonClickEvent += (sender, e) => { var form = new LookRoomSettionForm(); form.AddForm(room); }; if (room.IsSharedRoom == true) { //如果是分享的房间,不能编辑和修改 return; } //编辑 var btnEditor = frameRow.AddEditorControl(); btnEditor.ButtonClickEvent += (sender, e) => { var form = new EditorRoomInforForm(); form.AddForm(room); form.FinishEditorEvent += (roomName) => { btnName.Text = roomName; }; }; //删除 var btnDelete = frameRow.AddDeleteControl(); btnDelete.ButtonClickEvent += (sender, e) => { //确认删除房间? this.ShowMassage(ShowMsgType.Confirm, Language.StringByID(R.MyInternationalizationString.uDeleteRoomMsg), () => { HdlRoomLogic.Current.RemoveRoom(room.Id); //保存顺序 this.listRoomSort.Remove(room.Id); HdlRoomLogic.Current.SaveRoomSort(string.Empty, this.listRoomSort); var listAllRoom = HdlRoomLogic.Current.GetAllListRooms(); if (listAllRoom.Count <= 1) { //根据指定模式,初始化桌布控件 this.InitFrameTableByMode(); } else { bool canMove = false; for (int i = 0; i < listView.ChildrenCount; i++) { var myRow = listView.GetChildren(i) as RowLayoutControl; if (myRow != null && myRow.MainKeys == room.Id) { canMove = true; continue; } if (canMove == true) { //它之后的行,全部往上移 myRow.Y -= frameRow.Height; } } frameRow.RemoveFromParent(); //调整桌布高度 this.AdjustContrlTableHeight(); } //删除房间的话,主页需要重新刷新 UserView.UserPage.Instance.RefreshForm = true; }); }; } #endregion #region ■ 删除住宅___________________________ /// /// 删除住宅 /// private void DeleteResidence() { //开启进度条 this.ShowProgressBar(); if (Common.Config.Instance.Home.IsOthreShare == false) { //删除主人自己的住宅 var Pra = new DeleteResidencePra(); Pra.HomeId = Common.Config.Instance.Home.Id; Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); bool result = UserCenterLogic.GetResultStatuByRequestHttps("App/DelHome", true, Pra); if (result == false) { //关闭进度条 this.CloseProgressBar(); return; } } else { //删除分享的住宅 var Pra2 = new { HomeId = Common.Config.Instance.Home.Id, PrimaryUserId = Common.Config.Instance.Home.MainUserDistributedMark }; bool result = UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/SubAccountDelShareHome", false, Pra2); if (result == false) { //关闭进度条 this.CloseProgressBar(); return; } } //因为删除的是当前的住宅,所以需要先切换到别的住宅 string deleteId = Common.Config.Instance.Home.Id; string deleteFile = Common.Config.Instance.Home.FileName; int index = 0; for (; index < Common.Config.Instance.HomeFilePathList.Count; index++) { if (Common.Config.Instance.HomeFilePathList[index] == deleteFile) { //当前住宅所处的位置(住宅数肯定大于1,不然不会出现删除按钮) if (index == 0) { //取下一位 index++; } else { //取前一位 index--; } break; } } var myHouse = HdlResidenceLogic.Current.GetHouseByFilePath(Common.Config.Instance.HomeFilePathList[index]); //切换住宅 Common.Config.Instance.HomeId = myHouse.Id; Common.Global.CreateHomeDirectory(myHouse.Id); Common.Config.Instance.Save(); //刷新个人中心的内存及线程 UserCenterLogic.InitUserCenterMenmoryAndThread(false); //删除住宅 HdlResidenceLogic.Current.DeleteHouseMemmory(deleteId); Application.RunOnMainThread(() => { //关闭界面 this.CloseForm(); }); } #endregion #region ■ 保存住宅地图地址___________________ /// /// 保存住宅地址 /// /// 显示控件 /// 纬度 /// 经度 /// 位置名称 private void SaveResidenceAdrress(NormalViewControl btnLocation, double latitude, double longitude, string addresName) { //如果不是虚拟住宅,才更新云端 if (Common.Config.Instance.Home.IsVirtually == false) { //开启进度条 this.ShowProgressBar(); var Pra = new EditorResidencePra(); Pra.HomeId = Common.Config.Instance.Home.Id; Pra.Name = Common.Config.Instance.Home.Name; Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken(); Pra.Latitude = latitude; Pra.Longitude = longitude; //编辑住宅 bool flage = UserCenterLogic.GetResultStatuByRequestHttps("App/EditHome", true, Pra); if (flage == false) { //关闭进度条 this.CloseProgressBar(); return; } //设置全部网关的住宅地址 var listGateway = HdlGatewayLogic.Current.GetAllLocalGateway(); foreach (var gateway in listGateway) { ZigBee.Device.ZbGateway realWay = null; if (HdlGatewayLogic.Current.GetRealGateway(ref realWay, gateway) == false) { //错误:网关对象丢失 continue; } var result = HdlGatewayLogic.Current.SetGatewaySite(gateway, longitude, latitude, ShowErrorMode.YES); if (result == false) { //关闭进度条 this.CloseProgressBar(); return; } } //关闭进度条 this.CloseProgressBar(); } //保存缓存 Common.Config.Instance.Home.Longitude = longitude; Common.Config.Instance.Home.Latitude = latitude; Common.Config.Instance.Home.ResidenceAddressName = addresName; Common.Config.Instance.Home.Save(); HdlThreadLogic.Current.RunMain(() => { btnLocation.Text = addresName; }); } #endregion #region ■ 一般方法___________________________ /// /// 检测App的住宅模式 0:还没有设置有模式 1:楼层模式 2:没有楼层模式 /// /// private int CheckAppResidenceMode() { if (Common.Config.Instance.Home.FloorDics.Count > 0) { //拥有楼层 return 1; } if (HdlRoomLogic.Current.GetAllListRooms().Count > 1) { //无楼层模式 return 2; } return 0; } /// /// 检测楼层名称 /// /// 主键 /// 新的楼层名称 /// private bool CheckFloorName(string keys, string newFloorName) { if (newFloorName == string.Empty) { //请输入住宅名称 string msg = Language.StringByID(R.MyInternationalizationString.uPleaseInputFloorName); this.ShowMassage(ShowMsgType.Tip, msg); return false; } foreach (string floorKeys in Common.Config.Instance.Home.FloorDics.Keys) { if (floorKeys != keys && newFloorName == Common.Config.Instance.Home.FloorDics[floorKeys]) { //楼层名称已经存在 string msg = Language.StringByID(R.MyInternationalizationString.uFloorNameIsExist); this.ShowMassage(ShowMsgType.Tip, msg); return false; } } return true; } /// /// 调整控件桌布高度 /// private void AdjustContrlTableHeight() { //调整桌布高度 listView.AdjustRealHeight(Application.GetRealHeight(23)); this.frameTable.Height = listView.Bottom; this.listBackContr.AdjustTableHeight(); } #endregion #region ■ 结构体_____________________________ /// /// 删除住宅的启动参数 /// public class DeleteResidencePra : IfacePraCommon { /// /// RequestVersion /// public string RequestVersion = Common.CommonPage.RequestVersion; /// /// LoginAccessToken /// public string LoginAccessToken = Common.Config.Instance.Token; /// /// ZigbeeHomeId /// public string HomeId = ""; } #endregion } }