summaryrefslogtreecommitdiff
path: root/src/MVPtoMVVM.mvvm/viewmodels
diff options
context:
space:
mode:
authorJason Lepp <jlepp@arcresources.com>2010-11-05 10:22:58 -0600
committerJason Lepp <jlepp@arcresources.com>2010-11-05 10:22:58 -0600
commitdc521b38304846c4058f099cb3aed6efc05d57bb (patch)
treeaea7b0e5a93cc1ddc4c1e1901900063a9136fe14 /src/MVPtoMVVM.mvvm/viewmodels
parent5d4fcd1e2dfd8b673b7bc4af43cab770d2b622a3 (diff)
Update synchronizer per lagattack's suggestionsHEADmaster
Diffstat (limited to 'src/MVPtoMVVM.mvvm/viewmodels')
-rw-r--r--src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs4
-rw-r--r--src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs8
-rw-r--r--src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs19
3 files changed, 19 insertions, 12 deletions
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs b/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs
index e05d305..5ca0d7f 100644
--- a/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs
+++ b/src/MVPtoMVVM.mvvm/viewmodels/MainWindowViewModel.cs
@@ -24,7 +24,7 @@ namespace MVPtoMVVM.mvvm.viewmodels
this.todoItemRepository = todoItemRepository;
AddNewItemCommand = new SimpleCommand(AddNewItem);
CancelChangesCommand = new SimpleCommand(RefreshChanges);
- updater = new Synchronizer<MainWindowViewModel>(PropertyChanged);
+ updater = new Synchronizer<MainWindowViewModel>(() => PropertyChanged);
TodoItems = new ObservableCollection<TodoItemViewModel>();
RefreshChanges();
}
@@ -57,7 +57,7 @@ namespace MVPtoMVVM.mvvm.viewmodels
public void Update(Expression<Func<MainWindowViewModel, object>> property)
{
- updater.Update(property);
+ updater.Update(this, property);
}
}
} \ No newline at end of file
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs b/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs
index 5c4488b..09c6302 100644
--- a/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs
+++ b/src/MVPtoMVVM.mvvm/viewmodels/Synchronizer.cs
@@ -6,16 +6,16 @@ namespace MVPtoMVVM.mvvm.viewmodels
{
public class Synchronizer<T> where T : INotifyPropertyChanged
{
- private readonly PropertyChangedEventHandler eventHandler;
+ private readonly Func<PropertyChangedEventHandler> eventHandler;
- public Synchronizer(PropertyChangedEventHandler eventHandler)
+ public Synchronizer(Func<PropertyChangedEventHandler> eventHandler)
{
this.eventHandler = eventHandler;
}
- public void Update(Expression<Func<T, object>> property)
+ public void Update(T viewModel, Expression<Func<T, object>> property)
{
- eventHandler(null, new PropertyChangedEventArgs(GetPropertyNameFrom(property)));
+ eventHandler()(viewModel, new PropertyChangedEventArgs(GetPropertyNameFrom(property)));
}
string GetPropertyNameFrom(Expression<Func<T, object>> property)
diff --git a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
index cfe1ae8..609f5b9 100644
--- a/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
+++ b/src/MVPtoMVVM.mvvm/viewmodels/ToDoItemViewModel.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
+using System.Linq.Expressions;
+using System.Windows;
using MVPtoMVVM.domain;
using MVPtoMVVM.repositories;
using System.Linq;
@@ -24,7 +26,7 @@ namespace MVPtoMVVM.mvvm.viewmodels
this.todoItemRepository = todoItemRepository;
SaveCommand = new SimpleCommand(Save, CanSave);
DeleteCommand = new SimpleCommand(Delete);
- synchronizer = new Synchronizer<TodoItemViewModel>(PropertyChanged);
+ synchronizer = new Synchronizer<TodoItemViewModel>(() => PropertyChanged);
validations = new Dictionary<string, IValidation>
{
{"Description", new Validation(() => !string.IsNullOrEmpty(Description), "Cannot have an empty description.")},
@@ -60,11 +62,16 @@ namespace MVPtoMVVM.mvvm.viewmodels
{
description = value;
IsDirty = true;
- synchronizer.Update(x => x.Description);
+ Update(x => x.Description);
SaveCommand.Changed();
}
}
+ private void Update(Expression<Func<TodoItemViewModel, object>> func)
+ {
+ synchronizer.Update(this, func);
+ }
+
private DateTime dueDate;
public DateTime DueDate
{
@@ -73,17 +80,17 @@ namespace MVPtoMVVM.mvvm.viewmodels
{
dueDate = value;
IsDirty = true;
- synchronizer.Update(x => x.DueDate);
- synchronizer.Update(x => x.ShowDueSoonAlert);
+ Update(x => x.DueDate);
+ Update(x => x.ShowDueSoonAlert);
SaveCommand.Changed();
}
}
- public bool ShowDueSoonAlert
+ public Visibility ShowDueSoonAlert
{
get
{
- return DueDate <= DateTime.Today.AddDays(1);
+ return DueDate <= DateTime.Today.AddDays(1) ? Visibility.Visible : Visibility.Hidden;
}
}