PlaylistHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using FNZCM.BlazorWasm.Models.Fe;
  2. using System.Collections;
  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 FePlaylist[] PlaylistTableStore
  11. {
  12. get => localStorage.GetByCallerMemberName<FePlaylist[]>() ?? Array.Empty<FePlaylist>();
  13. private set => localStorage.SetByCallerMemberName(value);
  14. }
  15. public IReadOnlyDictionary<Guid, FePlaylist> PlayListLoadSave { get; }
  16. public PlaylistHelper(LocalStorageHelper localStorage)
  17. {
  18. this.localStorage = localStorage;
  19. PlayListLoadSave = new ListTableDictAdapter(this);
  20. //import old data and remove
  21. var oldTable = localStorage.Get<Dictionary<Guid, string>>("PlayListLoadSave");
  22. if (oldTable is not { Count: > 0 }) return;
  23. var newTable = PlaylistTableStore.ToList();
  24. var index = 0;
  25. foreach (var item in oldTable)
  26. {
  27. newTable.Insert(index++, new FePlaylist { Id = item.Key, Name = item.Value });
  28. }
  29. PlaylistTableStore = newTable.ToArray();
  30. localStorage.Remove("PlayListLoadSave");
  31. }
  32. public class ListTableDictAdapter : IReadOnlyDictionary<Guid, FePlaylist>
  33. {
  34. private readonly PlaylistHelper context;
  35. public ListTableDictAdapter(PlaylistHelper context) => this.context = context;
  36. public int Count => context.PlaylistTableStore.Length;
  37. public IEnumerator<KeyValuePair<Guid, FePlaylist>> GetEnumerator() => context.PlaylistTableStore.Select(p => new KeyValuePair<Guid, FePlaylist>(p.Id, p)).GetEnumerator();
  38. IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  39. public bool ContainsKey(Guid key) => context.PlaylistTableStore.Any(p => p.Id == key);
  40. public bool TryGetValue(Guid key, out FePlaylist value) => null != (value = context.PlaylistTableStore.FirstOrDefault(p => p.Id == key));
  41. public FePlaylist this[Guid key] => context.PlaylistTableStore.FirstOrDefault(p => p.Id == key);
  42. public IEnumerable<Guid> Keys => context.PlaylistTableStore.Select(p => p.Id);
  43. public IEnumerable<FePlaylist> Values => context.PlaylistTableStore;
  44. }
  45. public List<string> this[Guid playlistId]
  46. {
  47. get => localStorage.GetByCallerMemberName<List<string>>($"playlist-{playlistId}") ?? new List<string>();
  48. set => localStorage.SetByCallerMemberName(value, $"playlist-{playlistId}");
  49. }
  50. public void CreateNewPlaylist(string name)
  51. {
  52. var newItem = new FePlaylist { Name = name, Id = Guid.NewGuid() };
  53. var newTable = PlaylistTableStore.ToList();
  54. newTable.Add(newItem);
  55. PlaylistTableStore = newTable.ToArray();
  56. Changed?.Invoke();
  57. }
  58. public void AddTrackToPlaylist(string trackPath, Guid playlistId)
  59. {
  60. var p = this[playlistId];
  61. p.Add(trackPath);
  62. this[playlistId] = p;
  63. Changed?.Invoke();
  64. }
  65. public void RemoveTrackInPlaylist(Guid playlistId, int index, string trackPathForConfirm)
  66. {
  67. var p = this[playlistId];
  68. if (index < p.Count && p[index] == trackPathForConfirm) p.RemoveAt(index);
  69. this[playlistId] = p;
  70. Changed?.Invoke();
  71. }
  72. public void DeletePlaylist(Guid playlistId)
  73. {
  74. localStorage.RemoveByCallerMemberName($"playlist-{playlistId}");
  75. var newTableArray = PlaylistTableStore;
  76. var item = newTableArray.FirstOrDefault(p => p.Id == playlistId);
  77. if (item == null) return;
  78. var newTable = newTableArray.ToList();
  79. newTable.Remove(item);
  80. PlaylistTableStore = newTable.ToArray();
  81. Changed?.Invoke();
  82. }
  83. public void UpdatePlaylistName(Guid playlistId, string name)
  84. {
  85. var newTableArray = PlaylistTableStore;
  86. var item = newTableArray.FirstOrDefault(p => p.Id == playlistId);
  87. if (item == null) return;
  88. item.Name = name;
  89. PlaylistTableStore = newTableArray.ToArray();
  90. Changed?.Invoke();
  91. }
  92. public void ListMoveUp(int index)
  93. {
  94. if (index <= 0) return;
  95. var l = new ObservableCollection<FePlaylist>(PlaylistTableStore);
  96. l.Move(index, index - 1);
  97. PlaylistTableStore = l.ToArray();
  98. Changed?.Invoke();
  99. }
  100. public void ListMoveDown(int index)
  101. {
  102. var l = new ObservableCollection<FePlaylist>(PlaylistTableStore);
  103. if (index >= l.Count - 1) return;
  104. l.Move(index, index + 1);
  105. PlaylistTableStore = l.ToArray();
  106. Changed?.Invoke();
  107. }
  108. public void ListMove(Guid id, int index)
  109. {
  110. if (index < 0) return;
  111. var l = new ObservableCollection<FePlaylist>(PlaylistTableStore);
  112. if (index >= l.Count) return;
  113. var item = l.FirstOrDefault(p => p.Id == id);
  114. if (item == null) return;
  115. var oldIndex = l.IndexOf(item);
  116. l.Move(oldIndex, index);
  117. PlaylistTableStore = l.ToArray();
  118. Changed?.Invoke();
  119. }
  120. public void TrackMoveUp(Guid playlistId, int index)
  121. {
  122. if (index <= 0) return;
  123. var l = new ObservableCollection<string>(this[playlistId]);
  124. l.Move(index, index - 1);
  125. this[playlistId] = l.ToList();
  126. Changed?.Invoke();
  127. }
  128. public void TrackMoveDown(Guid playlistId, int index)
  129. {
  130. var l = new ObservableCollection<string>(this[playlistId]);
  131. if (index >= l.Count - 1) return;
  132. l.Move(index, index + 1);
  133. this[playlistId] = l.ToList();
  134. Changed?.Invoke();
  135. }
  136. public void TrackMove(Guid fromListId, int fromTrackIndex, string fromTrackPath, Guid toListId, int toTrackIndex)
  137. {
  138. var fromList = this[fromListId];
  139. if (fromList == null) return;
  140. if (fromTrackIndex > fromList.Count - 1) return;
  141. if (fromList[fromTrackIndex] != fromTrackPath) return;
  142. fromList.RemoveAt(fromTrackIndex);
  143. if (fromListId != toListId)
  144. {
  145. var toList = this[toListId];
  146. if (toList == null) return;
  147. if (toTrackIndex > toList.Count) return;
  148. toList.Insert(toTrackIndex, fromTrackPath);
  149. this[toListId] = toList;
  150. }
  151. else
  152. {
  153. fromList.Insert(toTrackIndex, fromTrackPath);
  154. }
  155. this[fromListId] = fromList;
  156. Changed?.Invoke();
  157. }
  158. }
  159. }