/// <summary> /// Converts the given contract into a data model. /// </summary> /// <param name="photoContract">The data contract.</param> /// <returns>The data model.</returns> public static Photo ToDataModel(this PhotoContract photoContract) { var photo = new Photo { Id = photoContract.Id, CategoryId = photoContract.CategoryId, ThumbnailUrl = photoContract.ThumbnailUrl, StandardUrl = photoContract.StandardUrl, HighResolutionUrl = photoContract.HighResolutionUrl, User = photoContract.User.ToDataModel(), Caption = photoContract.Description, CreatedAt = photoContract.CreatedAt, NumberOfAnnotations = photoContract.NumberOfAnnotations, GoldCount = photoContract.NumberOfGoldVotes, CategoryName = photoContract.CategoryName, Reports = photoContract.Reports, Status = photoContract.Status }; if (photoContract.Annotations != null) { photo.Annotations = new ObservableCollection<Annotation>( photoContract.Annotations.Select(a => a.ToDataModel())); } return photo; }
public GiveGoldDialog(Photo photo) { InitializeComponent(); ViewModel = ServiceLocator.Current.GetInstance<GiveGoldViewModel>(); ViewModel.Photo = photo; DataContext = ViewModel; // Register for events Loaded += GiveGoldDialog_Loaded; }
private async void OnGiveGold(Photo photo) { try { await _authEnforcementHandler.CheckUserAuthentication(); await _navigationFacade.ShowGiveGoldDialog(photo); } catch (SignInRequiredException) { // Swallow exception. User canceled the Sign-in dialog. } catch (ServiceException) { await _dialogService.ShowGenericServiceErrorNotification(); } }
/// <summary> /// Initializes a new instance of the <see cref="PhotoDetailsViewModelArgs" /> class. /// </summary> /// <param name="category">The category.</param> /// <param name="photo">The photo.</param> public PhotoDetailsViewModelArgs(CategoryPreview category, Photo photo) { Category = category; Photo = photo; }
/// <summary> /// Uploads the photo. /// </summary> /// <param name="stream">The memory stream.</param> /// <param name="localPath">The local path.</param> /// <param name="caption">The caption.</param> /// <param name="categoryId">The id of the assocaited category.</param> /// <returns>The uploaded photo.</returns> public async Task<Photo> UploadPhoto(Stream stream, string localPath, string caption, string categoryId) { await SimulateWaitAndError(); var photo = new Photo { Caption = caption, ThumbnailUrl = localPath, HighResolutionUrl = localPath, StandardUrl = localPath, CreatedAt = DateTime.Now, User = AppEnvironment.Instance.CurrentUser }; PhotoStreams.FirstOrDefault(s => s.CateogoryId == categoryId).Photos.Insert(0, photo); AppEnvironment.Instance.CurrentUser.GoldBalance++; return photo; }
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> /// Updates the photo. /// </summary> /// <param name="photo">The photo.</param> public async Task UpdatePhoto(Photo photo) { try { var photoContract = photo.ToDataContract(); await _mobileServiceClient.InvokeApiAsync<PhotoContract, PhotoContract>("photo", photoContract, HttpMethod.Put, null); } catch (Exception e) { throw new ServiceException("UpdatePhoto error", e); } }
/// <summary> /// Posts the annotation. /// </summary> /// <param name="photo">The photo.</param> /// <param name="annotationText">The text.</param> /// <param name="goldCount">The amount of gold being given.</param> /// <returns>The annotation including the id.</returns> public async Task<Annotation> PostAnnotation(Photo photo, string annotationText, int goldCount) { try { var annotationContract = new AnnotationContract { CreatedAt = DateTime.Now, Text = annotationText, From = AppEnvironment.Instance.CurrentUser.ToDataContract(), PhotoId = photo.Id, PhotoOwnerId = photo.User.UserId, GoldCount = goldCount }; var result = await _mobileServiceClient.InvokeApiAsync<AnnotationContract, AnnotationContract>("annotation", annotationContract, HttpMethod.Post, new Dictionary<string, string>()); if (result != null) { photo.GoldCount += result.GoldCount; // In this case we know how much gold we have given, // so we can reduce the cached amount instead of reloading // the current user. if (AppEnvironment.Instance.CurrentUser != null) { AppEnvironment.Instance.CurrentUser.GoldBalance -= result.GoldCount; } } return result.ToDataModel(); } catch (MobileServiceInvalidOperationException invalidOperationException) { var content = await invalidOperationException.Response.Content.ReadAsStringAsync(); var serviceFault = TryGetServiceFault(content); if (invalidOperationException.Response.StatusCode == HttpStatusCode.Forbidden && serviceFault?.Code == 3999) { throw new InsufficientBalanceException("Users balance is too low", invalidOperationException); } throw new ServiceException("PostAnnotation error", invalidOperationException); } catch (Exception e) { throw new ServiceException("PostAnnotation error", e); } }
/// <summary> /// Deletes the photo. /// </summary> /// <param name="photo">The photo.</param> public async Task DeletePhoto(Photo photo) { await SimulateWaitAndError(); var category = PhotoStreams.FirstOrDefault(c => c.CateogoryId == photo.CategoryId); var photoInCategory = category?.Photos.FirstOrDefault(p => p.CategoryId == photo.CategoryId); if (photoInCategory != null) { category.Photos.Remove(photoInCategory); } }
/// <summary> /// Displays a dialog that lets the user give gold /// </summary> /// <returns>The annotation. If dialog is being canceled, null is returned.</returns> public async Task<Annotation> ShowGiveGoldDialog(Photo photo) { var dialog = new GiveGoldDialog(photo); await dialog.ShowAsync(); return dialog.ViewModel.Annotation; }
/// <summary> /// Navigates to the upload view to update data of /// the existing photo. /// </summary> /// <param name="photo">The photo to update.</param> /// <param name="category">The associated category.</param> public void NavigateToUploadView(Photo photo, Category category) { Navigate(typeof(UploadViewModel), new UploadViewModelEditPhotoArgs { Photo = photo, Category = category }, false); }
/// <summary> /// Navigates to the photo details view. /// </summary> /// <param name="photo">The photo.</param> public void NavigateToPhotoDetailsView(Photo photo) { Navigate(typeof(PhotoDetailsViewModel), new PhotoDetailsViewModelArgs(null, photo)); }
/// <summary> /// Navigates to the photo details view. /// </summary> /// <param name="category">The category.</param> /// <param name="photo">The photo.</param> public void NavigateToPhotoDetailsView(CategoryPreview category, Photo photo) { Navigate(typeof(PhotoDetailsViewModel), new PhotoDetailsViewModelArgs(category, photo)); }
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; } }
/// <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); }
/// <summary> /// Deletes the photo. /// </summary> /// <param name="photo">The photo.</param> public async Task DeletePhoto(Photo photo) { try { await _mobileServiceClient.InvokeApiAsync($"photo/{photo.Id}", HttpMethod.Delete, null); } catch (Exception e) { throw new ServiceException("DeletePhoto error", e); } }
/// <summary> /// Posts the annotation. /// </summary> /// <param name="photo">The photo.</param> /// <param name="annotationText">The text.</param> /// <param name="goldCount">The amount of gold being given.</param> /// <returns>The annotation including the id.</returns> public async Task<Annotation> PostAnnotation(Photo photo, string annotationText, int goldCount) { await SimulateWaitAndError(); var annotation = new Annotation { CreatedTime = DateTime.Now, Id = Guid.NewGuid().ToString(), Text = annotationText, From = AppEnvironment.Instance.CurrentUser, GoldCount = goldCount }; Annotations.Add(annotation); photo.NumberOfAnnotations++; photo.GoldCount += annotation.GoldCount; if (AppEnvironment.Instance.CurrentUser != null) { AppEnvironment.Instance.CurrentUser.GoldBalance -= annotation.GoldCount; } return annotation; }
/// <summary> /// Reports the photo as inappropriate. /// </summary> /// <param name="photo">The photo to report.</param> /// <param name="reportReason">The reason for the report.</param> public async Task ReportPhoto(Photo photo, ReportReason reportReason) { try { var reportContract = new ReportContract { ReportReason = reportReason, ContentId = photo.Id, ContentType = ContentType.Photo }; await _mobileServiceClient.InvokeApiAsync<ReportContract, ReportContract>("report", reportContract, HttpMethod.Post, null); } catch (Exception e) { throw new ServiceException("ReportPhoto error", e); } }
/// <summary> /// Reports the photo as inappropriate. /// </summary> /// <param name="photo">The photo.</param> /// <param name="reportReason">The reason for the report.</param> public async Task ReportPhoto(Photo photo, ReportReason reportReason) { await SimulateWaitAndError(); }
/// <summary> /// Updates the current user's profile photo /// </summary> /// <param name="photo">The new profile photo</param> /// <returns>The updated user</returns> public async Task<User> UpdateUserProfilePhoto(Photo photo) { try { var userContract = AppEnvironment.Instance.CurrentUser.ToDataContract(); userContract.ProfilePhotoId = photo.Id; var result = await _mobileServiceClient.InvokeApiAsync<UserContract, UserContract>("user", userContract, HttpMethod.Post, null); var updatedUser = result.ToDataModel(); AppEnvironment.Instance.CurrentUser = updatedUser; return updatedUser; } catch (Exception e) { throw new ServiceException("UpdateUserProfilePhoto error", e); } }
/// <summary> /// Updates the photo. /// </summary> /// <param name="photo">The photo.</param> public async Task UpdatePhoto(Photo photo) { await SimulateWaitAndError(); }
/// <summary> /// Updates the current user's profile photo /// </summary> /// <param name="photo">The new profile photo</param> /// <returns>The updated user</returns> public async Task<User> UpdateUserProfilePhoto(Photo photo) { await SimulateWaitAndError(); AppEnvironment.Instance.CurrentUser.ProfilePictureUrl = photo.ThumbnailUrl; return AppEnvironment.Instance.CurrentUser; }
/// <summary> /// Action to take when a photo has been selected /// </summary> /// <param name="photo">The photo.</param> private void OnPhotoSelected(Photo photo) { SelectedPhoto = photo; _navigationFacade.NavigateToPhotoDetailsView(Category, photo); }