-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathCustomerViewModel.cs
346 lines (307 loc) · 10.3 KB
/
CustomerViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ---------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// The MIT License (MIT)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// ---------------------------------------------------------------------------------
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Threading.Tasks;
using Microsoft.UI.Dispatching;
using CommunityToolkit.WinUI;
using Contoso.Models;
namespace Contoso.App.ViewModels
{
/// <summary>
/// Provides a bindable wrapper for the Customer model class, encapsulating various services for access by the UI.
/// </summary>
public class CustomerViewModel : BindableBase, IEditableObject
{
private DispatcherQueue dispatcherQueue = DispatcherQueue.GetForCurrentThread();
/// <summary>
/// Initializes a new instance of the CustomerViewModel class that wraps a Customer object.
/// </summary>
public CustomerViewModel(Customer model = null) => Model = model ?? new Customer();
private Customer _model;
/// <summary>
/// Gets or sets the underlying Customer object.
/// </summary>
public Customer Model
{
get => _model;
set
{
if (_model != value)
{
_model = value;
RefreshOrders();
// Raise the PropertyChanged event for all properties.
OnPropertyChanged(string.Empty);
}
}
}
/// <summary>
/// Gets or sets the customer's first name.
/// </summary>
public string FirstName
{
get => Model.FirstName;
set
{
if (value != Model.FirstName)
{
Model.FirstName = value;
IsModified = true;
OnPropertyChanged();
OnPropertyChanged(nameof(Name));
}
}
}
/// <summary>
/// Gets or sets the customer's last name.
/// </summary>
public string LastName
{
get => Model.LastName;
set
{
if (value != Model.LastName)
{
Model.LastName = value;
IsModified = true;
OnPropertyChanged();
OnPropertyChanged(nameof(Name));
}
}
}
/// <summary>
/// Gets the customers full (first + last) name.
/// </summary>
public string Name => IsNewCustomer && string.IsNullOrEmpty(FirstName)
&& string.IsNullOrEmpty(LastName) ? "New customer" : $"{FirstName} {LastName}";
/// <summary>
/// Gets or sets the customer's address.
/// </summary>
public string Address
{
get => Model.Address;
set
{
if (value != Model.Address)
{
Model.Address = value;
IsModified = true;
OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets the customer's company.
/// </summary>
public string Company
{
get => Model.Company;
set
{
if (value != Model.Company)
{
Model.Company = value;
IsModified = true;
OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets the customer's phone number.
/// </summary>
public string Phone
{
get => Model.Phone;
set
{
if (value != Model.Phone)
{
Model.Phone = value;
IsModified = true;
OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets the customer's email.
/// </summary>
public string Email
{
get => Model.Email;
set
{
if (value != Model.Email)
{
Model.Email = value;
IsModified = true;
OnPropertyChanged();
}
}
}
/// <summary>
/// Gets or sets a value that indicates whether the underlying model has been modified.
/// </summary>
/// <remarks>
/// Used when sync'ing with the server to reduce load and only upload the models that have changed.
/// </remarks>
public bool IsModified { get; set; }
/// <summary>
/// Gets the collection of the customer's orders.
/// </summary>
public ObservableCollection<Order> Orders { get; } = new ObservableCollection<Order>();
private Order _selectedOrder;
/// <summary>
/// Gets or sets the currently selected order.
/// </summary>
public Order SelectedOrder
{
get => _selectedOrder;
set => Set(ref _selectedOrder, value);
}
private bool _isLoading;
/// <summary>
/// Gets or sets a value that indicates whether to show a progress bar.
/// </summary>
public bool IsLoading
{
get => _isLoading;
set => Set(ref _isLoading, value);
}
private bool _isNewCustomer;
/// <summary>
/// Gets or sets a value that indicates whether this is a new customer.
/// </summary>
public bool IsNewCustomer
{
get => _isNewCustomer;
set => Set(ref _isNewCustomer, value);
}
private bool _isInEdit = false;
/// <summary>
/// Gets or sets a value that indicates whether the customer data is being edited.
/// </summary>
public bool IsInEdit
{
get => _isInEdit;
set => Set(ref _isInEdit, value);
}
/// <summary>
/// Saves customer data that has been edited.
/// </summary>
public async Task SaveAsync()
{
IsInEdit = false;
IsModified = false;
if (IsNewCustomer)
{
IsNewCustomer = false;
App.ViewModel.Customers.Add(this);
}
await App.Repository.Customers.UpsertAsync(Model);
}
/// <summary>
/// Raised when the user cancels the changes they've made to the customer data.
/// </summary>
public event EventHandler AddNewCustomerCanceled;
/// <summary>
/// Cancels any in progress edits.
/// </summary>
public async Task CancelEditsAsync()
{
if (IsNewCustomer)
{
AddNewCustomerCanceled?.Invoke(this, EventArgs.Empty);
}
else
{
await RevertChangesAsync();
}
}
/// <summary>
/// Discards any edits that have been made, restoring the original values.
/// </summary>
public async Task RevertChangesAsync()
{
IsInEdit = false;
if (IsModified)
{
await RefreshCustomerAsync();
IsModified = false;
}
}
/// <summary>
/// Enables edit mode.
/// </summary>
public void StartEdit() => IsInEdit = true;
/// <summary>
/// Reloads all of the customer data.
/// </summary>
public async Task RefreshCustomerAsync()
{
RefreshOrders();
Model = await App.Repository.Customers.GetAsync(Model.Id);
}
/// <summary>
/// Resets the customer detail fields to the current values.
/// </summary>
public void RefreshOrders() => Task.Run(LoadOrdersAsync);
/// <summary>
/// Loads the order data for the customer.
/// </summary>
public async Task LoadOrdersAsync()
{
await dispatcherQueue.EnqueueAsync(() =>
{
IsLoading = true;
});
var orders = await App.Repository.Orders.GetForCustomerAsync(Model.Id);
await dispatcherQueue.EnqueueAsync(() =>
{
Orders.Clear();
foreach (var order in orders)
{
Orders.Add(order);
}
IsLoading = false;
});
}
/// <summary>
/// Called when a bound DataGrid control causes the customer to enter edit mode.
/// </summary>
public void BeginEdit()
{
// Not used.
}
/// <summary>
/// Called when a bound DataGrid control cancels the edits that have been made to a customer.
/// </summary>
public async void CancelEdit() => await CancelEditsAsync();
/// <summary>
/// Called when a bound DataGrid control commits the edits that have been made to a customer.
/// </summary>
public async void EndEdit() => await SaveAsync();
}
}