/// <summary>
        /// Loads the archive
        /// </summary>
        public void LoadArchive()
        {
            RL.Logger?.LogInformationSource($"The archive {DisplayName} is being loaded");

            // Clear existing items
            ClearAndDisposeItems();

            // Load the archive data
            ArchiveData = Manager.LoadArchive(ArchiveFileStream);

            // Load the archive
            var data = Manager.LoadArchiveData(ArchiveData, ArchiveFileStream);

            // Dispose the current generator
            ArchiveFileGenerator?.Dispose();

            // Save the generator
            ArchiveFileGenerator = data.Generator;

            // Add each directory
            foreach (var dir in data.Directories)
            {
                // Check if it's the root directory
                if (dir.DirectoryName == String.Empty)
                {
                    // Add the files
                    Files.AddRange(dir.Files.Select(x => new ArchiveFileViewModel(x, this)));

                    continue;
                }

                // Keep track of the previous item
                ArchiveDirectoryViewModel prevItem = this;

                // Enumerate each sub directory
                foreach (string subDir in dir.DirectoryName.Trim(Manager.PathSeparatorCharacter).Split(Manager.PathSeparatorCharacter))
                {
                    // Set the previous item and create the item if it doesn't already exist
                    prevItem = prevItem.FindItem(x => x.ID == subDir) ?? prevItem.Add(subDir);
                }

                // Add the files
                prevItem.Files.AddRange(dir.Files.Select(x => new ArchiveFileViewModel(x, this)));
            }
        }
Exemple #2
0
        /// <summary>
        /// Updates the loaded directory thumbnails
        /// </summary>
        /// <returns>The task</returns>
        public async Task ChangeLoadedDirAsync(ArchiveDirectoryViewModel previousDir, ArchiveDirectoryViewModel newDir)
        {
            RL.Logger?.LogDebugSource($"The loaded archive directory is changing from {previousDir?.DisplayName ?? "NULL"} to {newDir?.DisplayName ?? "NULL"}");

            // Stop refreshing thumbnails
            if (IsRefreshingThumbnails)
            {
                CancelRefreshingThumbnails = true;
            }

            RL.Logger?.LogDebugSource($"Updating loaded archive dir from {previousDir?.DisplayName} to {newDir.DisplayName}");

            // Lock the access to the archive
            using (await ArchiveLock.LockAsync())
            {
                try
                {
                    // Check if the operation should be canceled
                    if (CancelRefreshingThumbnails)
                    {
                        RL.Logger?.LogDebugSource($"Canceled refreshing thumbnails for archive dir {newDir.DisplayName}");

                        return;
                    }

                    // Indicate that we are refreshing the thumbnails
                    IsRefreshingThumbnails = true;

                    RL.Logger?.LogDebugSource($"Refreshing thumbnails for archive dir {newDir.DisplayName}");

                    // Remove all thumbnail image sources from memory
                    previousDir?.Files.ForEach(x =>
                    {
                        x.IsInitialized   = false;
                        x.ThumbnailSource = null;
                    });

                    // Load the thumbnail image sources for the new directory
                    await Task.Run(() =>
                    {
                        // Load the thumbnail for each file
                        foreach (var x in newDir.Files.
                                 // Copy to an array to avoid an exception when the files refresh
                                 ToArray())
                        {
                            // Check if the operation should be canceled
                            if (CancelRefreshingThumbnails)
                            {
                                RL.Logger?.LogDebugSource($"Canceled refreshing thumbnails for archive dir {newDir.DisplayName}");

                                return;
                            }

                            // Load the thumbnail
                            x.LoadThumbnail();

                            // Indicate that the thumbnail has been loaded
                            x.IsInitialized = true;
                        }
                    });

                    RL.Logger?.LogDebugSource($"Finished thumbnails for archive dir {newDir.DisplayName}");
                }
                finally
                {
                    IsRefreshingThumbnails     = false;
                    CancelRefreshingThumbnails = false;
                }
            }
        }