Skip to content

Commit

Permalink
A little code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Deadpikle committed Feb 6, 2019
1 parent f7b0e96 commit 8aba2d1
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 63 deletions.
21 changes: 21 additions & 0 deletions EasyBackup/ViewModels/BackupInProgressVIewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace EasyBackup.ViewModels
{
class BackupInProgressViewModel : BaseViewModel
{
#region Private Members

private BackupPerformer _backupPerformer;
private string _status;
private Brush _statusColor;
Expand All @@ -30,6 +32,8 @@ class BackupInProgressViewModel : BaseViewModel
private SoundPlayer _successSoundPlayer;
private SoundPlayer _failureSoundPlayer;

#endregion

public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List<FolderFileItem> items, string backupLocation) : base(viewModelChanger)
{
Items = items;
Expand Down Expand Up @@ -60,6 +64,8 @@ public BackupInProgressViewModel(IChangeViewModel viewModelChanger, List<FolderF
RunBackup();
}

#region Properties

public List<FolderFileItem> Items { get; set; }

public string BackupLocation { get; set; }
Expand Down Expand Up @@ -94,6 +100,10 @@ public Brush StatusColor
set { _statusColor = value; NotifyPropertyChanged(); }
}

#endregion

#region Running Backup

private void RunBackup()
{
var redBrush = new SolidColorBrush(Colors.Red);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -206,6 +220,8 @@ private void _backupPerformer_BackupFailed(Exception e)
StatusColor = new SolidColorBrush(Colors.Red);
}

#endregion

public ICommand CancelBackup
{
get { return new RelayCommand(PopToSetupView); }
Expand All @@ -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();
}
}
Expand Down
8 changes: 8 additions & 0 deletions EasyBackup/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BaseViewModel>();
var initialViewModel = new SetupBackupViewModel(this);
_viewModels.Push(initialViewModel);
Expand Down
134 changes: 71 additions & 63 deletions EasyBackup/ViewModels/SetupBackupViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ namespace EasyBackup.ViewModels
{
class SetupBackupViewModel : BaseViewModel, IDropTarget
{

#region Private Members

private ObservableCollection<FolderFileItem> _items;
private FolderFileItem _selectedItem;
private string _backupLocation;
Expand All @@ -32,20 +35,17 @@ class SetupBackupViewModel : BaseViewModel, IDropTarget
private bool _isCancelCheckBackupSizeEnabled;
private BackupPerformer _backupSizeChecker;

#endregion

public SetupBackupViewModel(IChangeViewModel viewModelChanger) : base(viewModelChanger)
{
Items = new ObservableCollection<FolderFileItem>();
// 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<FolderFileItem> Items
{
get { return _items; }
Expand Down Expand Up @@ -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<object>(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();
Expand All @@ -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();
Expand All @@ -163,11 +214,6 @@ private void AddPath(string path)
IsCheckBackupSizeStatusVisible = false;
}

public ICommand RemoveItem
{
get { return new RelayCommand<object>(list => RemoveItemFromList(list)); }
}

private void RemoveItemFromList(object items)
{
if (items != null)
Expand All @@ -182,19 +228,14 @@ private void RemoveItemFromList(object items)
}
}

public ICommand SaveTemplate
{
get { return new RelayCommand(SaveItemsToDisk); }
}

private void SaveItemsToDisk()
{
var saveFileDialog = new Ookii.Dialogs.Wpf.VistaSaveFileDialog();
saveFileDialog.AddExtension = true;
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 };
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -258,33 +289,18 @@ 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();
aboutWindow.Owner = Application.Current.MainWindow;
aboutWindow.Show();
}

public ICommand CheckBackupSize
{
get { return new RelayCommand(ScanBackupAndCheckSize); }
}

private async void ScanBackupAndCheckSize()
{
IsCheckingBackupSize = true;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 8aba2d1

Please sign in to comment.