PlaylistHelper.cs 4.3 KB

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