using System;
using Shared.Common;
namespace Shared.Phone.Device.CommonForm
{
public class SceneCategoryView : FrameLayout
{
///
/// scene
///
public SceneUI scene;
///
/// room
///
public Shared.Common.Room room;
///
/// delayTimeBtn
///
private Button delayTimeBtn;
///
/// delayImgBtn
///
private Button delayImgBtn;
///
/// sceneImg
///
private Button sceneImg;
///
/// SceneCategoryView
///
///
///
public SceneCategoryView(int x, int y)
{
X = Application.GetRealWidth(x);
Y = Application.GetRealHeight(y);
Width = Application.GetRealWidth(1080);
Height = Application.GetRealHeight(446);
BackgroundColor = ZigbeeColor.Current.GXCGrayBackgroundColor;
}
///
/// init
///
public void Init(SceneUI sceneUI,Common.Room curRoom)
{
this.scene = sceneUI;
this.room = curRoom;
InitFrame();
InitStatu();
}
///
/// InitFrame
///
private void InitFrame()
{
var sceneRowLayout = new RowLayout
{
Y = Application.GetRealHeight(46),
Height = Application.GetRealHeight(446 - 46),
LineColor = ZigbeeColor.Current.GXCGrayBackgroundColor
};
AddChidren(sceneRowLayout);
sceneImg = new Button()
{
X = Application.GetRealWidth(179),
Width = Application.GetRealWidth(844),
Height = Application.GetRealHeight(400),
UnSelectedImagePath = scene.IconPath,
Radius = (uint)Application.GetRealHeight(17)
};
sceneRowLayout.AddChidren(sceneImg);
var leftFL = new FrameLayout
{
X = Application.GetRealWidth(CommonFormResouce.X_Left),
Y = Application.GetRealHeight(58),
Width = Application.GetRealWidth(251),
Height = Application.GetRealHeight(282),
BackgroundColor = ZigbeeColor.Current.GXCBlackBackgroundColor2,
Radius = (uint)Application.GetRealHeight(17)
};
sceneRowLayout.AddChidren(leftFL);
var collectionBtn = new Button
{
X = Application.GetRealWidth(23),
Y = Application.GetRealHeight(23),
Width = Application.GetMinRealAverage(65),
Height = Application.GetMinRealAverage(65),
UnSelectedImagePath = "Item/Collection.png",
SelectedImagePath = "Item/CollectionSelected.png",
IsSelected = Common.Room.CurrentRoom.GetLoveRoom().SceneUIFilePathList.Contains(scene.FileName),
Tag = scene
};
leftFL.AddChidren(collectionBtn);
var sceneNameBtn = new Button()
{
Width = Application.GetRealWidth(176),
Height = Application.GetRealHeight(150),
TextColor = ZigbeeColor.Current.GXCTextWhiteColor,
Text = scene.Name,
Gravity = Gravity.Center,
IsMoreLines = true
};
leftFL.AddChidren(sceneNameBtn);
var collection = new Button
{
Tag = scene
};
leftFL.AddChidren(collection);
collection.MouseUpEventHandler += (sender, e) =>
{
if (room.IsLove)
{
Common.Room.CurrentRoom.GetLoveRoom().DeleteScene(scene);
RemoveViewByTag(collectionBtn.Tag);
}
else
{
collectionBtn.IsSelected = !collectionBtn.IsSelected;
if (collectionBtn.IsSelected)
{
Common.Room.CurrentRoom.GetLoveRoom().AddScene(scene);
}
else
{
Common.Room.CurrentRoom.GetLoveRoom().DeleteScene(scene);
}
}
};
delayTimeBtn = new Button
{
X = Application.GetRealWidth(780),
Y = Application.GetRealHeight(317),
Width = Application.GetRealWidth(200),
Height = Application.GetRealHeight(58),
Text = CommonFormResouce.GetTimeString(scene.SceneDelayTime),
TextAlignment = TextAlignment.CenterRight,
TextColor = ZigbeeColor.Current.GXCTextSelectedColor3
};
sceneRowLayout.AddChidren(delayTimeBtn);
delayImgBtn = new Button
{
X = Application.GetRealWidth(900),
Y = Application.GetRealHeight(30),
Width = Application.GetMinRealAverage(52),
Height = Application.GetMinRealAverage(52),
UnSelectedImagePath = "Scene/Delaying.png",
Visible=false
};
sceneRowLayout.AddChidren(delayImgBtn);
//定时
var delayBtn = new RowLayoutDeleteButton
{
TextID = R.MyInternationalizationString.Delay,
Tag = scene.SceneDelayTime
};
sceneRowLayout.AddLeftView(delayBtn);
//编辑
var settingBtn = new Device.CommonForm.RowLayoutEditButton()
{
TextID = R.MyInternationalizationString.Editor,
Tag = scene.SceneDelayTime
};
//删除
var deleteBtn = new Device.CommonForm.RowLayoutDeleteButton
{
Tag = scene
};
if (room.IsSharedRoom)
{
}
else
{
if (room.IsLove)
{
sceneRowLayout.AddRightView(deleteBtn);
}
else
{
sceneRowLayout.AddRightView(settingBtn);
sceneRowLayout.AddRightView(deleteBtn);
}
}
//删除场景
EventHandler delEvent = (sender, e) =>
{
if (room.IsLove)
{
Shared.Common.Room.CurrentRoom.GetLoveRoom().DeleteScene(scene);
RemoveViewByTag((sender as Button).Tag);
}
else
{
var tip = new CustomAlert { };
AddChidren(tip);
tip.Show(Language.StringByID(R.MyInternationalizationString.ConfirmDelete));
tip.ResultEventHandler += async (e1) =>
{
if (e1)
{
//0 移除失败 1 移除成功 2 没有该场景
var removeSceneAllData = await ZigBee.Device.Scene.DeleteSceneAsync(scene.Id);
if (removeSceneAllData == null || removeSceneAllData.removeSceneData == null)
{
CommonPage.Instance.FailureToServer();
return;
}
//1成功
if (removeSceneAllData.removeSceneData.Result == 1)
{
room.RemoveScene(scene);
//RefreshBodyView();
RemoveViewByTag((sender as Button).Tag);
}
//0 移除失败
else if (removeSceneAllData.removeSceneData.Result == 0)
{
CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.FailedPleaseTryAgain);
return;
}
//2 没有该场景
else if (removeSceneAllData.removeSceneData.Result == 2)
{
room.RemoveScene(scene);
//RefreshBodyView();
RemoveViewByTag((sender as Button).Tag);
CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.TheSceneIsNull);
return;
}
}
};
}
};
deleteBtn.MouseUpEventHandler += delEvent;
//编辑场景
settingBtn.MouseUpEventHandler += (sender, e) =>
{
SceneUI.EditScene(scene, room);
};
//编辑延时
delayBtn.MouseUpEventHandler += (sender, e) =>
{
if (scene.RemainTime > 0)
{
CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
return;
}
var timeSelect = new SelectTime();
CommonPage.Instance.AddChidren(timeSelect);
timeSelect.TempTime = scene.SceneDelayTime;
timeSelect.Init();
timeSelect.TimeAction = (t) =>
{
scene.SceneDelayTime = t;
delayTimeBtn.Text = CommonFormResouce.GetTimeString(t);
};
};
}
///
/// InitStatu
///
private void InitStatu()
{
sceneImg.MouseUpEventHandler += SceneUpHandler;
}
///
/// SceneUpHandler
///
///
///
private async void SceneUpHandler(object sender, MouseEventArgs args)
{
if (scene.RemainTime > 0 && scene.SceneDelayTime == 0)
{
CommonFormResouce.ShowTip(Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
return;
}
//1成功 0失败
//后面补上相关提示
// Error参数含义
//1:网关无法解析命令数据。
//2:协调器正在升级或备份/恢复数据
//3:操作设备/组/场景不存在
//4:其他错误
//5:数据传输错误(在某次客户端向网关发送数据的过程中,网关在合理时间范围内接收客户端数据不完整导致该错误发生。如客户端向网关一次发送100个字节的数据,但网关等待接收了一秒只接收了80个字节。发生该错误,网关将主动关闭客户端连接)
var sceneOpenAllData = await ZigBee.Device.Scene.ControlSceneAsync(scene.Id, scene.SceneDelayTime);
if (sceneOpenAllData == null || sceneOpenAllData.sceneOpenData == null)
{
return;
}
if (sceneOpenAllData.sceneOpenData.Result == 0)
{
if (sceneOpenAllData.errorResponData == null)
{
return;
}
CommonPage.Instance.ShowErrorInfoAlert(R.MyInternationalizationString.ControlSceneFail);
}
scene.RemainTime = scene.SceneDelayTime;
scene.SceneDelayTime = 0;
new System.Threading.Thread(() =>
{
while (scene.RemainTime > 0)
{
System.Threading.Thread.Sleep(1000);
Application.RunOnMainThread(() =>
{
SetTimeText(CommonFormResouce.GetTimeString(scene.RemainTime));
});
scene.RemainTime -= 1;
}
Application.RunOnMainThread(() =>
{
SetTimeImage();
});
})
{ IsBackground = true }.Start();
}
///
/// SetTimeText
///
///
public void SetTimeText(string name)
{
delayImgBtn.Visible = true;
delayTimeBtn.Text = name;
}
///
/// SetTimeImage
///
public void SetTimeImage()
{
delayImgBtn.Visible = false;
delayTimeBtn.Text = string.Empty;
}
}
}