wei
2021-04-19 db1b76418c083e74fe16a65cddddf578de3d09cc
HDL_ON/UI/UI2/3-Intelligence/Scene/CatchSceneAddPage.cs
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
using HDL_ON.Entity;
using HDL_ON.UI.CSS;
using Shared;
@@ -19,6 +21,36 @@
        private Button btnChooseZoneViewTitle;
        private VerticalScrolViewLayout functionListView;
        private Button btnComplete;
        #region 图标选择部分图标
        /// <summary>
        /// 背景图选项区域
        /// </summary>
        FrameLayout pictureOptionView;
        /// <summary>
        /// 背景图选项选择区域
        /// </summary>
        VerticalScrolViewLayout optionView;
        /// <summary>
        /// 默认图库按钮
        /// </summary>
        Button btnDefaultGallery;
        /// <summary>
        /// 拍照按钮
        /// </summary>
        Button btnTakePicture;
        /// <summary>
        /// 相册按钮
        /// </summary>
        Button btnAlbum;
        /// <summary>
        /// 取消按钮
        /// </summary>
        Button btnCancel;
        #endregion
        /// <summary>
        /// 捕捉的区域列表
        /// </summary>
@@ -37,7 +69,6 @@
                RemoveFromParent();
            };
        }
        public void LoadPage()
        {
@@ -74,6 +105,14 @@
            //2020-12-03 修改图片加载方法
            ImageUtlis.Current.LoadLocalOrNetworkImages(scene.ImagePath, addSceneImageView);
            /// <summary>
            /// 修改场景背景事件
            /// </summary>
            addSceneImageView.MouseUpEventHandler = (sender, e) =>
            {
                LoadPictureOptionView();
            };
            #region 场景名称row
            sceneNameView = new FrameLayout()
@@ -174,71 +213,6 @@
                BackgroundColor = CSS_Color.BackgroundColor,
            });
            #endregion
            #region 场景延时row
            /*
            if (DB_ResidenceData.Instance.GatewayType == 1)
            {
                FrameLayout sceneDelayRow = new FrameLayout()
                {
                    Y = sceneNameView.Bottom,
                    Height = Application.GetRealWidth(50),
                };
                contentView.AddChidren(sceneDelayRow);
                Button btnSceneDelayRight = new Button()
                {
                    X = Application.GetRealWidth(339),
                    Gravity = Gravity.CenterVertical,
                    Width = Application.GetMinRealAverage(16),
                    Height = Application.GetMinRealAverage(16),
                    UnSelectedImagePath = "Public/Right.png",
                };
                sceneDelayRow.AddChidren(btnSceneDelayRight);
                var btnSceneDelayInfo = new Button()
                {
                    Width = Application.GetRealWidth(327),
                    TextAlignment = TextAlignment.CenterRight,
                    TextColor = CSS_Color.PromptingColor1,
                    TextSize = CSS_FontSize.PromptFontSize_FirstLevel,
                    Text = scene.GetDelayText()
                };
                sceneDelayRow.AddChidren(btnSceneDelayInfo);
                Button btnSceneDelayTitle = new Button()
                {
                    X = Application.GetRealWidth(16),
                    //Width = Application.GetRealWidth(90),
                    TextAlignment = TextAlignment.CenterLeft,
                    TextColor = CSS_Color.FirstLevelTitleColor,
                    TextSize = CSS_FontSize.TextFontSize,
                    TextID = StringId.SceneDelay,
                };
                sceneDelayRow.AddChidren(btnSceneDelayTitle);
                btnSceneDelayTitle.MouseUpEventHandler = (sender, e) =>
                {
                    Action<string> action = (obj) => {
                        scene.delay = obj;
                        btnSceneDelayInfo.Text = scene.GetDelayText();
                    };
                    Dictionary<string, string> items = new Dictionary<string, string>();
                    items.Add("30", "30s");
                    items.Add("60", "1min");
                    items.Add("120", "2min");
                    items.Add("300", "5min");
                    new PublicAssmebly().SetSceneDelayDialog(items, action, scene.delay);
                };
                contentView.AddChidren(new Button()
                {
                    Y = sceneDelayRow.Bottom,
                    Height = Application.GetRealHeight(8),
                    BackgroundColor = CSS_Color.BackgroundColor,
                });
            }
            */
            #endregion
            #region 选择区域
            FrameLayout chooseZoneView = new FrameLayout()
