-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathPhotoDetailsViewModel.cs
More file actions
680 lines (596 loc) · 21.4 KB
/
PhotoDetailsViewModel.cs
File metadata and controls
680 lines (596 loc) · 21.4 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
//-----------------------------------------------------------------------------------
// 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.Linq;
using System.Threading.Tasks;
using PhotoSharingApp.Portable.DataContracts;
using PhotoSharingApp.Universal.Commands;
using PhotoSharingApp.Universal.Extensions;
using PhotoSharingApp.Universal.Facades;
using PhotoSharingApp.Universal.Models;
using PhotoSharingApp.Universal.Services;
using PhotoSharingApp.Universal.Views;
namespace PhotoSharingApp.Universal.ViewModels
{
/// <summary>
/// The ViewModel for the photo details view.
/// </summary>
public class PhotoDetailsViewModel : ViewModelBase
{
/// <summary>
/// The authentication enforcement handler
/// </summary>
private readonly IAuthEnforcementHandler _authEnforcementHandler;
/// <summary>
/// The category
/// </summary>
private CategoryPreview _category;
private readonly IDialogService _dialogService;
/// <summary>
/// To track if this instance is busy.
/// </summary>
private bool _isBusy;
/// <summary>
/// Is photo deletion canceled.
/// </summary>
private bool _isDeletePhotoCanceled;
/// <summary>
/// Is Photo already loaded from service.
/// </summary>
private bool _isPhotoLoadedFromService;
/// <summary>
/// Is setting the profile photo canceled.
/// </summary>
private bool _isSetProfilePhotoCanceled;
/// <summary>
/// The Navigation Facade.
/// </summary>
private readonly INavigationFacade _navigationFacade;
/// <summary>
/// The photo
/// </summary>
private Photo _photo;
/// <summary>
/// The photo service
/// </summary>
private readonly IPhotoService _photoService;
/// <summary>
/// Selected annotation
/// </summary>
private Annotation _selectedAnnotation;
/// <summary>
/// The user
/// </summary>
private User _user;
/// <summary>
/// Initializes a new instance of the <see cref="PhotoDetailsViewModel" /> class.
/// </summary>
/// <param name="navigationFacade">The navigation facade.</param>
/// <param name="photoService">The photo service.</param>
/// <param name="authEnforcementHandler">The auth enforcement handler.</param>
/// <param name="dialogService">The dialog service.</param>
public PhotoDetailsViewModel(INavigationFacade navigationFacade, IPhotoService photoService,
IAuthEnforcementHandler authEnforcementHandler, IDialogService dialogService)
{
_navigationFacade = navigationFacade;
_photoService = photoService;
_authEnforcementHandler = authEnforcementHandler;
_dialogService = dialogService;
// Initialize commands
GotoCameraCommand = new RelayCommand(OnGotoCamera);
DeleteAnnotationCommand = new RelayCommand(OnDeleteAnnotation);
GiveGoldCommand = new RelayCommand(OnGiveGold);
ReportPhotoCommand = new RelayCommand<ReportReason>(OnReportPhoto);
ReportAnnotationCommand = new RelayCommand(OnReportAnnotation);
EditPhotoCommand = new RelayCommand(OnEditPhoto);
UserSelectedCommand = new RelayCommand<User>(OnUserSelected);
DeletePhotoCommand = new RelayCommand(OnDeletePhoto);
CancelDeletePhotoCommand = new RelayCommand(OnCancelDeletePhoto);
SetProfilePhotoCommand = new RelayCommand(OnSetProfilePhoto);
CancelSetProfileCommand = new RelayCommand(OnCancelSetProfile);
}
/// <summary>
/// Gets the comments.
/// </summary>
/// <value>
/// The comments.
/// </value>
public ObservableCollection<Annotation> Annotations { get; } = new ObservableCollection<Annotation>();
/// <summary>
/// Gets or sets the category.
/// </summary>
/// <value>
/// The category.
/// </value>
public CategoryPreview Category
{
get { return _category; }
set
{
if (value != _category)
{
_category = value;
NotifyPropertyChanged(nameof(Category));
}
}
}
/// <summary>
/// Gets the cancel delete photo command.
/// </summary>
public RelayCommand CancelDeletePhotoCommand { get; private set; }
/// <summary>
/// Gets the cancel set profile photo command.
/// </summary>
public RelayCommand CancelSetProfileCommand { get; private set; }
/// <summary>
/// Gets the delete Annotation command.
/// </summary>
public RelayCommand DeleteAnnotationCommand { get; private set; }
/// <summary>
/// Gets the delete photo command.
/// </summary>
public RelayCommand DeletePhotoCommand { get; private set; }
/// <summary>
/// Gets the edit photo command.
/// </summary>
public RelayCommand EditPhotoCommand { get; private set; }
/// <summary>
/// Gets the give gold command.
/// </summary>
public RelayCommand GiveGoldCommand { get; private set; }
/// <summary>
/// Gets the goto camera command.
/// </summary>
public RelayCommand GotoCameraCommand { get; private set; }
/// <summary>
/// Gets the set profile photo command.
/// </summary>
public RelayCommand SetProfilePhotoCommand { get; private set; }
/// <summary>
/// Determines if photo can be set as user's profile pic.
/// </summary>
public bool CanSetProfilePicture => IsCurrentUsersPhoto && IsPhotoActive;
/// <summary>
/// Gets or sets a value indicating if photo has been reported.
/// </summary>
public bool HasReported { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is busy.
/// </summary>
/// <value>
/// <c>true</c> if this instance is busy; otherwise, <c>false</c>.
/// </value>
public bool IsBusy
{
get { return _isBusy; }
set
{
if (value != _isBusy)
{
_isBusy = value;
NotifyPropertyChanged(nameof(IsBusy));
}
}
}
/// <summary>
/// Determines if the photo belongs to the current user.
/// </summary>
public bool IsCurrentUsersPhoto => AppEnvironment.Instance.CurrentUser != null && AppEnvironment.Instance.CurrentUser.UserId == Photo.User.UserId;
/// <summary>
/// Determines if the photo is active.
/// </summary>
public bool IsPhotoActive => _photo.HasActiveStatus;
/// <summary>
/// Determines if user canceled setting a profile photo.
/// </summary>
public bool IsSetProfilePhotoCanceled
{
get
{
return _isSetProfilePhotoCanceled;
}
set
{
_isSetProfilePhotoCanceled = value;
NotifyPropertyChanged(nameof(IsSetProfilePhotoCanceled));
// When IsDeletePhotoCanceled is set to true, the CloseMenuFlyoutAction is triggered.
// We then re-set it to false, making the CloseMenuFlyoutAction possible to trigger again.
if (value)
{
IsSetProfilePhotoCanceled = false;
}
}
}
/// <summary>
/// Determines if user canceled deleting a photo.
/// </summary>
public bool IsDeletePhotoCanceled
{
get
{
return _isDeletePhotoCanceled;
}
set
{
_isDeletePhotoCanceled = value;
NotifyPropertyChanged(nameof(IsDeletePhotoCanceled));
// When IsDeletePhotoCanceled is set to true, the CloseMenuFlyoutAction is triggered.
// We then re-set it to false, making the CloseMenuFlyoutAction possible to trigger again.
if (value)
{
IsDeletePhotoCanceled = false;
}
}
}
/// <summary>
/// Determines if user can delete selected annotation.
/// </summary>
public bool IsUserAbleToDeleteAnnotation
{
get
{
if (_selectedAnnotation == null || AppEnvironment.Instance.CurrentUser == null)
{
return false;
}
return string.Equals(_selectedAnnotation.From.UserId, AppEnvironment.Instance.CurrentUser.UserId);
}
}
/// <summary>
/// Determines if user can report selected annotation.
/// </summary>
public bool IsUserAbleToReportAnnotation
{
get
{
if (_selectedAnnotation == null || AppEnvironment.Instance.CurrentUser == null)
{
return false;
}
return !string.Equals(_selectedAnnotation.From.UserId, AppEnvironment.Instance.CurrentUser.UserId);
}
}
/// <summary>
/// Determines if user can report the photo.
/// </summary>
public bool IsUserAbleToReportPhoto
{
get
{
if (AppEnvironment.Instance.CurrentUser == null ||
AppEnvironment.Instance.CurrentUser.UserId == _photo.User.UserId)
{
return false;
}
foreach (var report in _photo.Reports)
{
if (report.ReporterUserId == AppEnvironment.Instance.CurrentUser.UserId)
{
return false;
}
}
if (HasReported == true)
{
return false;
}
return true;
}
}
/// <summary>
/// Determines if user can update the photo.
/// </summary>
public bool IsUserAbleToUpdatePhoto
{
get
{
if (AppEnvironment.Instance.CurrentUser == null)
{
return false;
}
return string.Equals(Photo.User.UserId, AppEnvironment.Instance.CurrentUser.UserId);
}
}
/// <summary>
/// Gets or sets the photo.
/// </summary>
/// <value>
/// The photo.
/// </value>
public Photo Photo
{
get { return _photo; }
set
{
if (value != _photo)
{
_photo = value;
NotifyPropertyChanged(nameof(Photo));
}
}
}
/// <summary>
/// Gets the report Annotation command.
/// </summary>
/// <value>
/// The report Annotation command.
/// </value>
public RelayCommand ReportAnnotationCommand { get; private set; }
/// <summary>
/// Gets the report photo command.
/// </summary>
/// <value>
/// The report photo command.
/// </value>
public RelayCommand<ReportReason> ReportPhotoCommand { get; private set; }
/// <summary>
/// List of report reasons.
/// </summary>
public ReportReason[] ReportReasons { get; } =
Enum.GetValues(typeof(ReportReason)).Cast<ReportReason>().ToArray();
/// <summary>
/// Gets or sets selected annotation
/// </summary>
public Annotation SelectedAnnotation
{
get { return _selectedAnnotation; }
set
{
_selectedAnnotation = value;
NotifyPropertyChanged(nameof(IsUserAbleToDeleteAnnotation));
NotifyPropertyChanged(nameof(IsUserAbleToReportAnnotation));
}
}
/// <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 user selected command.
/// </summary>
public RelayCommand<User> UserSelectedCommand { get; private set; }
/// <summary>
/// Load the state.
/// </summary>
/// <param name="args">The PhotoDetailsViewModelArgs.</param>
public async Task LoadState(PhotoDetailsViewModelArgs args)
{
await base.LoadState();
Category = args.Category;
Photo = args.Photo;
_isPhotoLoadedFromService = false;
await ShowDetails();
}
/// <summary>
/// Load the state.
/// </summary>
/// <param name="args">The PhotoDetailsViewModelPhotoIdArgs.</param>
public async Task LoadState(PhotoDetailsViewModelPhotoIdArgs args)
{
await base.LoadState();
Photo = await _photoService.GetPhotoDetails(args.PhotoId);
_isPhotoLoadedFromService = true;
await ShowDetails();
}
private void OnCancelDeletePhoto()
{
IsDeletePhotoCanceled = true;
}
private void OnCancelSetProfile()
{
IsSetProfilePhotoCanceled = true;
}
private async void OnDeleteAnnotation()
{
try
{
var deleteAnnotation =
await _dialogService.ShowYesNoNotification("DeleteComment_Message", "DeleteComment_Title");
if (deleteAnnotation)
{
await _photoService.RemoveAnnotation(_selectedAnnotation);
Annotations.Remove(_selectedAnnotation);
}
}
catch (ServiceException)
{
await _dialogService.ShowNotification("DeleteCommentErrorMessage", "GenericErrorTitle");
}
}
private async void OnDeletePhoto()
{
if (AppEnvironment.Instance.CurrentUser.ProfilePictureId == Photo.Id)
{
await _dialogService.ShowNotification("DeleteProfilePicture_Message", "DeleteProfilePicture_Title");
return;
}
try
{
IsBusy = true;
await _photoService.DeletePhoto(Photo);
_navigationFacade.NavigateToProfileView();
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
private void OnEditPhoto()
{
_navigationFacade.NavigateToUploadView(Photo, Category.ToCategory());
}
private async void OnGiveGold()
{
try
{
await _authEnforcementHandler.CheckUserAuthentication();
var annotation = await _navigationFacade.ShowGiveGoldDialog(_photo);
if (annotation != null)
{
Annotations.Insert(0, annotation);
}
}
catch (SignInRequiredException)
{
//Swallow exception. User canceled the Sign-in dialog.
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
}
private void OnGotoCamera()
{
_navigationFacade.NavigateToCameraView(Category);
}
private async void OnSetProfilePhoto()
{
try
{
IsBusy = true;
await _photoService.UpdateUserProfilePhoto(Photo);
_navigationFacade.NavigateToProfileView();
User = AppEnvironment.Instance.CurrentUser;
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
private async void OnReportAnnotation()
{
try
{
IsBusy = true;
await _authEnforcementHandler.CheckUserAuthentication();
var result = await _dialogService.ShowYesNoNotification("ReportContent_Message", "ReportContent_Title");
if (result)
{
await _photoService.ReportAnnotation(_selectedAnnotation);
}
}
catch (SignInRequiredException)
{
// Swallow exception. User canceled the Sign-in dialog.
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
private async void OnReportPhoto(ReportReason reason)
{
try
{
IsBusy = true;
await _authEnforcementHandler.CheckUserAuthentication();
var result = await _dialogService.ShowYesNoNotification("ReportContent_Message", "ReportContent_Title");
if (result)
{
await _photoService.ReportPhoto(_photo, reason);
HasReported = true;
NotifyPropertyChanged(nameof(IsUserAbleToReportPhoto));
}
}
catch (SignInRequiredException)
{
// Swallow exception. User canceled the Sign-in dialog.
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
finally
{
IsBusy = false;
}
}
private void OnUserSelected(User user)
{
_navigationFacade.NavigateToProfileView(user);
}
/// <summary>
/// Load details for a photo.
/// </summary>
private async Task ShowDetails()
{
try
{
var photoWithAnnotations = _photo;
// Avoid making the service call again if it's already been made.
if (!_isPhotoLoadedFromService)
{
photoWithAnnotations = await _photoService.GetPhotoDetails(Photo.Id);
// Update photo gold count.
Photo.GoldCount = photoWithAnnotations.GoldCount;
}
// Some views will pass the photo without a corresponding
// category instance, so we need to update it here.
if (Category == null)
{
Category = photoWithAnnotations.ExtractCategoryPreview();
}
var annotations = photoWithAnnotations.Annotations.OrderByDescending(p => p.CreatedTime);
Annotations.Clear();
foreach (var annotation in annotations)
{
Annotations.Add(annotation);
}
Photo.Reports.Clear();
foreach (var report in photoWithAnnotations.Reports)
{
Photo.Reports.Add(report);
}
NotifyPropertyChanged(nameof(IsUserAbleToReportPhoto));
}
catch (ServiceException)
{
await _dialogService.ShowGenericServiceErrorNotification();
}
}
}
}