public void LoadElements() { PlayListTracksOC.Clear(); if (!isLoaded) { int count = 0; string sql = $"SELECT idTrack, title, duration FROM Track WHERE idTrack IN (SELECT trackID FROM Track_has_Playlist WHERE playlistID = {PlaylistController.Playlist.TrackListID})"; List <Dictionary <string, object> > tracks = DatabaseConnector.SelectQueryDB(sql); int trackid = 0; string name = ""; int duration = 0; foreach (var dictionary in tracks) { foreach (var key in dictionary) { if (key.Key.Equals("idTrack")) { trackid = (int)key.Value; count++; } else if (key.Key.Equals("title")) { name = (string)key.Value; count++; } else if (key.Key.Equals("duration")) { duration = (int)key.Value; count++; } } if (count % 3 == 0) { Track track = new Track() { TrackId = trackid, Name = name, Duration = duration }; PlayListTracksOC.Add(track); if (!PlaylistController.Playlist.Tracks.Any(item => item.TrackId == track.TrackId)) { PlaylistController.Playlist.Tracks.AddLast(track); } } } isLoaded = true; } else { foreach (var item in PlaylistController.Playlist.Tracks) { if (!PlayListTracksOC.Contains(item)) { PlayListTracksOC.Add(item); } } } }
public void DeleteTrackClick(object sender) { int index = (int)sender; //get the ID of the track to be deleted int count = PlayListTracksOC.Count; for (int i = 0; i < count; i++) { if (PlayListTracksOC.ElementAt(i).TrackId == index)//compare the index to all ID in the playlist collection { Track track = PlayListTracksOC.ElementAt(i); PlaylistController.RemoveTrack(track); // removes the track out of the database LoadElements(); // refreshes the GUI break; } } }