using System; using Shared; using HDL_ON.UI.CSS; using System.Collections.Generic; using HDL_ON.DAL.Server; namespace HDL_ON.UI { /// /// 管理配置给音箱的设备或着场景列表 /// public class SmartSpeakerSelectDevicesPage : FrameLayout { /// /// bodyView /// FrameLayout bodyView; /// /// 全选按钮 /// Button btnChooseAll; /// /// 当前 /// VerticalScrolViewLayout bodyScrolView; ///// ///// 底部保存操作按钮 ///// //Button btnSave; /// /// 音箱参数 /// SpeakerInfo speakerInfo; /// /// 设备、场景列表 /// List targetInfoList = new List(); /// /// 当前房间的设备和场景 /// List roomFunctionOrSceneList = new List(); /// /// 管理配置给音箱的设备或着场景列表 /// /// 音箱参数 /// 当前房间的功能或者场景 public SmartSpeakerSelectDevicesPage(SpeakerInfo speakerInfo, List roomFunctionOrSceneList) { bodyView = this; bodyView.BackgroundColor = CSS_Color.MainBackgroundColor; this.speakerInfo = speakerInfo; this.roomFunctionOrSceneList = roomFunctionOrSceneList; } /// /// 加载视图 /// public void LoadPage() { //加载顶部菜单栏 new TopViewDiv(bodyView, Language.StringByID(StringId.DataManagement)).LoadTopView(); //顶部全选按钮 var allView = new FrameLayout() { Y = Application.GetRealHeight(64), Height = Application.GetRealHeight(50), BackgroundColor = CSS_Color.MainBackgroundColor, }; bodyView.AddChidren(allView); Button btnAllRoomText = new Button() { X = Application.GetRealWidth(16), Width = Application.GetRealWidth(280), TextID = StringId.SelectedAll, TextSize = CSS_FontSize.SubheadingFontSize, TextColor = CSS_Color.FirstLevelTitleColor, TextAlignment = TextAlignment.CenterLeft, }; allView.AddChidren(btnAllRoomText); btnChooseAll = new Button() { X = Application.GetRealWidth(331), Gravity = Gravity.CenterVertical, Width = Application.GetMinRealAverage(28), Height = Application.GetMinRealAverage(28), UnSelectedImagePath = "Public/ChooseIcon.png", SelectedImagePath = "Public/ChooseOnIcon.png", }; allView.AddChidren(btnChooseAll); allView.AddChidren(new Button() { Gravity = Gravity.CenterHorizontal, Y = Application.GetRealHeight(49), Height = Application.GetMinReal(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor, }); bodyScrolView = new VerticalScrolViewLayout() { Y = allView.Bottom, Height = Application.GetRealHeight(450), }; bodyView.AddChidren(bodyScrolView); var btnSave = new ConfirmButton() { Y = Application.GetRealHeight(519 + 64), TextID = StringId.Save }; bodyView.AddChidren(btnSave); //全选按钮点击事件 LoadEvent_AllSharedDataChange(allView, btnChooseAll); //保存按钮点击事件 LoadEvent_Save(btnSave); //加载房间的功能和场景,并比较配置过的数据实现选中效果 GetDeviceAndSceneList(); } /// /// 全选按钮点击事件 /// void LoadEvent_AllSharedDataChange(FrameLayout allView, Button btnChooseAll) { EventHandler eventHandler = (sender, e) => { btnChooseAll.IsSelected = !btnChooseAll.IsSelected; var IsSelected = btnChooseAll.IsSelected; //全部数据选中状态设置为true或者false foreach (var info in roomFunctionOrSceneList) { info.IsSelect = IsSelected; } //刷新一次列表 RefreshListView(); }; btnChooseAll.MouseUpEventHandler = eventHandler; allView.MouseUpEventHandler = eventHandler; } /// /// 保存按钮点击事件 /// /// void LoadEvent_Save(Button btnSave) { EventHandler eventHandler = (sender, e) => { AddDevicesSceneList(targetInfoList); }; btnSave.MouseUpEventHandler = eventHandler; } /// /// 全量更新配置数据(功能和场景同时更新) /// /// 最新的配置数据 void AddDevicesSceneList(List updateList) { var waitPage = new Loading(); bodyView.AddChidren(waitPage); waitPage.Start(Language.StringByID(StringId.PleaseWait)); System.Threading.Tasks.Task.Run(() => { try { //查找需要选中的目标 foreach (var info in roomFunctionOrSceneList) { var targetData = updateList.Find((obj) => obj.targetId == info.targetId); if (info.IsSelect) { if (targetData == null) { updateList.Add(info); } } else { if (targetData != null) { updateList.Remove(targetData); } } } //构建请求参数 var updateSpeakerDeviceListObj = new UpdateSpeakerDeviceListObj() { homeId = speakerInfo.homeId, tokenId = speakerInfo.tokenId, targetInfos = updateList }; //发起请求 var revertObj = new HttpServerRequest().UpdateSpeakerDeviceList(updateSpeakerDeviceListObj); if (revertObj.Code == StateCode.SUCCESS) { Application.RunOnMainThread(() => { //保存成功、页面关闭 Utlis.ShowTip(Language.StringByID(StringId.SavedSuccessfully)); if (bodyView != null) { bodyView.RemoveFromParent(); } }); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code); } } catch { } finally { Application.RunOnMainThread(() => { if (waitPage != null) { waitPage.RemoveFromParent(); waitPage = null; } }); } }); } /// /// 加载房间的设备和场景列表 /// void GetDeviceAndSceneList() { bodyScrolView.RemoveAll(); var waitPage = new Loading(); bodyView.AddChidren(waitPage); waitPage.Start(Language.StringByID(StringId.PleaseWait)); System.Threading.Tasks.Task.Run(() => { try { var revertObj = new HttpServerRequest().GetSpeakerDeviceList(0, speakerInfo.tokenId); if (revertObj.Code == StateCode.SUCCESS) { var list = Newtonsoft.Json.JsonConvert.DeserializeObject>(revertObj.Data.ToString()); if (list != null && list.Count > 0) { targetInfoList = list; } else { targetInfoList = new List(); } //默认全选 bool isSelectAll = true; //查找需要选中的目标 foreach (var info in roomFunctionOrSceneList) { var targetData = targetInfoList.Find((obj) => obj.targetId == info.targetId); if(targetData == null) { info.IsSelect = false; //有一个没选择则全选设置为false isSelectAll = false; } else { info.IsSelect = true; } } Application.RunOnMainThread(() => { //设置当前全选 btnChooseAll.IsSelected = isSelectAll; RefreshListView(); }); } else { //提示错误 IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code); } } catch { } finally { Application.RunOnMainThread(() => { if (waitPage != null) { waitPage.RemoveFromParent(); waitPage = null; } }); } }); } /// /// RefreshListView /// void RefreshListView() { bodyScrolView.RemoveAll(); if (roomFunctionOrSceneList == null) return; foreach (var roomData in roomFunctionOrSceneList) { AddRowView(roomData); } } /// /// AddRowView /// /// void AddRowView(SpeakerTargetInfo info) { var roomView = new FrameLayout() { Height = Application.GetRealHeight(50), BackgroundColor = CSS_Color.MainBackgroundColor, Tag = "row" }; bodyScrolView.AddChidren(roomView); Button btnRoomText = new Button() { X = Application.GetRealWidth(16), Width = Application.GetRealWidth(280), TextSize = CSS_FontSize.SubheadingFontSize, TextColor = CSS_Color.FirstLevelTitleColor, TextAlignment = TextAlignment.CenterLeft, Text = info.targetName, }; roomView.AddChidren(btnRoomText); Button btnChoose = new Button() { X = Application.GetRealWidth(331), Gravity = Gravity.CenterVertical, Width = Application.GetMinRealAverage(28), Height = Application.GetMinRealAverage(28), UnSelectedImagePath = "Public/ChooseIcon.png", SelectedImagePath = "Public/ChooseOnIcon.png", Tag = "ChooseIcon" }; roomView.AddChidren(btnChoose); btnChoose.IsSelected = info.IsSelect; var btnLine = new Button() { Gravity = Gravity.CenterHorizontal, //Y = Application.GetRealHeight(49), Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor, }; bodyScrolView.AddChidren(btnLine); EventHandler eventHandler = (sender, e) => { btnChoose.IsSelected = !btnChoose.IsSelected; info.IsSelect = btnChoose.IsSelected; //1.如果点击选择,判断是否需要设置全选 if (btnChoose.IsSelected) { //1.1 默认全选 bool isSelectAll = true; foreach (var targetInfo in roomFunctionOrSceneList) { if(targetInfo.IsSelect == false) { //1.2 有一个还没选中就退出,无需选中全选按钮 isSelectAll = false; break; } } if (isSelectAll) { btnChooseAll.IsSelected = true; } } else { //取消全选 if (btnChooseAll.IsSelected) { btnChooseAll.IsSelected = false; } } }; btnChoose.MouseUpEventHandler = eventHandler; roomView.MouseUpEventHandler = eventHandler; btnRoomText.MouseUpEventHandler = eventHandler; } } }