using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using ZigBee.Device;
namespace Shared.Phone.UserCenter.SharedContent
{
///
/// 配置新共享内容的主界面
///
public class AddNewSharedListRoomForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalListControl listView = null;
///
/// 分享按钮
///
private BottomClickButton btnShard = null;
///
/// 成员信息
///
private MemberInfoRes memberResult = null;
///
/// 成员的分享数据
///
private MemberShardInfoData memberShardInfo = null;
///
/// 选择的房间
///
private Dictionary dicSelectRoom = new Dictionary();
///
/// 当前选择的楼层ID
///
private string nowSelectFloorId = string.Empty;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
/// 成员信息
/// 成员的共享信息
public void ShowForm(MemberInfoRes i_memberResult, MemberShardInfoData i_memberShardInfo)
{
this.memberResult = i_memberResult;
this.memberShardInfo = i_memberShardInfo;
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddShared));
//初始化右上角的控件
this.InitTopRightMenuControl();
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
var frameBack = new FrameLayout();
frameBack.Height = Application.GetRealHeight(141);
frameBack.BackgroundColor = UserCenterColor.Current.White;
bodyFrameLayout.AddChidren(frameBack);
//房间列表
var btnTitle = new NormalViewControl(800, 60, true);
btnTitle.X = ControlCommonResourse.XXLeft;
btnTitle.Y = Application.GetRealHeight(52);
btnTitle.TextID = R.MyInternationalizationString.RoomList;
btnTitle.TextColor = UserCenterColor.Current.TextColor2;
btnTitle.TextSize = 15;
frameBack.AddChidren(btnTitle);
this.listView = new VerticalListControl(29);
listView.Y = frameBack.Bottom;
listView.Height = bodyFrameLayout.Height - frameBack.Bottom;
listView.BackgroundColor = UserCenterColor.Current.White;
bodyFrameLayout.AddChidren(listView);
this.btnShard = new BottomClickButton();
btnShard.TextID = R.MyInternationalizationString.uShared2;
bodyFrameLayout.AddChidren(btnShard);
btnShard.Visible = false;
btnShard.ButtonClickEvent += (sender, e) =>
{
var listRoom = new List();
foreach (var room in this.dicSelectRoom.Values)
{
listRoom.Add(room);
}
//执行分享指定区域
this.DoSharedContentByRoom(listRoom);
};
HdlThreadLogic.Current.RunThread(() =>
{
//初始化区域列表
this.InitAreaListRow(this.nowSelectFloorId);
});
}
///
/// 初始化右上角的控件
///
private void InitTopRightMenuControl()
{
//获取楼层
var dicFloor = HdlRoomLogic.Current.GetFloorSortList();
if (dicFloor.Count == 0)
{
return;
}
var btnIconContr = new MostRightIconControl(69, 69);
btnIconContr.UnSelectedImagePath = "Item/Drop_Down.png";
topFrameLayout.AddChidren(btnIconContr);
btnIconContr.InitControl();
var btnFloor = new NormalViewControl(300, 69, true);
btnFloor.Gravity = Gravity.CenterVertical;
btnFloor.X = btnIconContr.X + btnIconContr.btnIcon.X - Application.GetRealWidth(300);
btnFloor.TextAlignment = TextAlignment.CenterRight;
topFrameLayout.AddChidren(btnFloor);
foreach (var floorId in dicFloor.Keys)
{
//第一个楼层
this.nowSelectFloorId = floorId;
btnFloor.Text = dicFloor[floorId];
break;
}
btnIconContr.ButtonClickEvent += (sender, e) =>
{
//楼层菜单
var contr = new TopRightFloorMenuControl(dicFloor.Count, 2, Language.StringByID(R.MyInternationalizationString.SelectFloor));
foreach (var floorId in dicFloor.Keys)
{
contr.AddRowMenu(floorId, () =>
{
//记录起选择的ID
this.nowSelectFloorId = floorId;
btnFloor.Text = dicFloor[this.nowSelectFloorId];
//初始化中部信息
this.InitMiddleFrame();
});
}
};
}
#endregion
#region ■ 区域列表___________________________
///
/// 初始化区域列表
///
/// 楼层ID
private void InitAreaListRow(string floorId)
{
dicSelectRoom.Clear();
var listRoom = HdlRoomLogic.Current.GetFloorSortRoom(floorId, false);
HdlThreadLogic.Current.RunMain(() =>
{
var listDataRoom = new List();
for (int i = 0; i < listRoom.Count; i++)
{
//检测能否显示该房间
if (this.CheckCanShowRow(listRoom[i]) == true)
{
listDataRoom.Add(listRoom[i]);
}
}
for (int i = 0; i < listDataRoom.Count; i++)
{
//添加房间行
this.AddRoomRow(listDataRoom[i], i != listDataRoom.Count - 1);
}
//调整真实高度
listView.AdjustRealHeight(Application.GetRealHeight(23));
if (listView.Y + listView.Height > btnShard.Y)
{
//当这个列表控件的高度已经超过了【分享按钮】时,将它的高度直接扩大到最大化
listView.RecoverHeight();
//然后再添加一个空白的控件,促使它能够向上滑动
var frameTemp = new FrameLayout();
frameTemp.Height = bodyFrameLayout.Height - btnShard.Y;
listView.AddChidren(frameTemp);
}
if (listDataRoom.Count == 0)
{
//如果没有能够共享的房间
bodyFrameLayout.RemoveAll();
//无可共享的房间
this.ShowNotDataImage(bodyFrameLayout, Language.StringByID(R.MyInternationalizationString.uNotCanShardRoomMsg), "Item/NotShardPic.png", 383, 279);
}
});
}
///
/// 添加房间行
///
///
///
private void AddRoomRow(Common.Room room, bool addLine)
{
var row = new FrameRowControl(listView.rowSpace / 2);
row.MainKeys = room.Id;
listView.AddChidren(row);
//图标
var btnIcon = row.AddLeftIcon(81);
btnIcon.UnSelectedImagePath = "Item/RoomIconSelected.png";
//房间名
var btnName = row.AddLeftCaption(room.Name, 650);
btnName.TextSize = 15;
if (addLine == true)
{
//底线
row.AddBottomLine();
}
row.ButtonClickEvent += (sender, e) =>
{
var form = new AddNewSharedContentForm();
form.AddForm(room, memberShardInfo);
};
//选择
var btnSelect = row.AddMostRightEmptyIcon(69, 69);
btnSelect.UnSelectedImagePath = "Item/ItemUnSelected.png";
btnSelect.SelectedImagePath = "Item/ItemSelected.png";
row.ChangedChidrenBindMode(btnSelect, ChidrenBindMode.NotBind);
btnSelect.ButtonClickEvent += (sender, e) =>
{
if (btnSelect.IsSelected == true)
{
btnSelect.IsSelected = false;
dicSelectRoom.Remove(room.Id);
if (dicSelectRoom.Count == 0)
{
btnShard.Visible = false;
}
}
else
{
btnSelect.IsSelected = true;
dicSelectRoom[room.Id] = room;
if (btnShard.Visible == false)
{
btnShard.Visible = true;
}
}
};
}
#endregion
#region ■ 分享指定区域_______________________
///
/// 执行分享指定区域
///
/// 指定要分享的房间
public void DoSharedContentByRoom(List listRoom)
{
//获取需要上传的设备(按房间List顺序分组)
var dicDevice = new Dictionary>();
//获取需要上传的场景,这个东西不要获取它的绑定目标(按房间List顺序分组)
var dicScene = new Dictionary>();
//获取需要上传的房间数据
int fileCount = this.GetUpLoadRoomData(listRoom, dicDevice, dicScene);
var listCheckFile = new HashSet();
HdlThreadLogic.Current.RunThread(() =>
{
//打开进度条
this.ShowProgressBar();
for (int index = 0; index < listRoom.Count; index++)
{
//执行上传
var result = HdlShardLogic.Current.DoUploadSharedContent(memberShardInfo, listRoom[index], dicDevice[index], dicScene[index], fileCount, listCheckFile);
if (result == false)
{
break;
}
}
//关闭进度条
this.CloseProgressBar();
//不管成功还是失败,都刷新界面
HdlThreadLogic.Current.RunMain(() =>
{
if (this.Parent != null)
{
//重新刷新界面
this.InitMiddleFrame();
}
});
});
}
///
/// 获取需要上传的房间数据
///
/// 需要上传的房间列表
/// 获取需要上传的设备(按房间List顺序分组)
/// 获取需要上传的场景,这个东西不要获取它的绑定目标(按房间List顺序分组)
///
private int GetUpLoadRoomData(List listRoom, Dictionary> dicDevice, Dictionary> dicScene)
{
var listAllFile = new HashSet();
for (int i = 0; i < listRoom.Count; i++)
{
//按房间List顺序分组的设备列表
var listDevice = new List();
dicDevice[i] = listDevice;
//按房间List顺序分组的场景列表
var listScene = new List();
dicScene[i] = listScene;
//获取房间全部设备
foreach (var deviceKeys in listRoom[i].ListDevice)
{
var device = Common.LocalDevice.Current.GetDevice(deviceKeys);
if (device == null || memberShardInfo.dicAllShardKeys.ContainsKey(device.FilePath) == true)
{
//异常
continue;
}
string deviceFile = device.FilePath;
if (memberShardInfo.dicAllShardKeys.ContainsKey(deviceFile) == false)
{
//或者已经分享了的,不再显示
listDevice.Add(device);
if (listAllFile.Contains(deviceFile) == false)
{
listAllFile.Add(deviceFile);
}
}
}
//获取场景里面嵌套的子设备和子场景(计算总数用)
var listCheck = new HashSet();
var listChirdDevice = new List();
var listChirdScene = new List();
//获取房间全部场景
foreach (var sceneId in listRoom[i].ListSceneId)
{
var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
if (sceneUi == null || memberShardInfo.dicAllShardKeys.ContainsKey(sceneUi.FileName) == true)
{
//异常,或者已经分享了的,不再显示
continue;
}
listScene.Add(sceneUi);
//从缓存获取场景的执行目标(这个函数是计算总数用)
HdlShardLogic.Current.GetSceneDeviceList(sceneUi, listCheck, listChirdScene, listChirdDevice);
if (listAllFile.Contains(sceneUi.FileName) == false)
{
listAllFile.Add(sceneUi.FileName);
}
}
foreach (var device in listChirdDevice)
{
//嵌套子设备的文件名字(计算总数用)
string deviceFile = device.FilePath;
if (listAllFile.Contains(deviceFile) == false)
{
listAllFile.Add(deviceFile);
}
}
foreach (var secene in listChirdScene)
{
//嵌套子场景的文件名字(计算总数用)
if (listAllFile.Contains(secene.FileName) == false)
{
listAllFile.Add(secene.FileName);
}
}
//房间文件
listAllFile.Add(listRoom[i].FileName);
}
//如果有楼层的话
if (Common.Config.Instance.Home.FloorDics.Count > 0)
{
return listAllFile.Count + 1;
}
return listAllFile.Count;
}
#endregion
#region ■ 界面重新激活事件___________________
///
/// 自身的上层界面关闭后,它自身处于最上层时,触发的事件
///
public override int FormActionAgainEvent()
{
//重新刷新界面
this.InitMiddleFrame();
return 1;
}
#endregion
#region ■ 一般方法___________________________
///
/// 检测该房间能否显示
///
///
///
private bool CheckCanShowRow(Common.Room room)
{
if (room.ListDevice.Count == 0 && room.ListSceneId.Count == 0)
{
return false;
}
if (room.IsLove == true)
{
return false;
}
if (memberShardInfo.dicShardRoom.ContainsKey(room.FileName) == false)
{
//没有这个房间
return true;
}
foreach (var deviceKeys in room.ListDevice)
{
var device = Common.LocalDevice.Current.GetDevice(deviceKeys);
if (device == null)
{
continue;
}
if (memberShardInfo.dicAllShardKeys.ContainsKey(device.FilePath) == false)
{
//存在未分享的设备的话,此房间可以显示
return true;
}
}
foreach (var sceneId in room.ListSceneId)
{
var sceneUi = HdlSceneLogic.Current.GetSceneUIBySceneId(sceneId);
if (sceneUi == null)
{
continue;
}
if (memberShardInfo.dicAllShardKeys.ContainsKey(sceneUi.FileName) == false)
{
//存在未分享的场景的话,此房间可以显示
return true;
}
}
return false;
}
#endregion
#region ■ 结构体_____________________________
///
/// 接收分享数据
///
private class MemberShardInfoResult
{
///
/// 文件名字
///
public string ShareName = string.Empty;
///
/// 主键
///
public string DistributedMark = string.Empty;
}
#endregion
}
}