PlaylistHandler.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. namespace MusicPlayer
  6. {
  7. public class PlaylistHandler
  8. {
  9. private List<Playlist> playlists;
  10. private APIHandler api;
  11. private readonly string basedir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\.mpplaylists\\";
  12. public PlaylistHandler(APIHandler api)
  13. {
  14. this.playlists = new List<Playlist>();
  15. this.api = api;
  16. Populate();
  17. }
  18. private void Populate()
  19. {
  20. try {
  21. Directory.GetFiles(basedir).ToList().ForEach(f =>
  22. {
  23. if (f.EndsWith(".txt"))
  24. {
  25. playlists.Add(new Playlist(Path.GetFileName(f.Replace(".txt","")) ,basedir, api));
  26. }
  27. });
  28. }
  29. catch (DirectoryNotFoundException)
  30. {
  31. Directory.CreateDirectory(basedir);
  32. Populate();
  33. }
  34. }
  35. public void MakeNewPlaylistByName(string name)
  36. {
  37. playlists.Add(new Playlist(name, basedir, api));
  38. }
  39. public Playlist GetPlaylistByName(string name)
  40. {
  41. Playlist toFind = null;
  42. playlists.ForEach(p =>
  43. {
  44. if (p.name == name) { toFind = p; }
  45. });
  46. return toFind;
  47. }
  48. public List<Playlist> GetPlaylists()
  49. {
  50. return playlists;
  51. }
  52. }
  53. }