JLChen
2021-04-08 cf3d4880046912f8b46c9e54769986a179faa26c
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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 SmartSpeakerListPage : FrameLayout
    {
        FrameLayout bodyView;
        /// <summary>
        /// 
        /// </summary>
        FrameLayout emptyTipFrameLayout;
        /// <summary>
        /// 当前
        /// </summary>
        VerticalScrolViewLayout bodyScrolView;
        /// <summary>
        /// 内容为空提示View
        /// </summary>
        FrameLayout emptyTipView;
       
        /// 目前对接的音箱类型
        /// 小度=DuerOS
        /// Google Home = Google
        /// Alexa Echo = Alexa
        /// Alice = Alice
        /// 天猫精灵=Aligenie
        /// 思必驰=AISpeech
       
 
 
        /// <summary>
        /// 
        /// </summary>
        public SmartSpeakerListPage()
        {
            bodyView = this;
            bodyView.BackgroundColor = CSS_Color.BackgroundColor;
        }
 
        /// <summary>
        /// 
        /// </summary>
        public void LoadPage()
        {
            new TopViewDiv(bodyView, Language.StringByID(StringId.SmartSpeaker)).LoadTopView();
 
            int bodyY = Application.GetRealHeight(64);
            bodyScrolView = new VerticalScrolViewLayout()
            {
                Y = bodyY,
                Height = bodyView.Height - bodyY,
                BackgroundColor = CSS_Color.BackgroundColor,
            };
            bodyView.AddChidren(bodyScrolView);
 
            //AddEmptyTipView();
 
            LoadSmartSpeakertListView(bodyScrolView);
        }
 
        /// <summary>
        /// 添加内容为空提示页面
        /// </summary>
        void AddEmptyTipView()
        {
            emptyTipView = new FrameLayout()
            {
                Height = bodyScrolView.Height,
                Width = bodyScrolView.Width,
            };
            bodyScrolView.AddChidren(emptyTipView);
 
            var tipView = new EmptyTipView()
            {
                Gravity = Gravity.Center
            };
            emptyTipView.AddChidren(tipView);
        }
 
        /// <summary>
        /// 加载音箱列表
        /// </summary>
        /// <param name="VerticalScrolViewMiddle"></param>
        void LoadSmartSpeakertListView(VerticalScrolViewLayout VerticalScrolViewMiddle)
        {
            VerticalScrolViewMiddle.RemoveAll();
 
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
 
                    var revertObj = new HttpServerRequest().GetSpeakerList();
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        var speakerListRes = Newtonsoft.Json.JsonConvert.DeserializeObject<SpeakerListRes>(revertObj.Data.ToString());
                        if (speakerListRes != null && speakerListRes.list != null && speakerListRes.list.Count > 0)
                        {
                            Application.RunOnMainThread(() =>
                            {
                                foreach (var speaker in speakerListRes.list)
                                {
                                    AddRowView(speaker, VerticalScrolViewMiddle);
                                }
 
                            });
                        }
                        else
                        {
                            Application.RunOnMainThread(() =>
                            {
                                AddEmptyTipView();
                            });
                        }
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            AddEmptyTipView();
                        });
                        //提示错误
                        IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code);
                    }
                }
                catch
                {
 
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            });
        }
 
 
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="speakerInfo"></param>
        /// <param name="VerticalScrolViewMiddle"></param>
        void AddRowView(SpeakerInfo speakerInfo, VerticalScrolViewLayout VerticalScrolViewMiddle)
        {
            //透明填充分割view
            var lineView = new FrameLayout()
            {
                Height = Application.GetRealWidth(12),
            };
            VerticalScrolViewMiddle.AddChidren(lineView);
 
            var rowView = new FrameLayout()
            {
                Gravity = Gravity.CenterHorizontal,
                Width = Application.GetRealWidth(343),
                Height = Application.GetRealWidth(200),
                BackgroundColor = CSS_Color.MainBackgroundColor,
                Radius = (uint)Application.GetRealWidth(12),
                BorderColor = 0x00000000,
                BorderWidth = 0,
            };
            VerticalScrolViewMiddle.AddChidren(rowView);
 
            #region 音箱类型
            var view1 = new FrameLayout()
            {
                Height = Application.GetRealWidth(49),
            };
            rowView.AddChidren(view1);
 
            var btnSpeakerNameTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(200),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                Text = GetSpeakerTypeString(speakerInfo.platformName)
            };
            view1.AddChidren(btnSpeakerNameTitle);
 
            Button line1 = new Button()
            {
                Y = view1.Bottom,
                Gravity = Gravity.CenterHorizontal,
                Height = Application.GetRealWidth(1),
                Width = Application.GetRealWidth(311),
                BackgroundColor = CSS_Color.DividingLineColor,
            };
            rowView.AddChidren(line1);
 
            #endregion
 
            #region 备注
            var view2 = new FrameLayout()
            {
                Y = line1.Bottom,
                Height = Application.GetRealWidth(49),
            };
            rowView.AddChidren(view2);
 
            var btnNicknameTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(200),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                TextID = StringId.Remarks,
            };
            view2.AddChidren(btnNicknameTitle);
 
            var btnNickname = new Button()
            {
                Width = Application.GetRealWidth(295),
                TextAlignment = TextAlignment.CenterRight,
                TextColor = CSS_Color.PromptingColor1,
                TextSize = CSS_FontSize.TextFontSize,
                Text = speakerInfo.remark
            };
            view2.AddChidren(btnNickname);
 
            if (string.IsNullOrEmpty(speakerInfo.remark)) {
                btnNickname.TextID = StringId.SmartSpeaker;
            }
 
            var btnNicknameRight = new Button()
            {
                X = Application.GetRealWidth(311),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/Right.png",
            };
            view2.AddChidren(btnNicknameRight);
 
            Button line2 = new Button()
            {
                Y = view2.Bottom,
                Gravity = Gravity.CenterHorizontal,
                Height = Application.GetRealWidth(1),
                Width = Application.GetRealWidth(311),
                BackgroundColor = CSS_Color.DividingLineColor,
            };
            rowView.AddChidren(line2);
 
 
            //保存事件
            Action<string> renameAction = (newName) =>
            {
                RenameAlexaRemark(newName, speakerInfo, btnNickname);
            };
 
            btnNickname.MouseUpEventHandler += (sender, e) =>
            {
                new PublicAssmebly().LoadDialog_EditParater(StringId.Remarks, speakerInfo.remark, renameAction, StringId.RemarksCannotBeBlank, 0, new List<string>());
 
                //HDLCommon.Current.ShowEditTextDialog(speakerInfo.remark, renameAction, Language.StringByID(R.MyInternationalizationString.rename));
            };
            #endregion
 
            #region 数据管理
            var view3 = new FrameLayout()
            {
                Y = line2.Bottom,
                Height = Application.GetRealWidth(49),
            };
            rowView.AddChidren(view3);
 
            var btnDataTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(200),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                TextID = StringId.DataManagement,
            };
            view3.AddChidren(btnDataTitle);
 
            var btnDataRight = new Button()
            {
                X = Application.GetRealWidth(311),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/Right.png",
            };
            view3.AddChidren(btnDataRight);
 
            Button line3 = new Button()
            {
                Y = view3.Bottom,
                Gravity = Gravity.CenterHorizontal,
                Height = Application.GetRealWidth(1),
                Width = Application.GetRealWidth(311),
                BackgroundColor = CSS_Color.DividingLineColor,
            };
            rowView.AddChidren(line3);
 
            EventHandler<MouseEventArgs> eHandler = (sender, e) =>
            {
                //跳转页面
                GotoSpeakerManagementPage(speakerInfo);
            };
            view3.MouseUpEventHandler += eHandler;
            btnDataTitle.MouseUpEventHandler += eHandler;
            btnDataRight.MouseUpEventHandler += eHandler;
            #endregion
 
            #region 解除绑定
            var view4 = new FrameLayout()
            {
                Y = line3.Bottom,
                Height = Application.GetRealWidth(49),
            };
            rowView.AddChidren(view4);
 
            var btnUnbindTitle = new Button()
            {
                X = Application.GetRealWidth(16),
                Width = Application.GetRealWidth(200),
                TextAlignment = TextAlignment.CenterLeft,
                TextColor = CSS_Color.FirstLevelTitleColor,
                TextSize = CSS_FontSize.TextFontSize,
                TextID = StringId.Unbind,
            };
            view4.AddChidren(btnUnbindTitle);
 
            var btnUnbindRight = new Button()
            {
                X = Application.GetRealWidth(311),
                Gravity = Gravity.CenterVertical,
                Width = Application.GetMinRealAverage(16),
                Height = Application.GetMinRealAverage(16),
                UnSelectedImagePath = "Public/Right.png",
            };
            view4.AddChidren(btnUnbindRight);
 
 
            #endregion
 
 
 
        }
 
        /// <summary>
        /// 请求修改备注
        /// </summary>
        /// <param name="newName"></param>
        /// <param name="speakerInfo"></param>
        /// <param name="btnName"></param>
        void RenameAlexaRemark(string newName, SpeakerInfo speakerInfo, Button btnName)
        {
 
            var waitPage = new Loading();
            bodyView.AddChidren(waitPage);
            waitPage.Start(Language.StringByID(StringId.PleaseWait));
 
            System.Threading.Tasks.Task.Run(() =>
            {
                try
                {
                    var updateSpeakerRemarkObj = new UpdateSpeakerRemarkObj()
                    {
                        homeId = speakerInfo.homeId,
                        tokenId = speakerInfo.tokenId,
                        remark = newName
                    };
 
                    var revertObj = new HttpServerRequest().UpdateSpeakerRemark(updateSpeakerRemarkObj);
                    if (revertObj.Code == StateCode.SUCCESS)
                    {
                        //AmendTheSuccess 修改成功
                        Application.RunOnMainThread(() =>
                        {
                            speakerInfo.remark = newName;
                            btnName.Text = newName;
                            Utlis.ShowTip(Language.StringByID(StringId.ModifySuccess));
                            //Utlis.ShowAlertOnMainThread(Language.StringByID(R.MyInternationalizationString.AmendTheSuccess));
 
                        });
                    }
                    else
                    {
                        //提示错误
                        IMessageCommon.Current.ShowErrorInfoAlter(revertObj.Code);
                    }
                }
                catch
                {
 
                }
                finally
                {
                    Application.RunOnMainThread(() =>
                    {
                        if (waitPage != null)
                        {
                            waitPage.RemoveFromParent();
                            waitPage = null;
                        }
                    });
                }
            });
 
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="platformName"></param>
        /// <returns></returns>
        string GetSpeakerTypeString(string platformName){
            var name = platformName;
            if (platformName == SpeakerType.DuerOS.ToString())
            {
                name = "小度";
            }
            else if (platformName == SpeakerType.Google.ToString()){
                name = "Google Home";
            }
            else if (platformName == SpeakerType.Alexa.ToString())
            {
                name = "Alexa Echo";
            }
            else if (platformName == SpeakerType.Alice.ToString())
            {
                name = "Alice";
            }
            else if (platformName == SpeakerType.Aligenie.ToString())
            {
                name = "天猫精灵";
            }
            else if (platformName == SpeakerType.AISpeech.ToString())
            {
                name = "思必驰";
            }
 
            return name;
        }
 
        /// <summary>
        /// 跳转AlexaDeviceListPage页面
        /// </summary>
        /// <param name="speakerInfo"></param>
        void GotoSpeakerManagementPage(SpeakerInfo speakerInfo)
        {
            ////没绑定忘记提示先绑定网关,禁止跳转
            //if (!UserConfig.Instance.CheckWhetherGatewayIdNotNull())
            //{
            //    Utlis.ShowAlertOnMainThread(Language.StringByID(R.MyInternationalizationString.PleaseBindTheGatewayFirst));
            //    return;
            //}
 
 
            ////跳转Alexa管理页面
            //AlexaDeviceListPage speakerInfoDeviceListPage = new AlexaDeviceListPage(speakerInfo);
            //UserMiddle.SettingPageView.AddChidren(speakerInfoDeviceListPage);
            //speakerInfoDeviceListPage.ShowPage();
            //UserMiddle.SettingPageView.PageIndex = UserMiddle.SettingPageView.ChildrenCount - 1;
        }
    }
 
 
 
}