123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using FNZCM.BlazorWasm.Utility;
- using Microsoft.AspNetCore.Components;
- using System.Collections.ObjectModel;
- namespace FNZCM.BlazorWasm.Helpers
- {
- public class PlaylistHelper
- {
- public event Action Changed;
- private readonly LocalStorageHelper localStorage;
- public PlaylistHelper(LocalStorageHelper localStorage) => this.localStorage = localStorage;
- public Dictionary<Guid, string> PlayListLoadSave { get => localStorage.Get<Dictionary<Guid, string>>() ?? new Dictionary<Guid, string>(); set => localStorage.Set(value); }
- public List<string> this[Guid playlistId]
- {
- get => localStorage.Get<List<string>>($"playlist-{playlistId}") ?? new List<string>();
- set => localStorage.Set(value, $"playlist-{playlistId}");
- }
- public void CreateNewPlaylist(string name)
- {
- var pls = PlayListLoadSave;
- pls[Guid.NewGuid()] = name;
- PlayListLoadSave = pls;
- Changed?.Invoke();
- }
- public void AddTrackToPlaylist(string trackPath, Guid playlistId)
- {
- var p = this[playlistId];
- p.Add(trackPath);
- this[playlistId] = p;
- Changed?.Invoke();
- }
- public void RemoveTrackInPlaylist(Guid playlistId, int index, string trackPathForConfirm)
- {
- var p = this[playlistId];
- if (index < p.Count && p[index] == trackPathForConfirm) p.RemoveAt(index);
- this[playlistId] = p;
- Changed?.Invoke();
- }
- public void Deleteplaylist(Guid playlistId)
- {
- localStorage.Remove($"playlist-{playlistId}");
- var pls = PlayListLoadSave;
- pls.Remove(playlistId);
- PlayListLoadSave = pls;
- Changed?.Invoke();
- }
- public void UpdatePlaylistName(Guid playlistId, string name)
- {
- var pls = PlayListLoadSave;
- pls[playlistId] = name;
- PlayListLoadSave = pls;
- Changed?.Invoke();
- }
- public void ListMoveUp(int index)
- {
- if (index < 1) return;
- // let's say index = 3 and count = 6
- var pls = PlayListLoadSave;
- var re = new Dictionary<Guid, string>();
- foreach (var item in pls.Take(index - 1)) // take2 01→01
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→2
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index - 1).Take(1)) // skip2t1 2→3
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index + 1)) // skip4 45→45
- {
- re.Add(item.Key, item.Value);
- }
- PlayListLoadSave = re;
- Changed?.Invoke();
- }
- public void ListMoveDown(int index)
- {
- var pls = PlayListLoadSave;
- if (index >= pls.Count - 1) return;
- // let's say index = 3 and count = 6
- var re = new Dictionary<Guid, string>();
- foreach (var item in pls.Take(index)) // take3 012→012
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index + 1).Take(1)) // skip4t1 4→3
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index).Take(1)) // skip3t1 3→4
- {
- re.Add(item.Key, item.Value);
- }
- foreach (var item in pls.Skip(index + 2)) // skip5 5→5
- {
- re.Add(item.Key, item.Value);
- }
- PlayListLoadSave = re;
- Changed?.Invoke();
- }
- public void TrackMoveUp(Guid playlistId, int index)
- {
- var l = new ObservableCollection<string>(this[playlistId]);
- if (index > 0) l.Move(index, index - 1);
- this[playlistId] = l.ToList();
- Changed?.Invoke();
- }
- public void TrackMoveDown(Guid playlistId, int index)
- {
- var l = new ObservableCollection<string>(this[playlistId]);
- if (index < l.Count - 1) l.Move(index, index + 1);
- this[playlistId] = l.ToList();
- Changed?.Invoke();
- }
- }
- }
|