Skip to content

Commit

Permalink
Adding "backup" functionality whenever the Clear command is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldaw committed Nov 24, 2015
1 parent 8885512 commit b88a38e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ManVan/Commands/ClearCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void Execute(object parameter)

if (result == ConfirmResult.Affirmative)
{
LocalDataService.Backup(_viewModel.Entries);
_viewModel.Entries = new ObservableCollection<EntryViewModel>();
LocalDataService.Save(_viewModel.Entries);
_viewModel.Refresh();
Expand Down
20 changes: 20 additions & 0 deletions src/ManVan/Services/LocalDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ public class LocalDataService
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
+ "\\ManVan\\entries.manvan";

public static void Backup(IEnumerable<EntryViewModel> entries)
{
var xs = new XmlSerializer(entries.GetType());

var path = StoragePath;
var file = new FileInfo(path);
if (file.DirectoryName == null)
throw new Exception("DirectoryName should not be null");
if (!Directory.Exists(file.DirectoryName))
Directory.CreateDirectory(file.DirectoryName);
var newFileName = "entries_" + DateTime.Now.ToString(
"yyyyMMddhhmmss") + ".manvan";
var newFilePath = Path.Combine(
file.DirectoryName, newFileName);
using (TextWriter writer = new StreamWriter(newFilePath))
{
xs.Serialize(writer, entries);
}
}

public static void Save(IEnumerable<EntryViewModel> entries)
{
var xs = new XmlSerializer(entries.GetType());
Expand Down

0 comments on commit b88a38e

Please sign in to comment.