Multi-Platform Package Manager for Stable Diffusion
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.2 KiB

using System;
using System.Threading.Tasks;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using FluentAvalonia.UI.Controls;
using StabilityMatrix.Avalonia.Controls;
using StabilityMatrix.Avalonia.Languages;
using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.Views.Dialogs;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.FileInterfaces;
using StabilityMatrix.Core.Processes;
using StabilityMatrix.Core.Services;
namespace StabilityMatrix.Avalonia.ViewModels.Dialogs;
[View(typeof(DownloadResourceDialog))]
[ManagedService]
[Transient]
public partial class DownloadResourceViewModel(
IDownloadService downloadService,
ISettingsManager settingsManager,
ITrackedDownloadService trackedDownloadService
) : ContentDialogViewModelBase
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FileNameWithHash))]
private string? fileName;
public string FileNameWithHash => $"{FileName} [{Resource.HashSha256?.ToLowerInvariant()[..7]}]";
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(FileSizeText))]
private long fileSize;
[ObservableProperty]
private RemoteResource resource;
public string? FileSizeText => FileSize == 0 ? null : Size.FormatBase10Bytes(FileSize);
public string? ShortHash => Resource.HashSha256?.ToLowerInvariant()[..7];
/// <inheritdoc />
public override async Task OnLoadedAsync()
{
await base.OnLoadedAsync();
// Get download size
if (!Design.IsDesignMode && Resource.Url is { } url)
{
FileSize = await downloadService.GetFileSizeAsync(url.ToString());
}
}
[RelayCommand]
private void OpenInfoUrl()
{
if (Resource.InfoUrl is { } url)
{
ProcessRunner.OpenUrl(url);
}
}
public TrackedDownload StartDownload()
{
var sharedFolderType =
Resource.ContextType as SharedFolderType?
?? throw new InvalidOperationException("ContextType is not SharedFolderType");
var modelsDir = new DirectoryPath(settingsManager.ModelsDirectory).JoinDir(
sharedFolderType.GetStringValue()
);
var download = trackedDownloadService.NewDownload(
Resource.Url,
modelsDir.JoinFile(Resource.FileName)
);
// Set extraction properties
download.AutoExtractArchive = Resource.AutoExtractArchive;
download.ExtractRelativePath = Resource.ExtractRelativePath;
download.ContextAction = new ModelPostDownloadContextAction();
download.Start();
EventManager.Instance.OnToggleProgressFlyout();
return download;
}
public BetterContentDialog GetDialog()
{
return new BetterContentDialog
{
MinDialogWidth = 400,
Title = "Download Model",
Content = new DownloadResourceDialog { DataContext = this },
PrimaryButtonText = Resources.Action_Continue,
CloseButtonText = Resources.Action_Cancel,
DefaultButton = ContentDialogButton.Primary
};
}
}