PlaylistHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using FNZCM.BlazorWasm.Utility;
  2. using Microsoft.AspNetCore.Components;
  3. using System.Collections.ObjectModel;
  4. namespace FNZCM.BlazorWasm.Helpers
  5. {
  6. public class PlaylistHelper
  7. {
  8. public event Action Changed;
  9. private readonly LocalStorageHelper localStorage;
  10. public PlaylistHelper(LocalStorageHelper localStorage) => this.localStorage = localStorage;
  11. public Dictionary<Guid, string> PlayListLoadSave { get => localStorage.Get<Dictionary<Guid, string>>() ?? new Dictionary<Guid, string>(); set => localStorage.Set(value); }
  12. public List<string> this[Guid playlistId]
  13. {
  14. get => localStorage.Get<List<string>>($"playlist-{playlistId}") ?? new List<string>();
  15. set => localStorage.Set(value, $"playlist-{playlistId}");
  16. }
  17. public void CreateNewPlaylist(string name)
  18. {
  19. var pls = PlayListLoadSave;
  20. pls[Guid.NewGuid()] = name;
  21. PlayListLoadSave = pls;
  22. Changed?.Invoke();
  23. }
  24. public void AddTrackToPlaylist(string trackPath, Guid playlistId)
  25. {
  26. var p = this[playlistId];
  27. p.Add(trackPath);
  28. this[playlistId] = p;
  29. Changed?.Invoke();
  30. }
  31. public void RemoveTrackInPlaylist(Guid playlistId, int index, string trackPathForConfirm)
  32. {
  33. var p = this[playlistId];
  34. if (index < p.Count && p[index] == trackPathForConfirm) p.RemoveAt(index);
  35. this[playlistId] = p;
  36. Changed?.Invoke();
  37. }
  38. public void Deleteplaylist(Guid playlistId)
  39. {
  40. localStorage.Remove($"playlist-{playlistId}");
  41. var pls = PlayListLoadSave;
  42. pls.Remove(playlistId);
  43. PlayListLoadSave = pls;
  44. Changed?.Invoke();
  45. }
  46. public void UpdatePlaylistName(Guid playlistId, string name)
  47. {
  48. var pls = PlayListLoadSave;
  49. pls[playlistId] = name;
  50. PlayListLoadSave = pls;
  51. Changed?.Invoke();
  52. }
  53. public void ListMoveUp(int index)
  54. {
  55. if (index < 1) return;
  56. // let's say index = 3 and count = 6
  57. var pls = PlayListLoadSave;
  58. var re = new Dictionary<Guid, string>();
  59. foreach (var item in pls.Take(index - 1)) // take2 01→01
  60. {
  61. re.Add(item.Key, item.Value);
  62. }
  63. foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→2
  64. {
  65. re.Add(item.Key, item.Value);
  66. }
  67. foreach (var item in pls.Skip(index - 1).Take(1)) // skip2t1 2→3
  68. {
  69. re.Add(item.Key, item.Value);
  70. }
  71. foreach (var item in pls.Skip(index + 1)) // skip4 45→45
  72. {
  73. re.Add(item.Key, item.Value);
  74. }
  75. PlayListLoadSave = re;
  76. Changed?.Invoke();
  77. }
  78. public void ListMoveDown(int index)
  79. {
  80. var pls = PlayListLoadSave;
  81. if (index >= pls.Count - 1) return;
  82. // let's say index = 3 and count = 6
  83. var re = new Dictionary<Guid, string>();
  84. foreach (var item in pls.Take(index)) // take3 012→012
  85. {
  86. re.Add(item.Key, item.Value);
  87. }
  88. foreach (var item in pls.Skip(index + 1).Take(1)) // skip4t1 4→3
  89. {
  90. re.Add(item.Key, item.Value);
  91. }
  92. foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→4
  93. {
  94. re.Add(item.Key, item.Value);
  95. }
  96. foreach (var item in pls.Skip(index + 2)) // skip5 5→5
  97. {
  98. re.Add(item.Key, item.Value);
  99. }
  100. PlayListLoadSave = re;
  101. Changed?.Invoke();
  102. }
  103. public void TrackMoveUp(Guid playlistId, int index)
  104. {
  105. var l = new ObservableCollection<string>(this[playlistId]);
  106. if (index > 0) l.Move(index, index - 1);
  107. this[playlistId] = l.ToList();
  108. Changed?.Invoke();
  109. }
  110. public void TrackMoveDown(Guid playlistId, int index)
  111. {
  112. var l = new ObservableCollection<string>(this[playlistId]);
  113. if (index < l.Count - 1) l.Move(index, index + 1);
  114. this[playlistId] = l.ToList();
  115. Changed?.Invoke();
  116. }
  117. }
  118. }