Добавлен вывод названия трека и обложки в плеере
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using MudBlazor;
|
||||
using PlaylistShared.Shared;
|
||||
using PlaylistShared.Shared.DTO;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace PlaylistShared.Pwa.Services;
|
||||
|
||||
@@ -6,8 +9,11 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
{
|
||||
private readonly TokenStorage _tokenStorage;
|
||||
private readonly ISnackbar _snackbar;
|
||||
private readonly HttpClient _http;
|
||||
|
||||
private string? _currentTrackId;
|
||||
private string? _currentTrackTitle;
|
||||
private string? _currentTrackCoverUrl;
|
||||
private bool _isPlaying;
|
||||
private double _currentVolume = 70;
|
||||
private double _currentProgress;
|
||||
@@ -15,6 +21,8 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
private string _totalTime = "0:00";
|
||||
|
||||
public string? CurrentTrackId => _currentTrackId;
|
||||
public string? CurrentTrackTitle => _currentTrackTitle;
|
||||
public string? CurrentTrackCoverUrl => _currentTrackCoverUrl;
|
||||
public bool IsPlaying => _isPlaying;
|
||||
public double CurrentVolume
|
||||
{
|
||||
@@ -31,14 +39,15 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
|
||||
public event Action? OnStateChanged;
|
||||
|
||||
public AudioPlayerService(TokenStorage tokenStorage, ISnackbar snackbar)
|
||||
public AudioPlayerService(TokenStorage tokenStorage, ISnackbar snackbar, HttpClient httpClient)
|
||||
{
|
||||
_tokenStorage = tokenStorage;
|
||||
_snackbar = snackbar;
|
||||
_http = httpClient;
|
||||
}
|
||||
|
||||
// Внешние команды (вызываются из компонентов)
|
||||
public async Task LoadAndPlayAsync(string trackId, string? accessToken = null, string? sharedPlaylistId = null)
|
||||
public async Task LoadAndPlayAsync(string trackId, string? accessToken = null, string? sharedPlaylistId = null, string? title = null, string? coverUrl = null)
|
||||
{
|
||||
// Если accessToken не передан, пытаемся получить его из хранилища
|
||||
if (string.IsNullOrWhiteSpace(accessToken))
|
||||
@@ -54,7 +63,25 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
return;
|
||||
}
|
||||
|
||||
// Если title и coverUrl не переданы, нужно запросить через API
|
||||
if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(coverUrl))
|
||||
{
|
||||
try
|
||||
{
|
||||
var trackInfo = await GetTrackInfo(trackId, accessToken, sharedPlaylistId);
|
||||
title = trackInfo?.Title;
|
||||
coverUrl = trackInfo?.CoverUri;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Логируем ошибку, но продолжаем без обложки/названия
|
||||
Console.WriteLine($"Failed to fetch track info: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
_currentTrackId = trackId;
|
||||
_currentTrackTitle = title ?? "Неизвестный трек";
|
||||
_currentTrackCoverUrl = coverUrl;
|
||||
_isPlaying = true;
|
||||
OnStateChanged?.Invoke();
|
||||
OnLoadAndPlayRequested?.Invoke(trackId, accessToken, sharedPlaylistId);
|
||||
@@ -133,4 +160,27 @@ public class AudioPlayerService : IAudioPlayerService
|
||||
_currentTime = "0:00";
|
||||
OnStateChanged?.Invoke();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Вспомогательный метод для получения информации о треке через API
|
||||
/// </summary>
|
||||
/// <param name="trackId"></param>
|
||||
/// <param name="accessToken"></param>
|
||||
/// <param name="sharedPlaylistId"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<(string? Title, string? CoverUri)?> GetTrackInfo(string trackId, string? accessToken, string? sharedPlaylistId)
|
||||
{
|
||||
var url = $"/api/audio/track-info/{trackId}";
|
||||
if (!string.IsNullOrEmpty(accessToken))
|
||||
url += $"?access_token={accessToken}";
|
||||
else if (!string.IsNullOrEmpty(sharedPlaylistId))
|
||||
url += $"?shared_id={sharedPlaylistId}";
|
||||
|
||||
var response = await _http.GetFromJsonAsync<ApiResponse<TrackInfoDto>>(url);
|
||||
if (response?.Success == true)
|
||||
{
|
||||
return (response.Data.Title, response.Data.CoverUri);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user