wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
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
using Shared;
using System;
using System.Collections.Generic;
using System.Text;
 
namespace HDL_ON.Stan
{
    /// <summary>
    /// 界面相关的逻辑
    /// </summary>
    public class HdlFormLogic
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 界面相关的逻辑
        /// </summary>
        private static HdlFormLogic m_Current;
        /// <summary>
        /// 界面相关的逻辑
        /// </summary>
        public static HdlFormLogic Current
        {
            get
            {
                if (m_Current == null)
                {
                    m_Current = new HdlFormLogic();
                }
                return m_Current;
            }
        }
 
        /// <summary>
        /// <para>当前已经打开了的画面(继承于UserCenterCommonForm才能使用)。</para>
        /// <para>画面打开时,会自动追击,画面关闭时,自动移除。</para>
        /// <para>新添加的界面会放在前面</para>
        /// </summary>
        private List<CommonFormBase> ListActionForm = new List<CommonFormBase>();
        /// <summary>
        /// 当前正在操作的画面ID(没人会懂它为何要存在)
        /// </summary>
        public string NowActionFormID
        {
            get
            {
                return ListActionForm.Count > 0 ? ListActionForm[0].FormID : string.Empty;
            }
        }
        /// <summary>
        /// 显示监视内存的控件
        /// </summary>
        private NormalViewControl btnMemory = null;
 
        #endregion
 
        #region ■ 添加界面相关_______________________
 
        /// <summary>
        /// 检测能否添加画面
        /// </summary>
        /// <returns>true:可以追加 false:不可追加</returns>
        /// <param name="form">Form</param>
        public bool CheckCanAddForm(CommonFormBase form)
        {
            //获取画面英文名字
            string formId = GetFormID(form);
 
            //暂时这样弄看看,如果重复,则关闭掉原来的界面
            var formTemp = this.GetFormByName(formId);
            formTemp?.CloseForm();
 
            return true;
        }
 
        /// <summary>
        /// 把打开的画面添加到内存中
        /// </summary>
        /// <param name="form">Form.</param>
        public void AddActionForm(CommonFormBase form)
        {
            //获取画面英文名字
            string formId = GetFormID(form);
 
            //内存添加
            form.FormID = formId;
            this.ListActionForm.Insert(0, form);
        }
 
        /// <summary>
        /// 从列表中移除画面
        /// </summary>
        /// <param name="i_closeForm">关闭的界面</param>
        public void RemoveActionForm(CommonFormBase i_closeForm)
        {
            //获取画面ID
            string formId = GetFormID(i_closeForm);
            var removeForm = this.GetFormByName(formId);
            if (removeForm == null)
            {
                return;
            }
            //移除画面
            this.ListActionForm.Remove(removeForm);
 
            var actionForm = MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1);
            if (actionForm == null)
            {
                return;
            }
            //关闭的界面为EditorCommonForm的时候
            if ((i_closeForm is EditorCommonForm) && (actionForm is EditorCommonForm))
            {
                //接下来激活的界面
                try
                {
                    var Myform = actionForm as EditorCommonForm;
                    //重置左滑使能
                    Myform.ScrollLeftEnabled = Myform.ScrollLeftEnabled;
                    //触发界面再次激活的事件
                    Myform.FormActionAgainEvent();
                }
                catch { }
            }
        }
 
        /// <summary>
        /// 获取画面ID
        /// </summary>
        /// <returns>The form name.</returns>
        /// <param name="form">Form.</param>
        private string GetFormID(CommonFormBase form)
        {
            if (form.FormID != string.Empty)
            {
                return form.FormID;
            }
            //将命名空间去掉
            string[] Arry = form.ToString().Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            string formName = Arry[Arry.Length - 1].Trim();
            return formName;
        }
 
        #endregion
 
        #region ■ 设备状态更新推送___________________
 
        /// <summary>
        /// 设备状态更新推送
        /// </summary>
        /// <param name="i_LocalDevice">本地设备对象</param>
        /// <param name="refreshCardContr">是否刷新主页,分类,房间等等的设备卡片的状态,默认不刷新(此变量目前是给bus接收那里使用的)</param>
        public void DeviceStatuPush(Entity.Function i_LocalDevice, bool refreshCardContr = false)
        {
            HdlThreadLogic.Current.RunMain(() =>
            {
                for (int i = 0; i < this.ListActionForm.Count; i++)
                {
                    this.ListActionForm[i]?.DeviceStatuPush(i_LocalDevice);
                }
                if (refreshCardContr == true)
                {
                    //刷新主页,分类,房间等等的设备卡片状态
                    this.RefreshAllDeviceCardControl(i_LocalDevice);
                }
 
            }, ShowErrorMode.NO);
        }
 
        #endregion
 
        #region ■ 手动刷新各设备卡片_________________
 
        /// <summary>
        /// 手动刷新主页,分类,房间等等的设备卡片状态
        /// </summary>
        /// <param name="i_device">需要刷新的设备对象</param>
        public void RefreshAllDeviceCardControl(Entity.Function i_device)
        {
            //刷新主页
            UI.HomePage.UpdataFunctionStates(i_device);
            //刷新分类
            UI.ClassificationPage.UpdataInfo(i_device);
            //刷新房间
            UI.RoomPage.UpdataStates(i_device);
            //刷新功能
            UI.FunctionPage.UpdataStates(i_device);
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 关闭所有打开了的界面
        /// </summary>
        /// <param name="tagetFrom">目标界面,如果指定了的话,则关闭目标界面上层的全部界面(它自身不关闭)</param>
        public void CloseAllOpenForm(string tagetFrom = null)
        {
            //最后一个是装载主页的容器,所以不能删除
            while (MainPage.BasePageView.ChildrenCount > 1)
            {
                var view = MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1);
                if (view is CommonFormBase)
                {
                    if (((CommonFormBase)view).FormID == tagetFrom)
                    {
                        //只关闭到指定目标界面
                        return;
                    }
                    ((CommonFormBase)view).CloseForm();
                }
                else
                {
                    view.RemoveFromParent();
                }
            }
        }
 
        /// <summary>
        /// 获取当前正在激活的界面
        /// </summary>
        /// <returns></returns>
        public CommonFormBase GetNowActionForm()
        {
            return MainPage.BasePageView.GetChildren(MainPage.BasePageView.ChildrenCount - 1) as CommonFormBase;
        }
 
        /// <summary>
        /// 根据名字获取指定已经打开了的界面
        /// </summary>
        /// <param name="i_formName"></param>
        /// <returns></returns>
        public CommonFormBase GetFormByName(string i_formName)
        {
            for (int i = 0; i < this.ListActionForm.Count; i++)
            {
                if (this.ListActionForm[i].FormID == i_formName)
                {
                    return this.ListActionForm[i];
                }
            }
            return null;
        }
 
        /// <summary>
        /// 关闭指定的画面
        /// </summary>
        /// <param name="formName">指定要关闭的画面英文名字</param>
        public void CloseFormByFormName(string formName)
        {
            var form = this.GetFormByName(formName);
            //关闭指定画面
            form?.CloseForm();
        }
 
        /// <summary>
        /// 判断指定的界面是否打开
        /// </summary>
        /// <param name="formId"></param>
        /// <returns></returns>
        public bool IsFormOpen(string formId)
        {
            var form = this.GetNowActionForm();
 
            return form != null ? form.FormID == formId : false;
        }
 
        /// <summary>
        /// 获取全部已经打开了的界面
        /// </summary>
        /// <returns></returns>
        public List<CommonFormBase> GetAllOpenForm()
        {
            var list = new List<CommonFormBase>();
            try
            {
                for (int i = 0; i < this.ListActionForm.Count; i++)
                {
                    list.Add(this.ListActionForm[i]);
                }
                return list;
            }
            catch { return list; }
        }
 
