JLChen
2021-04-12 6992b9bc927550ed3ec82eff8ec4a5aa65e6fa63
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System;
using Shared;
using HDL_ON.UI.CSS;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
 
namespace HDL_ON.UI
{
 
    /// <summary>
    /// 管理配置给音箱的设备列表
    /// </summary>
    public class SmartSpeakerSelectDevicesPage : FrameLayout
    {
        /// <summary>
        /// 
        /// </summary>
        FrameLayout bodyView;
        /// <summary>
        /// 全选按钮
        /// </summary>
        Button btnChooseAll;
        /// <summary>
        /// 当前
        /// </summary>
        VerticalScrolViewLayout bodyScrolView;
        ///// <summary>
        ///// 底部保存操作按钮
        ///// </summary>
        //Button btnSave;
 
        /// <summary>
        /// 音箱参数
        /// </summary>
        SpeakerInfo speakerInfo;
 
        /// <summary>
        /// 设备、场景列表
        /// </summary>
        List<SpeakerTargetInfo> targetInfoList = new List<SpeakerTargetInfo>();
        
        /// <summary>
        /// 当前房间的设备和场景
        /// </summary>
        List<SpeakerTargetInfo> roomFunctionOrSceneList = new List<SpeakerTargetInfo>();
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="speakerInfo"></param>
        /// <param name="roomFunctionOrSceneList"></param>
        public SmartSpeakerSelectDevicesPage(SpeakerInfo speakerInfo, List<SpeakerTargetInfo> roomFunctionOrSceneList)
        {
            bodyView = this;
            bodyView.BackgroundColor = CSS_Color.MainBackgroundColor;
            this.speakerInfo = speakerInfo;
            this.roomFunctionOrSceneList = roomFunctionOrSceneList;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public void LoadPage()
        {
            new TopViewDiv(bodyView, Language.StringByID(StringId.DataManagement)).LoadTopView();
 
            //顶部全选按钮
            var allRoomView = new FrameLayout()
            {
                Y = Application.GetRealHeight(64),
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
            };
            bodyView.AddChidren(allRoomView);
 
            Button btnAllRoomText = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(280),
                TextID = StringId.SelectedAll,
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextAlignment = TextAlignment.CenterLeft,
            };
            allRoomView.AddChidren(btnAllRoomText);
 
            btnChooseAll = new Button()
            {
                X = Application.GetRealWidth(331),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(28),
                Height = Application.GetMinRealAverage(28),
                UnSelectedImagePath = "Public/ChooseIcon.png",
                SelectedImagePath = "Public/ChooseOnIcon.png",
                //IsSelected = true,
            };
            allRoomView.AddChidren(btnChooseAll);
 
            allRoomView.AddChidren(new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                Y = Application.GetRealHeight(49),
                Height = Application.GetMinReal(1),
                Width = Application.GetRealWidth(343),
                BackgroundColor = CSS_Color.DividingLineColor,
            });
 
            bodyScrolView = new VerticalScrolViewLayout()
            {
                Y = allRoomView.Bottom,
                Height = Application.GetRealHeight(450),
            };
            bodyView.AddChidren(bodyScrolView);
 
            var btnSave = new ConfirmButton()
            {
                Y = Application.GetRealHeight(519 + 64),
                TextID = StringId.Save
            };
            bodyView.AddChidren(btnSave);
 
            //全选按钮点击事件
            LoadEvent_AllSharedDataChange(allRoomView, btnChooseAll);
            LoadEvent_Save(btnSave);
 
            GetDeviceAndSceneList();
        }
 
 
        /// <summary>
        /// 全选按钮点击事件
        /// </summary>
        void LoadEvent_AllSharedDataChange(FrameLayout allRoomView, Button btnChooseAll)
        {
            EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            {
                btnChooseAll.IsSelected = !btnChooseAll.IsSelected;
                var IsSelected = btnChooseAll.IsSelected;
 
                foreach (var info in roomFunctionOrSceneList)
                {
                    info.IsSelect = IsSelected;
                }
 
                RefreshListView();
            };
            btnChooseAll.MouseUpEventHandler = eventHandler;
            allRoomView.MouseUpEventHandler = eventHandler;
 
        }
 
