Ionite
1 year ago
9 changed files with 209 additions and 12 deletions
@ -0,0 +1,78 @@ |
|||||||
|
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.Helper; |
||||||
|
using StabilityMatrix.Core.Models; |
||||||
|
using StabilityMatrix.Core.Processes; |
||||||
|
using StabilityMatrix.Core.Services; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; |
||||||
|
|
||||||
|
[View(typeof(DownloadResourceDialog))] |
||||||
|
public partial class DownloadResourceViewModel : ContentDialogViewModelBase |
||||||
|
{ |
||||||
|
private readonly IDownloadService downloadService; |
||||||
|
|
||||||
|
[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(); |
||||||
|
|
||||||
|
public DownloadResourceViewModel(IDownloadService downloadService) |
||||||
|
{ |
||||||
|
this.downloadService = downloadService; |
||||||
|
} |
||||||
|
|
||||||
|
/// <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 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 |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,50 @@ |
|||||||
|
<controls:UserControlBase xmlns="https://github.com/avaloniaui" |
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||||
|
xmlns:vm="clr-namespace:StabilityMatrix.Avalonia.ViewModels" |
||||||
|
xmlns:ui="using:FluentAvalonia.UI.Controls" |
||||||
|
xmlns:mocks="clr-namespace:StabilityMatrix.Avalonia.DesignData" |
||||||
|
xmlns:controls="clr-namespace:StabilityMatrix.Avalonia.Controls" |
||||||
|
xmlns:system="clr-namespace:System;assembly=System.Runtime" |
||||||
|
xmlns:vmDialogs="clr-namespace:StabilityMatrix.Avalonia.ViewModels.Dialogs" |
||||||
|
xmlns:lang="clr-namespace:StabilityMatrix.Avalonia.Languages" |
||||||
|
xmlns:processes="clr-namespace:StabilityMatrix.Core.Processes;assembly=StabilityMatrix.Core" |
||||||
|
d:DataContext="{x:Static mocks:DesignData.DownloadResourceViewModel}" |
||||||
|
x:DataType="vmDialogs:DownloadResourceViewModel" |
||||||
|
mc:Ignorable="d" d:DesignWidth="500" d:DesignHeight="450" |
||||||
|
x:Class="StabilityMatrix.Avalonia.Views.Dialogs.DownloadResourceDialog"> |
||||||
|
|
||||||
|
<Grid RowDefinitions="*,*" Margin="8,4"> |
||||||
|
<StackPanel> |
||||||
|
<ui:SettingsExpander |
||||||
|
Command="{Binding OpenInfoUrlCommand}" |
||||||
|
Header="{Binding FileNameWithHash}" |
||||||
|
Description="{Binding Resource.ByAuthor}" |
||||||
|
Footer="{Binding FileSizeText}"> |
||||||
|
</ui:SettingsExpander> |
||||||
|
|
||||||
|
<ui:SettingsExpander |
||||||
|
Header="{x:Static lang:Resources.Label_License}" > |
||||||
|
<ui:SettingsExpander.Footer> |
||||||
|
<Button |
||||||
|
Tapped="LicenseButton_OnTapped" |
||||||
|
Classes="transparent-full" |
||||||
|
Foreground="{DynamicResource HyperlinkButtonForeground}"> |
||||||
|
<StackPanel Orientation="Horizontal"> |
||||||
|
<ui:SymbolIcon |
||||||
|
Symbol="Link" |
||||||
|
FontSize="15" |
||||||
|
Foreground="{DynamicResource HyperlinkButtonForeground}" |
||||||
|
Margin="0,1,4,0"/> |
||||||
|
<TextBlock |
||||||
|
Foreground="{DynamicResource HyperlinkButtonForeground}" |
||||||
|
Text="{Binding Resource.LicenseType}"/> |
||||||
|
</StackPanel> |
||||||
|
</Button> |
||||||
|
</ui:SettingsExpander.Footer> |
||||||
|
</ui:SettingsExpander> |
||||||
|
|
||||||
|
</StackPanel> |
||||||
|
</Grid> |
||||||
|
</controls:UserControlBase> |
@ -0,0 +1,27 @@ |
|||||||
|
using Avalonia; |
||||||
|
using Avalonia.Controls; |
||||||
|
using Avalonia.Input; |
||||||
|
using Avalonia.Markup.Xaml; |
||||||
|
using StabilityMatrix.Avalonia.ViewModels.Dialogs; |
||||||
|
using StabilityMatrix.Core.Processes; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Avalonia.Views.Dialogs; |
||||||
|
|
||||||
|
public partial class DownloadResourceDialog : UserControl |
||||||
|
{ |
||||||
|
public DownloadResourceDialog() |
||||||
|
{ |
||||||
|
InitializeComponent(); |
||||||
|
} |
||||||
|
|
||||||
|
private void InitializeComponent() |
||||||
|
{ |
||||||
|
AvaloniaXamlLoader.Load(this); |
||||||
|
} |
||||||
|
|
||||||
|
private void LicenseButton_OnTapped(object? sender, TappedEventArgs e) |
||||||
|
{ |
||||||
|
var url = ((DownloadResourceViewModel)DataContext!).Resource.LicenseUrl; |
||||||
|
ProcessRunner.OpenUrl(url!.ToString()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,17 @@ |
|||||||
|
using System.Diagnostics.CodeAnalysis; |
||||||
|
using StabilityMatrix.Core.Services; |
||||||
|
|
||||||
|
namespace StabilityMatrix.Core.Models; |
||||||
|
|
||||||
|
public class ModelPostDownloadContextAction : IContextAction |
||||||
|
{ |
||||||
|
/// <inheritdoc /> |
||||||
|
public object? Context { get; set; } |
||||||
|
|
||||||
|
[SuppressMessage("Performance", "CA1822:Mark members as static")] |
||||||
|
public void Invoke(IModelIndexService modelIndexService) |
||||||
|
{ |
||||||
|
// Request reindex |
||||||
|
modelIndexService.BackgroundRefreshIndex(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue