App.razor.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using FNZCM.Shared.MediaModels;
  2. using FNZCM.Shared.MetadataModels;
  3. using Microsoft.AspNetCore.Components;
  4. using Microsoft.JSInterop;
  5. namespace FNZCM.BlazorWasm.UI
  6. {
  7. public partial class App
  8. {
  9. [Inject]
  10. private NavigationManager nm { get; set; }
  11. [Inject]
  12. private IJSRuntime js { get; set; }
  13. private LoadingProgress? Progress { get; set; }
  14. private Dictionary<string, Library>? Libraries { get; set; }
  15. private Dictionary<string, MediaTag>? MediaTags { get; set; }
  16. private Library CurrentLibrary { get; set; }
  17. private string CurrentLibraryKey { get; set; }
  18. private Disc? CurrentDisc { get; set; }
  19. private string CurrentDiscKey { get; set; }
  20. private List<KeyValuePair<string, Library>> SelectedLibs { get; set; } = new();
  21. protected override async Task OnInitializedAsync()
  22. {
  23. ApiClient.HellNm = nm;
  24. base.OnInitialized();
  25. do
  26. {
  27. Progress = await ApiClient.GetProgress();
  28. StateHasChanged();
  29. if (Progress?.IsLoading == false) break;
  30. Thread.Sleep(1000);
  31. } while (true);
  32. Libraries = await ApiClient.GetLibraries();
  33. StateHasChanged();
  34. MediaTags = await ApiClient.GetMediaTags();
  35. if (Libraries?.Any() == true)
  36. {
  37. var f = Libraries.OrderBy(p => p.Key).FirstOrDefault();
  38. CurrentLibraryKey = f.Key;
  39. CurrentLibrary = f.Value;
  40. }
  41. StateHasChanged();
  42. }
  43. private void SelectLibrary(string libKey)
  44. {
  45. CurrentLibrary = Libraries[libKey];
  46. CurrentLibraryKey = libKey;
  47. }
  48. private void SelectDisc(string discKey)
  49. {
  50. CurrentDisc = CurrentLibrary.Discs[discKey];
  51. CurrentDiscKey = discKey;
  52. js.InvokeVoidAsync("ShowModalDisc");
  53. }
  54. private string GetDiscDurationDisplay(string lib, string disc)
  55. {
  56. var totalSeconds = Libraries[lib].Discs[disc].MainTracks.Sum(p =>
  57. {
  58. var mediaPath = $"/media/{lib}/{disc}/{p.Key}";
  59. return MediaTags.TryGetValue(mediaPath, out var tag)
  60. ? tag.Duration
  61. : 0;
  62. });
  63. var ts = TimeSpan.FromSeconds(totalSeconds);
  64. return ts.Hours > 0
  65. ? $"{Math.Floor(ts.TotalHours):00}:{ts.Minutes:00}:{ts.Seconds:00}"
  66. : $"{Math.Floor(ts.TotalMinutes):00}:{ts.Seconds:00}";
  67. }
  68. public void SelectedLibraryChanged()
  69. {
  70. if (SelectedLibs.Count > 0)
  71. {
  72. var f = SelectedLibs.First();
  73. CurrentLibraryKey = f.Key;
  74. CurrentLibrary = f.Value;
  75. StateHasChanged();
  76. }
  77. }
  78. }
  79. }