1
wxr
2023-03-31 7e42cc13a14b7de31c9f5d5c61cdf24f3246335d
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
using System;
using System.Collections.Generic;
using Shared.SimpleControl.Phone.Music;
 
namespace Shared.SimpleControl.Phone
{
    public class UserSchedule : FrameLayout
    {
        VerticalScrolViewLayout bodyView;
        List<Timer> timers;
 
        public UserSchedule()
        {
            BackgroundColor = SkinStyle.Current.MainColor;
            timers = new List<Timer> ();
        }
 
        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<Timer> 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){
                        Console.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.RegionID };
                    var requestJson = Newtonsoft.Json.JsonConvert.SerializeObject (requestObj);
                    var revertObj = MainPage.RequestHttps ("GetTimerList", requestJson);
                    if (revertObj.StateCode == "SUCCESS") {
                        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 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.Guid) {
                        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.Guid);
 
                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.Guid);
 
                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.Guid);
 
                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.Guid);
 
                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.Guid);
 
                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);
                    if (delRevertObj.StateCode == "SUCCESS") {
                        rowView.RemoveFromParent ();
                        timers.Remove (timers.Find((obj) => obj.Guid == delObj.Guid));
                    }
                };
 
                #region goSchedule
 
                EventHandler<MouseEventArgs> eventHandler = (sender, er) => {
                    Action<Timer> 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) {
                            var delObj = new Timer () { Guid = (sender as Button).GetTagByKey ("guid").ToString () };
                            var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj);
                            var delRevertObj = MainPage.RequestHttps ("DeleteTimer", delRequestJson);
                            if (delRevertObj.StateCode == "SUCCESS") {
                                rowView.RemoveFromParent ();
                            }
                        }
                    };
                };
                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) {
                            var delObj = new Timer () { Guid = (sender as Button).GetTagByKey ("guid").ToString () };
                            var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj);
                            var delRevertObj = MainPage.RequestHttps ("DeleteTimer", delRequestJson);
                            if (delRevertObj.StateCode == "SUCCESS") {
                                rowView.RemoveFromParent ();
                            }
                        }
                    };
                };
                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) {
                            var delObj = new Timer () { Guid = (sender as Button).GetTagByKey ("guid").ToString () };
                            var delRequestJson = Newtonsoft.Json.JsonConvert.SerializeObject (delObj);
                            var delRevertObj = MainPage.RequestHttps ("DeleteTimer", delRequestJson);
                            if (delRevertObj.StateCode == "SUCCESS") {
                                rowView.RemoveFromParent ();
                            }
                        }
                    };
                };
#endregion
 
            });
 
        }
    }
}