using Shared.Common;
using System;
using System.Collections.Generic;
namespace Shared.Phone.UserCenter.HdlBackup
{
///
/// 备份画面
///
public class HdlManualBackUpForm : UserCenterCommonForm
{
#region ■ 变量声明___________________________
///
/// 列表控件
///
private VerticalScrolViewLayout listView = null;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//设置标题信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uBackupAndRecover));
//右上添加按钮
var btnTopIcon = new TopLayoutMostRightView();
btnTopIcon.UnSelectedImagePath = "Item/Add.png";
btnTopIcon.SelectedImagePath = "Item/AddSelected.png";
topFrameLayout.AddChidren(btnTopIcon);
btnTopIcon.MouseUpEventHandler += (sender, e) =>
{
//显示添加备考名画面
this.ShowAddBackupForm();
};
//初始化中部控件
this.InitMiddleFrame();
}
///
/// 初始化中部控件
///
private void InitMiddleFrame()
{
listView = new VerticalScrolViewLayout();
listView.Height = bodyFrameLayout.Height;
bodyFrameLayout.AddChidren(listView);
new System.Threading.Thread(() =>
{
//从云端获取数据
this.SetBackupInfoToForm();
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 从云端获取数据_____________________
///
/// 从云端获取数据
///
private async void SetBackupInfoToForm()
{
//进度条
this.ShowProgressBar();
//从云端获取数据
var pageData = await HdlBackupLogic.Current.GetBackupListNameFromDB();
//关闭
this.CloseProgressBar();
if (pageData == null)
{
return;
}
Application.RunOnMainThread(() =>
{
listView.RemoveAll();
});
foreach (BackupListNameInfo fileInfo in pageData)
{
Application.RunOnMainThread(() =>
{
//添加备份行
this.AddRowlayout(fileInfo);
});
}
}
#endregion
#region ■ 添加备份行_________________________
///
/// 添加备份行
///
///
private void AddRowlayout(BackupListNameInfo fileInfo)
{
//行
var rowLayout = new StatuRowLayout(listView);
//图标
var btnPoint = new RowLeftIconView();
btnPoint.SelectedImagePath = "Center/BackupSelected.png";
btnPoint.UnSelectedImagePath = "Center/Backup.png";
rowLayout.AddChidren(btnPoint);
//备份名字
var txtText = new RowCenterView();
txtText.Text = fileInfo.BackupName;
rowLayout.AddChidren(txtText);
txtText.X -= ControlCommonResourse.PointXXLeft;
//编辑备注名
rowLayout.MouseUpEvent += (sender, e) =>
{
//显示编辑备考名画面
this.ShowEditorBackupForm(fileInfo);
};
//下载图标
var btnLoad = new MostRightEmptyView();
//启用点亮功能
btnLoad.UseClickStatu = true;
btnLoad.UnSelectedImagePath = "Item/DownLoad.png";
btnLoad.SelectedImagePath = "Item/DownLoadSelected.png";
rowLayout.AddChidren(btnLoad, ChidrenBindMode.NotBind);
//下载图标太右边不好看
btnLoad.X -= Application.GetRealWidth(50);
btnLoad.MouseUpEventHandler += (sender, e) =>
{
//是否要下载并恢复数据?
string msg = Language.StringByID(R.MyInternationalizationString.uDownLoadAndRecoverMsg);
this.ShowConfirmMsg(msg, "LoadBackupInfo", fileInfo.Id);
};
//删除
var btnDelete = new RowDeleteButton();
rowLayout.AddRightView(btnDelete);
btnDelete.MouseUpEventHandler += (sender, e) =>
{
//确定要删除文件吗?
string msg = Language.StringByID(R.MyInternationalizationString.uDoDeleteFileMsg);
this.ShowConfirmMsg(msg, "DeleteBackInfo", fileInfo.Id, ShowErrorMode.YES);
};
}
#endregion
#region ■ 读取备份文档_______________________
///
/// 读取备份文档
///
///
public void LoadBackupInfo(string BackupClassId)
{
new System.Threading.Thread(async () =>
{
//从云端获取备份的文件
bool result = await HdlBackupLogic.Current.LoadAppBackupInfo(BackupClassId);
if (result == false)
{
//文件恢复失败
string msg2 = Language.StringByID(R.MyInternationalizationString.uFileRecoverFail);
this.ShowNormalMsg(msg2);
return;
}
Application.RunOnMainThread(() =>
{
//关闭所有界面
UserCenterLogic.CloseAllOpenForm();
//切换到主页
UserView.UserPage.Instance.Fresh();
});
//文件恢复成功
string msg = Language.StringByID(R.MyInternationalizationString.uFileRecoverSuccess);
this.ShowNormalMsg(msg);
})
{ IsBackground = true }.Start();
}
///
/// 读取备份的文档的进度
///
///
///
private void LoadBackupInfoProgress(decimal nowValue, decimal count)
{
//设置最大值
this.SetProgressMax(count);
//显示百分比
this.SetProgressValue(nowValue);
}
#endregion
#region ■ 上传数据___________________________
///
/// 上传数据
///
///
private void UpLoadBackInfo(string backName)
{
new System.Threading.Thread(async () =>
{
//创建一个备份名字
string backupClassId = await HdlBackupLogic.Current.CreatNewBackupNameToDB(backName);
if (backupClassId == null)
{
//创建备份名字失败
string msg = Language.StringByID(R.MyInternationalizationString.uCreatBackupNameFail);
this.ShowErrorMsg(msg);
return;
}
//上传数据到云端
bool result = await HdlBackupLogic.Current.UpLoadBackupFileToDB(backupClassId);
if (result == false)
{
//文件上传失败
string msg = Language.StringByID(R.MyInternationalizationString.uFileUpLoadFail);
this.ShowErrorMsg(msg);
//如果上传失败的话,就把它删除
this.DeleteBackInfo(backupClassId, ShowErrorMode.NO);
return;
}
new System.Threading.Thread(() =>
{
//从云端获取数据
this.SetBackupInfoToForm();
})
{ IsBackground = true }.Start();
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 编辑备份名称_______________________
///
/// 编辑备份名称
///
///
///
private async void EditorBackInfo(string BackupClassId, string backName)
{
//开启进度条
this.ShowProgressBar();
var Pra = new EditorBackUpNamePra();
Pra.BackupClassId = BackupClassId;
Pra.BackupName = backName;
bool result = await UserCenterLogic.GetResultStatuByRequestHttps("App/UpdateHomeAppGatewayName", Pra);
if (result == false)
{
//编辑备份名称失败
string msg = Language.StringByID(R.MyInternationalizationString.uEditorBackupNameFail);
this.ShowErrorMsg(msg);
//关闭
this.CloseProgressBar();
return;
}
new System.Threading.Thread(() =>
{
//从云端获取数据
this.SetBackupInfoToForm();
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 删除备份文档_______________________
///
/// 删除备份文档
///
///
///
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.ShowErrorMsg(msg);
}
return;
}
new System.Threading.Thread(() =>
{
//从云端获取数据
this.SetBackupInfoToForm();
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 显示编辑备考名画面_________________
///
/// 显示编辑备考名画面
///
///
///
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.InputText = fileInfo.BackupName;
//按下确认按钮
dialogForm.ComfirmClickEvent += (() =>
{
string txtvalue = dialogForm.InputText;
//检测备考名称
if (this.CheckBackupName(txtvalue) == false)
{
return;
}
//画面关闭
dialogForm.CloseDialog();
//名字一样时,不处理
if (txtvalue != fileInfo.BackupName)
{
//编辑备份名称
this.EditorBackInfo(fileInfo.Id, txtvalue);
}
});
}
///
/// 显示添加备考名画面
///
///
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 += (() =>
{
string txtvalue = dialogForm.InputText;
//检测备考名称
if (this.CheckBackupName(txtvalue) == false)
{
return;
}
//画面关闭
dialogForm.CloseDialog();
//上传备份
this.UpLoadBackInfo(txtvalue);
});
}
#endregion
#region ■ 一般方法___________________________
///
/// 把本地所有文件移动到自动备份文件夹
///
private void CopyLocationAllFileToAutoBackDirectory()
{
List listAllFile = Global.FileListByHomeId();
string strroot = UserCenterResourse.LocalRootPath;
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);
}
}
///
/// 检测备考名称
///
///
///
private bool CheckBackupName(string backName)
{
if (backName == string.Empty)
{
//请输入备注名称
string msg = Language.StringByID(R.MyInternationalizationString.uPleaseInpuBackup);
this.ShowErrorMsg(msg);
return false;
}
return true;
}
#endregion
}
}