From fe2a1cdeada60ddb52c438cc2cacf3683d5e0789 Mon Sep 17 00:00:00 2001 From: Jeeses313 Date: Fri, 3 Nov 2023 01:00:01 +0200 Subject: [PATCH] Fix folders having duplicate songs --- src/musicplayer/MusicPlayer.java | 2 ++ src/musicplayer/SongList.java | 8 +++----- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/musicplayer/MusicPlayer.java b/src/musicplayer/MusicPlayer.java index 2e66ebc..4a516f9 100644 --- a/src/musicplayer/MusicPlayer.java +++ b/src/musicplayer/MusicPlayer.java @@ -220,6 +220,8 @@ public void changed(ObservableValue ov, String t, String t1) { } else { this.currentSong = this.list.getRandomSong("", this.currentFolder); this.nextSong = this.list.getRandomSong(currentSong, this.currentFolder); + listView.getSelectionModel().select(currentSong); + listView.scrollTo(listView.getSelectionModel().getSelectedIndex()); } currentSongLabel.setText("Playing now: " + currentSong); nextSongLabel.setText("Playing next: " + nextSong); diff --git a/src/musicplayer/SongList.java b/src/musicplayer/SongList.java index d71658a..4c225fc 100644 --- a/src/musicplayer/SongList.java +++ b/src/musicplayer/SongList.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.Random; import java.util.Stack; +import java.util.stream.Collectors; import javafx.scene.media.Media; public class SongList { @@ -37,15 +38,12 @@ public boolean init(String path) { this.initPart(musicFiles, folderStack); Collections.sort(folderNames.subList(1, folderNames.size())); for (String folderName : folderNames) { - this.folderContents.get(folderName).sort((name1, name2) -> name1.toLowerCase().compareTo(name2.toLowerCase())); + this.folderContents.put(folderName, new ArrayList<>(this.folderContents.get(folderName).stream().sorted((name1, name2) -> name1.toLowerCase().compareTo(name2.toLowerCase())).distinct().collect(Collectors.toList()))); } } catch (Exception e) { } - if (this.folderContents.get("All").isEmpty()) { - return false; - } - return true; + return !this.folderContents.get("All").isEmpty(); } private void initPart(File[] musicFiles, Stack folderStack) throws Exception {