gxc
2019-10-29 081ea8d273048fd03756718ac6fb48a3c09218e9
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
using Shared.Common;
using System;
using System.Collections.Generic;
 
namespace Shared.Phone.UserCenter.HdlBackup
{
    /// <summary>
    /// 备份画面
    /// </summary>
    public class HdlManualBackUpForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 列表控件
        /// </summary>
        private VerticalListControl listView = null;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置标题信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uBackupAndRecover));
 
            //右上添加按钮
            var btnTopIcon = new MostRightIconControl(69, 69);
            btnTopIcon.UnSelectedImagePath = "Item/Add.png";
            topFrameLayout.AddChidren(btnTopIcon);
            btnTopIcon.InitControl();
            btnTopIcon.ButtonClickEvent += (sender, e) =>
            {
                //显示添加备考名画面
                this.ShowAddBackupForm();
            };
 
            //初始化中部控件
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部控件
        /// </summary>
        private void InitMiddleFrame()
        {
            //清空bodyFrame
            this.ClearBodyFrame();
 
            listView = new VerticalListControl(29);
            listView.BackgroundColor = UserCenterColor.Current.White;
            listView.Height = bodyFrameLayout.Height;
            bodyFrameLayout.AddChidren(listView);
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //从云端获取数据
                this.SetBackupInfoToForm();
            });
        }
 
        #endregion
 
        #region ■ 从云端获取数据_____________________
 
        /// <summary>
        /// 从云端获取数据
        /// </summary>
        private async void SetBackupInfoToForm()
        {
            //进度条
            this.ShowProgressBar();
 
            //从云端获取数据
            var pageData = await HdlBackupLogic.Current.GetBackupListNameFromDB();
            //关闭
            this.CloseProgressBar();
 
            if (pageData == null)
            {
                return;
            }
 
            Application.RunOnMainThread(() =>
            {
                if (this.Parent == null || listView == null)
                {
                    return;
 
                }
                listView.RemoveAll();
                int count = pageData.Count - 1;
                for (int i = 0; i < pageData.Count; i++)
                {
                    //添加备份行
                    this.AddRowlayout(pageData[i], i != count);
                }
            });
        }
 
        #endregion
 
        #region ■ 添加备份行_________________________
 
        /// <summary>
        /// 添加备份行
        /// </summary>
        /// <param name="fileInfo"></param>
        private void AddRowlayout(BackupListNameInfo fileInfo, bool addLine)
        {
            //行
            var rowLayout = new RowLayoutControl(listView.rowSpace / 2);
            listView.AddChidren(rowLayout);
 
            //图标
            var btnPoint = rowLayout.frameTable.AddLeftIcon();
            btnPoint.UnSelectedImagePath = "Center/Backup.png";
 
            //备份名字
            var txtText = rowLayout.frameTable.AddLeftCaption(fileInfo.BackupName, 700);
            if (addLine == true)
            {
                //底线
                rowLayout.frameTable.AddBottomLine();
            }
 
            //编辑备注名
            rowLayout.frameTable.ButtonClickEvent += (sender, e) =>
            {
                //显示编辑备考名画面
                this.ShowEditorBackupForm(fileInfo);
            };
 
            //下载图标
            var btnLoad = rowLayout.frameTable.AddMostRightEmptyIcon(81, 81);
            rowLayout.frameTable.ChangedChidrenBindMode(btnLoad, ChidrenBindMode.NotBind);
            //启用点亮功能
            btnLoad.UseClickStatu = true;
            btnLoad.UnSelectedImagePath = "Item/DownLoad.png";
            btnLoad.SelectedImagePath = "Item/DownLoadSelected.png";
            btnLoad.ButtonClickEvent += (sender, e) =>
            {
                //是否要下载并恢复数据?
                string msg = Language.StringByID(R.MyInternationalizationString.uDownLoadAndRecoverMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                 {
                     //读取备份文档
                     this.LoadBackupInfo(fileInfo.Id);
                 });
            };
 
            //删除
            var btnDelete = new NormalViewControl(Application.GetRealWidth(177), rowLayout.Height, false);
            btnDelete.BackgroundColor = UserCenterColor.Current.RowDeleteButtonColor;
            btnDelete.TextAlignment = TextAlignment.Center;
            btnDelete.TextColor = UserCenterColor.Current.White;
            btnDelete.TextID = R.MyInternationalizationString.uDelete;
            rowLayout.AddRightView(btnDelete);
            btnDelete.ButtonClickEvent += (sender, e) =>
            {
                //确定要删除文件吗?
                string msg = Language.StringByID(R.MyInternationalizationString.uDoDeleteFileMsg);
                this.ShowMassage(ShowMsgType.Confirm, msg, () =>
                 {
                     //删除备份文档
                     this.DeleteBackInfo(fileInfo.Id, ShowErrorMode.YES);
                 });
            };
        }
 
        #endregion
 
        #region ■ 读取备份文档_______________________
 
        /// <summary>
        /// 读取备份文档
        /// </summary>
        /// <param name="BackupClassId"></param>
        private void LoadBackupInfo(string BackupClassId)
        {
            HdlThreadLogic.Current.RunThread(async () =>
            {
                //从云端获取备份的文件
                bool result = await HdlBackupLogic.Current.LoadAppBackupInfo(BackupClassId);
                if (result == false)
                {
                    //文件恢复失败
                    string msg2 = Language.StringByID(R.MyInternationalizationString.uFileRecoverFail);
                    this.ShowMassage(ShowMsgType.Error, msg2);
                    return;
                }
 
                Application.RunOnMainThread(() =>
                {
                    //关闭所有界面
                    UserCenterLogic.CloseAllOpenForm();
                    //切换到主页
                    UserView.UserPage.Instance.Fresh();
                });
 
                //文件恢复成功
                string msg = Language.StringByID(R.MyInternationalizationString.uFileRecoverSuccess);
                this.ShowMassage(ShowMsgType.Normal, msg);
            });
        }
 
        #endregion
 
        #region ■ 上传数据___________________________
 
        /// <summary>
        /// 上传数据
        /// </summary>
        /// <param name="backName"></param>
        private void UpLoadBackInfo(string backName)
        {
            HdlThreadLogic.Current.RunThread(async () =>
            {
                //创建一个备份名字
                string backupClassId = await HdlBackupLogic.Current.CreatNewBackupNameToDB(backName);
                if (backupClassId == null)
                {
                    //创建备份名字失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uCreatBackupNameFail);
                    this.ShowMassage(ShowMsgType.Error, msg);
 
                    return;
                }
 
                //上传数据到云端
                bool result = await HdlBackupLogic.Current.UpLoadBackupFileToDB(backupClassId);
                if (result == false)
                {
                    //文件上传失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uFileUpLoadFail);
                    this.ShowMassage(ShowMsgType.Error, msg);
 
                    //如果上传失败的话,就把它删除
                    this.DeleteBackInfo(backupClassId, ShowErrorMode.NO);
 
                    return;
                }
 
                HdlThreadLogic.Current.RunThread(() =>
                {
                    //从云端获取数据
                    this.SetBackupInfoToForm();
                });
            });
        }
 
        #endregion
 
        #region ■ 编辑备份名称_______________________
 
        /// <summary>
        /// 编辑备份名称
        /// </summary>
        /// <param name="BackupClassId"></param>
        /// <param name="backName"></param>
        private async void EditorBackInfo(string BackupClassId, string backName)
        {
            //开启进度条
            this.ShowProgressBar();
 
            var Pra = new EditorBackUpNamePra();
            Pra.BackupClassId = BackupClassId;
            Pra.BackupName = backName;
            //获取控制主人账号的Token
            Pra.LoginAccessToken = UserCenterLogic.GetConnectMainToken();
 
            bool result = await UserCenterLogic.GetResultStatuByRequestHttps("App/UpdateHomeAppGatewayName", true, Pra);
            if (result == false)
            {
                //编辑备份名称失败
                string msg = Language.StringByID(R.MyInternationalizationString.uEditorBackupNameFail);
                this.ShowMassage(ShowMsgType.Error, msg);
                //关闭
                this.CloseProgressBar();
                return;
            }
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //从云端获取数据
                this.SetBackupInfoToForm();
            });
        }
 
        #endregion
 
        #region ■ 删除备份文档_______________________
 
        /// <summary>
        /// 删除备份文档
        /// </summary>
        /// <param name="BackupClassId"></param>
        /// <param name="showMode"></param>
        public async void DeleteBackInfo(string BackupClassId, ShowErrorMode showMode = ShowErrorMode.YES)
        {
            //进度条
            this.ShowProgressBar();
 
            bool success = await HdlBackupLogic.Current.DeleteDbBackupData(BackupClassId);
 
            //关闭进度条
            this.CloseProgressBar();
 
            if (success == false)
            {
                if (showMode == ShowErrorMode.YES)
                {
                    //删除备份失败
                    string msg = Language.StringByID(R.MyInternationalizationString.uDeleteBackupFail);
                    this.ShowMassage(ShowMsgType.Error, msg);
                }
                return;
            }
 
            HdlThreadLogic.Current.RunThread(() =>
            {
                //从云端获取数据
                this.SetBackupInfoToForm();
            });
        }
 
        #endregion
 
        #region ■ 显示编辑备考名画面_________________
 
        /// <summary>
        /// 显示编辑备考名画面
        /// </summary>
        /// <param name="fileInfo"></param>
        /// <returns></returns>
        private void ShowEditorBackupForm(BackupListNameInfo fileInfo)
        {
            //生成一个弹窗画面
            var dialogForm = new DialogInputFrameControl(this, DialogFrameMode.OnlyInput);
 
            //编辑备份
            dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uEditorBackup));
            //请输入备注名称
            dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInpuBackup));
            dialogForm.Text = fileInfo.BackupName;
 
            //按下确认按钮
            dialogForm.ComfirmClickEvent += ((textValue) =>
            {
                //检测备考名称
                if (this.CheckBackupName(textValue) == false)
                {
                    return;
                }
 
                //画面关闭
                dialogForm.CloseDialog();
 
                //名字一样时,不处理
                if (textValue != fileInfo.BackupName)
                {
                    //编辑备份名称
                    this.EditorBackInfo(fileInfo.Id, textValue);
                }
            });
        }
 
        /// <summary>
        /// 显示添加备考名画面
        /// </summary>
        /// <returns></returns>
        private void ShowAddBackupForm()
        {
            //生成一个弹窗画面
            var dialogForm = new DialogInputFrameControl(this, DialogFrameMode.OnlyInput);
 
            //添加备份
            dialogForm.SetTitleText(Language.StringByID(R.MyInternationalizationString.uAddBackup));
            //请输入备注名称
            dialogForm.SetTipText(Language.StringByID(R.MyInternationalizationString.uPleaseInpuBackup));
 
            //按下确认按钮
            dialogForm.ComfirmClickEvent += ((textValue) =>
            {
                //检测备考名称
                if (this.CheckBackupName(textValue) == false)
                {
                    return;
                }
 
                //画面关闭
                dialogForm.CloseDialog();
 
                //上传备份
                this.UpLoadBackInfo(textValue);
            });
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 把本地所有文件移动到自动备份文件夹
        /// </summary>
        private void CopyLocationAllFileToAutoBackDirectory()
        {
            List<string> listAllFile = Global.FileListByHomeId();
 
            string strroot = Common.Config.Instance.FullPath;
            string autoPath = System.IO.Path.Combine(strroot, DirNameResourse.LocalMemoryDirectory, DirNameResourse.AutoBackupDirectory);
            foreach (string file in listAllFile)
            {
                //判断是否是应该上传的文件
                if (HdlBackupLogic.Current.IsNotUpLoadFile(file) == true)
                {
                    continue;
                }
                string sourseName = System.IO.Path.Combine(strroot, file);
                string newName = System.IO.Path.Combine(autoPath, file);
                System.IO.File.Copy(sourseName, newName, true);
            }
        }
 
        /// <summary>
        /// 检测备考名称
        /// </summary>
        /// <param name="backName"></param>
        /// <returns></returns>
        private bool CheckBackupName(string backName)
        {
            if (backName == string.Empty)
            {
                //请输入备注名称
                string msg = Language.StringByID(R.MyInternationalizationString.uPleaseInpuBackup);
                this.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
            if (backName.Contains("##") == true)
            {
                //存在非法字符「##」
                string msg = Language.StringByID(R.MyInternationalizationString.uErrorFieldIsEsixt);
                if (msg.Contains("{0}") == true)
                {
                    msg = string.Format(msg, "##");
                }
                this.ShowMassage(ShowMsgType.Error, msg);
                return false;
            }
 
            return true;
        }
        #endregion
    }
}