Main.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. namespace MusicPlayer
  8. {
  9. public class Main
  10. {
  11. public APIHandler api;
  12. public MainForm form;
  13. public NetworkHandler nw;
  14. public PlaylistHandler pl;
  15. public AudioHandler audio;
  16. public SongsTable table;
  17. private ImageList imagelist;
  18. private List<string> genres;
  19. private List<string> artists;
  20. public List<Song> currentPlayingList;
  21. public Main(NetworkHandler nw, APIHandler api, MainForm form, PlaylistHandler pl)
  22. {
  23. this.nw = nw;
  24. this.api = api;
  25. this.form = form;
  26. form.main = this;
  27. this.pl = pl;
  28. audio = new AudioHandler(this);
  29. table = new SongsTable();
  30. imagelist = new ImageList();
  31. form.SongsTableView.DataSource = table;
  32. form.SongsTableView.Columns[5].Visible = false;
  33. genres = new List<string>();
  34. artists = new List<string>();
  35. currentPlayingList = new List<Song>();
  36. Populate();
  37. }
  38. private void Populate()
  39. {
  40. form.AlbumListView.LargeImageList = imagelist;
  41. this.api.GetAlbums().ForEach(a => { var item = form.AlbumListView.Items.Add(a.albumnaam); imagelist.Images.Add(a.albumnaam, a.cover); item.ImageKey = a.albumnaam;});
  42. this.api.GetArtists().ForEach(a => { artists.Add(a.naam); form.ArtistListBox.Items.Add(a.naam); });
  43. this.api.GetGenres().ForEach(g => { genres.Add(g.name); form.GenreListBox.Items.Add(g.name); });
  44. this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name));
  45. }
  46. public void Repopulate()
  47. {
  48. form.AlbumListView.Items.Clear();
  49. form.ArtistListBox.Items.Clear();
  50. form.GenreListBox.Items.Clear();
  51. form.PlaylistBox.Items.Clear();
  52. this.api.GetAlbums().ForEach(a => form.AlbumListView.Items.Add(a.albumnaam));
  53. this.api.GetArtists().ForEach(a => form.ArtistListBox.Items.Add(a.naam));
  54. this.api.GetGenres().ForEach(g => form.GenreListBox.Items.Add(g.name));
  55. this.pl.GetPlaylists().ForEach(p => form.PlaylistBox.Items.Add(p.name));
  56. }
  57. public void ArtistFilter(string artist)
  58. {
  59. table.Clear();
  60. api.GetSongsByArtist(artist).ForEach(s =>
  61. {
  62. table.Add(s);
  63. });
  64. }
  65. public void FilterCurrentPlaying()
  66. {
  67. table.Clear();
  68. currentPlayingList.ForEach(s =>
  69. {
  70. table.Add(s);
  71. });
  72. }
  73. public void SearchArtist(string search)
  74. {
  75. form.ArtistListBox.Items.Clear();
  76. if (search.Length > 1)
  77. {
  78. string sPattern = search;
  79. foreach (string s in artists)
  80. {
  81. if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  82. {
  83. form.ArtistListBox.Items.Add(s);
  84. }
  85. }
  86. }
  87. else
  88. {
  89. artists.ForEach(a => form.ArtistListBox.Items.Add(a));
  90. }
  91. }
  92. public void SearchGenre(string search)
  93. {
  94. form.GenreListBox.Items.Clear();
  95. if (search.Length > 1)
  96. {
  97. string sPattern = search;
  98. foreach (string s in genres)
  99. {
  100. if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
  101. {
  102. form.GenreListBox.Items.Add(s);
  103. }
  104. }
  105. }
  106. else
  107. {
  108. genres.ForEach(g => form.GenreListBox.Items.Add(g));
  109. }
  110. }
  111. public void GenreFilter(string genre)
  112. {
  113. table.Clear();
  114. api.GetSongsByGenre(genre).ForEach(s =>
  115. {
  116. table.Add(s);
  117. });
  118. }
  119. public void PlaylistFilter(string name)
  120. {
  121. table.Clear();
  122. pl.GetPlaylistByName(name).GetSongs().ForEach(s => table.Add(s));
  123. }
  124. public void AlbumFilter(string album)
  125. {
  126. table.Clear();
  127. api.GetSongsByAlbum(album).ForEach(s =>
  128. {
  129. table.Add(s);
  130. });
  131. }
  132. public static string SecondsToTimestamp(int seconds)
  133. {
  134. string str = "";
  135. //Hours
  136. str += (seconds / 3600).ToString("D2") + ":";
  137. seconds %= 3600;
  138. //Minutes
  139. str += (seconds / 60).ToString("D2") + ":";
  140. seconds %= 60;
  141. //Seconds
  142. str += seconds.ToString("D2");
  143. return str;
  144. }
  145. }
  146. }