Files
decePubClient/Extensions/GenericExtensions.cs
T

95 lines
2.7 KiB
C#
Raw Normal View History

using System.Collections.Specialized;
2022-02-14 01:51:52 +01:00
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Web;
2022-02-14 01:51:37 +01:00
2022-02-14 01:51:52 +01:00
using Blazored.LocalStorage;
using decePubClient.Models;
using decePubClient.Models.Types;
using decePubClient.Resources;
using decePubClient.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Localization;
namespace decePubClient.Extensions;
public static class GenericExtensions
2022-02-14 01:51:37 +01:00
{
public static string GetPassedTime(this DateTime dateTime, CoalescingStringLocalizer localizer)
2022-02-14 01:51:52 +01:00
{
var timeframe = DateTime.Now - dateTime.ToLocalTime();
return (int)timeframe.TotalHours switch
2022-02-14 01:51:52 +01:00
{
>= 24 => localizer["{0}d", (int)timeframe.TotalDays],
>= 1 => localizer["{0}h", (int)timeframe.TotalHours],
_ => localizer["{0}m", (int)timeframe.TotalMinutes]
};
2022-02-14 01:51:52 +01:00
}
2026-10-06 23:13:23 +02:00
// a count as a phone's narrow action buttons show it: 999, 1.2k, 12k, 1.2M
public static string Compact(this int count) => count switch
{
< 1000 => count.ToString(CultureInfo.CurrentCulture),
< 10_000 => (count / 1000d).ToString("0.#", CultureInfo.CurrentCulture) + "k",
< 1_000_000 => (count / 1000).ToString(CultureInfo.CurrentCulture) + "k",
_ => (count / 1_000_000d).ToString("0.#", CultureInfo.CurrentCulture) + "M"
};
public static string GetFileSize(this long size, CoalescingStringLocalizer localizer) => size switch
2022-02-14 01:51:52 +01:00
{
>= 1_048_576 => localizer["{0}MB", size / 1_048_576],
_ => localizer["{0}KB", size / 1_024]
};
2022-02-14 01:51:52 +01:00
public static string GetVisibilityIcon(this Visibility visibility)
2022-02-14 01:51:52 +01:00
{
switch (visibility)
2022-02-14 01:51:52 +01:00
{
case Visibility.Public:
2022-02-14 01:51:52 +01:00
return "ion-md-globe";
case Visibility.Unlisted:
2022-02-14 01:51:52 +01:00
return "ion-md-unlock";
case Visibility.FollowersOnly:
2022-02-14 01:51:52 +01:00
return "ion-md-lock";
case Visibility.Direct:
2022-02-14 01:51:52 +01:00
return "ion-md-paper-plane";
default:
return default;
}
}
public static string GetTimelineTypeIcon(this TimelineType timelineType) => timelineType switch
2022-02-14 01:51:52 +01:00
{
TimelineType.Home => "ion-md-home",
TimelineType.Local => "ion-md-people",
TimelineType.Federation => "ion-md-globe",
TimelineType.Nearby => "ion-md-locate",
TimelineType.Groups => "ion-md-contacts",
TimelineType.Direct => "ion-md-mail",
TimelineType.Notifications => "ion-md-notifications",
TimelineType.Favourites => "ion-md-heart",
TimelineType.Bookmarks => "ion-md-bookmark",
TimelineType.Tag => "ion-md-pricetag",
TimelineType.Lists => "ion-md-list",
_ => default
};
2022-02-14 01:51:52 +01:00
public static string GetMediaTypeIcon(this MediaType mediaType)
{
switch (mediaType)
{
case MediaType.Images:
return "ion-md-images";
case MediaType.Video:
return "ion-md-videocam";
case MediaType.Audio:
return "ion-md-volume-high";
default:
return default;
}
}
2022-02-14 01:51:37 +01:00
}