Example #1
0
        private async Task <string[]> SaveImageAttachmentsAsync()
        {
            var newImages = ImageAttachments.OfType <EditingNewImageAttachmentViewModel>().ToArray();

            if (_removedImageAttachments.Count > 0 || newImages.Length > 0)
            {
                var currAccount = Account;

                if (currAccount == null)
                {
                    throw new NullReferenceException("Account was null");
                }

                IFolder imagesFolderPortable = await FileHelper.GetOrCreateImagesFolder(currAccount.LocalAccountId);

                List <string> finalImageNames = ImageAttachments.Select(i => i.ImageAttachment.ImageName).ToList();

                // Delete images
                foreach (var removedImage in _removedImageAttachments)
                {
                    try
                    {
                        var file = await imagesFolderPortable.GetFileAsync(removedImage.ImageAttachment.ImageName);

                        await file.DeleteAsync();
                    }

                    catch (Exception ex)
                    {
                        TelemetryExtension.Current?.TrackException(ex);
                    }
                }

                // Add new images
                foreach (var newImage in newImages)
                {
                    try
                    {
                        await newImage.TempFile.MoveAsync(Path.Combine(imagesFolderPortable.Path, newImage.TempFile.Name), NameCollisionOption.ReplaceExisting);

                        if (newImage.TempFile is Helpers.TempFile tempFile)
                        {
                            tempFile.DetachTempDisposer();
                        }
                    }

                    catch (Exception ex)
                    {
                        TelemetryExtension.Current?.TrackException(ex);

                        try
                        {
                            finalImageNames.Remove(newImage.ImageAttachment.ImageName);
                        }
                        catch { }
                    }
                }

                // If there were new images, add them to the needing upload list
                var newImageNames = newImages.Select(i => i.ImageAttachment.ImageName).Intersect(finalImageNames).ToArray();
                if (newImageNames.Any())
                {
                    var dataStore = await AccountDataStore.Get(currAccount.LocalAccountId);

                    await dataStore.AddImagesToUploadAsync(newImageNames);
                }

                return(finalImageNames.ToArray());
            }
            else
            {
                return(null);
            }
        }
 /// <summary>
 /// Updates the counters for the file and image attachments
 /// </summary>
 public void UpdateAttachmentCounts()
 {
     AttachmentFilesCount  = FileAttachments.Count();
     AttachmentImagesCount = ImageAttachments.Count();
 }