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

Improve the Duration Shape #16745

Merged
merged 13 commits into from
Sep 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
@inject IOptions<CookieAuthenticationOptions> CookieAuthenticationOptions

@model OrchardCore.Users.Models.TwoFactorLoginSettings

@{
var duration = await DisplayAsync(await New.Duration(timeSpan: CookieAuthenticationOptions.Value.ExpireTimeSpan));
}
<fieldset>
<legend>@T["Two-Factor Settings"]</legend>

Expand All @@ -14,8 +16,7 @@
<span asp-validation-for="AllowRememberClientTwoFactorAuthentication"></span>
<label class="form-check-label" asp-for="AllowRememberClientTwoFactorAuthentication">@T["Allow users to remember client"]</label>
<span class="hint dashed">
@T["When selected, users may use Remember Client during login to avoid having to provide a token every time."]
@T["The client will be remembered for:"] @await DisplayAsync(await New.Duration(timeSpan: CookieAuthenticationOptions.Value.ExpireTimeSpan))
@T["When selected, users may use Remember Client during login to avoid having to provide a token every time. The client will be remembered for: {0}", duration]
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Localization;
using OrchardCore.DisplayManagement.Descriptors;
using OrchardCore.DisplayManagement.Html;
using OrchardCore.Modules;

namespace OrchardCore.DisplayManagement.Shapes;
Expand Down Expand Up @@ -110,8 +109,18 @@ public IHtmlContent TimeSpan(DateTime? Utc, DateTime? Origin)
}

return time.TotalMilliseconds > 0
? H["a moment ago"]
: H["in a moment"];
? H["a moment ago"]
: H["in a moment"];
}

[Shape]
public async Task<IHtmlContent> DateTime(IHtmlHelper Html, DateTime? Utc, string Format)
{
Utc ??= _clock.UtcNow;
var zonedTime = await _localClock.ConvertToLocalAsync(Utc.Value);
Format ??= S[LongDateTimeFormat].Value;

return Html.Raw(Html.Encode(zonedTime.ToString(Format, CultureInfo.CurrentUICulture)));
}

[Shape]
Expand All @@ -122,58 +131,76 @@ public IHtmlContent Duration(TimeSpan? timeSpan)
return HtmlString.Empty;
}

var days = (int)timeSpan.Value.TotalDays;
var hours = timeSpan.Value.Hours;
var minutes = timeSpan.Value.Minutes;
var seconds = timeSpan.Value.Seconds;
var tag = new TagBuilder("span");
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

tag.AddCssClass("timespan-preview-value");

tag.Attributes["title"] = timeSpan.ToString();

var builder = new HtmlContentBuilder();
tag.InnerHtml.AppendHtml(GetDuration(timeSpan.Value));

return tag;
}

private LocalizedHtmlString GetDuration(TimeSpan timeSpan)
{
var days = timeSpan.Days;
var hours = timeSpan.Hours;
var minutes = timeSpan.Minutes;
var seconds = timeSpan.Seconds;

if (days > 0)
{
builder.AppendHtml(H.Plural(days, "{1} day", "{1} days", days));
return GetDurationInDays(timeSpan, days);
}

if (hours > 0)
{
if (builder.Count > 0)
{
builder.AppendWhitespace();
}
builder.AppendHtml(H.Plural(hours, "{1} hour", "{1} hours", hours));
return GetDurationInHours(timeSpan, hours);
}

if (minutes > 0)
{
if (builder.Count > 0)
{
builder.AppendWhitespace();
}

builder.AppendHtml(H.Plural(hours, "{1} minute", "{1} minutes", minutes));
return GetDurationInMinutes(timeSpan, minutes);
}

if (seconds > 0)
return H.Plural(seconds, "1 second", "{0} seconds");
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
}

private LocalizedHtmlString GetDurationInDays(TimeSpan timeSpan, int days)
{
var totalDays = timeSpan.TotalDays;

if (days == totalDays)
{
if (builder.Count > 0)
{
builder.AppendWhitespace();
}
return H.Plural(days, "1 day", "{0} days");
}

builder.AppendHtml(H.Plural(seconds, "{1} second", "{1} seconds", seconds));
return H.Plural((int)Math.Round(totalDays), "Approximately a day", "Approximately {0} days");
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
}

private LocalizedHtmlString GetDurationInHours(TimeSpan timeSpan, int hours)
{
var totalHours = timeSpan.TotalHours;

if (hours == totalHours)
{
return H.Plural(hours, "1 hour", "{0} hours");
}

return builder;
return H.Plural((int)Math.Round(totalHours), "Approximately an hour", "Approximately {0} hours");
}

[Shape]
public async Task<IHtmlContent> DateTime(IHtmlHelper Html, DateTime? Utc, string Format)
private LocalizedHtmlString GetDurationInMinutes(TimeSpan timeSpan, int minutes)
{
Utc ??= _clock.UtcNow;
var zonedTime = await _localClock.ConvertToLocalAsync(Utc.Value);
Format ??= S[LongDateTimeFormat].Value;
var totalMinutes = timeSpan.TotalMinutes;

return Html.Raw(Html.Encode(zonedTime.ToString(Format, CultureInfo.CurrentUICulture)));
if (minutes == totalMinutes)
{
return H.Plural(minutes, "1 minute", "{0} minutes");
}

return H.Plural((int)Math.Round(totalMinutes), "Approximately a minute", "Approximately {0} minutes");
}
}

Expand Down
Loading