-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathProfileViewModel.cs
346 lines (308 loc) · 10.9 KB
/
ProfileViewModel.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.Specialized;
using System.Linq;
using System.Threading.Tasks;
using PhotoSharingApp.Portable.DataContracts;
using PhotoSharingApp.Universal.Commands;
using PhotoSharingApp.Universal.ComponentModel;
using PhotoSharingApp.Universal.Facades;
using PhotoSharingApp.Universal.Models;
using PhotoSharingApp.Universal.Services;
using PhotoSharingApp.Universal.Views;
using Windows.UI.Xaml;
using Windows.ApplicationModel.Resources;
namespace PhotoSharingApp.Universal.ViewModels
{
/// <summary>
/// ViewModel for Profile view.
/// </summary>
public class ProfileViewModel : ViewModelBase
{
private bool _arePhotosEmpty;
private User _user;
private readonly IDialogService _dialogService;
private int _goldBalance;
private bool _isBusy;
private readonly INavigationFacade _navigationFacade;
private int _photosCount;
private readonly IPhotoService _photoService;
/// <summary>
/// Initializes a new instance of the <see cref="ProfileViewModel" /> class.
/// </summary>
/// <param name="navigationFacade">The navigation facade.</param>
/// <param name="photoService">The photo service.</param>
/// <param name="dialogService">The dialog service.</param>
public ProfileViewModel(INavigationFacade navigationFacade, IPhotoService photoService,
IDialogService dialogService)
{
_navigationFacade = navigationFacade;
_photoService = photoService;
_dialogService = dialogService;
Photos = new IncrementalLoadingCollection<Photo>(s =>
{
Func<Task<PagedResponse<Photo>>> f = async () =>
{
if (IsShowingCurrentUser)
{
var stream = await photoService.GetPhotosForCurrentUser(s);
return stream;
}
return await photoService.GetPhotosForUser(User, s);
};
return f();
}, async () => await _dialogService.ShowGenericServiceErrorNotification(),
OnPhotosLoadingFinished);
// Photos collection is being loaded asynchronously, so we need to
// watch it to see if the user has any pictures uploaded already.
Photos.CollectionChanged += PhotosCollectionChanged;
// Initialize commands
PhotoSelectedCommand = new RelayCommand<Photo>(OnPhotoSelected);
DeletePhotoCommand = new RelayCommand<Photo>(OnDeletePhoto);
SetProfilePhotoCommand = new RelayCommand<Photo>(OnSetProfilePhoto);
// Before pictures are retrieved from the service,
// we want to prevent an initial notification showing up
// that the user has no pictures.
ArePhotosEmpty = false;
}
/// <summary>
/// loads the page header caption string from the resource file
/// </summary>
public string PageHeaderText
{
get
{
var resourceLoader = ResourceLoader.GetForCurrentView();
return resourceLoader.GetString("ProfilePage_PageHeader");
}
}
/// <summary>
/// Returns true, if the user is shown that is signed in. Otherwise, false.
/// </summary>
public bool IsShowingCurrentUser => User == AppEnvironment.Instance.CurrentUser;
/// <summary>
/// Gets or sets whether photos are available.
/// </summary>
public bool ArePhotosEmpty
{
get { return _arePhotosEmpty; }
set
{
if (value != _arePhotosEmpty)
{
_arePhotosEmpty = value;
NotifyPropertyChanged(nameof(ArePhotosEmpty));
}
}
}
/// <summary>
/// Gets the current user;
/// </summary>
public User User
{
get { return _user; }
private set
{
if (value != _user)
{
_user = value;
NotifyPropertyChanged(nameof(User));
}
}
}
/// <summary>
/// Gets the delete photo command.
/// </summary>
public RelayCommand<Photo> DeletePhotoCommand { get; private set; }
/// <summary>
/// Gets the gold balance
/// </summary>
public int GoldBalance
{
get { return _goldBalance; }
private set
{
if (value != _goldBalance)
{
_goldBalance = value;
NotifyPropertyChanged(nameof(GoldBalance));
}
}
}
/// <summary>
/// Gets or sets a value indicating whether this instance is busy.
/// </summary>
public bool IsBusy
{
get { return _isBusy; }
set
{
if (value != _isBusy)
{
_isBusy = value;
NotifyPropertyChanged(nameof(IsBusy));
}
}
}
/// <summary>
/// Gets or sets the photos.
/// </summary>
public IncrementalLoadingCollection<Photo> Photos { get; set; }
/// <summary>
/// Gets or sets the photos count.
/// </summary>
public int PhotosCount
{
get { return _photosCount; }
set
{
if (value != _photosCount)
{
_photosCount = value;
NotifyPropertyChanged(nameof(PhotosCount));
}
}
}
/// <summary>
/// Gets photo selected command.
/// </summary>
public RelayCommand<Photo> PhotoSelectedCommand { get; private set; }
/// <summary>
/// Gets the delete photo command.
/// </summary>
public RelayCommand<Photo> SetProfilePhotoCommand { get; private set; }
/// <summary>
/// Loads the state.
/// </summary>
public override async Task LoadState()
{
await base.LoadState();
User = AppEnvironment.Instance.CurrentUser;
if (User != null)
{
RunCounterAnimations();
}
}
/// <summary>
/// Loads the state.
/// </summary>
/// <param name="viewModelArgs">The arguments.</param>
public async Task LoadState(ProfileViewModelArgs viewModelArgs)
{
await base.LoadState();
User = viewModelArgs.User;
RunCounterAnimations();
}
private async void OnDeletePhoto(Photo photo)
{
if (AppEnvironment.Instance.CurrentUser.ProfilePictureId == photo.Id)
{
await _dialogService.ShowNotification("DeleteProfilePicture_Message", "DeleteProfilePicture_Title");
return;
}
try
{
IsBusy = true;
await _photoService.DeletePhoto(photo);
await OnRefresh();
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
/// <summary>
/// Action to take when a photo has been selected
/// </summary>
/// <param name="photo">the photo.</param>
private void OnPhotoSelected(Photo photo)
{
_navigationFacade.NavigateToPhotoDetailsView(photo);
}
private void OnPhotosLoadingFinished()
{
ArePhotosEmpty = !Photos.Any();
}
private async Task OnRefresh()
{
await Photos.Refresh();
PhotosCount = Photos.Count;
}
private async void OnSetProfilePhoto(Photo photo)
{
try
{
IsBusy = true;
await _photoService.UpdateUserProfilePhoto(photo);
User = AppEnvironment.Instance.CurrentUser;
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
private void PhotosCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
ArePhotosEmpty = !Photos.Any();
}
private void RunCounterAnimations()
{
var dispatcherTimer = new DispatcherTimer { Interval = TimeSpan.FromMilliseconds(20) };
dispatcherTimer.Tick += (s, e) =>
{
var needToProceed = false;
// Photos counter animation
if (PhotosCount < Photos.Count)
{
PhotosCount++;
needToProceed = true;
}
// Gold counter animation
if (GoldBalance < User.GoldBalance)
{
needToProceed = true;
GoldBalance += 1;
if (GoldBalance > User.GoldBalance)
{
GoldBalance = User.GoldBalance;
}
}
if (!needToProceed)
{
dispatcherTimer.Stop();
}
};
dispatcherTimer.Start();
}
}
}