summaryrefslogtreecommitdiff
path: root/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs')
-rw-r--r--src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs42
1 files changed, 39 insertions, 3 deletions
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
index f84b4c2..90ab367 100644
--- a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
+++ b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
@@ -1,12 +1,14 @@
using System;
+using System.Collections.Generic;
using System.ComponentModel;
-using System.Windows.Input;
+using System.Text;
using MVPtoMVVM.domain;
using MVPtoMVVM.repositories;
+using System.Linq;
namespace MVPtoMVVM.mvvm.viewmodels
{
- public class TodoItemViewModel : INotifyPropertyChanged
+ public class TodoItemViewModel : INotifyPropertyChanged, IDataErrorInfo
{
private readonly ITodoItemRepository todoItemRepository;
private Synchronizer<TodoItemViewModel> updater;
@@ -17,6 +19,11 @@ namespace MVPtoMVVM.mvvm.viewmodels
SaveCommand = new SimpleCommand(Save, CanSave);
DeleteCommand = new SimpleCommand(Delete);
updater = new Synchronizer<TodoItemViewModel>(PropertyChanged);
+ validations = new Dictionary<string, IValidation>
+ {
+ {"Description", new Validation(() => !string.IsNullOrEmpty(Description), "Cannot have an empty description.")},
+ {"DueDate", new Validation(() => DueDate >= DateTime.Now, "Due Date must occur on or after today.")}
+ };
}
private void Delete()
@@ -28,7 +35,7 @@ namespace MVPtoMVVM.mvvm.viewmodels
private bool CanSave()
{
- return !string.IsNullOrEmpty(Description) && DueDate >= DateTime.Today;
+ return validations.Values.All(x => x.IsValid);
}
private void Save()
@@ -54,6 +61,8 @@ namespace MVPtoMVVM.mvvm.viewmodels
}
private DateTime dueDate;
+ private IDictionary<string, IValidation> validations;
+
public DateTime DueDate
{
get { return dueDate; }
@@ -66,5 +75,32 @@ namespace MVPtoMVVM.mvvm.viewmodels
public IObservableCommand SaveCommand { get; set; }
public IObservableCommand DeleteCommand { get; set; }
public MainWindowViewModel Parent { get; set; }
+
+ public string this[string columnName]
+ {
+ get
+ {
+ var validation = validations[columnName];
+ return validation.IsValid ? null : validation.Message;
+ }
+ }
+
+ public string Error
+ {
+ get { return BuildErrors(); }
+ }
+
+ private string BuildErrors()
+ {
+ var builder = new StringBuilder();
+ foreach (var validation in validations.Values)
+ {
+ if(!validation.IsValid)
+ {
+ builder.AppendLine(validation.Message);
+ }
+ }
+ return builder.ToString();
+ }
}
} \ No newline at end of file