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", FilePath = "~/Models/Lora/electricity-light.safetensors",
Title = "Auroral Background", Title = "Auroral Background",
PreviewImagePath = "https://image.civitai.com/xG1nkqKTMzGDvpLrqFT7WA/" +
"78fd2a0a-42b6-42b0-9815-81cb11bb3d05/00009-2423234823.jpeg",
ConnectedModel = new ConnectedModelInfo ConnectedModel = new ConnectedModelInfo
{ {
VersionName = "Lightning Auroral", VersionName = "Lightning Auroral",
@ -170,7 +172,7 @@ public static class DesignData
FilePath = "~/Models/Lora/model.safetensors", FilePath = "~/Models/Lora/model.safetensors",
Title = "Some model" Title = "Some model"
}, },
} },
}, },
new(settingsManager, downloadService, modelFinder) 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 CheckpointBrowserViewModel.ModelCards = new
ObservableCollection<CheckpointBrowserCardViewModel> ObservableCollection<CheckpointBrowserCardViewModel>

41
StabilityMatrix.Avalonia/ViewModels/CheckpointFile.cs

@ -39,10 +39,12 @@ public partial class CheckpointFile : ViewModelBase
/// </summary> /// </summary>
[ObservableProperty] [ObservableProperty]
private string title = string.Empty; private string title = string.Empty;
public string? PreviewImagePath { get; set; } /// <summary>
public Bitmap? PreviewImage { get; set; } /// Path to preview image. Can be local or remote URL.
public bool IsPreviewImageLoaded => PreviewImage != null; /// </summary>
[ObservableProperty]
private string? previewImagePath;
[ObservableProperty] [ObservableProperty]
private ConnectedModelInfo? connectedModel; 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] [RelayCommand]
private async Task DeleteAsync() private async Task DeleteAsync()
{ {
@ -89,6 +103,14 @@ public partial class CheckpointFile : ViewModelBase
{ {
await Task.Run(() => File.Delete(PreviewImagePath)); 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) catch (IOException ex)
{ {
@ -169,16 +191,6 @@ public partial class CheckpointFile : ViewModelBase
if (ConnectedModel?.ModelId == null) return; if (ConnectedModel?.ModelId == null) return;
ProcessRunner.OpenUrl($"https://civitai.com/models/{ConnectedModel.ModelId}"); 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> /// <summary>
/// Indexes directory and yields all checkpoint files. /// Indexes directory and yields all checkpoint files.
@ -226,7 +238,6 @@ public partial class CheckpointFile : ViewModelBase
if (previewImage != null) if (previewImage != null)
{ {
checkpointFile.PreviewImagePath = files[previewImage]; checkpointFile.PreviewImagePath = files[previewImage];
checkpointFile.LoadPreviewImage().SafeFireAndForget();
} }
yield return checkpointFile; yield return checkpointFile;

5
StabilityMatrix.Avalonia/ViewModels/CheckpointsPageViewModel.cs

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

5
StabilityMatrix.Avalonia/Views/CheckpointsPage.axaml

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

Loading…
Cancel
Save