Main.cs 5.8 KB

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