using System; using System.Collections.Generic; using Shared.SimpleControl.Phone.Music; namespace Shared.SimpleControl.Phone { public class UserSchedule : FrameLayout { VerticalScrolViewLayout bodyView; List timers; public UserSchedule() { BackgroundColor = SkinStyle.Current.MainColor; timers = 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.GetRealHeight (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.GetRealHeight (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 tt = new Timer () { TimeZone = Convert.ToInt32 (DateTime.Now.ToString ("%z")), RoomName = "ALL", Periodicity = ((int)DateTime.Now.DayOfWeek).ToString (), TimerType = TimerType.Week, TimerName = "Schedule " + timers.Count.ToString(), ExecutionTime = DateTime.Now.ToString ("HH:mm") }; Action refLoad = (obj) => { timers.Add (obj); initSchedule (obj); }; var scheduleShowView = new AddSchedule (refLoad); UserMiddle.SettingPageView.AddChidren (scheduleShowView); scheduleShowView.ShowPage (tt,timers,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 InitScheduleView () { MainPage.Loading.Start ("Please wait..."); System.Threading.Tasks.Task.Run (() => { try { var requestObj = new Timer () { RegionID = UserConfig.Instance.CurrentRegion.Id }; var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj); var revertObj = MainPage.RequestHttps (API.GetTimerList, requestJson); if (revertObj.StateCode.ToUpper() == StateCode.SUCCESS) { timers = Newtonsoft.Json.JsonConvert.DeserializeObject> (revertObj.ResponseData.ToString ()); foreach (var schedule in timers) { initSchedule (schedule); } } else { //提示错误 ShowGetTimerListErrorInfo (revertObj.StateCode); } } catch (Exception ex) { Utlis.WriteLine (ex.Message); } finally { Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } }); } /// //(1)Success 则调用此接口成功 //(2)NoRecord 住宅未绑定网关! //(3)NoExist 住宅不存在! /// void ShowGetTimerListErrorInfo (string stateCodeStr) { string mes = ""; if (stateCodeStr == "NoRecord") { // 住宅未绑定网关! mes = ErrorCode.HomeNoRecord; } else if (stateCodeStr == "NoExist") { //住宅不存在! mes = ErrorCode.HomeNoExist; } else if (stateCodeStr == ErrorCode.NetworkError) { mes = ErrorCode.NetworkError; } else { mes = ErrorCode.OperationFailed + ErrorCode.Reason + stateCodeStr; } if (!string.IsNullOrEmpty (mes)) { Application.RunOnMainThread (() => { new Alert ("", mes, Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } } 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) { 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.ExecutionTime; break; case "btnScheduleDay": btn.Text = ""; if (schedule.Periodicity.Contains ("0")) { btn.Text += "Sun "; } if (schedule.Periodicity.Contains ("1")) { btn.Text += "Mon "; } if (schedule.Periodicity.Contains ("2")) { btn.Text += "Tues "; } if (schedule.Periodicity.Contains ("3")) { btn.Text += "Wed "; } if (schedule.Periodicity.Contains ("4")) { btn.Text += "Thur "; } if (schedule.Periodicity.Contains ("5")) { btn.Text += "Fri "; } if (schedule.Periodicity.Contains ("6")) { btn.Text += "Sat "; } break; } } } } } } } } 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.ExecutionTime, 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.Periodicity.Contains ("0")) { btnScheduleDay.Text += "Sun "; } if (schedule.Periodicity.Contains ("1")) { btnScheduleDay.Text += "Mon "; } if (schedule.Periodicity.Contains ("2")) { btnScheduleDay.Text += "Tues "; } if (schedule.Periodicity.Contains ("3")) { btnScheduleDay.Text += "Wed "; } if (schedule.Periodicity.Contains ("4")) { btnScheduleDay.Text += "Thur "; } if (schedule.Periodicity.Contains ("5")) { btnScheduleDay.Text += "Fri "; } if (schedule.Periodicity.Contains ("6")) { btnScheduleDay.Text += "Sat "; } var tempSiwtch = new Button () { Width = Application.GetMinRealAverage (90), Height = Application.GetMinRealAverage (53), X = Application.GetRealWidth (640 - 90 - 20), Y = Application.GetRealHeight (100), UnSelectedImagePath = "Item/SwitchClose.png", SelectedImagePath = "Item/SwitchOpen.png", IsSelected = schedule.IsStart, Tag = schedule.Id }; rowView.AddChidren (tempSiwtch); tempSiwtch.MouseUpEventHandler += (sdd, ffd) => { tempSiwtch.IsSelected = !tempSiwtch.IsSelected; MainPage.Loading.Start ("Loading"); System.Threading.Tasks.Task.Run (() => { try { var switchObj = new Timer () { Id = (sdd as Button).Tag.ToString () }; var switchRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (switchObj); var switchRevertObj = MainPage.RequestHttps (API.IsEnableTimer, switchRequestJson); if (switchRevertObj.StateCode.ToUpper() != 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; //string uuid = (sender as Button).Tag.ToString (); DeleteTimerAction ((sender as Button).Tag.ToString (), rowView, true); //var delObj = new Timer () { Guid = (sender as Button).Tag.ToString () }; //var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj); //var delRevertObj = MainPage.RequestHttps ("DeleteTimer", delRequestJson); //if (delRevertObj.StateCode.ToUpper() == StateCode.SUCCESS) { // rowView.RemoveFromParent (); // timers.Remove (timers.Find((obj) => obj.Guid == delObj.Guid)); //} }; #region goSchedule EventHandler eventHandler = (sender, er) => { Action refLoad = (obj) => { reinitSchedule (obj); }; var scheduleShowView = new AddSchedule (refLoad); UserMiddle.SettingPageView.AddChidren (scheduleShowView); scheduleShowView.ShowPage (schedule, timers, true); UserMiddle.SettingPageView.PageIndex = 2; }; rowView.MouseUpEventHandler += eventHandler; btnScheduleDay.MouseUpEventHandler += eventHandler; btnScheduleTime.MouseUpEventHandler += eventHandler; btnScheduleName.MouseUpEventHandler += eventHandler; #endregion #region delSchedule btnScheduleName.MouseLongEventHandler += (sender, e) => { 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); } }; }; btnScheduleTime.MouseLongEventHandler += (sender, e) => { 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); } }; }; btnScheduleDay.MouseLongEventHandler += (sender, e) => { 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); } }; }; #endregion }); } /// /// 删除定时器 /// /// /// /// void DeleteTimerAction (string GUID, View rowView, bool bTimersRemove = false) { //2020-01-11 Application.RunOnMainThread (() => { MainPage.Loading.Start ("Please wait..."); }); var delObj = new Timer () { Id = GUID }; var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj); var delRevertObj = MainPage.RequestHttps (API.DeleteTimer, delRequestJson); if (delRevertObj.StateCode.ToUpper () == StateCode.SUCCESS) { rowView.RemoveFromParent (); if (bTimersRemove) { timers.Remove (timers.Find ((obj) => obj.Id == delObj.Id)); } } else { //删除失败 Application.RunOnMainThread (() => { new Alert ("", ErrorCode.FailedDelete, Language.StringByID (R.MyInternationalizationString.Close)).Show (); }); } Application.RunOnMainThread (() => { MainPage.Loading.Hide (); }); } } }