@@ -273,8 +247,6 @@
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            contentView.AddChidren(btnLine1);
            #endregion
@@ -342,8 +314,6 @@
                }
            };
        }
        /// <summary>
        /// 加载功能列表
@@ -432,6 +402,273 @@
            }
        }
        /// <summary>
        /// 加载图标选择选项
        /// </summary>
        void LoadPictureOptionView()
        {
            var pView = new FrameLayout()
            {
                BackgroundColor = CSS_Color.DialogTransparentColor1,
            };
            bodyView.AddChidren(pView);
            pictureOptionView = new FrameLayout()
            {
                Y = Application.GetRealHeight(445),
                Height = Application.GetRealHeight(250),
                AnimateSpeed = 0.3f,
                Animate = Animate.DownToUp,
            };
            pView.AddChidren(pictureOptionView);
            optionView = new VerticalScrolViewLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealHeight(150),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
            };
            pictureOptionView.AddChidren(optionView);
            btnDefaultGallery = new Button()
            {
                Height = Application.GetRealHeight(50),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.DefaultGallery,
            };
            optionView.AddChidren(btnDefaultGallery);
            optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
            btnTakePicture = new Button()
            {
                Height = Application.GetRealHeight(50),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.TakePicture,
            };
            optionView.AddChidren(btnTakePicture);
            optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
            btnAlbum = new Button()
            {
                Height = Application.GetRealHeight(50),
                TextAlignment = TextAlignment.Center,
                TextColor = CSS_Color.TextualColor,
                SelectedTextColor = CSS_Color.MainColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextID = StringId.Album,
            };
            optionView.AddChidren(btnAlbum);
            optionView.AddChidren(new Button() { Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
            btnCancel = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(8) + optionView.Bottom,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
                TextID = StringId.Cancel,
                TextColor = CSS_Color.WarningColor,
                TextSize = CSS_FontSize.SubheadingFontSize,
            };
            pictureOptionView.AddChidren(btnCancel);
            LoadEvent_PictureOptionViewEventList(pView);
        }
        /// <summary>
        /// 加载背景图选择区域事件列表
        /// </summary>
        void LoadEvent_PictureOptionViewEventList(FrameLayout pView)
        {
            pictureOptionView.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            pView.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnCancel.MouseUpEventHandler = (sender, e) =>
            {
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnTakePicture.MouseDownEventHandler = (sender, e) => {
                btnTakePicture.IsSelected = true;
            };
            btnTakePicture.MouseUpEventHandler = (sender, e) =>
            {
                btnTakePicture.IsSelected = false;
                var imageName = Guid.NewGuid().ToString();
                CropImage.TakePicture((imagePath) =>
                {
                    CropImageCallBack(imagePath);
                }, imageName, 4, 3);
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnAlbum.MouseDownEventHandler = (sender, e) => {
                btnAlbum.IsSelected = true;
            };
            btnAlbum.MouseUpEventHandler = (sender, e) =>
            {
                btnAlbum.IsSelected = false;
                //从相册选择图片裁剪
                var imageName = Guid.NewGuid().ToString();
                //var imageName = scene.sid;
                CropImage.SelectPicture((imagePath) =>
                {
                    CropImageCallBack(imagePath);
                }, imageName, 4, 3);
                //if (pageTitleId == StringId.EditScene)
                //{
                //    scene.SaveFunctionData();
                //}
                pictureOptionView.Parent.RemoveFromParent();
            };
            btnDefaultGallery.MouseUpEventHandler = (sender, e) => {
                pictureOptionView.Parent.RemoveFromParent();
                Action<string> action = (obj) => {
                    scene.ImagePath = obj;
                    addSceneImageView.ImageBytes = null;//解决有ImageBytes不加载ImagePath
                    addSceneImageView.ImagePath = scene.ImagePath;
                };
                var galleryPage = new GalleryPage(scene.ImagePath, action);
                MainPage.BasePageView.AddChidren(galleryPage);
                galleryPage.LoadPage(true);
                MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
            };
        }
        /// <summary>
        /// 裁剪完照片回调,统一处理
        /// </summary>
        /// <param name="imagePath">裁剪后的真实路径</param>
        /// <param name="imageName">自定义的图片名称</param>
        void CropImageCallBack(string selectImagePath)
        {
            if (string.IsNullOrEmpty(selectImagePath) == true)
            {
                return;
            }
            //上传成功到回调
            Action<string> uploadSuccessAction = (imageUrl) =>
            {
                //2020-12-03 需要讲最新的图片路径同步到云端
                scene.ImagePath = imageUrl;
            };
            //上传图片到云端
            UploadImage(selectImagePath, addSceneImageView, scene, uploadSuccessAction);
        }
        /// <summary>
        /// 上传图片方法
        /// </summary>
        /// <param name="selectImagePath">裁剪后的图片路径</param>
        /// <param name="imageView"></param>
        /// <param name="uploadSuccessAction"></param>
        void UploadImage(string selectImagePath, ImageView imageView, Scene mScene, Action<string> uploadSuccessAction)
        {
            try
            {
                //MainPage.Log("SelectPicture 裁剪图片返回路径: " + selectImagePath);
                //1.读取裁剪后的图片,然后删除
                var imageBytes = Shared.IO.FileUtils.ReadFile(selectImagePath);
                System.IO.File.Delete(selectImagePath);
                var waitPage = new Loading();
                bodyView.AddChidren(waitPage);
                waitPage.Start(Language.StringByID(StringId.PleaseWait));
                //开始上传
                new Thread(() =>
                {
                    try
                    {
                        var newImageName = mScene.name.Trim() + ".png";
                        var uploadImageObj = new UploadImageObj()
                        {
                            prefix = "Scene" + Utlis.GetTimestamp(),
                            fileName = newImageName,
                            uid = mScene.sid,
                            content = imageBytes,
                        };
                        var imageUrl = ImageUtlis.Current.UploadImage(uploadImageObj);
                        if (!string.IsNullOrEmpty(imageUrl) && imageUrl.Contains(newImageName))
                        {
                            //上传成功
                            Application.RunOnMainThread(() =>
                            {
                                //Utlis.WriteLine("上传成功:" + imageUrl);
                                //1.2如果是自定义图片删除之前的
                                if (!string.IsNullOrEmpty(imageView.ImagePath) && !imageView.ImagePath.Contains("Classification/Room/Roombg"))
                                {
                                    //Utlis.WriteLine("删除: " + imageView.ImagePath);
                                    System.IO.File.Delete(imageView.ImagePath);
                                }
                                //重命名保存
                                ImageUtlis.Current.WriteFileByBytes(imageUrl, imageBytes);
                                imageView.ImagePath = null;
                                imageView.ImageBytes = imageBytes;
                                //上传成功
                                Utlis.ShowTip(Language.StringByID(StringId.UploadSuccessfully));
                                uploadSuccessAction?.Invoke(imageUrl);
                            });
                        }
                        else
                        {
                            //上传失败
                            Application.RunOnMainThread(() =>
                            {
                                //上传失败
                                Utlis.ShowTip(Language.StringByID(StringId.UploadFailed));
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                    finally
                    {
                        Application.RunOnMainThread(() =>
                        {
                            if (waitPage != null)
                            {
                                waitPage.RemoveFromParent();
                                waitPage = null;
                            }
                        });
                    }
                })
                { IsBackground = true }.Start();
            }
            catch { }
        }
    }
}