using Shared.Phone.UserCenter;
using System;
using System.Collections.Generic;
using System.Text;
namespace Shared.Phone.MainPage.Controls
{
///
/// 场景卡片控件
///
public class SceneCardControl : FrameLayoutControl
{
#region ■ 变量声明___________________________
///
/// 卡片需要被移除的事件
///
public Action CardNeedRemoveEvent = null;
///
/// 场景图片控件
///
private ImageView btnScenePic = null;
///
/// 场景背景图控件
///
private PicViewControl btnSceneBackGroud = null;
///
/// 时间图标控件
///
private IconViewControl btnTimeIcon = null;
///
/// 延时时间显示控件
///
private NormalViewControl btnTimeView = null;
///
/// 场景名字控件
///
private NormalViewControl btnSceneName = null;
///
/// 场景ID
///
private int SceneId = 0;
#endregion
#region ■ 初始化_____________________________
///
/// 场景卡片控件(宽度:458 + 14 * 2 高度:305 + 43)
///
public SceneCardControl()
{
//图片真实宽度+图片自身左右空白
this.Width = Application.GetMinRealAverage(458 + 14 * 2);
//图片高度+阴影
this.Height = Application.GetMinRealAverage(305 + 43);
//取消点击特效
this.UseClickStatu = false;
this.ButtonClickEvent += (sender, e) =>
{
//开启Loading特效
this.StartLoadingApreal();
//调用场景
this.SetSceneAction();
};
}
///
/// 初始化控件
///
/// 场景对象
public void InitControl(Common.SceneUI i_scene)
{
this.SceneId = i_scene.Id;
//场景图片控件
this.btnScenePic = new ImageView();
btnScenePic.X = Application.GetMinRealAverage(14);
btnScenePic.Width = Application.GetMinRealAverage(458);
btnScenePic.Height = Application.GetMinRealAverage(305);
btnScenePic.Radius = (uint)Application.GetMinRealAverage(17);
this.AddChidren(btnScenePic, ChidrenBindMode.NotBind);
//场景背景图控件
this.btnSceneBackGroud = new PicViewControl(this.Width, this.Height, false);
btnSceneBackGroud.UnSelectedImagePath = "Scene/Background.png";
this.AddChidren(btnSceneBackGroud, ChidrenBindMode.BindEventOnly);
//延时时间显示控件
this.btnTimeView = new NormalViewControl(Application.GetMinRealAverage(280), Application.GetMinRealAverage(63), false);
btnTimeView.X = Application.GetMinRealAverage(37);
btnTimeView.Y = Application.GetMinRealAverage(10);
btnTimeView.TextColor = 0xffffb400;
btnTimeView.IsBold = true;
this.AddChidren(btnTimeView, ChidrenBindMode.BindEventOnly);
//时间图标控件
this.btnTimeIcon = new IconViewControl(63);
btnTimeIcon.X = Application.GetMinRealAverage(37);
btnTimeIcon.Y = Application.GetMinRealAverage(20);
btnTimeIcon.UnSelectedImagePath = "Item/Time.png";
this.AddChidren(btnTimeIcon, ChidrenBindMode.BindEventOnly);
//收藏控件
var btnCollect = new IconViewControl(107);
btnCollect.X = Application.GetMinRealAverage(350);
btnCollect.Y = Application.GetMinRealAverage(12);
btnCollect.UnSelectedImagePath = "Item/Collection1.png";
btnCollect.SelectedImagePath = "Item/CollectionSelected1.png";
this.AddChidren(btnCollect, ChidrenBindMode.NotBind);
btnCollect.IsSelected = HdlRoomLogic.Current.IsCollectInRoom(i_scene);
btnCollect.ButtonClickEvent += (sender, e) =>
{
//状态取反
btnCollect.IsSelected = !btnCollect.IsSelected;
if (btnCollect.IsSelected == false)
{
//取消收藏
HdlSceneLogic.Current.DeleteLoveScene(i_scene);
if (HdlRoomLogic.Current.CurrentRoom.IsLove == true)
{
//如果当前房间是我的喜爱的话,回调卡片被删除的事件
this.CardNeedRemoveEvent?.Invoke();
}
}
else
{
//添加收藏
HdlSceneLogic.Current.AddLoveScene(i_scene);
}
};
//场景名称控件
this.btnSceneName = new NormalViewControl(Application.GetMinRealAverage(280), Application.GetMinRealAverage(63), false);
btnSceneName.X = Application.GetMinRealAverage(37);
btnSceneName.Y = Application.GetMinRealAverage(236);
btnSceneName.IsBold = true;
btnSceneName.TextColor = UserCenterColor.Current.White;
this.AddChidren(btnSceneName, ChidrenBindMode.BindEventOnly);
//刷新控件状态
this.RefreshControlInfo(i_scene);
}
#endregion
#region ■ 调用场景___________________________
///
/// 调用场景
///
private async void SetSceneAction()
{
//这个场景不见的情况应该不可能
var scene = HdlSceneLogic.Current.GetSceneUIBySceneId(this.SceneId);
if (scene.RemainTime > 0 )
{
//该场景正在延时,请稍后
var msgContr = new ShowMsgControl(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.TheSceneIsDelaying));
msgContr.Show();
return;
}
//执行调用场景
var result = await ZigBee.Device.Scene.ControlSceneAsync(scene.Id, scene.SceneDelayTime);
if (result == null || result.sceneOpenData == null)
{
//控制场景失败
string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
//拼接上【网关回复超时】的Msg
msg = UserCenterLogic.CombineGatewayTimeOutMsg(msg, result);
var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
msgContr.Show();
return;
}
if (result.sceneOpenData.Result == 0)
{
//控制场景失败
string msg = Language.StringByID(R.MyInternationalizationString.ControlSceneFail);
var msgContr = new ShowMsgControl(ShowMsgType.Tip, msg);
msgContr.Show();
return;
}
//修改时间
scene.RemainTime = scene.SceneDelayTime;
scene.SceneDelayTime = 0;
//开启延时倒计时特效
this.StartRemainTimeApreal(scene, true);
}
#endregion
#region ■ 刷新控件状态_______________________
///
/// 刷新控件状态
///
/// 场景对象
public void RefreshControlInfo(Common.SceneUI i_scene)
{
//场景名称必须刷新
this.btnSceneName.Text = i_scene.Name;
//刷新图片
if (i_scene.IconPathType == 0)
{
if (this.btnScenePic.ImagePath != i_scene.IconPath)
{
this.btnScenePic.ImagePath = i_scene.IconPath;
}
}
else
{
string fullPath = System.IO.Path.Combine(Common.Config.Instance.FullPath, i_scene.IconPath);
if (this.btnScenePic.ImagePath != fullPath)
{
this.btnScenePic.ImagePath = fullPath;
}
}
//开启延时倒计时特效
this.StartRemainTimeApreal(i_scene, false);
}
#endregion
#region ■ 延时倒计时特效_____________________
///
/// 开启延时倒计时特效
///
///
/// 是否更改场景对象里面的那个时间,旨在对应同一个场景在多个地方显示的同步问题
private void StartRemainTimeApreal(Common.SceneUI i_scene, bool setTime)
{
if (i_scene.RemainTime <= 0)
{
return;
}
int remainTine = i_scene.RemainTime;
string hourText = Language.StringByID(R.MyInternationalizationString.Hour);
string minuText = Language.StringByID(R.MyInternationalizationString.Minute);
string secondText = Language.StringByID(R.MyInternationalizationString.Second);
//时间图标不显示
this.btnTimeIcon.Visible = false;
//显示剩余的时间
this.btnTimeView.Text = this.GetTimeString(i_scene.RemainTime, hourText, minuText, secondText);
new System.Threading.Thread(() =>
{
while (remainTine > 0)
{
System.Threading.Thread.Sleep(1000);
Application.RunOnMainThread(() =>
{
this.btnTimeView.Text = this.GetTimeString(remainTine, hourText, minuText, secondText);
});
if (setTime == true)
{
//覆盖剩余时间
i_scene.RemainTime = remainTine;
}
}
Application.RunOnMainThread(() =>
{
//无条件覆盖剩余时间
i_scene.RemainTime = 0;
//倒计时结束时,时间图标显示
this.btnTimeIcon.Visible = true;
this.btnTimeView.Text = string.Empty;
});
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ Loading特效________________________
///
/// 开启Loading特效
///
private void StartLoadingApreal()
{
//弄个遮罩遮住
var frameBack = new FrameLayout();
frameBack.BackgroundImagePath = "Scene/SceneActionGroud.png";
this.AddChidren(frameBack);
//再加个转圈的
var loadContr = new MyProgressLoading();
loadContr.LoadingBackgroundColor = UserCenterColor.Current.Transparent;
frameBack.AddChidren(loadContr);
loadContr.StartLoading();
loadContr.DisponeEvent += () =>
{
frameBack.RemoveFromParent();
};
}
#endregion
#region ■ 控件摧毁___________________________
///
/// 控件摧毁
///
public override void RemoveFromParent()
{
this.CardNeedRemoveEvent = null;
base.RemoveFromParent();
}
#endregion
#region ■ 一般方法___________________________
///
/// 获取时间的翻译文本
///
///
/// 小时的文本
/// 分的文本
/// 秒的文本
///
private string GetTimeString(int second, string hourText, string minuText, string secondText)
{
string timeStr = string.Empty;
int hour = second / 3600;
int minu = second % 3600 / 60;
int sec = second % 60;
if (hour >= 0)
{
timeStr += hour + hourText;
}
if (minu > 0)
{
timeStr += minu + minuText;
}
if (sec > 0)
{
timeStr += sec + secondText;
}
return timeStr;
}
#endregion
#region ■ 自定义控件_________________________
///
/// 自定义控件(需要的它移除事件)
///
private class MyProgressLoading : ProgressLoading
{
///
/// 控件销毁的事件
///
public Action DisponeEvent = null;
///
/// 控件销毁
///
public override void RemoveFromParent()
{
base.RemoveFromParent();
this.DisponeEvent?.Invoke();
this.DisponeEvent = null;
}
}
#endregion
}
}