JLChen
2020-06-04 6d55af8792cf8fbef0055e677b900fc352dba9a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Collections.Generic;
using Shared.SimpleControl.Phone.Music;
 
namespace Shared.SimpleControl.Phone
{
    public class UserSchedule : FrameLayout
    {
        VerticalScrolViewLayout bodyView;
 
        public UserSchedule()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
        }
 
        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) => {
                MainPage.Loading.Start (Language.StringByID(R.MyInternationalizationString.load));
                System.Threading.Tasks.Task.Run (() => {
                    try {
                        System.Threading.Thread.Sleep (100);
                        Application.RunOnMainThread (() => {
                            var tt = new Timer () { TimeZone = Convert.ToInt32 (DateTime.Now.ToString ("%z")), RegionID = UserConfig.Instance.CurrentRegion.RegionID, Periodicity = ((int)DateTime.Now.DayOfWeek).ToString (), TimerType = TimerType.Week, TimerName = "Schedule " + DateTime.Now.ToString ("yy-MM-dd"), ExecutionTime = DateTime.Now.ToString ("HH:mm") };
                            Action<Timer> refLoad = (obj) => {
                                initSchedule (obj);
                            };
                            var scheduleShowView = new AddSchedule (refLoad);
                            UserMiddle.SettingPageView.AddChidren (scheduleShowView);
                            scheduleShowView.ShowPage (tt);
                            UserMiddle.SettingPageView.PageIndex = 2;
                        });
                    }catch(Exception ex){
                        Console.WriteLine (ex.Message);
                    }
                    finally{
                        Application.RunOnMainThread (() => {
                            MainPage.Loading.Hide ();
                        });
                    }
                });
            };
            #endregion
 
            bodyView = new VerticalScrolViewLayout () { 
                Y= topView.Bottom,
                Height = Application.GetRealHeight (Application.DesignHeight - 126),
                BackgroundColor = SkinStyle.Current.ViewColor
            };
            AddChidren (bodyView);
            InitScheduleView ();
        }
 
        void InitScheduleView ()
        {
            MainPage.Loading.Start (Language.StringByID(R.MyInternationalizationString.load));
            System.Threading.Tasks.Task.Run (() => {
                try {
                    var requestObj = new Timer () { RegionID = UserConfig.Instance.CurrentRegion.RegionID };
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                    var revertObj = MainPage.RequestHttps ("GetTimerList", requestJson);
                    if (revertObj.StateCode == "SUCCESS") {
                        var timers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Timer>> (revertObj.ResponseData.ToString ());
                        foreach (var schedule in timers) {
                            initSchedule (schedule);
                        }
                    }
                } catch (Exception ex) {
                    Console.WriteLine (ex.Message);
                } finally {
                    Application.RunOnMainThread (() => {
                        MainPage.Loading.Hide ();
                    });
                }
            });
        }
    
    
        void initSchedule(Timer schedule)
        {
 
            Application.RunOnMainThread (() => {
                var rowView = new RowLayout () {
                    Height = Application.GetRealHeight (180),
                };
                bodyView.AddChidren (rowView);
 
                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,
                };
                rowView.AddChidren (btnScheduleName);
 
                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,
                };
                rowView.AddChidren (btnScheduleRoomName);
 
                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,
                };
                rowView.AddChidren (btnScheduleTime);
                //if (chooseTime.Hour > 12) {
                //    chooseTimeString = (chooseTime.Hour -12) + ":" + chooseTime.Minute + " PM";
                //} else {
                //    chooseTimeString = chooseTime.Hour + ":" + chooseTime.Minute + " AM";
                //}
 
                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,
                };
                rowView.AddChidren (btnScheduleDay);
 
                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.Guid
                };
                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 () { Guid = (sdd as Button).Tag.ToString() };
                            var switchRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (switchObj);
                            var switchRevertObj = MainPage.RequestHttps ("IsEnableTimer", switchRequestJson);
                            if (switchRevertObj.StateCode != "SUCCESS") {
                                Application.RunOnMainThread (() => {
                                    tempSiwtch.IsSelected = !tempSiwtch.IsSelected;
                                });
                            }
                        } catch (Exception ex) {
                            Console.WriteLine (ex.Message);
                        } finally {
                            Application.RunOnMainThread (() => {
                                MainPage.Loading.Hide ();
                            });
                        }
                    });
                };
 
                Button btnDel = new Button () {
                    TextID = R.MyInternationalizationString.Del,
                    BackgroundColor = SkinStyle.Current.DelColor,
                    Tag = schedule.Guid
                };
                rowView.AddRightView (btnDel);
                btnDel.MouseUpEventHandler += (sender, er) => {
                    var delObj = new Timer () { Guid = (sender as Button).Tag.ToString () };
                    var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj);
                    var delRevertObj = MainPage.RequestHttps ("DeleteTimer", delRequestJson);
                    //var timers = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Timer>> (revertObj.ResponseData.ToString ());
                    if (delRevertObj.StateCode == "SUCCESS") {
                        rowView.RemoveFromParent ();
                    }
                };
            });
 
        }
    }
}