using System;
using System.Collections.Generic;
using Shared.SimpleControl.Phone.Music;
namespace Shared.SimpleControl.Phone
{
public class UserSchedule : FrameLayout
{
///
/// 上下滑动父控件
///
VerticalScrolViewLayout bodyView;
///
/// 定时器列表
///
List timerList;
///
/// 对象构造函数
///
/// 返回函数
public UserSchedule ()
{
BackgroundColor = SkinStyle.Current.MainColor;
timerList = new List ();
}
///
/// 对象主入口
///
public void ShowPage ()
{
#region 标题
var topView = new FrameLayout () {
Y = Application.GetRealHeight (36),
Height = Application.GetRealHeight (90),
Width = Application.GetRealWidth (640),
};
AddChidren (topView);
var title = new Button () {
TextAlignment = TextAlignment.Center,
TextID = R.MyInternationalizationString.Schedule,
TextColor = SkinStyle.Current.TextColor1,
TextSize = 19,
};
topView.AddChidren (title);
var back = new Button () {
Height = Application.GetRealWidth (100),
Width = Application.GetRealWidth (85),
UnSelectedImagePath = "Item/Back.png",
SelectedImagePath = "Item/BackSelected.png",
};
topView.AddChidren (back);
back.MouseUpEventHandler += (sender, e) => {
(Parent as PageLayout).PageIndex -= 1;
};
var ItemButton = new Button () {
Width = Application.GetRealWidth (55),
Height = Application.GetRealWidth (55),
UnSelectedImagePath = "Item/+.png",
SelectedImagePath = "Item/+.png",
X = Application.GetRealWidth (640 - 80),
Y = Application.GetRealHeight (17),
};
topView.AddChidren (ItemButton);
ItemButton.MouseUpEventHandler += (sender, e) => {
if (UserConfig.Instance.internetStatus == 0) {
new Alert ("", "Unable to save data, please check the network.", "Close").Show ();
return;
}
MainPage.Loading.Start ("Please wait...");
System.Threading.Tasks.Task.Run (() => {
try {
System.Threading.Thread.Sleep (100);
Application.RunOnMainThread (() => {
var timer = new Timer () { TimeZone = Convert.ToInt32 (DateTime.Now.ToString ("%z")),timerType = TimerType.Week, timerName = "Schedule " + timerList.Count.ToString(), executeUtcTime = DateTime.Now.ToString ("HH:mm") };
Action refLoad = (obj) => {
timerList.Add (obj);
InitSchedule (obj);
};
var scheduleShowView = new AddSchedule (refLoad);
UserMiddle.SettingPageView.AddChidren (scheduleShowView);
scheduleShowView.ShowPage (timer,timerList,false);
UserMiddle.SettingPageView.PageIndex = 2;
});
}catch(Exception ex){
Utlis.WriteLine (ex.Message);
}
finally{
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
});
}
});
};
#endregion
bodyView = new VerticalScrolViewLayout () {
Y= topView.Bottom,
Height = Application.GetRealHeight(1136 - 126),
BackgroundColor = SkinStyle.Current.ViewColor
};
AddChidren (bodyView);
///加载计数器列表界面
InitScheduleView ();
}
///
/// 显示的定时器列表界面
///
///
void InitSchedule (Timer schedule)
{
Application.RunOnMainThread (() => {
var rowView = new RowLayout () {
Height = Application.GetRealHeight (180),
};
bodyView.AddChidren (rowView);
rowView.AddTag ("guid", schedule.id);
var btnScheduleName = new Button () {
X = Application.GetRealWidth (30),
Height = Application.GetRealHeight (60),
Width = Application.GetRealWidth (290),
TextAlignment = TextAlignment.CenterLeft,
TextColor = SkinStyle.Current.TextColor1,
Text = schedule.timerName,
TextSize = 14,
Tag = "btnScheduleName",
};
rowView.AddChidren (btnScheduleName);
btnScheduleName.AddTag ("guid", schedule.id);
var btnScheduleRoomName = new Button () {
X = btnScheduleName.Right,
Height = Application.GetRealHeight (60),
Width = Application.GetRealWidth (290),
TextAlignment = TextAlignment.CenterRight,
TextColor = SkinStyle.Current.TextColor1,
Text = schedule.RoomName,
Tag = "btnScheduleRoomName",
};
rowView.AddChidren (btnScheduleRoomName);
btnScheduleRoomName.AddTag ("guid", schedule.id);
var btnScheduleTime = new Button () {
X = Application.GetRealWidth (30),
Y = Application.GetRealHeight (60),
Height = Application.GetRealHeight (60),
Width = Application.GetRealWidth (150),
Text = schedule.executeUtcTime,
TextColor = SkinStyle.Current.TextColor1,
TextAlignment = TextAlignment.CenterLeft,
TextSize = 24,
Tag = "btnScheduleTime",
};
rowView.AddChidren (btnScheduleTime);
btnScheduleTime.AddTag ("guid", schedule.id);
var btnScheduleDay = new Button () {
X = Application.GetRealWidth (33),
Y = btnScheduleTime.Bottom,
Height = Application.GetRealHeight (60),
TextColor = SkinStyle.Current.TextColor1,
Width = Application.GetRealWidth (400),
TextAlignment = TextAlignment.CenterLeft,
TextSize = 14,
Tag = "btnScheduleDay",
};
rowView.AddChidren (btnScheduleDay);
btnScheduleDay.AddTag ("guid", schedule.id);
if (schedule.whichDay.Contains (0)) {
btnScheduleDay.Text += "Sun ";
}
if (schedule.whichDay.Contains (1)) {
btnScheduleDay.Text += "Mon ";
}
if (schedule.whichDay.Contains (2)) {
btnScheduleDay.Text += "Tues ";
}
if (schedule.whichDay.Contains (3)) {
btnScheduleDay.Text += "Wed ";
}
if (schedule.whichDay.Contains (4)) {
btnScheduleDay.Text += "Thur ";
}
if (schedule.whichDay.Contains (5)) {
btnScheduleDay.Text += "Fri ";
}
if (schedule.whichDay.Contains (6)) {
btnScheduleDay.Text += "Sat ";
}
var tempSiwtch = new Button () {
Width = Application.GetRealWidth (90),
Height = Application.GetRealWidth (53),
X = Application.GetRealWidth (640 - 90 - 20),
Y = Application.GetRealHeight (100),
UnSelectedImagePath = "Item/SwitchClose.png",
SelectedImagePath = "Item/SwitchOpen.png",
IsSelected = schedule.isEnable,
Tag = schedule.id
};
rowView.AddChidren (tempSiwtch);
tempSiwtch.MouseUpEventHandler += (sdd, ffd) => {
MainPage.Loading.Start ("Loading");
System.Threading.Tasks.Task.Run (() => {
try {
var switchRevertObj =SmartHome.Send.SwitchTimer (schedule);
if (switchRevertObj.Code== StateCode.SUCCESS) {
Application.RunOnMainThread (() => {
tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
});
}
} catch (Exception ex) {
Utlis.WriteLine (ex.Message);
} finally {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
});
}
});
};
Button btnDel = new Button () {
TextID = R.MyInternationalizationString.Del,
BackgroundColor = SkinStyle.Current.DelColor,
Tag = schedule.id
};
rowView.AddRightView (btnDel);
btnDel.MouseUpEventHandler += (sender, er) => {
if ((sender as Button).Tag == null) return;
DeleteTimerAction ((sender as Button).Tag.ToString (), rowView, true);
};
#region goSchedule
EventHandler eventHandler = (sender, er) => {
Action refLoad = (obj) => {
ReinitSchedule (obj);
};
var scheduleShowView = new AddSchedule (refLoad);
UserMiddle.SettingPageView.AddChidren (scheduleShowView);
scheduleShowView.ShowPage (schedule, timerList, true);
UserMiddle.SettingPageView.PageIndex = 2;
};
rowView.MouseUpEventHandler += eventHandler;
btnScheduleDay.MouseUpEventHandler += eventHandler;
btnScheduleTime.MouseUpEventHandler += eventHandler;
btnScheduleName.MouseUpEventHandler += eventHandler;
#endregion
#region delSchedule
EventHandler Del = (sender, er) => {
var alert = new Alert ("", Language.StringByID (R.MyInternationalizationString.SureDelete),
Language.StringByID (R.MyInternationalizationString.Cancel),
Language.StringByID (R.MyInternationalizationString.Confrim));
alert.Show ();
alert.ResultEventHandler += (sender2, e2) => {
if (e2) {
if ((sender as Button).GetTagByKey ("guid") == null) return;
DeleteTimerAction ((sender as Button).GetTagByKey ("guid").ToString (), rowView);
}
};
};
btnScheduleName.MouseLongEventHandler += Del;
btnScheduleTime.MouseLongEventHandler += Del;
btnScheduleDay.MouseLongEventHandler += Del;
#endregion
});
}
///
/// 更新的定时器状态
///
///
void ReinitSchedule (Timer schedule)
{
for (int i = 0; i < bodyView.ChildrenCount; i++) {
var view = bodyView.GetChildren (i);
if (view.GetType () == typeof (RowLayout)) {
var rowView = view as RowLayout;
if (rowView.GetTagByKey ("guid").ToString () == schedule.id.ToString ()) {
for (int j = 0; j < rowView.ChildrenCount; j++) {
var viewChilren = rowView.GetChildren (j);
if (viewChilren.GetType () == typeof (Button)) {
var btn = viewChilren as Button;
if (btn.Tag != null) {
switch (btn.Tag.ToString ()) {
case "btnScheduleName":
btn.Text = schedule.timerName;
break;
case "btnScheduleRoomName":
btn.Text = schedule.RoomName;
break;
case "btnScheduleTime":
btn.Text = schedule.executeUtcTime;
break;
case "btnScheduleDay":
btn.Text = "";
if (schedule.whichDay.Contains (0)) {
btn.Text += "Sun ";
}
if (schedule.whichDay.Contains (1)) {
btn.Text += "Mon ";
}
if (schedule.whichDay.Contains (2)) {
btn.Text += "Tues ";
}
if (schedule.whichDay.Contains (3)) {
btn.Text += "Wed ";
}
if (schedule.whichDay.Contains (4)) {
btn.Text += "Thur ";
}
if (schedule.whichDay.Contains (5)) {
btn.Text += "Fri ";
}
if (schedule.whichDay.Contains (6)) {
btn.Text += "Sat ";
}
break;
}
}
}
}
}
}
}
}
///
/// 读取定时器列表
///
void InitScheduleView ()
{
SmartHome.Method.GetDeviceList ();
MainPage.Loading.Start ("Please wait...");
System.Threading.Tasks.Task.Run (() => {
try {
var revertObj = SmartHome.Send.GetTimerList ();
if (revertObj.Code == StateCode.SUCCESS) {
if (revertObj.Data == null) {
return;
}
if (string.IsNullOrEmpty (revertObj.Data.ToString ())) {
return;
}
var jsonDate = Newtonsoft.Json.Linq.JObject.Parse (revertObj.Data.ToString ());
var array = jsonDate ["list"].ToString ();
var jArray = Newtonsoft.Json.Linq.JArray.Parse (array);
for (int a = 0; a < jArray.Count; a++) {
Timer timer = new Timer ();
var jay = jArray [a];
timer.id = jay ["id"].ToString ();
timer.timerName =jay ["timerName"].ToString ();
timer.RoomName = jay ["room"].ToString ();
timer.userId = jay ["userId"].ToString ();
timer.isEnable = Boolean.Parse (jay ["isEnable"].ToString ());
timer.executeUtcTime = SmartHome.Method.GetLocalUtcTime(jay ["executeUtcTime"].ToString ());
timer.timerType = (TimerType)System.Enum.Parse (typeof (TimerType), jay ["timerType"].ToString ());
var whichDayJson = jay ["whichDay"].ToString ();
var whichDayAry = Newtonsoft.Json.Linq.JArray.Parse (whichDayJson);
for (int b = 0; b < whichDayAry.Count; b++) {
var days = whichDayAry [b].ToString ();
timer.whichDay.Add (int.Parse (days));
}
var controlDataStr = jay ["controlData"].ToString ();
var controlDataJosn = Newtonsoft.Json.JsonConvert.DeserializeObject (controlDataStr);
if (controlDataJosn != null) {
timer.controlData = controlDataJosn;
}
var timerJosn = timerList.Find ((c) => c.id == timer.id);
if (timerJosn == null && !string.IsNullOrEmpty (timer.id)) {
timerList.Add (timer);
}
}
} else {
//提示错误
IMessageCommon.Current.ShowErrorInfoAlter (revertObj.Code);
//SmartHome.Method method = new SmartHome.Method ();
//method.ShowGetTimerListErrorInfo (revertObj.Data.ToString ());
}
} catch (Exception ex) {
Utlis.WriteLine (ex.Message);
} finally {
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
for (int i = 0; i < timerList.Count; i++) {
var schedule = timerList [i];
InitSchedule (schedule);
}
});
}
});
}
///
/// 删除定时器
///
///
///
///
void DeleteTimerAction (string GUID, View rowView, bool bTimersRemove = false)
{
//2020-01-11
Application.RunOnMainThread (() => {
MainPage.Loading.Start ("Please wait...");
});
var delRevertObj =SmartHome.Send.DelTimer (GUID);
if (delRevertObj.Code == StateCode.SUCCESS) {
rowView.RemoveFromParent ();
if (bTimersRemove) {
timerList.Remove (timerList.Find ((obj) => obj.id.ToString() == GUID));
}
} else {
//删除失败
Application.RunOnMainThread (() => {
new Alert ("", ErrorCode.FailedDelete, Language.StringByID (R.MyInternationalizationString.Close)).Show ();
});
}
Application.RunOnMainThread (() => {
MainPage.Loading.Hide ();
});
}
}
}