comparison SilverlightGlimpse/SilverlightValidation/ViewModels/UserViewModel.cs @ 78:dd6bcd2535b6

Working version
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 22:43:53 +0100
parents a0bcd783e612
children
comparison
equal deleted inserted replaced
77:86ed4919b126 78:dd6bcd2535b6
1 using System; 1 using System;
2 using System.ComponentModel;
3 using System.Linq; 2 using System.Linq;
4 using System.Windows;
5 using System.Windows.Input;
6 using FluentValidation; 3 using FluentValidation;
7 using SilverlightValidation.Interfaces; 4 using SilverlightValidation.Interfaces;
8 using SilverlightValidation.Validators; 5 using SilverlightValidation.Validators;
9 using SilverlightValidation.Models; 6 using SilverlightValidation.Models;
10 using SilverlightValidation.Commands;
11 using GalaSoft.MvvmLight.Messaging;
12 using SilverlightValidation.Messages;
13 7
14 namespace SilverlightValidation.ViewModels 8 namespace SilverlightValidation.ViewModels
15 { 9 {
16 public class UserViewModel : ViewModelBase, IUserModel, IEditableObject 10 public class UserViewModel : ViewModelBase, IUserModel
17 { 11 {
18 #region Fields 12 #region Fields
19 13
20 private readonly UserModelValidator _validator; 14 private readonly UserModelValidator _validator;
21 private UserModel _data; 15 private UserModel _data;
22 private UserModel _backup;
23 16
24 #endregion 17 #endregion
25 18
26 #region Constructor 19 #region Constructor
27 20
28 public UserViewModel(UserModel model, UserModelValidator validator) 21 public UserViewModel(UserModel model, UserModelValidator validator)
29 { 22 {
30 _validator = validator; 23 _validator = validator;
31 _data = model; 24 _data = model;
32 _backup = model.Clone();
33
34 OkCommand = new RelayCommand(OkCommandExecute);
35 CancelCommand = new RelayCommand(CancelCommandExecute);
36 }
37
38 #endregion
39
40 #region Methods
41
42 private void SetProperties(IUserModel source)
43 {
44 _data.Username = source.Username;
45 _data.Password = source.Password;
46 _data.Email = source.Email;
47 _data.DateOfBirth = source.DateOfBirth;
48 _data.Description = source.Description;
49 } 25 }
50 26
51 #endregion 27 #endregion
52 28
53 #region Properties 29 #region Properties
60 { 36 {
61 if (_data.Username != value) 37 if (_data.Username != value)
62 { 38 {
63 _data.Username = value; 39 _data.Username = value;
64 RaisePropertyChanged(UsernameProperty); 40 RaisePropertyChanged(UsernameProperty);
65 IsChanged = true;
66 } 41 }
67 42
68 ClearError(UsernameProperty); 43 ClearError(UsernameProperty);
69 var validationResult = _validator.Validate(this, UsernameProperty); 44 var validationResult = _validator.Validate(this, UsernameProperty);
70 if (!validationResult.IsValid) 45 if (!validationResult.IsValid)
80 { 55 {
81 if (_data.Password != value) 56 if (_data.Password != value)
82 { 57 {
83 _data.Password = value; 58 _data.Password = value;
84 RaisePropertyChanged(PasswordProperty); 59 RaisePropertyChanged(PasswordProperty);
85 IsChanged = true;
86 } 60 }
87 61
88 ClearError(PasswordProperty); 62 ClearError(PasswordProperty);
89 var validationResult = _validator.Validate(this, PasswordProperty); 63 var validationResult = _validator.Validate(this, PasswordProperty);
90 if (!validationResult.IsValid) 64 if (!validationResult.IsValid)
100 { 74 {
101 if (_data.Email != value) 75 if (_data.Email != value)
102 { 76 {
103 _data.Email = value; 77 _data.Email = value;
104 RaisePropertyChanged(EmailProperty); 78 RaisePropertyChanged(EmailProperty);
105 IsChanged = true;
106 } 79 }
107 80
108 ClearError(EmailProperty); 81 ClearError(EmailProperty);
109 var validationResult = _validator.Validate(this, EmailProperty); 82 var validationResult = _validator.Validate(this, EmailProperty);
110 if (!validationResult.IsValid) 83 if (!validationResult.IsValid)
120 { 93 {
121 if (_data.DateOfBirth != value) 94 if (_data.DateOfBirth != value)
122 { 95 {
123 _data.DateOfBirth = value; 96 _data.DateOfBirth = value;
124 RaisePropertyChanged(DateOfBirthProperty); 97 RaisePropertyChanged(DateOfBirthProperty);
125 IsChanged = true;
126 } 98 }
127 99
128 ClearError(DateOfBirthProperty); 100 ClearError(DateOfBirthProperty);
129 var validationResult = _validator.Validate(this, DateOfBirthProperty); 101 var validationResult = _validator.Validate(this, DateOfBirthProperty);
130 if (!validationResult.IsValid) 102 if (!validationResult.IsValid)
140 { 112 {
141 if (_data.Description != value) 113 if (_data.Description != value)
142 { 114 {
143 _data.Description = value; 115 _data.Description = value;
144 RaisePropertyChanged(DescriptionProperty); 116 RaisePropertyChanged(DescriptionProperty);
145 IsChanged = true;
146 } 117 }
147 118
148 ClearError(DescriptionProperty); 119 ClearError(DescriptionProperty);
149 var validationResult = _validator.Validate(this, DescriptionProperty); 120 var validationResult = _validator.Validate(this, DescriptionProperty);
150 if (!validationResult.IsValid) 121 if (!validationResult.IsValid)
151 validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage)); 122 validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage));
152 } 123 }
153 } 124 }
154 125
155 #endregion 126 #endregion
156
157 #region Commands
158
159 public ICommand OkCommand { get; set; }
160 public ICommand CancelCommand { get; set; }
161
162 private void OkCommandExecute(object obj)
163 {
164 RefreshToViewErrors();
165
166 if (IsChanged && !HasErrors)
167 {
168 // save here
169 Messenger.Default.Send<UserViewResponseMessage>(
170 new UserViewResponseMessage() { UserViewModel = this });
171 }
172 }
173
174 // in case user hasn't touched the form
175 private void RefreshToViewErrors()
176 {
177 Username = _data.Username;
178 Password = _data.Password;
179 Email = _data.Email;
180 DateOfBirth = _data.DateOfBirth;
181 }
182
183 private void CancelCommandExecute(object obj)
184 {
185 Messenger.Default.Send<UserViewResponseMessage>(
186 new UserViewResponseMessage() { UserViewModel = null });
187 }
188
189 #endregion
190
191 private void ResetFormData()
192 {
193 SetProperties(_backup);
194 ClearAllErrors();
195 IsChanged = false;
196 }
197
198 public bool IsChanged { get; private set; }
199
200 #region IEditableObject for datagrid
201
202 private bool inEdit;
203 public void BeginEdit()
204 {
205 if (inEdit) return;
206 inEdit = true;
207 }
208
209 public void CancelEdit()
210 {
211 if (!inEdit) return;
212 inEdit = false;
213 ResetFormData();
214 }
215
216 public void EndEdit()
217 {
218 if (!inEdit) return;
219 }
220
221 #endregion
222 } 127 }
223 } 128 }