Browse Source

Checkpoint folder loading improvements

pull/55/head
Ionite 1 year ago
parent
commit
efc2268ac8
No known key found for this signature in database
  1. 9
      StabilityMatrix.Avalonia/ViewModels/CheckpointFolder.cs
  2. 40
      StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs

9
StabilityMatrix.Avalonia/ViewModels/CheckpointFolder.cs

@ -48,8 +48,12 @@ public partial class CheckpointFolder : ViewModelBase
/// <summary>
/// True if the category is enabled for the manager page.
/// </summary>
[ObservableProperty]
private bool isCategoryEnabled = true;
[ObservableProperty] private bool isCategoryEnabled = true;
/// <summary>
/// True if currently expanded in the UI.
/// </summary>
[ObservableProperty] private bool isExpanded = true;
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsDragBlurEnabled))]
@ -320,6 +324,7 @@ public partial class CheckpointFolder : ViewModelBase
DirectoryPath = folder,
FolderType = FolderType, // Inherit our folder type
ParentFolder = this,
IsExpanded = false, // Subfolders are collapsed by default
};
await subFolder.IndexAsync(progress);

40
StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs

@ -6,6 +6,7 @@ using System.Linq;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentAvalonia.UI.Controls;
@ -40,8 +41,9 @@ public partial class CheckpointsPageViewModel : PageViewModelBase
settingsManager.Transaction(s => s.IsImportAsConnected = value);
}
}
public ObservableCollection<CheckpointFolder> CheckpointFolders { get; set; } = new();
[ObservableProperty]
private ObservableCollection<CheckpointFolder> checkpointFolders = new();
public CheckpointsPageViewModel(
ISharedFolders sharedFolders,
@ -61,42 +63,48 @@ public partial class CheckpointsPageViewModel : PageViewModelBase
// Set UI states
IsImportAsConnected = settingsManager.Settings.IsImportAsConnected;
await IndexFolders();
}
private async Task IndexFolders()
{
var modelsDirectory = settingsManager.ModelsDirectory;
// Get all folders within the shared folder root
if (string.IsNullOrWhiteSpace(modelsDirectory))
{
CheckpointFolders.Clear();
return;
}
// Skip if the shared folder root doesn't exist
if (!Directory.Exists(modelsDirectory))
{
Logger.Debug($"Skipped shared folder index - {modelsDirectory} doesn't exist");
CheckpointFolders.Clear();
return;
}
var folders = Directory.GetDirectories(modelsDirectory);
CheckpointFolders.Clear();
// Results
var indexedFolders = new ConcurrentBag<CheckpointFolder>();
// Index all folders
var tasks = folders.Select(f => Task.Run(async () =>
var indexTasks = folders.Select(f => Task.Run(async () =>
{
var checkpointFolder = new CheckpointFolder(settingsManager, downloadService, modelFinder)
{
Title = Path.GetFileName(f),
DirectoryPath = f
DirectoryPath = f,
IsExpanded = true, // Top level folders expanded by default
};
await checkpointFolder.IndexAsync();
indexedFolders.Add(checkpointFolder);
return checkpointFolder;
})).ToList();
await Task.WhenAll(tasks);
// Add to observable collection by alphabetical order
foreach (var checkpointFolder in indexedFolders.OrderBy(f => f.Title))
{
CheckpointFolders.Add(checkpointFolder);
}
await Task.WhenAll(indexTasks);
// Set new observable collection, ordered by alphabetical order
CheckpointFolders =
new ObservableCollection<CheckpointFolder>(indexTasks
.Select(t => t.Result)
.OrderBy(f => f.Title));
}
[RelayCommand]

Loading…
Cancel
Save