comparison SilverlightGlimpse/SilverlightValidation/ViewModels/UserViewModel.cs @ 69:a0bcd783e612

Latest work
author Steven Hollidge <stevenhollidge@hotmail.com>
date Mon, 23 Apr 2012 11:06:10 +0100
parents
children dd6bcd2535b6
comparison
equal deleted inserted replaced
68:81337ebf885a 69:a0bcd783e612
1 using System;
2 using System.ComponentModel;
3 using System.Linq;
4 using System.Windows;
5 using System.Windows.Input;
6 using FluentValidation;
7 using SilverlightValidation.Interfaces;
8 using SilverlightValidation.Validators;
9 using SilverlightValidation.Models;
10 using SilverlightValidation.Commands;
11 using GalaSoft.MvvmLight.Messaging;
12 using SilverlightValidation.Messages;
13
14 namespace SilverlightValidation.ViewModels
15 {
16 public class UserViewModel : ViewModelBase, IUserModel, IEditableObject
17 {
18 #region Fields
19
20 private readonly UserModelValidator _validator;
21 private UserModel _data;
22 private UserModel _backup;
23
24 #endregion
25
26 #region Constructor
27
28 public UserViewModel(UserModel model, UserModelValidator validator)
29 {
30 _validator = validator;
31 _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 }
50
51 #endregion
52
53 #region Properties
54
55 private const string UsernameProperty = "Username";
56 public string Username
57 {
58 get { return _data.Username; }
59 set
60 {
61 if (_data.Username != value)
62 {
63 _data.Username = value;
64 RaisePropertyChanged(UsernameProperty);
65 IsChanged = true;
66 }
67
68 ClearError(UsernameProperty);
69 var validationResult = _validator.Validate(this, UsernameProperty);
70 if (!validationResult.IsValid)
71 validationResult.Errors.ToList().ForEach(x => SetError(UsernameProperty, x.ErrorMessage));
72 }
73 }
74
75 private const string PasswordProperty = "Password";
76 public string Password
77 {
78 get { return _data.Password; }
79 set
80 {
81 if (_data.Password != value)
82 {
83 _data.Password = value;
84 RaisePropertyChanged(PasswordProperty);
85 IsChanged = true;
86 }
87
88 ClearError(PasswordProperty);
89 var validationResult = _validator.Validate(this, PasswordProperty);
90 if (!validationResult.IsValid)
91 validationResult.Errors.ToList().ForEach(x => SetError(PasswordProperty, x.ErrorMessage));
92 }
93 }
94
95 private const string EmailProperty = "Email";
96 public string Email
97 {
98 get { return _data.Email; }
99 set
100 {
101 if (_data.Email != value)
102 {
103 _data.Email = value;
104 RaisePropertyChanged(EmailProperty);
105 IsChanged = true;
106 }
107
108 ClearError(EmailProperty);
109 var validationResult = _validator.Validate(this, EmailProperty);
110 if (!validationResult.IsValid)
111 validationResult.Errors.ToList().ForEach(x => SetError(EmailProperty, x.ErrorMessage));
112 }
113 }
114
115 private const string DateOfBirthProperty = "DateOfBirth";
116 public DateTime? DateOfBirth
117 {
118 get { return _data.DateOfBirth; }
119 set
120 {
121 if (_data.DateOfBirth != value)
122 {
123 _data.DateOfBirth = value;
124 RaisePropertyChanged(DateOfBirthProperty);
125 IsChanged = true;
126 }
127
128 ClearError(DateOfBirthProperty);
129 var validationResult = _validator.Validate(this, DateOfBirthProperty);
130 if (!validationResult.IsValid)
131 validationResult.Errors.ToList().ForEach(x => SetError(DateOfBirthProperty, x.ErrorMessage));
132 }
133 }
134
135 private const string DescriptionProperty = "Description";
136 public string Description
137 {
138 get { return _data.Description; }
139 set
140 {
141 if (_data.Description != value)
142 {
143 _data.Description = value;
144 RaisePropertyChanged(DescriptionProperty);
145 IsChanged = true;
146 }
147
148 ClearError(DescriptionProperty);
149 var validationResult = _validator.Validate(this, DescriptionProperty);
150 if (!validationResult.IsValid)
151 validationResult.Errors.ToList().ForEach(x => SetError(DescriptionProperty, x.ErrorMessage));
152 }
153 }
154
155 #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 }
223 }