wxr
2021-05-12 9acd3887f1f8db40d59f991b1726a61aa7c7637c
HDL_ON/UI/UI2/4-PersonalCenter/SmartSpeaker/SmartSpeakerSelectDevicesPage.cs
New file
@@ -0,0 +1,433 @@
using System;
using Shared;
using HDL_ON.UI.CSS;
using System.Collections.Generic;
using HDL_ON.DAL.Server;
namespace HDL_ON.UI
{
    /// <summary>
    /// 管理配置给音箱的设备或着场景列表
    /// </summary>
    public class SmartSpeakerSelectDevicesPage : FrameLayout
    {
        /// <summary>
        /// bodyView
        /// </summary>
        FrameLayout bodyView;
        /// <summary>
        /// 全选按钮
        /// </summary>
        Button btnChooseAll;
        /// <summary>
        /// 当前
        /// </summary>
        VerticalScrolViewLayout bodyScrolView;
        ///// <summary>
        ///// 底部保存操作按钮
        ///// </summary>
        //Button btnSave;
        /// <summary>
        /// 音箱参数
        /// </summary>
        SpeakerInfo speakerInfo;
        /// <summary>
        /// 设备、场景列表
        /// </summary>
        List<SpeakerTargetInfo> targetInfoList = new List<SpeakerTargetInfo>();
        /// <summary>
        /// 当前房间的设备和场景
        /// </summary>
        List<SpeakerTargetInfo> roomFunctionOrSceneList = new List<SpeakerTargetInfo>();
        /// <summary>
        /// 管理配置给音箱的设备或着场景列表
        /// </summary>
        /// <param name="speakerInfo">音箱参数</param>
        /// <param name="roomFunctionOrSceneList">当前房间的功能或者场景</param>
        public SmartSpeakerSelectDevicesPage(SpeakerInfo speakerInfo, List<SpeakerTargetInfo> roomFunctionOrSceneList)
        {
            bodyView = this;
            bodyView.BackgroundColor = CSS_Color.MainBackgroundColor;
            this.speakerInfo = speakerInfo;
            this.roomFunctionOrSceneList = roomFunctionOrSceneList;
        }
        /// <summary>
        /// 加载视图
        /// </summary>
        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();
        }
        /// <summary>
        /// 全选按钮点击事件
        /// </summary>
        void LoadEvent_AllSharedDataChange(FrameLayout allView, Button btnChooseAll)
        {
            EventHandler<MouseEventArgs> 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;
        }
        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="btnSave"></param>
        void LoadEvent_Save(Button btnSave)
        {
            EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            {
                AddDevicesSceneList(targetInfoList);
            };
            btnSave.MouseUpEventHandler = eventHandler;
        }
        /// <summary>
        /// 全量更新配置数据(功能和场景同时更新)
        /// </summary>
        /// <param name="updateList">最新的配置数据</param>
        void AddDevicesSceneList(List<SpeakerTargetInfo> 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;
                        }
                    });
                }
            });
        }
        /// <summary>
        /// 加载房间的设备和场景列表
        /// </summary>
        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<List<SpeakerTargetInfo>>(revertObj.Data.ToString());
                        if (list != null && list.Count > 0)
                        {
                            targetInfoList = list;
                        }
                        else
                        {
                            targetInfoList = new List<SpeakerTargetInfo>();
                        }
                        //默认全选
                        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;
                        }
                    });
                }
            });
        }
        /// <summary>
        /// RefreshListView
        /// </summary>
        void RefreshListView()
        {
            bodyScrolView.RemoveAll();
            if (roomFunctionOrSceneList == null) return;
            foreach (var roomData in roomFunctionOrSceneList)
            {
                AddRowView(roomData);
            }
        }
        /// <summary>
        /// AddRowView
        /// </summary>
        /// <param name="info"></param>
        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<MouseEventArgs> 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;
        }
    }
}