using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.Commands; using Tango.Core.DI; using Tango.Explorer; using Tango.PPC.BackupRestore.Views; using Tango.PPC.Common; using Tango.PPC.Common.BackupRestore; using Tango.PPC.Storage; using static Tango.PPC.BackupRestore.ViewModels.BackupErrorViewVM; namespace Tango.PPC.BackupRestore.ViewModels { public class BackupViewVM : PPCViewModel { private String _backupFileName; [TangoInject] public IBackupManager BackupManager { get; set; } private BackupRestoreProgressEventArgs _currentBackupProgress; public BackupRestoreProgressEventArgs CurrentBackupProgress { get { return _currentBackupProgress; } set { _currentBackupProgress = value; RaisePropertyChangedAuto(); } } private bool _isBackupJobs; public bool IsBackupJobs { get { return _isBackupJobs; } set { if (value) { _isBackupJobs = value; RaisePropertyChangedAuto(); _isBackupFull = false; RaisePropertyChanged(nameof(IsBackupFull)); } else { RaisePropertyChangedAuto(); } } } private bool _isBackupFull; public bool IsBackupFull { get { return _isBackupFull; } set { if (value) { _isBackupFull = value; RaisePropertyChangedAuto(); _isBackupJobs = false; RaisePropertyChanged(nameof(IsBackupJobs)); } else { RaisePropertyChangedAuto(); } } } private String _backupLocation; public String BackupLocation { get { return _backupLocation; } set { _backupLocation = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } private String _backupName; public String BackupName { get { return _backupName; } set { _backupName = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } } public RelayCommand BackupCommand { get; set; } public RelayCommand BrowseBackupLocationCommand { get; set; } public BackupViewVM() { BrowseBackupLocationCommand = new RelayCommand(BrowseBackupLocation); BackupCommand = new RelayCommand(StartBackup, () => !String.IsNullOrWhiteSpace(BackupName) && BackupLocation != null); IsBackupJobs = true; } private async void StartBackup() { await NavigationManager.NavigateTo(nameof(BackupProgressView), false); try { IsFree = false; NavigationManager.IsBackEnabled = false; await BackupManager.CreateBackup(_backupFileName, BackupName, new BackupSettings() { Mode = IsBackupFull ? BackupMode.Full : BackupMode.Jobs, }); await NavigationManager.NavigateTo(nameof(BackupCompletedView), false); } catch (Exception ex) { LogManager.Log(ex, "The backup operation failed."); await NavigationManager.NavigateWithObject(new BackupErrorNavigationObject() { Error = ex.FlattenMessage(), }, false); } finally { IsFree = true; NavigationManager.IsBackEnabled = true; } } public override void OnApplicationStarted() { } public override void OnApplicationReady() { base.OnApplicationReady(); BackupManager.Progress += BackupManager_Progress; } private void BackupManager_Progress(object sender, BackupRestoreProgressEventArgs e) { CurrentBackupProgress = e; } private async void BrowseBackupLocation() { var result = await NavigationManager. NavigateForResult( new Storage.Models.StorageNavigationRequest() { Intent = Storage.Models.StorageNavigationIntent.SaveFile, DefaultFileName = $"Tango-Backup-{DateTime.Now.ToFileName()}", Filter = ExplorerFileDefinition.Backup.Extension, Title = "Select Destination Backup File", }); if (result != null) { _backupFileName = result.Path + ExplorerFileDefinition.Backup.Extension; BackupLocation = result.Path + ExplorerFileDefinition.Backup.Extension; } } } }