From 8aba2d1cbac39e10b78b3c250e6a2d3e463d0467 Mon Sep 17 00:00:00 2001 From: Deadpikle Date: Wed, 6 Feb 2019 14:18:25 +0700 Subject: [PATCH] A little code cleanup --- .../ViewModels/BackupInProgressVIewModel.cs | 21 +++ EasyBackup/ViewModels/MainWindowViewModel.cs | 8 ++ EasyBackup/ViewModels/SetupBackupViewModel.cs | 134 ++++++++++-------- 3 files changed, 100 insertions(+), 63 deletions(-) diff --git a/EasyBackup/ViewModels/BackupInProgressVIewModel.cs b/EasyBackup/ViewModels/BackupInProgressVIewModel.cs index a5d589a..b5ddc36 100644 --- a/EasyBackup/ViewModels/BackupInProgressVIewModel.cs +++ b/EasyBackup/ViewModels/BackupInProgressVIewModel.cs @@ -16,6 +16,8 @@ namespace EasyBackup.ViewModels { class BackupInProgressViewModel : BaseViewModel { + #region Private Members + private BackupPerformer _backupPerformer; private string _status; private Brush _statusColor; @@ -30,6 +32,8 @@ class BackupInProgressViewModel : BaseViewModel private SoundPlayer _successSoundPlayer; private SoundPlayer _failureSoundPlayer; + #endregion + public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List items, string backupLocation) : base(viewModelChanger) { Items = items; @@ -60,6 +64,8 @@ public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List Items { get; set; } public string BackupLocation { get; set; } @@ -94,6 +100,10 @@ public Brush StatusColor set { _statusColor = value; NotifyPropertyChanged(); } } + #endregion + + #region Running Backup + private void RunBackup() { var redBrush = new SolidColorBrush(Colors.Red); @@ -157,6 +167,10 @@ private void TellUserBackupFailed(string message) FinishButtonTitle = "Finish Backup"; } + #endregion + + #region Backup Performer -- Callbacks + private void _backupPerformer_CalculatedBytesOfItem(FolderFileItem item, ulong bytes) { if (_copyDataToProgressMap.ContainsKey(item)) @@ -206,6 +220,8 @@ private void _backupPerformer_BackupFailed(Exception e) StatusColor = new SolidColorBrush(Colors.Red); } + #endregion + public ICommand CancelBackup { get { return new RelayCommand(PopToSetupView); } @@ -217,6 +233,11 @@ private async void PopToSetupView() { await _backupPerformer.CancelAsync(); } + _backupPerformer.StartedCopyingItem -= _backupPerformer_StartedCopyingItem; + _backupPerformer.FinishedCopyingItem -= _backupPerformer_FinishedCopyingItem; + _backupPerformer.CopiedBytesOfItem -= _backupPerformer_CopiedBytesOfItem; + _backupPerformer.BackupFailed -= _backupPerformer_BackupFailed; + _backupPerformer.CalculatedBytesOfItem -= _backupPerformer_CalculatedBytesOfItem; PopViewModel(); } } diff --git a/EasyBackup/ViewModels/MainWindowViewModel.cs b/EasyBackup/ViewModels/MainWindowViewModel.cs index f0c15c9..dad6ab8 100644 --- a/EasyBackup/ViewModels/MainWindowViewModel.cs +++ b/EasyBackup/ViewModels/MainWindowViewModel.cs @@ -15,6 +15,14 @@ class MainWindowViewModel : ChangeNotifier, IChangeViewModel public MainWindowViewModel() { + // upgrading settings: https://stackoverflow.com/a/534335 + if (Properties.Settings.Default.UpgradeRequired) + { + Properties.Settings.Default.Upgrade(); + Properties.Settings.Default.UpgradeRequired = false; + Properties.Settings.Default.Save(); + } + _viewModels = new Stack(); var initialViewModel = new SetupBackupViewModel(this); _viewModels.Push(initialViewModel); diff --git a/EasyBackup/ViewModels/SetupBackupViewModel.cs b/EasyBackup/ViewModels/SetupBackupViewModel.cs index da15f6c..fab020e 100644 --- a/EasyBackup/ViewModels/SetupBackupViewModel.cs +++ b/EasyBackup/ViewModels/SetupBackupViewModel.cs @@ -20,6 +20,9 @@ namespace EasyBackup.ViewModels { class SetupBackupViewModel : BaseViewModel, IDropTarget { + + #region Private Members + private ObservableCollection _items; private FolderFileItem _selectedItem; private string _backupLocation; @@ -32,20 +35,17 @@ class SetupBackupViewModel : BaseViewModel, IDropTarget private bool _isCancelCheckBackupSizeEnabled; private BackupPerformer _backupSizeChecker; + #endregion + public SetupBackupViewModel(IChangeViewModel viewModelChanger) : base(viewModelChanger) { Items = new ObservableCollection(); - // upgrading settings: https://stackoverflow.com/a/534335 - if (Properties.Settings.Default.UpgradeRequired) - { - Properties.Settings.Default.Upgrade(); - Properties.Settings.Default.UpgradeRequired = false; - Properties.Settings.Default.Save(); - } LoadBackupTemplate(Properties.Settings.Default.LastUsedBackupTemplatePath); IsCheckBackupSizeStatusVisible = false; } + #region Properties + public ObservableCollection Items { get { return _items; } @@ -117,11 +117,67 @@ public bool PlaysSoundsOnComplete } } + #endregion + + #region ICommands + public ICommand AddFolder { get { return new RelayCommand(ChooseFolder); } } + public ICommand AddFile + { + get { return new RelayCommand(ChooseFile); } + } + + public ICommand RemoveItem + { + get { return new RelayCommand(list => RemoveItemFromList(list)); } + } + + public ICommand SaveTemplate + { + get { return new RelayCommand(SaveItemsToDisk); } + } + + public ICommand LoadTemplate + { + get { return new RelayCommand(LoadItemsFromDisk); } + } + + public ICommand ChooseBackupLocation + { + get { return new RelayCommand(PickBackupFolder); } + } + + public ICommand PerformBackup + { + get { return new RelayCommand(StartBackup); } + } + + public ICommand CheckBackupSize + { + get { return new RelayCommand(ScanBackupAndCheckSize); } + } + + public ICommand ShowAboutWindow + { + get { return new RelayCommand(ShowAboutWindowDialog); } + } + + public ICommand CancelCheckingBackupSize + { + get { return new RelayCommand(StopScanningBackupSize); } + } + + public ICommand RemoveAllItems + { + get { return new RelayCommand(CheckAndRemoveAllItems); } + } + + #endregion + private void ChooseFolder() { var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog(); @@ -132,11 +188,6 @@ private void ChooseFolder() } } - public ICommand AddFile - { - get { return new RelayCommand(ChooseFile); } - } - private void ChooseFile() { var dialog = new Ookii.Dialogs.Wpf.VistaOpenFileDialog(); @@ -163,11 +214,6 @@ private void AddPath(string path) IsCheckBackupSizeStatusVisible = false; } - public ICommand RemoveItem - { - get { return new RelayCommand(list => RemoveItemFromList(list)); } - } - private void RemoveItemFromList(object items) { if (items != null) @@ -182,11 +228,6 @@ private void RemoveItemFromList(object items) } } - public ICommand SaveTemplate - { - get { return new RelayCommand(SaveItemsToDisk); } - } - private void SaveItemsToDisk() { var saveFileDialog = new Ookii.Dialogs.Wpf.VistaSaveFileDialog(); @@ -194,7 +235,7 @@ private void SaveItemsToDisk() saveFileDialog.Filter = "Easy Backup Files | *.ebf"; saveFileDialog.DefaultExt = "ebf"; saveFileDialog.OverwritePrompt = true; - saveFileDialog.Title = "Choose save location"; + saveFileDialog.Title = "Choose Save Location"; if (saveFileDialog.ShowDialog(Application.Current.MainWindow).GetValueOrDefault()) { var backupTemplate = new BackupTemplate() { Paths = Items.ToList(), BackupLocation = BackupLocation }; @@ -204,11 +245,6 @@ private void SaveItemsToDisk() } } - public ICommand LoadTemplate - { - get { return new RelayCommand(LoadItemsFromDisk); } - } - private void LoadItemsFromDisk() { var openFileDialog = new Ookii.Dialogs.Wpf.VistaOpenFileDialog(); @@ -243,11 +279,6 @@ private void LoadBackupTemplate(string path) } } - public ICommand ChooseBackupLocation - { - get { return new RelayCommand(PickBackupFolder); } - } - private void PickBackupFolder() { var dialog = new Ookii.Dialogs.Wpf.VistaFolderBrowserDialog(); @@ -258,21 +289,11 @@ private void PickBackupFolder() } } - public ICommand PerformBackup - { - get { return new RelayCommand(StartBackup); } - } - private void StartBackup() { PushViewModel(new BackupInProgressViewModel(ViewModelChanger, Items.ToList(), BackupLocation)); } - public ICommand ShowAboutWindow - { - get { return new RelayCommand(ShowAboutWindowDialog); } - } - private void ShowAboutWindowDialog() { var aboutWindow = new AboutWindow(); @@ -280,11 +301,6 @@ private void ShowAboutWindowDialog() aboutWindow.Show(); } - public ICommand CheckBackupSize - { - get { return new RelayCommand(ScanBackupAndCheckSize); } - } - private async void ScanBackupAndCheckSize() { IsCheckingBackupSize = true; @@ -346,31 +362,23 @@ private void BackupPerformer_CalculatedBytesOfItem(FolderFileItem item, ulong by _totalBackupSize += bytes; } - public ICommand CancelCheckingBackupSize - { - get { return new RelayCommand(StopScanningBackupSize); } - } - private void StopScanningBackupSize() { _backupSizeChecker.Cancel(); IsCancelCheckBackupSizeEnabled = true; } - public ICommand RemoveAllItems - { - get { return new RelayCommand(CheckAndRemoveAllItems); } - } - private async void CheckAndRemoveAllItems() { var result = await DialogCoordinator.ShowMessageAsync(this, "Warning!", "Are you sure you want to remove all items?", - MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings() - { - AffirmativeButtonText = "Yes", - NegativeButtonText = "No", + MessageDialogStyle.AffirmativeAndNegative, + new MetroDialogSettings() + { + AffirmativeButtonText = "Yes", + NegativeButtonText = "No", ColorScheme = MetroDialogColorScheme.Theme - }); + } + ); if (result == MessageDialogResult.Affirmative) { Items.Clear();