Browse Source

Use BetterAdvancedImage to fix load stream errors

Also:
- fixes design time preview
- deletes cm-info file on file deletion
pull/55/head
Ionite 1 year ago
parent
commit
a20d39a48d
No known key found for this signature in database
  1. 9
      StabilityMatrix.Avalonia/DesignData/DesignData.cs
  2. 41
      StabilityMatrix.Avalonia/ViewModels/CheckpointFile.cs
  3. 5
      StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs
  4. 5
      StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml

9
StabilityMatrix.Avalonia/DesignData/DesignData.cs

@ -151,6 +151,8 @@ public static class DesignData
{
FilePath = "~/Models/Lora/electricity-light.safetensors",
Title = "Auroral Background",
PreviewImagePath = "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/" +
"78fd2a0a-42b6-42b0-9815-81cb11bb3d05/00009-2423234823.jpeg",
ConnectedModel = new ConnectedModelInfo
{
VersionName = "Lightning Auroral",
@ -170,7 +172,7 @@ public static class DesignData
FilePath = "~/Models/Lora/model.safetensors",
Title = "Some model"
},
}
},
},
new(settingsManager, downloadService, modelFinder)
{
@ -186,6 +188,11 @@ public static class DesignData
}
}
};
CheckpointsPageViewModel.CheckpointFolders[0].DisplayedCheckpointFiles =
CheckpointsPageViewModel.CheckpointFolders[0].CheckpointFiles;
CheckpointsPageViewModel.CheckpointFolders[1].DisplayedCheckpointFiles =
CheckpointsPageViewModel.CheckpointFolders[1].CheckpointFiles;
CheckpointBrowserViewModel.ModelCards = new
ObservableCollection<CheckpointBrowserCardViewModel>

41
StabilityMatrix.Avalonia/ViewModels/CheckpointFile.cs

@ -39,10 +39,12 @@ public partial class CheckpointFile : ViewModelBase
/// </summary>
[ObservableProperty]
private string title = string.Empty;
public string? PreviewImagePath { get; set; }
public Bitmap? PreviewImage { get; set; }
public bool IsPreviewImageLoaded => PreviewImage != null;
/// <summary>
/// Path to preview image. Can be local or remote URL.
/// </summary>
[ObservableProperty]
private string? previewImagePath;
[ObservableProperty]
private ConnectedModelInfo? connectedModel;
@ -75,6 +77,18 @@ public partial class CheckpointFile : ViewModelBase
}
}
private string GetConnectedModelInfoFilePath()
{
if (string.IsNullOrEmpty(FilePath))
{
throw new InvalidOperationException(
"Cannot get connected model info file path when FilePath is empty");
}
var modelNameNoExt = Path.GetFileNameWithoutExtension(FilePath);
var modelDir = Path.GetDirectoryName(FilePath) ?? "";
return Path.Combine(modelDir, $"{modelNameNoExt}.cm-info.json");
}
[RelayCommand]
private async Task DeleteAsync()
{
@ -89,6 +103,14 @@ public partial class CheckpointFile : ViewModelBase
{
await Task.Run(() => File.Delete(PreviewImagePath));
}
if (ConnectedModel != null)
{
var cmInfoPath = GetConnectedModelInfoFilePath();
if (File.Exists(cmInfoPath))
{
await Task.Run(() => File.Delete(cmInfoPath));
}
}
}
catch (IOException ex)
{
@ -169,16 +191,6 @@ public partial class CheckpointFile : ViewModelBase
if (ConnectedModel?.ModelId == null) return;
ProcessRunner.OpenUrl($"https://civitai.com/models/{ConnectedModel.ModelId}");
}
// Loads image from path
private async Task LoadPreviewImage()
{
if (PreviewImagePath == null) return;
await Dispatcher.UIThread.InvokeAsync(() =>
{
PreviewImage = new Bitmap(File.OpenRead(PreviewImagePath));
});
}
/// <summary>
/// Indexes directory and yields all checkpoint files.
@ -226,7 +238,6 @@ public partial class CheckpointFile : ViewModelBase
if (previewImage != null)
{
checkpointFile.PreviewImagePath = files[previewImage];
checkpointFile.LoadPreviewImage().SafeFireAndForget();
}
yield return checkpointFile;

5
StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs

@ -67,12 +67,13 @@ public partial class CheckpointsPageViewModel : PageViewModelBase
public override async Task OnLoadedAsync()
{
DisplayedCheckpointFolders = CheckpointFolders;
if (Design.IsDesignMode) return;
// Set UI states
IsImportAsConnected = settingsManager.Settings.IsImportAsConnected;
SearchFilter = string.Empty;
if (Design.IsDesignMode) return;
IsLoading = CheckpointFolders.Count == 0;
IsIndexing = CheckpointFolders.Count > 0;
await IndexFolders();

5
StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml

@ -101,9 +101,10 @@
TextWrapping="WrapWithOverflow"
IsVisible="{Binding IsConnectedModel}" />
<!-- Image -->
<Image
<controls:BetterAdvancedImage
Margin="0,0,0,4"
Source="{Binding PreviewImage}"
CornerRadius="4"
Source="{Binding PreviewImagePath}"
Stretch="Uniform"
IsVisible="{Binding IsConnectedModel}" />
</StackPanel>

Loading…
Cancel
Save