Commit dc521b3

Jason Lepp <jlepp@arcresources.com>
2010-11-05 16:22:58
Update synchronizer per lagattack's suggestions
1 parent 5d4fcd1
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
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)
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;
             }
         }
 
src/MVPtoMVVM.mvvm/MainWindow.xaml
@@ -4,7 +4,6 @@
         Title="MainWindow" Height="350" Width="525">
     <DockPanel LastChildFill="False">
         <DockPanel.Resources>
-            <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
             <Style x:Key="ValidationStyle" TargetType="Control">
         <Style.Triggers>
             <Trigger Property="Validation.HasError" Value="true">
@@ -19,7 +18,7 @@
             <ListView.ItemTemplate>
                 <DataTemplate>
                 <StackPanel Orientation="Horizontal">
-                    <Image Source="alert.png" Visibility="{Binding Path=ShowDueSoonAlert, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"/>
+                    <Image Source="alert.png" Visibility="{Binding Path=ShowDueSoonAlert, Mode=OneWay}"/>
                     <TextBox Width="200" Text="{Binding Path=Description, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ResourceKey=ValidationStyle}" />
                     <DatePicker SelectedDate="{Binding Path=DueDate, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource ResourceKey=ValidationStyle}" />
                     <Button Content="Save" Command="{Binding Path=SaveCommand}"></Button>