Example #1
0
        public Task <bool> SaveAsync(IEnumerable <DocumentFile> newFiles)
        {
            Guard.NotNull(newFiles, nameof(newFiles));

            if (!PlattformDetector.IsDesktop)
            {
                return(Task.FromResult(true));
            }

            return(FileQueue.EnqueueAsync(() =>
            {
                try
                {
                    var errors = 0;

                    var handledPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

                    foreach (
                        var file in
                        newFiles.Where(x => !x.IsInLocalFolder && x.File != null).Select(x => x.File))
                    {
                        try
                        {
                            if (!handledPaths.Add(file.Path))
                            {
                                continue;
                            }

                            if (!tokenMapping.Remove(file.Path))
                            {
                                recentList.Add(file);
                            }
                        }
                        catch
                        {
                            errors++;
                        }
                    }

                    foreach (var token in tokenMapping.Values)
                    {
                        try
                        {
                            recentList.Remove(token);
                        }
                        catch
                        {
                            errors++;
                        }
                    }

                    return Task.FromResult(errors == 0);
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies("SaveRecentList"));
                    throw;
                }
            }));
        }
Example #2
0
        public Task <IReadOnlyList <DocumentFile> > LoadAsync()
        {
            return(FileQueue.EnqueueAsync <IReadOnlyList <DocumentFile> >(async() =>
            {
                try
                {
                    var unsortedFiles =
                        new Dictionary <string, DocumentFile>(StringComparer.OrdinalIgnoreCase);

                    await LoadFilesFromLocalStoreAsync(unsortedFiles);
                    await LoadFilesFromRecentListAsync(unsortedFiles);

                    files.Clear();

                    foreach (var sortedFile in unsortedFiles.Values.OrderByDescending(x => x.ModifiedUtc))
                    {
                        files.Add(sortedFile);
                    }

                    return files;
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies("LoadRecentList"));
                    throw;
                }
            }));
        }
Example #3
0
 public static Task DeleteQueuedAsync(this StorageFile file)
 {
     return(FileQueue.EnqueueAsync(async() =>
     {
         try
         {
             await file.DeleteAsync(StorageDeleteOption.Default);
         }
         catch (Exception e)
         {
             HockeyClient.Current.TrackException(e, GetExceptionProperies(file, "Delete"));
             throw;
         }
     }));
 }
Example #4
0
        public static Task RenameQueuedAsync(this StorageFile file, string name, string extension)
        {
            Guard.ValidFileName(name, nameof(name));

            return(FileQueue.EnqueueAsync(async() =>
            {
                try
                {
                    await file.RenameAsync(name + extension, NameCollisionOption.GenerateUniqueName);
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies(file, "Rename"));
                    throw;
                }
            }));
        }
Example #5
0
        public static Task <List <StorageFile> > GetFilesQueuedAsync()
        {
            return(FileQueue.EnqueueAsync(async() =>
            {
                try
                {
                    var folder = await GetStorageFolderAsync();

                    return (await folder.GetFilesAsync()).ToList();
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies("GetFiles"));
                    throw;
                }
            }));
        }
Example #6
0
 public static Task <Document> OpenDocumentQueuedAsync(this StorageFile file)
 {
     return(FileQueue.EnqueueAsync(async() =>
     {
         try
         {
             using (IRandomAccessStream stream = await file.OpenReadAsync())
             {
                 return stream.Size > 0 ? JsonDocumentSerializer.Deserialize(stream.AsStreamForRead()) : Document.CreateNew(file.DisplayName);
             }
         }
         catch (Exception e)
         {
             HockeyClient.Current.TrackException(e, GetExceptionProperies(file, "Open"));
             throw;
         }
     }));
 }
Example #7
0
        public static Task <StorageFile> CreateOrOpenFileQueuedAsync(string name)
        {
            Guard.ValidFileName(name, nameof(name));

            return(FileQueue.EnqueueAsync(async() =>
            {
                try
                {
                    var folder = await GetStorageFolderAsync();

                    return await folder.CreateFileAsync(name, CreationCollisionOption.OpenIfExists);
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies("CreateOrOpen", name));
                    throw;
                }
            }));
        }
Example #8
0
        public static async Task MakeBackupAsync(IEnumerable <DocumentFile> files)
        {
            var backupFile = await LocalStore.CreateOrOpenFileQueuedAsync("Backup.zip");

            var histories = new Dictionary <string, JsonHistory>();

            foreach (var file in files.Where(x => x.Document != null))
            {
                var name = file.Name + ".mmd";

                if (file.Path != null)
                {
                    name = file.Path;
                    name = name.Replace('/', '_');
                    name = name.Replace(':', '_');
                    name = name.Replace('\\', '_');
                }

                histories.Add(name, new JsonHistory(file.Document));
            }

            await FileQueue.EnqueueAsync(async() =>
            {
                using (var transaction = await backupFile.OpenTransactedWriteAsync())
                {
                    using (var archive = new ZipArchive(transaction.Stream.AsStream(), ZipArchiveMode.Update))
                    {
                        foreach (var kvp in histories)
                        {
                            var entry = archive.GetEntry(kvp.Key) ?? archive.CreateEntry(kvp.Key);

                            using (var entrySteam = entry.Open())
                            {
                                JsonDocumentSerializer.Serialize(kvp.Value, entrySteam);
                            }
                        }
                    }

                    await transaction.CommitAsync();
                }
            });
        }
Example #9
0
        public static async Task SaveDocumentQueuedAsync(this StorageFile file, Document document)
        {
            var history = new JsonHistory(document);

            await FileQueue.EnqueueAsync(async() =>
            {
                try
                {
                    using (var transaction = await file.OpenTransactedWriteAsync())
                    {
                        JsonDocumentSerializer.Serialize(history, transaction.Stream.AsStreamForWrite());

                        await transaction.CommitAsync();
                    }
                }
                catch (Exception e)
                {
                    HockeyClient.Current.TrackException(e, GetExceptionProperies(file, "Open"));
                    throw;
                }
            });
        }