From 93239d46f1a0e483863f521b8e85372cdba4f076 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Fri, 30 Jun 2023 01:18:07 +0700 Subject: [PATCH 1/2] Added file size to import button It is done on CivitAI site, so why not here? Also generaly it is much better to know how much data you have to download --- StabilityMatrix/App.xaml.cs | 6 ++- StabilityMatrix/CheckpointBrowserPage.xaml | 10 +++- StabilityMatrix/Models/Api/CivitModel.cs | 23 +++++++++ StabilityMatrix/Models/FileSizeType.cs | 55 ++++++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 StabilityMatrix/Models/FileSizeType.cs diff --git a/StabilityMatrix/App.xaml.cs b/StabilityMatrix/App.xaml.cs index c02ba56b..6aea9972 100644 --- a/StabilityMatrix/App.xaml.cs +++ b/StabilityMatrix/App.xaml.cs @@ -216,7 +216,7 @@ namespace StabilityMatrix DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }) }; - + // HTTP Policies var retryStatusCodes = new[] { HttpStatusCode.RequestTimeout, // 408 @@ -247,7 +247,9 @@ namespace StabilityMatrix }); // Add Refit clients - serviceCollection.AddRefitClient(defaultRefitSettings) + // @NuarkNoir: When passing config to this method we are loosing sizeKB from response, who knows why. + // @NuarkNoit: Another solution, anyone? + serviceCollection.AddRefitClient() .ConfigureHttpClient(c => { c.BaseAddress = new Uri("https://civitai.com"); diff --git a/StabilityMatrix/CheckpointBrowserPage.xaml b/StabilityMatrix/CheckpointBrowserPage.xaml index 3a784e6e..1be92033 100644 --- a/StabilityMatrix/CheckpointBrowserPage.xaml +++ b/StabilityMatrix/CheckpointBrowserPage.xaml @@ -113,9 +113,15 @@ Appearance="Primary" Command="{Binding ImportCommand}" CommandParameter="{Binding CivitModel}" - Content="Import" HorizontalAlignment="Stretch" - Margin="0,8,0,0" /> + Margin="0,8,0,0"> + + + + + + + diff --git a/StabilityMatrix/Models/Api/CivitModel.cs b/StabilityMatrix/Models/Api/CivitModel.cs index b9d56f56..1d0b6f90 100644 --- a/StabilityMatrix/Models/Api/CivitModel.cs +++ b/StabilityMatrix/Models/Api/CivitModel.cs @@ -36,4 +36,27 @@ public class CivitModel [BsonRef("ModelVersions")] [JsonPropertyName("modelVersions")] public List? ModelVersions { get; set; } + + private FileSizeType? _fullFilesSize; + public FileSizeType FullFilesSize + { + get + { + if (_fullFilesSize == null) + { + var kbs = 0.0; + if (ModelVersions is not null) + { + var latestVersion = ModelVersions[0]; + if (latestVersion.Files is not null) + { + var latestModelFile = latestVersion.Files[0]; + kbs = latestModelFile.SizeKb; + } + } + _fullFilesSize = new FileSizeType(kbs); + } + return _fullFilesSize; + } + } } diff --git a/StabilityMatrix/Models/FileSizeType.cs b/StabilityMatrix/Models/FileSizeType.cs new file mode 100644 index 00000000..98dd7b5b --- /dev/null +++ b/StabilityMatrix/Models/FileSizeType.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using System.Collections.ObjectModel; +using System.Diagnostics; +using System.Drawing; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Security.Policy; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Media.Imaging; +using System.Windows.Threading; +using AsyncAwaitBestPractices; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using NLog; +using StabilityMatrix.Extensions; +using StabilityMatrix.Helper; + +namespace StabilityMatrix.Models; + +public class FileSizeType +{ + public double SizeInKB { get; private set; } + + public string HumanReadableRepresentation { get; private set; } + + public FileSizeType(double sizeInKB) + { + SizeInKB = sizeInKB; + HumanReadableRepresentation = ConvertToHumanReadable(); + } + + private string ConvertToHumanReadable() + { + string[] sizeUnits = { "KB", "MB", "GB", "TB" }; + double size = SizeInKB; + int unitIndex = 0; + + while (size >= 1024 && unitIndex < sizeUnits.Length - 1) + { + size /= 1024; + unitIndex++; + } + + return string.Format("{0} {1}", size.ToString("0.##", CultureInfo.InvariantCulture), sizeUnits[unitIndex]); + } + + public override string ToString() + { + return HumanReadableRepresentation; + } +} From 2cff40476fcc7f248e56f5e906d7aa3819e50230 Mon Sep 17 00:00:00 2001 From: Andrew nuark G Date: Fri, 30 Jun 2023 12:00:43 +0700 Subject: [PATCH 2/2] Applied requested changes to codestyle --- StabilityMatrix/App.xaml.cs | 4 +--- StabilityMatrix/Models/Api/CivitFile.cs | 2 +- StabilityMatrix/Models/FileSizeType.cs | 27 ++++--------------------- 3 files changed, 6 insertions(+), 27 deletions(-) diff --git a/StabilityMatrix/App.xaml.cs b/StabilityMatrix/App.xaml.cs index 6aea9972..3665af0b 100644 --- a/StabilityMatrix/App.xaml.cs +++ b/StabilityMatrix/App.xaml.cs @@ -247,9 +247,7 @@ namespace StabilityMatrix }); // Add Refit clients - // @NuarkNoir: When passing config to this method we are loosing sizeKB from response, who knows why. - // @NuarkNoit: Another solution, anyone? - serviceCollection.AddRefitClient() + serviceCollection.AddRefitClient(defaultRefitSettings) .ConfigureHttpClient(c => { c.BaseAddress = new Uri("https://civitai.com"); diff --git a/StabilityMatrix/Models/Api/CivitFile.cs b/StabilityMatrix/Models/Api/CivitFile.cs index 626f2e30..01d2fd7f 100644 --- a/StabilityMatrix/Models/Api/CivitFile.cs +++ b/StabilityMatrix/Models/Api/CivitFile.cs @@ -5,7 +5,7 @@ namespace StabilityMatrix.Models.Api; public class CivitFile { - [JsonPropertyName("sizeKb")] + [JsonPropertyName("sizeKB")] public double SizeKb { get; set; } [JsonPropertyName("pickleScanResult")] diff --git a/StabilityMatrix/Models/FileSizeType.cs b/StabilityMatrix/Models/FileSizeType.cs index 98dd7b5b..9edcf63b 100644 --- a/StabilityMatrix/Models/FileSizeType.cs +++ b/StabilityMatrix/Models/FileSizeType.cs @@ -1,23 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Collections.Immutable; -using System.Collections.ObjectModel; -using System.Diagnostics; -using System.Drawing; -using System.Globalization; -using System.IO; -using System.Linq; -using System.Security.Policy; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Media.Imaging; -using System.Windows.Threading; -using AsyncAwaitBestPractices; -using CommunityToolkit.Mvvm.ComponentModel; -using CommunityToolkit.Mvvm.Input; -using NLog; -using StabilityMatrix.Extensions; -using StabilityMatrix.Helper; +using System.Globalization; namespace StabilityMatrix.Models; @@ -35,9 +16,9 @@ public class FileSizeType private string ConvertToHumanReadable() { - string[] sizeUnits = { "KB", "MB", "GB", "TB" }; - double size = SizeInKB; - int unitIndex = 0; + var sizeUnits = new string[] { "KB", "MB", "GB", "TB" }; + var size = SizeInKB; + var unitIndex = 0; while (size >= 1024 && unitIndex < sizeUnits.Length - 1) {