        /// <summary>
        /// 保存按钮点击事件
        /// </summary>
        /// <param name="btnSave"></param>
        void LoadEvent_Save(Button btnSave)
        {
            EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            {
                AddDevicesSceneList(targetInfoList);
            };
 
            btnSave.MouseUpEventHandler = eventHandler;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="updateList"></param>
        void AddDevicesSceneList(List<SpeakerTargetInfo> updateList)
        {
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    //查找需要选中的目标
                    foreach (var info in roomFunctionOrSceneList)
                    {
                        var targetData = updateList.Find((obj) => obj.targetId == info.targetId);
                        if (info.IsSelect)
                        {
                            if (targetData == null)
                            {
                                updateList.Add(info);
                            }
 
                        }
                        else
                        {
                            if (targetData != null)
                            {
                                updateList.Remove(targetData);
                            }
                        }
 
                        
                    }
 
 
                    var updateSpeakerDeviceListObj = new UpdateSpeakerDeviceListObj()
                    {
                        homeId = speakerInfo.homeId,
                        tokenId = speakerInfo.tokenId,
                        targetInfos = updateList
                    };
 
                    var revertObj = new HttpServerRequest().UpdateSpeakerDeviceList(updateSpeakerDeviceListObj);
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        Application.RunOnMainThread(() =>
                        {
                            //保存成功、页面关闭
                            Utlis.ShowTip(Language.StringByID(StringId.SavedSuccessfully));
                            if (bodyView != null)
                            {
                                bodyView.RemoveFromParent();
                            }
                        });
 
                    }
                    else
                    {
                        //提示错误
                        IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code);
                    }
                }
                catch
                {
 
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            });
        }
       
        /// <summary>
        /// 加载房间的设备和场景列表
        /// </summary>
        void GetDeviceAndSceneList()
        {
            bodyScrolView.RemoveAll();
 
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    var revertObj = new HttpServerRequest().GetSpeakerDeviceList(0, speakerInfo.tokenId);
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SpeakerTargetInfo>>(revertObj.Data.ToString());
                        if (list != null && list.Count > 0)
                        {
                            targetInfoList = list;
                        }
                        else
                        {
                            targetInfoList = new List<SpeakerTargetInfo>();
                        }
 
                        //默认全选
                        bool isSelectAll = true;
                        //查找需要选中的目标
                        foreach (var info in roomFunctionOrSceneList)
                        {
                            var targetData = targetInfoList.Find((obj) => obj.targetId == info.targetId);
                            if(targetData == null)
                            {
                                info.IsSelect = false;
                                //有一个没选择则全选设置为false
                                isSelectAll = false;
                            }
                            else
                            {
                                info.IsSelect = true;
                            }
                        }
 
                        Application.RunOnMainThread(() => {
                            //设置当前全选
                            btnChooseAll.IsSelected = isSelectAll;
                            RefreshListView();
                        });
 
                    }
                    else
                    {
                        //提示错误
                        IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code);
                    }
                }
                catch
                {
 
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            });
        }
 
        /// <summary>
        /// RefreshListView
        /// </summary>
        void RefreshListView()
        {
            bodyScrolView.RemoveAll();
 
            if (roomFunctionOrSceneList == null) return;
 
            foreach (var roomData in roomFunctionOrSceneList)
            {
                AddRowView(roomData);
            }
 
        }
 
 
        /// <summary>
        /// AddRowView
        /// </summary>
        /// <param name="info"></param>
        void AddRowView(SpeakerTargetInfo info)
        {
 
            var roomView = new FrameLayout()
            {
                Height = Application.GetRealHeight(50),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Tag = "row"
            };
            bodyScrolView.AddChidren(roomView);
 
            Button btnRoomText = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(280),
                TextSize = CSS_FontSize.SubheadingFontSize,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextAlignment = TextAlignment.CenterLeft,
                Text = info.targetName,
            };
            roomView.AddChidren(btnRoomText);
 
            Button btnChoose = new Button()
            {
                X = Application.GetRealWidth(331),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(28),
                Height = Application.GetMinRealAverage(28),
                UnSelectedImagePath = "Public/ChooseIcon.png",
                SelectedImagePath = "Public/ChooseOnIcon.png",
                Tag = "ChooseIcon"
            };
            roomView.AddChidren(btnChoose);
 
            btnChoose.IsSelected = info.IsSelect;
 
            var btnLine = new Button()
            {
                Gravity = Gravity.CenterHorizontal,
                //Y = Application.GetRealHeight(49),
                Height = Application.GetRealHeight(1),
                Width = Application.GetRealWidth(343),
                BackgroundColor = CSS_Color.DividingLineColor,
            };
            bodyScrolView.AddChidren(btnLine);
 
 
            EventHandler<MouseEventArgs> eventHandler = (sender, e) =>
            {
                btnChoose.IsSelected = !btnChoose.IsSelected;
                info.IsSelect = btnChoose.IsSelected;
                //1.如果点击选择,判断是否需要设置全选
                if (btnChoose.IsSelected)
                {
                    //1.1 默认全选
                    bool isSelectAll = true;
                    foreach (var targetInfo in roomFunctionOrSceneList)
                    {
                        if(targetInfo.IsSelect == false)
                        {
                            //1.2 有一个还没选中就退出,无需选中全选按钮
                            isSelectAll = false;
                            break;
                        }
                    }
 
                    if (isSelectAll)
                    {
                        btnChooseAll.IsSelected = true;
                    }
                }
                else
                {
                    //取消全选
                    if (btnChooseAll.IsSelected)
                    {
                        btnChooseAll.IsSelected = false;
                    }
                }
 
            };
 
            btnChoose.MouseUpEventHandler = eventHandler;
            roomView.MouseUpEventHandler = eventHandler;
            btnRoomText.MouseUpEventHandler = eventHandler;
        }
 
     
    }
}