using System; using System.Collections.Generic; using Shared.Common; using Shared.Phone.Device.CommonForm; using Shared.Phone.UserCenter; using Shared.Phone.UserView; using ZigBee.Device; namespace Shared.Phone.MainPage { /// /// 未分配的管理界面 /// public class UnallocatedRoomForm : EditorCommonForm { #region ■ 变量声明___________________________ /// /// 添加到 的背景图控件 /// private FrameLayoutStatuControl frameAddBackGroud = null; /// /// 功能的桌布控件 /// private NormalFrameLayout DeviceBodyTableControl = null; /// /// 场景的列表控件 /// private VerticalFrameControl listSceneView = null; /// /// 设备的列表控件 /// private VerticalListControl listDeviceView = null; /// /// 场景的桌布控件 /// private NormalFrameLayout SceneBodyTableControl = null; /// /// 全选控件 /// private NormalViewControl btnAllSelect = null; /// /// 选择的场景 /// private Dictionary dicSelectScene = new Dictionary(); /// /// 选择的设备 /// private Dictionary dicSelectDevice = new Dictionary(); /// /// 当前选择的设备类型信息(为了处理速度使用) /// private DeviceRowInfo nowSelectDeviceInfo = null; /// /// 全部分组的设备信息 /// private Dictionary dicGroupDevice = null; /// /// 是否已经改变了数据 /// private bool dataHadChanged = false; /// /// 能否全选的状态 /// private bool canSelectAll = true; #endregion #region ■ 初始化_____________________________ /// /// 画面显示(底层会固定调用此方法,借以完成画面创建) /// public void ShowForm() { //初始化头部控件 this.InitTopFrameLayout(); //初始化中部控件 this.InitBodyFrameLayout(); //设置头部信息 base.SetTitleText(Language.StringByID(R.MyInternationalizationString.Unallocated)); //初始化全选控件 this.InitAllSelectControl(); //初始化中部信息 this.InitMiddleFrame(); } /// /// 初始化中部信息 /// private void InitMiddleFrame() { var tabControl = new SceneFunctionSwitchControl(); tabControl.Y = Application.GetRealHeight(40); bodyFrameLayout.AddChidren(tabControl); //初始化场景桌布 this.SceneBodyTableControl = new NormalFrameLayout(); SceneBodyTableControl.Y = tabControl.Bottom; SceneBodyTableControl.Height = bodyFrameLayout.Height - tabControl.Bottom; bodyFrameLayout.AddChidren(SceneBodyTableControl); //初始化设备桌布 this.DeviceBodyTableControl = new NormalFrameLayout(); DeviceBodyTableControl.Y = tabControl.Bottom; DeviceBodyTableControl.Height = bodyFrameLayout.Height - tabControl.Bottom; bodyFrameLayout.AddChidren(DeviceBodyTableControl); tabControl.SelectTabEvent += (index) => { //场景 if (index == 0) { //初始化场景分支的控件 this.InitSceneCaseControl(); //检测各控件状态 this.CheckControlsStatu(null); } //功能 else { //初始化功能分支的控件 this.InitDeviceCaseControl(); //检测各控件状态 this.CheckControlsStatu(this.nowSelectDeviceInfo); } }; //开始初始化场景功能切换控件 var listTitle = new List(); listTitle.Add(Language.StringByID(R.MyInternationalizationString.uScence)); listTitle.Add(Language.StringByID(R.MyInternationalizationString.uFunction)); //他会自动回调事件 tabControl.InitControl(listTitle); } #endregion #region ■ 场景分支___________________________ /// /// 初始化场景分支的控件 /// private void InitSceneCaseControl() { //隐藏设备列表 this.DeviceBodyTableControl.Visible = false; //显示场景 this.SceneBodyTableControl.Visible = true; if (this.SceneBodyTableControl.Name == "Scene") { //已经初始化过 return; } //打个标识而已 this.SceneBodyTableControl.Name = "Scene"; //开始执行初始化场景分支的控件 this.DoInitSceneCaseControl(); } /// /// 开始执行初始化场景分支的控件 /// private void DoInitSceneCaseControl() { HdlThreadLogic.Current.RunMainInThread(() => { //清空桌布 this.SceneBodyTableControl.RemoveAll(); this.listSceneView = new VerticalFrameControl(3); listSceneView.Y = Application.GetRealHeight(53); listSceneView.Height = SceneBodyTableControl.Height; SceneBodyTableControl.AddChidren(listSceneView); var listScene = HdlSceneLogic.Current.GetUnalloctedScenes(); if (listScene.Count == 0) { return; } foreach (var data in listScene) { //场景图片 var frameContr = new ScenePictrueControl(); listSceneView.AddChidren(frameContr); frameContr.InitControl(data); //添加选择控件 frameContr.AddSelectControl(); //收藏 frameContr.AddCollectionControl(); frameContr.CollectEvent += (collect) => { //如果当前是收藏房间,则需要刷新主页 if (HdlRoomLogic.Current.NowMainPageRoom.IsLove == true) { this.dataHadChanged = true; } }; frameContr.ButtonClickEvent += (sender, e) => { //选择 frameContr.IsSelected = !frameContr.IsSelected; if (frameContr.IsSelected == true) { dicSelectScene[data.Id] = data; } else { dicSelectScene.Remove(data.Id); } //检测各控件状态 this.CheckControlsStatu(null); }; } //促使它超过时,能够往上滑 var frameTemp = new FrameLayout(); frameTemp.Height = Application.GetRealHeight(202 + 23); listSceneView.AddChidren(frameTemp); }); } #endregion #region ■ 功能分支___________________________ /// /// 初始化功能分支的控件 /// private void InitDeviceCaseControl() { //隐藏场景列表 this.SceneBodyTableControl.Visible = false; //显示功能 this.DeviceBodyTableControl.Visible = true; if (this.DeviceBodyTableControl.Name == "Function") { //已经初始化过 return; } //打个标识而已 this.DeviceBodyTableControl.Name = "Function"; //开始执行初始化功能分支的控件 this.DoInitDeviceCaseControl(); } /// /// 开始执行初始化功能分支的控件 /// private void DoInitDeviceCaseControl() { HdlThreadLogic.Current.RunMainInThread(() => { //清空桌布,不清空这个的话,它里面还有上一次的东西 this.listDeviceView?.RemoveAll(); this.DeviceBodyTableControl.RemoveAll(); //获取分组后的设备列表 this.dicGroupDevice = this.GetAllGroupDevice(); if (dicGroupDevice.Count == 0) { this.listDeviceView = null; return; } //设备的背景容器 var frameDeviceBack = new FrameLayout(); frameDeviceBack.X = ControlCommonResourse.XXLeft; frameDeviceBack.Y = Application.GetRealHeight(369); frameDeviceBack.BackgroundColor = UserCenterColor.Current.White; frameDeviceBack.Width = bodyFrameLayout.Width; frameDeviceBack.Height = Application.GetRealHeight(1236); frameDeviceBack.Radius = (uint)Application.GetRealHeight(58); DeviceBodyTableControl.AddChidren(frameDeviceBack); //设备列表控件 this.listDeviceView = new VerticalListControl(35); listDeviceView.Y = Application.GetRealHeight(14); listDeviceView.Height = Application.GetRealHeight(1236 - 14); frameDeviceBack.AddChidren(listDeviceView); //初始化设备类型菜单 this.InitDeviceObjectMenu(); }); } #endregion #region ■ 初始化设备类型菜单_________________ /// /// 初始化设备类型菜单 /// private void InitDeviceObjectMenu() { //头部的白色背景 var functionBack1 = new FrameLayout(); functionBack1.X = ControlCommonResourse.XXLeft; functionBack1.Y = Application.GetRealHeight(55); functionBack1.Height = Application.GetRealHeight(160); functionBack1.Width = Application.GetRealWidth(1028); functionBack1.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor; DeviceBodyTableControl.AddChidren(functionBack1); functionBack1.SetCornerWithSameRadius(Application.GetRealHeight(17), HDLUtils.RectCornerTopLeft); var functionBack2 = new FrameLayout(); functionBack2.X = ControlCommonResourse.XXLeft; functionBack2.Y = functionBack1.Bottom - Application.GetRealHeight(50); functionBack2.Height = Application.GetRealHeight(279 - 160 + 50); functionBack2.Width = Application.GetRealWidth(1028); functionBack2.BackgroundColor = ZigbeeColor.Current.GXCBackgroundColor; DeviceBodyTableControl.AddChidren(functionBack2); functionBack2.SetCornerWithSameRadius(Application.GetRealHeight(58), HDLUtils.RectCornerBottomLeft); //左右滑动的控件 var HorizontalView = new HorizontalScrolViewLayout(); HorizontalView.X = Application.GetRealWidth(CommonFormResouce.X_Left); HorizontalView.Y = Application.GetRealHeight(55); HorizontalView.Height = Application.GetRealHeight(279); HorizontalView.Width = Application.GetRealWidth(1028); DeviceBodyTableControl.AddChidren(HorizontalView); //上一次选择的菜单 Controls.DeviceFunctionUnallocatedControl oldSelectContr = null; foreach (int Textid in this.dicGroupDevice.Keys) { var rowInfo = dicGroupDevice[Textid]; //设备类型的容器 var devieFrame = new FrameLayout(); devieFrame.Width = Application.GetRealWidth(220); HorizontalView.AddChidren(devieFrame); //菜单图片控件 var deviceObjContr = new Controls.DeviceFunctionUnallocatedControl(); devieFrame.AddChidren(deviceObjContr); deviceObjContr.InitControl(Language.StringByID(Textid), rowInfo.IconPath, rowInfo.IconPathSelected, rowInfo.listDeviceKeys); deviceObjContr.ButtonClickEvent += (sender, e) => { //选择的是同一个东西的话,不处理 if (this.nowSelectDeviceInfo.TextId != rowInfo.TextId) { //上一次的菜单取消,本次菜单选择 oldSelectContr.SetSelectStatu(false); deviceObjContr.SetSelectStatu(true); oldSelectContr = deviceObjContr; this.nowSelectDeviceInfo = rowInfo; HdlThreadLogic.Current.RunMainInThread(() => { //初始化设备列表控件 this.InitListDeviceControls(rowInfo); //检测各控件状态 this.CheckControlsStatu(this.nowSelectDeviceInfo); }); } }; if (this.nowSelectDeviceInfo == null) { //设置初始选择 this.nowSelectDeviceInfo = rowInfo; //记录初始选择的菜单控件 oldSelectContr = deviceObjContr; deviceObjContr.SetSelectStatu(true); } } if (this.nowSelectDeviceInfo != null) { //初始化默认的设备列表控件 this.InitListDeviceControls(nowSelectDeviceInfo); } } #endregion #region ■ 初始化设备列表控件_________________ /// /// 初始化设备列表控件 /// /// /// private void InitListDeviceControls(DeviceRowInfo rowInfo) { this.listDeviceView.RemoveAll(); //初始化设备列表控件 foreach (var deviceKey in rowInfo.listDeviceKeys) { var device = LocalDevice.Current.GetDevice(deviceKey); var deviceRow = new Controls.DeviceSelectUnallocatedControl(listDeviceView.rowSpace / 2); listDeviceView.AddChidren(deviceRow); deviceRow.InitControl(device); if (this.dicSelectDevice.ContainsKey(deviceKey) == true) { //如果它之前选择了的话 deviceRow.IsSelected = true; } deviceRow.ButtonClickEvent += (sender, e) => { deviceRow.IsSelected = !deviceRow.IsSelected; //记录缓存 if (deviceRow.IsSelected == true) { this.dicSelectDevice[deviceKey] = device; } else { this.dicSelectDevice.Remove(deviceKey); } //检测各控件状态 this.CheckControlsStatu(rowInfo); }; } //促使它超过时,能够往上滑 var frameTemp = new FrameLayout(); frameTemp.Height = Application.GetRealHeight(202 + 23); listDeviceView.AddChidren(frameTemp); } #endregion #region ■ 全选控件___________________________ /// /// 初始化全选控件 /// private void InitAllSelectControl() { this.btnAllSelect = new NormalViewControl(150, 70, true); btnAllSelect.TextAlignment = TextAlignment.CenterRight; btnAllSelect.X = topFrameLayout.Width - Application.GetRealWidth(150 + 115); btnAllSelect.Gravity = Gravity.CenterVertical; btnAllSelect.TextID = R.MyInternationalizationString.AllSelect; btnAllSelect.IsBold = true; topFrameLayout.AddChidren(btnAllSelect); btnAllSelect.ButtonClickEvent += (sender, e) => { //场景分支 if (SceneBodyTableControl.Visible == true) { if (listSceneView == null) { return; } for (int i = 0; ; i++) { var myContr = listSceneView.frameTable.GetChildren(i); if (myContr == null) { break; } if (myContr is ScenePictrueControl) { ((ScenePictrueControl)myContr).IsSelected = this.canSelectAll; var scene = ((ScenePictrueControl)myContr).scene; if (this.canSelectAll == true) { //添加缓存 dicSelectScene[scene.Id] = scene; } else { //移除缓存 dicSelectScene.Remove(scene.Id); } } } //检测各控件状态 this.CheckControlsStatu(null); } //功能分支 else { if (listDeviceView == null) { return; } for (int i = 0; ; i++) { var myContr = listDeviceView.GetChildren(i); if (myContr == null) { break; } if (myContr is Controls.DeviceSelectUnallocatedControl) { ((Controls.DeviceSelectUnallocatedControl)myContr).IsSelected = this.canSelectAll; var device = ((Controls.DeviceSelectUnallocatedControl)myContr).device; if (this.canSelectAll == true) { //添加缓存 dicSelectDevice[LocalDevice.Current.GetDeviceMainKeys(device)] = device; } else { //移除缓存 dicSelectDevice.Remove(LocalDevice.Current.GetDeviceMainKeys(device)); } } } //检测各控件状态 this.CheckControlsStatu(this.nowSelectDeviceInfo); } }; } #endregion #region ■ [添加到]控件_______________________ /// /// 初始化添加到控件 /// private void InitAddToControl() { //添加到 的背景图控件 this.frameAddBackGroud = new FrameLayoutStatuControl(); frameAddBackGroud.UseClickStatu = false; frameAddBackGroud.Height = Application.GetRealHeight(202); frameAddBackGroud.Gravity = Gravity.BottomCenter; frameAddBackGroud.BackgroundImagePath = "Room/AddBackground.png"; bodyFrameLayout.AddChidren(frameAddBackGroud); frameAddBackGroud.Visible = false; //添加到 按钮 var btnAdd = new NormalViewControl(1034, 156, true); btnAdd.Gravity = Gravity.Center; btnAdd.BackgroundColor = UserCenterColor.Current.White; btnAdd.TextColor = ZigbeeColor.Current.GXCTextSelectedColor4; btnAdd.TextAlignment = TextAlignment.Center; btnAdd.TextID = R.MyInternationalizationString.AddTo; btnAdd.TextSize = 17; btnAdd.Radius = (uint)Application.GetRealHeight(35); frameAddBackGroud.AddChidren(btnAdd, ChidrenBindMode.BindEvent); frameAddBackGroud.ButtonClickEvent += (sender, e) => { var selectZone = new SelectZone(); selectZone.title = Language.StringByID(R.MyInternationalizationString.AddTo); selectZone.Init(); selectZone.ZoneAction += (selectRoom) => { //变更了当前房间的东西,主页需要刷新 if (HdlRoomLogic.Current.NowMainPageRoom.Id == selectRoom.Id) { this.dataHadChanged = true; } //保存选择的设备和场景 this.SaveSelectDeviceAndScene(selectRoom); }; }; } /// /// 保存选择的设备和场景 /// /// private void SaveSelectDeviceAndScene(Room room) { foreach (var deviceKey in this.dicSelectDevice.Keys) { //添加设备 HdlRoomLogic.Current.AddDevice(room, dicSelectDevice[deviceKey], true); foreach (var rowInf in dicGroupDevice.Values) { //移除缓存 if (rowInf.listDeviceKeys.Contains(deviceKey) == true) { rowInf.listDeviceKeys.Remove(deviceKey); } } } foreach (var scene in this.dicSelectScene.Values) { //添加场景 HdlSceneLogic.Current.AddSceneToRoom(room, scene); } //清空缓存 this.dicSelectScene.Clear(); this.dicSelectDevice.Clear(); //右上角的文字切换为 全选 btnAllSelect.TextID = R.MyInternationalizationString.AllSelect; this.canSelectAll = true; //场景分支需要重新初始化 this.DoInitSceneCaseControl(); if (this.nowSelectDeviceInfo != null) { bool allHadDevice = true; foreach (var data in this.dicGroupDevice.Values) { if (data.listDeviceKeys.Count == 0) { //有分支已经没有设备了 allHadDevice = false; break; } } //如果所有分支都还有设备的话,就刷新当前的列表即可 if (allHadDevice == true) { //初始化设备列表控件 this.InitListDeviceControls(this.nowSelectDeviceInfo); } else { //如果存在分支已经没有设备了的话,那么它的菜单也需要删除掉,所以需要重新初始化 this.nowSelectDeviceInfo = null; this.DoInitDeviceCaseControl(); } } //隐藏 添加到 按钮 this.frameAddBackGroud.Visible = false; } #endregion #region ■ 整合设备___________________________ /// /// 获取分组后的设备 /// /// private Dictionary GetAllGroupDevice() { //全部的设备 var listDevice = HdlRoomLogic.Current.GetUnalloctedDevice(); //根据设备所属类型排序 listDevice = LocalDevice.Current.SortDeviceByBelongType(listDevice); var dic = new Dictionary(); foreach (var device in listDevice) { var typeInfo = LocalDevice.Current.GetDeviceBelongEnumInfo(device); //按所属ID分组 if (dic.ContainsKey(typeInfo.BeloneTextId) == false) { dic[typeInfo.BeloneTextId] = new DeviceRowInfo(); string path1 = string.Empty; string path2 = string.Empty; //获取图片 Common.LocalDevice.Current.GetDeviceFunctionTypeMenuIcon(typeInfo.ConcreteType, ref path1, ref path2); dic[typeInfo.BeloneTextId].IconPath = path1; dic[typeInfo.BeloneTextId].IconPathSelected = path2; dic[typeInfo.BeloneTextId].TextId = typeInfo.BeloneTextId; } dic[typeInfo.BeloneTextId].listDeviceKeys.Add(LocalDevice.Current.GetDeviceMainKeys(device)); } return dic; } #endregion #region ■ 界面关闭___________________________ /// /// 界面关闭 /// public override void CloseFormBefore() { if (this.dataHadChanged == true) { HdlThreadLogic.Current.RunMainInThread(() => { //刷新主页 HomeMainPageForm.Instance?.RefreshBodyView(); }); } base.CloseFormBefore(); } #endregion #region ■ 一般方法___________________________ /// /// 检测各控件状态 /// private void CheckControlsStatu(DeviceRowInfo rowInfo) { if (this.frameAddBackGroud == null) { //初始化添加到控件 this.InitAddToControl(); } if (dicSelectScene.Count == 0 && dicSelectDevice.Count == 0) { //隐藏 添加到 控件 if (frameAddBackGroud.Visible == true) { frameAddBackGroud.Visible = false; } } else { //显示 添加到 控件 if (frameAddBackGroud.Visible == false) { frameAddBackGroud.Visible = true; } } //当前选择的是场景分支 if (rowInfo == null) { if (this.dicSelectScene.Count > 0) { //右上角的文字切换为 取消 btnAllSelect.TextID = R.MyInternationalizationString.Cancel; this.canSelectAll = false; } else { //右上角的文字切换为 全选 btnAllSelect.TextID = R.MyInternationalizationString.AllSelect; this.canSelectAll = true; } } //当前选择的是功能分支 else { foreach (var deviceKey in rowInfo.listDeviceKeys) { //如果当前的设备类型选择分支下,存在之前选择的设备的话 if (this.dicSelectDevice.ContainsKey(deviceKey) == true) { //右上角的文字切换为 取消 btnAllSelect.TextID = R.MyInternationalizationString.Cancel; this.canSelectAll = false; return; } } //如果不存在的话,右上角的文字切换为 全选 btnAllSelect.TextID = R.MyInternationalizationString.AllSelect; this.canSelectAll = true; } } #endregion #region ■ 结构体_____________________________ /// /// 设备行信息 /// private class DeviceRowInfo { /// /// 文本ID,目前用来做主键 /// public int TextId = 0; /// /// 图标 /// public string IconPath = string.Empty; /// /// 图标 /// public string IconPathSelected = string.Empty; /// /// 设备回路主键 /// public List listDeviceKeys = new List(); } #endregion } }