#if __Android__
        /// <summary>
        /// 显示监听内存的界面
        /// </summary>
        public void ShowMemoryListenView(int div)
        {
            this.btnMemory = new NormalViewControl(Application.MainPage.Width, HdlControlResourse.TopMenuFrameHeight, false);
            btnMemory.TextAlignment = TextAlignment.Center;
            btnMemory.BackgroundColor = 0xffffb9b9;
            btnMemory.TextSize = 10;
            btnMemory.IsMoreLines = true;
            Application.MainPage.AddChidren(btnMemory);
            if (div == 2)
            {
                btnMemory.Y = Application.MainPage.Height - this.btnMemory.Height;
                btnMemory.BringToFront();
                btnMemory.Animate = Animate.DownToUp;
            }
            else
            {
                btnMemory.BringToFront();
                btnMemory.Animate = Animate.UpToDown;
            }
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                int count = 0;
                while (this.btnMemory != null)
                {
                    System.Threading.Thread.Sleep(1000);
                    count++;
                    if (count < 2) { continue; }
                    count = 0;
 
                    HdlThreadLogic.Current.RunMain(() =>
                    {
                        var obj = Application.Activity.GetSystemService(Android.Content.Context.ActivityService);
                        var activityManager = (Android.App.ActivityManager)obj;
                        var memoryInfos = activityManager.GetProcessMemoryInfo(new int[] { Android.OS.Process.MyPid() });
 
                        if (memoryInfos.Length > 0)
                        {
                            string text = "PrivateDirty:" + (memoryInfos[0].TotalPrivateDirty) + "KB";
                            text += "  Pss:" + (memoryInfos[0].TotalPss) + "KB\r\n";
                            text += "PrivateDirty:" + (memoryInfos[0].TotalPrivateDirty / 1024) + "MB";
                            text += "  Pss:" + (memoryInfos[0].TotalPss / 1024) + "MB";
                            if (btnMemory != null)
                            {
                                btnMemory.Text = text;
                            }
                        }
                        else
                        {
                            if (btnMemory != null)
                            {
                                btnMemory.Text = "获取内存异常";
                            }
                        }
 
                        obj = null;
                        activityManager = null;
                        memoryInfos = null;
                    });
                }
            });
        }
 
        /// <summary>
        /// 关闭监听内存的界面
        /// </summary>
        public void CloseMemoryListenView()
        {
            if (this.btnMemory != null)
            {
                this.btnMemory.RemoveFromParent();
                this.btnMemory = null;
            }
        }
 
        /// <summary>
        /// 监测是否是在监听
        /// </summary>
        /// <returns></returns>
        public bool CheckIsListeningMemory()
        {
            return this.btnMemory != null;
        }
#endif
 
        #endregion
    }
}