黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
438
439
440
441
442
443
444
445
446
447
448
449
450
using System;
using System.Collections.Generic;
using System.Text;
using ZigBee.Device;
 
namespace Shared.Phone.UserCenter.SharedContent
{
    /// <summary>
    /// 配置新共享内容的主界面
    /// </summary>
    public class AddNewSharedContentForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 查看的房间对象
        /// </summary>
        private Common.Room lookRoom = null;
        /// <summary>
        /// 成员ID
        /// </summary>
        private string ChildAccountId = string.Empty;
        /// <summary>
        /// 设备桌布控件
        /// </summary>
        private NormalFrameLayout frameDeviceTable = null;
        /// <summary>
        /// 设备桌布控件
        /// </summary>
        private NormalFrameLayout frameSceneTable = null;
        /// <summary>
        /// 功能按钮
        /// </summary>
        private BottomClickButton btnShard = null;
        /// <summary>
        /// 选择的场景
        /// </summary>
        private Dictionary<int, Common.SceneUI> dicSelectScene = new Dictionary<int, Common.SceneUI>();
        /// <summary>
        /// 选择的设备
        /// </summary>
        private Dictionary<string, CommonDevice> dicSelectDevice = new Dictionary<string, CommonDevice>();
        /// <summary>
        /// 当前选择的分支 0:场景  1:功能
        /// </summary>
        private int nowSwitchIndex = 0;
        /// <summary>
        /// 当前选择的设备索引
        /// </summary>
        private string nowDeviceIndex = string.Empty;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        /// <param name="i_room">查看的房间对象</param>
        /// <param name="i_ChildAccountId">成员ID</param>
        public void ShowForm(Common.Room i_room, string i_ChildAccountId)
        {
            this.lookRoom = i_room;
            this.ChildAccountId = i_ChildAccountId;
 
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddShared));
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
            this.dicSelectDevice = new Dictionary<string, CommonDevice>();
            this.dicSelectScene = new Dictionary<int, Common.SceneUI>();
 
            //初始化桌布控件
            this.frameSceneTable = new NormalFrameLayout();
            frameSceneTable.Y = Application.GetRealHeight(132);
            frameSceneTable.Height = bodyFrameLayout.Height - Application.GetRealHeight(132);
            bodyFrameLayout.AddChidren(frameSceneTable);
 
            this.frameDeviceTable = new NormalFrameLayout();
            frameDeviceTable.Y = frameSceneTable.Y;
            frameDeviceTable.Height = frameSceneTable.Height;
            bodyFrameLayout.AddChidren(frameDeviceTable);
            frameDeviceTable.Visible = false;
 
            //初始化分支控件
            this.InitSwitchControl();
 
            //房间
            var btnRoom = new NormalViewControl(400, 63, true);
            btnRoom.Y = Application.GetRealHeight(40);
            btnRoom.X = bodyFrameLayout.Width - Application.GetRealWidth(58 + 400);
            btnRoom.Text = this.lookRoom.Name;
            btnRoom.TextAlignment = TextAlignment.CenterRight;
            bodyFrameLayout.AddChidren(btnRoom);
 
            //分享按钮
            this.btnShard = new BottomClickButton();
            btnShard.TextID = R.MyInternationalizationString.uShared2;
            bodyFrameLayout.AddChidren(btnShard);
            btnShard.Visible = false;
            btnShard.ButtonClickEvent += (sender, e) =>
            {
                //保存分享
                this.SaveShardData();
            };
 
            //初始化场景列表
            this.InitSceneList();
            //初始功能列表
            this.InitFunctionList();
        }
 
        /// <summary>
        /// 初始化分支控件
        /// </summary>
        private void InitSwitchControl()
        {
            var tabControl = new SceneFunctionSwitchControl();
            tabControl.Y = Application.GetRealHeight(40);
            bodyFrameLayout.AddChidren(tabControl);
            //设置初始化值
            tabControl.SetDefultIndex(nowSwitchIndex);
            tabControl.SelectTabEvent += (tabIndex) =>
            {
                this.nowSwitchIndex = tabIndex;
                if (tabIndex == 0)
                {
                    //显示场景列表
                    this.frameSceneTable.Visible = true;
                    this.frameDeviceTable.Visible = false;
                }
                else
                {
                    //显示功能列表
                    this.frameSceneTable.Visible = false;
                    this.frameDeviceTable.Visible = true;
                }
            };
            //开始初始化场景功能切换控件
            var listTitle = new List<string>();
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uScence));
            listTitle.Add(Language.StringByID(R.MyInternationalizationString.uFunction));
            tabControl.InitControl(listTitle);
        }
 
        #endregion
 
        #region ■ 场景显示___________________________
 
        /// <summary>
        /// 初始化场景列表
        /// </summary>
        private void InitSceneList()
        {
            //清空桌布
            this.frameSceneTable.RemoveAll();
 
            //场景有点特殊,如果同一个房间内,已经分享了,则不再显示 
            //如果这个场景是由嵌套而分享的,这个时候,它可以显示出来
            var listScene = new List<int>();
            var shardRoom = HdlShardLogic.Current.GetShardRoomFromMemory(lookRoom.Id);
            if (shardRoom == null)
            {
                listScene.AddRange(lookRoom.ListSceneId);
            }
            else
            {
                foreach (var dataId in lookRoom.ListSceneId)
                {
                    if (shardRoom.ListSceneId.Contains(dataId) == false)
                    {
                        listScene.Add(dataId);
                    }
                }
            }
            if (listScene.Count == 0)
            {
                //无可共享的场景
                this.ShowNotDataImage(frameSceneTable, Language.StringByID(R.MyInternationalizationString.uNotCanShardSceneMsg), "Item/NotShardPic.png", 383, 279);
                return;
            }
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                var listView = new VerticalFrameControl(3);
                listView.Y = Application.GetRealHeight(53);
                listView.Height = bodyFrameLayout.Height - Application.GetRealHeight(187);
                frameSceneTable.AddChidren(listView);
 
                foreach (var dataId in listScene)
                {
                    var data = HdlSceneLogic.Current.GetSceneUIBySceneId(dataId);
                    if (data == null)
                    {
                        continue;
                    }
                    //场景图片
                    var frameContr = new ScenePictrueControl();
                    listView.AddChidren(frameContr);
                    frameContr.InitControl(data);
 
                    var btnSelect = new IconViewControl(58);
                    btnSelect.UnSelectedImagePath = "Item/ItemUnSelected.png";
                    btnSelect.SelectedImagePath = "Item/ItemSelected.png";
                    btnSelect.X = Application.GetRealWidth(887);
                    btnSelect.Y = Application.GetRealHeight(35);
                    frameContr.AddChidren(btnSelect, ChidrenBindMode.BindEvent);
                    if (dicSelectScene.ContainsKey(data.Id) == true)
                    {
                        btnSelect.IsSelected = true;
                    }
                    frameContr.ButtonClickEvent += (sender, e) =>
                    {
                        //选择
                        btnSelect.IsSelected = !btnSelect.IsSelected;
                        if (btnSelect.IsSelected == true)
                        {
                            dicSelectScene[data.Id] = data;
                            if (this.btnShard.Visible == false)
                            {
                                this.btnShard.Visible = true;
                            }
                        }
                        else
                        {
                            dicSelectScene.Remove(data.Id);
                            if (dicSelectScene.Count == 0 && dicSelectDevice.Count == 0)
                            {
                                this.btnShard.Visible = false;
                            }
                        }
                    };
                }
                //调整一下高度
                listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(185));
            });
        }
 
        #endregion
 
        #region ■ 功能显示___________________________
 
        /// <summary>
        /// 初始化功能列表
        /// </summary>
        private void InitFunctionList()
        {
            //清空桌布
            this.frameDeviceTable.RemoveAll();
 
            HdlThreadLogic.Current.RunMainInThread(() =>
            {
                //获取分组后的设备列表
                var dicGroupDevice = this.GetAllGroupDevice();
                if (dicGroupDevice.Count == 0)
                {
                    //无可共享的设备
                    this.ShowNotDataImage(frameDeviceTable, Language.StringByID(R.MyInternationalizationString.uNotCanShardDeviceMsg), "Item/NotShardPic.png", 383, 279);
                }
                else
                {
                    var frameBack = new FrameLayout();
                    frameBack.X = HdlControlResourse.XXLeft;
                    frameBack.Y = Application.GetRealHeight(178);
                    frameBack.BackgroundColor = UserCenterColor.Current.White;
                    frameBack.Width = bodyFrameLayout.Width;
                    frameBack.Height = Application.GetRealHeight(1650);
                    frameBack.Radius = (uint)Application.GetRealHeight(58);
                    frameDeviceTable.AddChidren(frameBack);
 
                    var listView = new VerticalListControl(23);
                    listView.Y = Application.GetRealHeight(23);
                    listView.Height = Application.GetRealHeight(1437 - 23);
                    frameBack.AddChidren(listView);
                    //初始化设备类型行
                    this.InitDeviceObjectRow(dicGroupDevice, listView);
                }
            });
        }
 
        #endregion
 
        #region ■ 初始化设备类型行___________________
 
        /// <summary>
        /// 初始化设备类型行
        /// </summary>
        /// <param name="dicData"></param>
        /// <param name="listView"></param>
        private void InitDeviceObjectRow(Dictionary<string, List<CommonDevice>> dicData, VerticalListControl listView)
        {
            var scrolContr = new RoomDeviceGroupMenuControl(dicData);
            this.frameDeviceTable.AddChidren(scrolContr);
            //设置初始值
            scrolContr.SetDefultIndex(nowDeviceIndex);
            scrolContr.SelectDeviceEvent += (listdevice) =>
            {
                this.nowDeviceIndex = scrolContr.nowSelectKeys;
                //添加设备行
                this.AddDeviceRow(listdevice, listView);
            };
            scrolContr.InitControl();
        }
 
        #endregion
 
        #region ■ 添加设备行_________________________
 
        /// <summary>
        /// 添加设备行
        /// </summary>
        /// <param name="listDevice"></param>
        /// <param name="listView"></param>
        private void AddDeviceRow(List<CommonDevice> listDevice, VerticalListControl listView)
        {
            listView.RemoveAll();
 
            foreach (var device in listDevice)
            {
                var frameRow = new FrameRowControl(listView.rowSpace / 2);
                frameRow.LeftOffset = Application.GetRealWidth(46) - HdlControlResourse.XXLeft;
                frameRow.RightOffset = -HdlControlResourse.XXLeft;
                listView.AddChidren(frameRow);
                //图标
                var btnIcon = frameRow.AddLeftIcon(81);
                HdlDeviceCommonLogic.Current.SetDeviceIconToControl(btnIcon, device);
                //名称
                var btnView = frameRow.AddLeftCaption(string.Empty, 600);
                btnView.Text = HdlDeviceCommonLogic.Current.GetDeviceEpointName(device);
                btnView.TextSize = 15;
                //底线
                frameRow.AddBottomLine();
                //选择
                var btnSelect = frameRow.AddMostRightEmptyIcon(69, 69);
                btnSelect.UnSelectedImagePath = "Item/ItemUnSelected.png";
                btnSelect.SelectedImagePath = "Item/ItemSelected.png";
 
                string mainKeys = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                if (dicSelectDevice.ContainsKey(mainKeys) == true)
                {
                    btnSelect.IsSelected = true;
                }
                frameRow.ButtonClickEvent += (sender, e) =>
                {
                    //选择
                    btnSelect.IsSelected = !btnSelect.IsSelected;
                    if (btnSelect.IsSelected == true)
                    {
                        dicSelectDevice[mainKeys] = device;
                        if (this.btnShard.Visible == false)
                        {
                            this.btnShard.Visible = true;
                        }
                    }
                    else
                    {
                        dicSelectDevice.Remove(mainKeys);
                        if (dicSelectScene.Count == 0 && dicSelectDevice.Count == 0)
                        {
                            this.btnShard.Visible = false;
                        }
                    }
                };
            }
 
            //列表自己有个23的Y轴坐标
            listView.AdjustRealHeightByBottomButton(Application.GetRealHeight(23), Application.GetRealHeight(300 - 23));
        }
 
        #endregion
 
        #region ■ 整合设备___________________________
 
        /// <summary>
        /// 获取分组后的设备
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, List<CommonDevice>> GetAllGroupDevice()
        {
            //全部的设备
            var listDevice = HdlRoomLogic.Current.GetRoomListDevice(lookRoom);
            var dic = new Dictionary<string, List<CommonDevice>>();
            var shardRoom = HdlShardLogic.Current.GetShardRoomFromMemory(lookRoom.Id);
            foreach (var device in listDevice)
            {
                string mainkey = HdlDeviceCommonLogic.Current.GetDeviceMainKeys(device);
                if (shardRoom.ListDevice.Contains(mainkey) == true)
                {
                    //如果已经分享了,则不显示
                    continue;
                }
                var typeInfo = HdlDeviceCommonLogic.Current.GetDeviceBelongEnumInfo(device);
                if (dic.ContainsKey(typeInfo.BeloneText) == false)
                {
                    dic[typeInfo.BeloneText] = new List<CommonDevice>();
                }
                dic[typeInfo.BeloneText].Add(device);
            }
            return dic;
        }
 
        #endregion
 
        #region ■ 保存分享___________________________
 
        /// <summary>
        /// 保存分享
        /// </summary>
        private void SaveShardData()
        {
            //选择的设备
            var listDevice = new List<CommonDevice>();
            foreach (var device in dicSelectDevice.Values)
            {
                listDevice.Add(device);
            }
            //选择的场景
            var listScene = new List<Common.SceneUI>();
            foreach (var scene in dicSelectScene.Values)
            {
                listScene.Add(scene);
            }
            HdlThreadLogic.Current.RunThread(() =>
            {
                //上传文件
                var result = HdlShardLogic.Current.UploadSharedContent(this.ChildAccountId, this.lookRoom, listDevice, listScene);
                if (result == true)
                {
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        //重新初始化界面
                        this.InitMiddleFrame();
                    });
                }
            });
        }
 
        #endregion
    }
}