#region ========================================================================= USING =====================================================================================
using Lumina.Presentation.Web.Common.DTO.Themes;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Localization;
using System.Collections.Generic;
#endregion
namespace Lumina.Presentation.Web.Core.Themes;
///
/// Builds the audio player model for the themed component template, resolving the localized strings and theme asset URLs server side.
///
public sealed class ThemeAudioPlayerBuilder
{
// The resource base name deliberately excludes the "Core.Resources" segment: the localization factory re-roots it with the configured ResourcesPath.
private const string VIEW_RESOURCE_BASE_NAME = "Lumina.Presentation.Web.Views.Shared._Layout";
private const string VIEW_RESOURCE_LOCATION = "Lumina.Presentation.Web";
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IStringLocalizer _localizer;
///
/// Initializes a new instance of the class.
///
/// Injected factory used to create the localizer of the audio player resources.
/// Injected accessor for the current HTTP context.
public ThemeAudioPlayerBuilder(IStringLocalizerFactory stringLocalizerFactory, IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
_localizer = stringLocalizerFactory.Create(VIEW_RESOURCE_BASE_NAME, VIEW_RESOURCE_LOCATION);
}
///
/// Builds the model of the audio player for the given theme.
///
/// The base URL of the assets of the active theme.
/// The raw template source of a queue item, or an empty string when the theme does not provide one.
/// The raw template source of a playlist item, or an empty string when the theme does not provide one.
/// The model available to the themed audio player template.
public ThemeAudioPlayerDto Build(string assetBase, string queueItemTemplate, string playlistItemTemplate)
{
Dictionary strings = new()
{
["toggleFullHeight"] = Localize("ToggleFullHeight"),
["toggleVisibility"] = Localize("ToggleVisibility"),
["previous"] = Localize("AudioPlayerPrevious"),
["next"] = Localize("AudioPlayerNext"),
["shuffle"] = Localize("AudioPlayerShuffle"),
["shuffleOff"] = Localize("AudioPlayerShuffleOff"),
["shuffleRelease"] = Localize("AudioPlayerShuffleRelease"),
["shuffleArtist"] = Localize("AudioPlayerShuffleArtist"),
["shuffleLibrary"] = Localize("AudioPlayerShuffleLibrary"),
["repeat"] = Localize("AudioPlayerRepeat"),
["repeatOff"] = Localize("AudioPlayerRepeatOff"),
["repeatAll"] = Localize("AudioPlayerRepeatAll"),
["repeatOne"] = Localize("AudioPlayerRepeatOne"),
["queue"] = Localize("AudioPlayerQueue")
};
return new ThemeAudioPlayerDto(
AppBase: GetAppBaseUrl(),
AssetBase: assetBase,
QueueItemTemplate: queueItemTemplate,
PlaylistItemTemplate: playlistItemTemplate,
Strings: strings);
}
///
/// Gets the absolute base URL of the current Web application request, used to reference the application scripts from a static theme template.
///
/// The absolute base URL with a trailing slash, or an empty string when the current HTTP context is unavailable.
private string GetAppBaseUrl()
{
HttpRequest? request = _httpContextAccessor.HttpContext?.Request;
if (request is null)
return string.Empty;
return $"{request.Scheme}://{request.Host}{request.PathBase}/";
}
///
/// Resolves a localized audio player string.
///
/// The resource key of the string.
/// The localized string.
private string Localize(string key)
{
return _localizer[key].Value;
}
}