Skip to content

Commit

Permalink
improve performance when updating data grid items
Browse files Browse the repository at this point in the history
  • Loading branch information
ScanMountGoat committed Jul 11, 2021
1 parent 021693d commit dde3ece
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions ArcExplorer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ public class MainWindowViewModel : ViewModelBase
public AvaloniaList<FileNodeBase> Files { get; } = new AvaloniaList<FileNodeBase>();

// TODO: Temporary workaround to avoid converting the FileTree logic to use FileGridItem.
public AvaloniaList<FileGridItem> Items { get; } = new AvaloniaList<FileGridItem>();
public AvaloniaList<FileGridItem> Items
{
get => items;
set => this.RaiseAndSetIfChanged(ref items, value);
}
private AvaloniaList<FileGridItem> items = new AvaloniaList<FileGridItem>();

public static Dictionary<Region, string> DescriptionByRegion { get; } = new Dictionary<Region, string>
{
Expand Down Expand Up @@ -81,7 +86,7 @@ public string? CurrentDirectoryPath
{
get => currentDirectoryPath;
set
{
{
if (arcFile != null && value != currentDirectoryPath)
{
this.RaiseAndSetIfChanged(ref currentDirectoryPath, value);
Expand All @@ -96,8 +101,8 @@ public string? CurrentDirectoryPath
}
private string? currentDirectoryPath;

public FolderNode? CurrentDirectory
{
public FolderNode? CurrentDirectory
{
get => currentDirectory;
set
{
Expand Down Expand Up @@ -186,15 +191,18 @@ public MainWindowViewModel()
// HACK: DataGrid doesn't seem to support multiple types, so use a wrapper type.
Files.CollectionChanged += (s, e) =>
{
Items.Clear();
var newItems = new List<FileGridItem>();
foreach (var node in Files)
{
if (node is FileNode file)
Items.Add(new FileGridItem(file));
newItems.Add(new FileGridItem(file));
else if (node is FolderNode folder)
Items.Add(new FileGridItem(folder));
newItems.Add(new FileGridItem(folder));
}

// Recreating the list is faster than adding items individually.
Items = new AvaloniaList<FileGridItem>(newItems);

// HACK: Adding files to the file list increments selected index?
SelectedFileIndex = 0;
};
Expand Down Expand Up @@ -251,7 +259,7 @@ private void InitializeArcFile(string arcPathText)
ArcVersion = arcFile.Version.ToString();

Files.Clear();
var newFiles = FileTree.CreateRootLevelNodes(arcFile,
var newFiles = FileTree.CreateRootLevelNodes(arcFile,
BackgroundTaskStart, BackgroundTaskReportProgress, BackgroundTaskEnd, ApplicationSettings.Instance.MergeTrailingSlash);
Files.AddRange(newFiles);
}
Expand Down Expand Up @@ -291,7 +299,7 @@ public void ExitFolder()
return;
}

// Go up one level in the file tree.
// Go up one level in the file tree.
var parent = FileTree.CreateFolderNode(arcFile, parentPath);
if (parent == null)
LoadRootNodes(arcFile);
Expand Down Expand Up @@ -360,7 +368,7 @@ public void ExtractAllFiles()
if (arcFile == null)
return;

FileTree.ExtractAllFiles(arcFile,
FileTree.ExtractAllFiles(arcFile,
BackgroundTaskStart, BackgroundTaskReportProgress, BackgroundTaskEnd,
ApplicationSettings.Instance.MergeTrailingSlash);
}
Expand Down

0 comments on commit dde3ece

Please sign in to comment.