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;
namespace HDL_ON.UI
{
public partial class AddRoomPage : FrameLayout
{
#region 控件列表
///
/// 当前窗体
///
FrameLayout bodyView;
///
/// 房间背景区域
///
FrameLayout roomBgView;
///
/// 房间背景显示按钮
///
ImageView btnRoomBg;
///
/// 房间信息编辑区域
///
VerticalScrolViewLayout roomInfoView;
///
///
///
Button btnEditInfoTitle;
///
/// 房间名称Row
///
FrameLayout roomNameRow;
///
/// 房间名称标题按钮
///
Button btnRoomNameTile;
///
/// 房间名称
///
Button btnRoomName;
///
/// 房间名称修改按钮
///
Button btnEditRoomNameIcon;
///
/// 所属楼层
///
FrameLayout floorView;
///
/// 房间所属楼层标题
///
Button btnFloorTitle;
///
/// 房间楼层信息
///
Button btnFloorInfo;
///
/// 房间楼层修改跳转按钮
///
Button btnFloorRight;
///
/// 功能管理区域
///
FrameLayout functionManagemantView;
///
/// 功能管理标题
///
Button btnFunctionManagementTitle;
///
/// 功能管理跳转按钮
///
Button btnFunctionManagementRight;
///
/// 操作选项按钮
///
Button btnOption;
#region 图标选择部分图标
///
/// 背景图选项区域
///
FrameLayout pictureOptionView;
///
/// 背景图选项选择区域
///
VerticalScrolViewLayout optionView;
///
/// 默认图库按钮
///
Button btnDefaultGallery;
///
/// 拍照按钮
///
Button btnTakePicture;
///
/// 相册按钮
///
Button btnAlbum;
///
/// 取消按钮
///
Button btnCancel;
#endregion
#endregion
Room room;
int rowCount = 1;
Action backAction;
///
/// 房间的功能列表,房间未保存到云端时,需要缓存起来
///
List roomFunctions = new List();
public AddRoomPage(Action action)
{
room = new Room();
bodyView = this;
backAction = action;
}
public void LoadPage()
{
bodyView.BackgroundColor = CSS_Color.BackgroundColor;
new TopViewDiv(bodyView, Language.StringByID(StringId.AddRoom)).LoadTopView();
roomBgView = new FrameLayout()
{
Y = Application.GetRealHeight(64),
Height = Application.GetRealWidth(232),
BackgroundColor = CSS_Color.MainBackgroundColor,
};
bodyView.AddChidren(roomBgView);
btnRoomBg = new ImageView()
{
Gravity = Gravity.CenterHorizontal,
Y = Application.GetRealWidth(20),
Width = Application.GetRealWidth(343),
Height = Application.GetRealWidth(192),
Radius = (uint)Application.GetRealWidth(12),
};
roomBgView.AddChidren(btnRoomBg);
//2020-12-03 修改图片加载方法
ImageUtlis.Current.LoadLocalOrNetworkImages(room.backgroundImage, btnRoomBg);
//房间信息编辑区域
roomInfoView = new VerticalScrolViewLayout()
{
Y = roomBgView.Bottom + Application.GetRealHeight(8),
Height = Application.GetRealHeight(50 * 5),
BackgroundColor = CSS_Color.MainBackgroundColor,
ScrollEnabled = false,
};
bodyView.AddChidren(roomInfoView);
btnEditInfoTitle = new Button()
{
Height = Application.GetRealWidth(50),
X = Application.GetRealWidth(12),
TextColor = CSS_Color.MainColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextAlignment = TextAlignment.CenterLeft,
IsBold = true,
TextID = StringId.EditRoomInfo,
};
roomInfoView.AddChidren(btnEditInfoTitle);
roomInfoView.AddChidren(new Button() { X = Application.GetRealWidth(16), Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
#region 房间名称Row
roomNameRow = new FrameLayout()
{
Height = Application.GetRealHeight(50),
};
roomInfoView.AddChidren(roomNameRow);
rowCount++;
btnRoomNameTile = new Button()
{
X = Application.GetRealWidth(16),
Width = Application.GetRealWidth(120),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.RoomName,
};
roomNameRow.AddChidren(btnRoomNameTile);
btnRoomName = new Button()
{
X = Application.GetRealWidth(100),
Width = Application.GetRealWidth(230),
TextAlignment = TextAlignment.CenterRight,
TextColor = CSS_Color.PromptingColor1,
TextSize = CSS_FontSize.TextFontSize,
Text = room.roomName
};
roomNameRow.AddChidren(btnRoomName);
btnEditRoomNameIcon = new Button()
{
X = Application.GetRealWidth(333),
Gravity = Gravity.CenterVertical,
Width = Application.GetMinRealAverage(28),
Height = Application.GetMinRealAverage(28),
UnSelectedImagePath = "Public/EditIcon.png",
};
roomNameRow.AddChidren(btnEditRoomNameIcon);
roomInfoView.AddChidren(new Button() { X = Application.GetRealWidth(16), Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
#endregion
#region 所属楼层
floorView = new FrameLayout()
{
Height = Application.GetRealHeight(50),
};
roomInfoView.AddChidren(floorView);
rowCount++;
btnFloorTitle = new Button()
{
X = Application.GetRealWidth(16),
Width = Application.GetRealWidth(120),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.Floor,
};
floorView.AddChidren(btnFloorTitle);
btnFloorInfo = new Button()
{
X = Application.GetRealWidth(100),
Width = Application.GetRealWidth(230),
TextAlignment = TextAlignment.CenterRight,
TextColor = CSS_Color.PromptingColor1,
TextSize = CSS_FontSize.TextFontSize,
Text = room.floorName
};
floorView.AddChidren(btnFloorInfo);
btnFloorRight = new Button()
{
X = Application.GetRealWidth(339),
Gravity = Gravity.CenterVertical,
Width = Application.GetMinRealAverage(16),
Height = Application.GetMinRealAverage(16),
UnSelectedImagePath = "Public/Right.png",
};
floorView.AddChidren(btnFloorRight);
roomInfoView.AddChidren(new Button() { X = Application.GetRealWidth(16), Height = Application.GetRealHeight(1), Width = Application.GetRealWidth(343), BackgroundColor = CSS_Color.DividingLineColor });
#endregion
#region 功能管理
functionManagemantView = new FrameLayout()
{
Height = Application.GetRealHeight(50),
};
roomInfoView.AddChidren(functionManagemantView);
rowCount++;
btnFunctionManagementTitle = new Button()
{
X = Application.GetRealWidth(16),
Width = Application.GetRealWidth(220),
TextAlignment = TextAlignment.CenterLeft,
TextColor = CSS_Color.FirstLevelTitleColor,
TextSize = CSS_FontSize.SubheadingFontSize,
TextID = StringId.FunctionManagement,
};
functionManagemantView.AddChidren(btnFunctionManagementTitle);
btnFunctionManagementRight = new Button()
{
X = Application.GetRealWidth(339),
Gravity = Gravity.CenterVertical,
Width = Application.GetMinRealAverage(16),
Height = Application.GetMinRealAverage(16),
UnSelectedImagePath = "Public/Right.png",
};
functionManagemantView.AddChidren(btnFunctionManagementRight);
#endregion
roomInfoView.Height = Application.GetRealHeight(51 * rowCount);
btnOption = new Button()
{
Y = Application.GetRealHeight(617),
Height = Application.GetRealHeight(50),
BackgroundColor = CSS_Color.MainBackgroundColor,
TextAlignment = TextAlignment.Center,
TextColor = CSS_Color.MainColor,
TextID = StringId.Save,
TextSize = CSS_FontSize.SubheadingFontSize,
};
bodyView.AddChidren(btnOption);
if (MainPage.Increase)
{
btnOption.Y = Application.GetRealHeight(607);
btnOption.Height = Application.GetRealHeight(60);
}
LoadEventList();
}
///
/// 加载图标选择选项
///
void LoadPictureOptionView()
{
var baseView = new FrameLayout()
{
BackgroundColor = CSS_Color.DialogTransparentColor1,
};
bodyView.AddChidren(baseView);
pictureOptionView = new FrameLayout()
{
Y = Application.GetRealHeight(445),
Height = Application.GetRealHeight(250),
AnimateSpeed = 0.3f,
Animate = Animate.DownToUp,
};
baseView.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() { X = Application.GetRealWidth(16), 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() { X = Application.GetRealWidth(16), 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() { X = Application.GetRealWidth(16), 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(baseView);
}
}
//------------------------------------------------------------------------------------------------------------------------------------
public partial class AddRoomPage
{
///
/// 加载事件列表
///
void LoadEventList()
{
LoadEvent_PictureOptionView();
LoadEvent_EditRoomName();
LoadEvent_SkinBindFloor();
LoadEvent_SkipEditFunctionList();
LoadEvent_SaveRoom();
}
///
/// 底部按钮事件
///
void LoadEvent_SaveRoom()
{
btnOption.MouseUpEventHandler = (sender, e) =>
{
if (string.IsNullOrEmpty(room.roomName))
{
new Tip()
{
CloseTime = 1,
Text = Language.StringByID(StringId.RoomNameCannotBeEmpty),
Direction = AMPopTipDirection.None,
}.Show(bodyView);
return;
}
if (string.IsNullOrEmpty(room.floorName))
{
new Tip()
{
CloseTime = 1,
Text = Language.StringByID(StringId.PlsBindFloor),
Direction = AMPopTipDirection.None,
}.Show(bodyView);
return;
}
foreach (var tempRoom in SpatialInfo.CurrentSpatial.RoomList)
{
if (room.roomName == tempRoom.roomName)
{
new PublicAssmebly().TipMsg(StringId.Tip, StringId.AddingFailed_RoomAlreadyExists);
return;
}
}
var waitPage = new Loading();
waitPage.Start();
new Thread(() =>
{
try
{
var addResult = SpatialInfo.CurrentSpatial.AddRoom(room, out room);//检查room是否为云端返回的最新数据
if (addResult == StateCode.SUCCESS)
{
var deviceIds = new List();
foreach (var f in roomFunctions)
{
deviceIds.Add(f.deviceId);
}
if (deviceIds.Count > 0)
{
var bindResult = FunctionList.List.FunctionsBindRooms(new List() { room.roomId }, deviceIds);
if (bindResult == StateCode.SUCCESS)
{
foreach (var f in roomFunctions)
{
f.roomIds.Add(room.roomId);
}
room.GetRoomFunctions(true);
}
}
Application.RunOnMainThread(() =>
{
backAction(room);
RemoveFromParent();
});
}
}
catch (Exception ex)
{
MainPage.Log($"添加房间失败msg:{ex.Message}");
}
finally
{
Application.RunOnMainThread(() =>
{
waitPage.Hide();
});
}
})
{ IsBackground = true }.Start();
};
}
///
/// 显示背景图选择选项
///
void LoadEvent_PictureOptionView()
{
btnRoomBg.MouseUpEventHandler = (sender, e) =>
{
LoadPictureOptionView();
};
}
///
/// 加载修改房间名称窗口事件
///
void LoadEvent_EditRoomName()
{
Action callBack = (str) =>
{
//房间名称不能为空
if (string.IsNullOrEmpty(str))
{
new Tip()
{
CloseTime = 1,
Text = Language.StringByID(StringId.RoomNameCannotBeEmpty),
Direction = AMPopTipDirection.None,
}.Show(bodyView);
return;
}
btnRoomName.Text = str;
room.roomName = str;
};
EventHandler eventHandler = (sender, e) =>
{
List roomNameList = new List();
foreach (var temp in SpatialInfo.CurrentSpatial.RoomList)
{
roomNameList.Add(temp.roomName);
}
new PublicAssmebly().LoadDialog_EditParater(StringId.RoomName, room.roomName, callBack, StringId.RoomNameCannotBeEmpty, StringId.RoomNameAlreadyExists, roomNameList);
};
btnEditRoomNameIcon.MouseUpEventHandler = eventHandler;
roomNameRow.MouseUpEventHandler = eventHandler;
btnRoomName.MouseUpEventHandler = eventHandler;
}
///
/// 跳转绑定楼层界面
///
void LoadEvent_SkinBindFloor()
{
EventHandler eventHandler = (sender, e) =>
{
Action floorsCallBackAction = () =>
{
btnFloorInfo.Text = room.floorName;
};
var residentialManagePage = new RoomBinglingFloorPage(floorsCallBackAction, room);
MainPage.BasePageView.AddChidren(residentialManagePage);
residentialManagePage.LoadPage();
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
};
btnFloorInfo.MouseUpEventHandler = eventHandler;
floorView.MouseUpEventHandler = eventHandler;
}
///
/// 跳转到编辑房间功能界面
///
void LoadEvent_SkipEditFunctionList()
{
EventHandler eventHandler = (sender, e) =>
{
Action> addFunctionAction = (functionList) =>
{
roomFunctions = functionList;
};
var residentialManagePage = new RoomEditFunctionPage(room,addFunctionAction);
MainPage.BasePageView.AddChidren(residentialManagePage);
residentialManagePage.LoadPage();
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
};
btnFunctionManagementRight.MouseUpEventHandler = eventHandler;
functionManagemantView.MouseUpEventHandler = eventHandler;
}
///
/// 加载背景图选择区域事件列表
///
void LoadEvent_PictureOptionViewEventList(FrameLayout baseView)
{
pictureOptionView.MouseUpEventHandler = (sender, e) =>
{
baseView.RemoveFromParent();
};
baseView.MouseUpEventHandler = (sender, e) =>
{
baseView.RemoveFromParent();
};
btnCancel.MouseUpEventHandler = (sender, e) =>
{
baseView.RemoveFromParent();
};
btnTakePicture.MouseDownEventHandler = (sender, e) =>
{
btnTakePicture.IsSelected = true;
};
//拍照
btnTakePicture.MouseUpEventHandler = (sender, e) =>
{
btnTakePicture.IsSelected = false;
var imageName = Guid.NewGuid().ToString();
//var imageName = room.sid;
CropImage.TakePicture((imagePath) =>
{
CropImageCallBack(imagePath);
}, imageName, 16, 9);
baseView.RemoveFromParent();
};
//选择照片
btnAlbum.MouseDownEventHandler = (sender, e) =>
{
btnAlbum.IsSelected = true;
};
btnAlbum.MouseUpEventHandler = (sender, e) =>
{
btnAlbum.IsSelected = false;
//从相册选择图片裁剪
var imageName = Guid.NewGuid().ToString();
//var imageName = room.sid;
CropImage.SelectPicture((imagePath) =>
{
CropImageCallBack(imagePath);
}, imageName, 16, 9);
baseView.RemoveFromParent();
};
//房间默认图库
btnDefaultGallery.MouseUpEventHandler = (sender, e) => {
pictureOptionView.Parent.RemoveFromParent();
Action action = (obj) => {
room.backgroundImage = obj;
btnRoomBg.ImageBytes = null;//解决有ImageBytes不加载ImagePath
btnRoomBg.ImagePath = room.backgroundImage;
};
var galleryPage = new RoomGalleryPage(room.backgroundImage, action);
MainPage.BasePageView.AddChidren(galleryPage);
galleryPage.LoadPage(true);
MainPage.BasePageView.PageIndex = MainPage.BasePageView.ChildrenCount - 1;
};
}
///
/// 裁剪完照片回调,统一处理
///
/// 裁剪后的真实路径
/// 自定义的图片名称
void CropImageCallBack(string selectImagePath)
{
if (string.IsNullOrEmpty(selectImagePath) == true)
{
return;
}
//上传成功到回调
Action uploadSuccessAction = (imageUrl) =>
{
//2020-12-03 需要讲最新的图片路径同步到云端
room.backgroundImage = imageUrl;
};
//上传图片到云端
UploadImage(selectImagePath, btnRoomBg, room, uploadSuccessAction);
}
///
/// 上传图片方法
///
/// 裁剪后的图片路径
///
///
void UploadImage(string selectImagePath, ImageView imageView, Room mRoom, Action uploadSuccessAction)
{
try
{
//MainPage.Log("SelectPicture 裁剪图片返回路径: " + selectImagePath);
//1.读取裁剪后的图片,然后删除
var imageBytes = ImageUtlis.Current.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 = mRoom.roomName.Trim() + ".png";
var uploadImageObj = new UploadImageObj()
{
prefix = "Room" + Utlis.GetTimestamp(),
fileName = newImageName,
uid = mRoom.uid,
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 { }
}
}
}