Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Displaying Sizes and Numbers using Humanize #2762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/NexusMods.App.UI/Controls/TreeDataGrid/SizeComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,38 @@ public sealed class SizeComponent : AFormattedValueComponent<Size>, IItemModelCo
{
/// <inheritdoc/>
public int CompareTo(SizeComponent? other) => Value.Value.CompareTo(other?.Value.Value ?? Size.Zero);
private static string _FormatValue(Size value) => ByteSize.FromBytes(value.Value).Humanize();

private static string _FormatValue(Size value)
{
var byteSize = ByteSize.FromBytes(value.Value);
return byteSize.Gigabytes < 1 ? byteSize.Humanize("0") : byteSize.Humanize("0.0");
Copy link
Member

@erri120 erri120 Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the expected result here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the size is a gigabyte or more it has 1 decimal place and if not then it displays no decimal places.

}

/// <inheritdoc/>
protected override string FormatValue(Size value) => _FormatValue(value);

/// <inheritdoc/>
public SizeComponent(
Size initialValue,
IObservable<Size> valueObservable,
bool subscribeWhenCreated = false) : base(initialValue, _FormatValue(initialValue), valueObservable, subscribeWhenCreated) { }
bool subscribeWhenCreated = false) : base(initialValue, _FormatValue(initialValue), valueObservable,
subscribeWhenCreated
)
{
}

/// <inheritdoc/>
public SizeComponent(
Size initialValue,
Observable<Size> valueObservable,
bool subscribeWhenCreated = false) : base(initialValue, _FormatValue(initialValue), valueObservable, subscribeWhenCreated) { }
bool subscribeWhenCreated = false) : base(initialValue, _FormatValue(initialValue), valueObservable,
subscribeWhenCreated
)
{
}

/// <inheritdoc/>
public SizeComponent(Size value) : base(value, _FormatValue(value)) { }
public SizeComponent(Size value) : base(value, _FormatValue(value))
{
}
}
38 changes: 38 additions & 0 deletions src/NexusMods.App.UI/Converters/SizeToStringTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Humanizer;
using Humanizer.Bytes;
using NexusMods.Paths;
using ReactiveUI;

namespace NexusMods.App.UI.Converters;

/// <summary>
/// NexusMods.Paths.Size To String Type Converter.
/// </summary>
public class SizeToStringTypeConverter : IBindingTypeConverter
{
/// <inheritdoc/>
public int GetAffinityForObjects(Type fromType, Type toType)
{
if (fromType == typeof(Size) && toType == typeof(string))
{
return 999;
}

return 0;
}

/// <inheritdoc/>
public bool TryConvert(object? from, Type toType, object? conversionHint, out object result)
{
if (toType == typeof(string) && from is Size fromSize)
{
var byteSize = ByteSize.FromBytes(fromSize.Value);
result = byteSize.Gigabytes < 1 ? byteSize.Humanize("0") : byteSize.Humanize("0.0");

return true;
}

result = null!;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using NexusMods.MnemonicDB.Abstractions;
using R3;
using ReactiveUI;
using Humanizer;
using NexusMods.App.UI.Converters;

namespace NexusMods.App.UI.Pages.CollectionDownload;

Expand Down Expand Up @@ -80,13 +82,13 @@ public CollectionDownloadView()
this.OneWayBind(ViewModel, vm => vm.AuthorAvatar, view => view.AuthorAvatar.Source)
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.DownloadCount, view => view.NumDownloads.Text)
this.OneWayBind(ViewModel, vm => vm.DownloadCount, view => view.NumDownloads.Text, v => v.ToString("N0"))
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.EndorsementCount, view => view.Endorsements.Text)
this.OneWayBind(ViewModel, vm => vm.EndorsementCount, view => view.Endorsements.Text, v => Convert.ToInt32(v).ToMetric(null, 1))
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.TotalDownloads, view => view.TotalDownloads.Text)
this.OneWayBind(ViewModel, vm => vm.TotalDownloads, view => view.TotalDownloads.Text, v => Convert.ToInt32(v).ToMetric(null, 1))
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.TotalSize, view => view.TotalSize.Text)
Expand All @@ -95,15 +97,15 @@ public CollectionDownloadView()
this.OneWayBind(ViewModel, vm => vm.OverallRating, view => view.OverallRating.Text)
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.RequiredDownloadsCount, view => view.RequiredDownloadsCount.Text)
this.OneWayBind(ViewModel, vm => vm.RequiredDownloadsCount, view => view.RequiredDownloadsCount.Text, v => v.ToString("N0"))
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.OptionalDownloadsCount, view => view.OptionalDownloadsCount.Text)
this.OneWayBind(ViewModel, vm => vm.OptionalDownloadsCount, view => view.OptionalDownloadsCount.Text, v => v.ToString("N0"))
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.RevisionNumber, view => view.Revision.Text, revision => $"Revision {revision}")
.DisposeWith(d);

this.WhenAnyValue(
view => view.ViewModel!.IsUpdateAvailable.Value,
view => view.ViewModel!.NewestRevisionNumber.Value)
Expand Down Expand Up @@ -188,7 +190,7 @@ public CollectionDownloadView()
.Subscribe(className =>
{
OverallRatingPanel.Classes.Add(className);
OverallRating.Text = className == "NoRating" ? "--" : ViewModel!.OverallRating.Value.ToString("P2");
OverallRating.Text = className == "NoRating" ? "--" : ViewModel!.OverallRating.Value.ToString("P0");
}
)
.DisposeWith(d);
Expand Down Expand Up @@ -222,5 +224,8 @@ public CollectionDownloadView()
}).DisposeWith(d);
});
}



}

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CollectionCardView()
this.OneWayBind(ViewModel, vm => vm.RevisionNumber, view => view.RevisionText.Text, revision => $"Revision {revision}")
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.NumDownloads, view => view.ModsCountText.Text, count => $"{count} Mods")
this.OneWayBind(ViewModel, vm => vm.NumDownloads, view => view.ModsCountText.Text, count => $"{count:N0} Mods")
.DisposeWith(d);

this.OneWayBind(ViewModel, vm => vm.TotalSize, view => view.TotalSize.Text)
Expand Down
5 changes: 4 additions & 1 deletion src/NexusMods.App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using NexusMods.App.BuildInfo;
using NexusMods.App.UI;
using NexusMods.App.UI.Converters;
using NexusMods.App.UI.Windows;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
Expand Down Expand Up @@ -109,7 +110,9 @@ public static AppBuilder BuildAvaloniaApp(IServiceProvider serviceProvider)

Locator.CurrentMutable.UnregisterCurrent(typeof(IViewLocator));
Locator.CurrentMutable.Register(serviceProvider.GetRequiredService<InjectedViewLocator>, typeof(IViewLocator));


Locator.CurrentMutable.RegisterConstant<IBindingTypeConverter>(new SizeToStringTypeConverter());

var logger = serviceProvider.GetRequiredService<ILogger<Startup>>();
ObservableSystem.RegisterUnhandledExceptionHandler(exception =>
{
Expand Down
Loading