diff --git a/src/main/java/musicplayer/LibraryUtils.java b/src/main/java/musicplayer/LibraryUtils.java index a9ec3a0..7e4ccdd 100644 --- a/src/main/java/musicplayer/LibraryUtils.java +++ b/src/main/java/musicplayer/LibraryUtils.java @@ -1,5 +1,6 @@ package musicplayer; +import musicplayer.callbacks.LibraryCallbackInterface; import musicplayer.db.Gateway; import musicplayer.model.ExtractedMetadata; import org.jaudiotagger.audio.AudioFileIO; @@ -12,25 +13,51 @@ import org.jaudiotagger.tag.TagException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Optional; public final class LibraryUtils { static final String musicFileExtensionRegex = ".*\\.(mp3|mp4|flac)"; + public static void processSongsWithCallback(List rootDirectory, LibraryCallbackInterface callbackInterface){ + Thread thread = new Thread(() -> { + try { + processSongs(rootDirectory); + callbackInterface.libraryUpdated(true); + } catch (IOException e) { + callbackInterface.libraryUpdated(false); + } + }); + thread.start(); + } + + public static void processSongsWithCallback(Path rootDirectory, LibraryCallbackInterface callbackInterface){ + Thread thread = new Thread(() -> { + try { + processSongs(rootDirectory); + callbackInterface.libraryUpdated(true); + } catch (IOException e) { + callbackInterface.libraryUpdated(false); + } + }); + thread.start(); + } + + public static void processSongs(List rootDirectories) throws IOException { + for(Path rootDirectory: rootDirectories) + processSongs(rootDirectory); + } + /** * Walk through all files and directories recursively and index any music files with a correct extension * * @param rootDirectory Directory from which to start searching */ - public static void processSongs(Path rootDirectory) { - try { - Files.walk(rootDirectory) - .filter(f -> f.toString().matches(musicFileExtensionRegex)) - .map(LibraryUtils::autoParse).filter(Optional::isPresent).map(Optional::get).forEach(Gateway::addSong); - } catch (IOException e) { - e.printStackTrace(); - } + public static void processSongs(Path rootDirectory) throws IOException { + Files.walk(rootDirectory) + .filter(f -> f.toString().matches(musicFileExtensionRegex)) + .map(LibraryUtils::autoParse).filter(Optional::isPresent).map(Optional::get).forEach(Gateway::addSong); } /** diff --git a/src/main/java/musicplayer/Player.java b/src/main/java/musicplayer/Player.java index 91843db..abc6155 100644 --- a/src/main/java/musicplayer/Player.java +++ b/src/main/java/musicplayer/Player.java @@ -1,5 +1,6 @@ package musicplayer; +import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.model.Song; import org.gstreamer.ElementFactory; import org.gstreamer.Gst; diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index e9f0909..224d350 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -1,5 +1,7 @@ package musicplayer; +import musicplayer.callbacks.LibraryCallbackInterface; +import musicplayer.callbacks.PlayerCallbackInterface; import musicplayer.db.DatabaseManager; import musicplayer.db.Gateway; import musicplayer.model.Album; @@ -12,7 +14,7 @@ import javax.swing.tree.TreeNode; import java.awt.event.*; import java.util.*; -public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface{ +public class PlayerGUI implements PlayerCallbackInterface, LibraryCallbackInterface { private JTree libraryView; private JTable playList; private JPanel mainPanel; diff --git a/src/main/java/musicplayer/LibraryCallbackInterface.java b/src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java similarity index 75% rename from src/main/java/musicplayer/LibraryCallbackInterface.java rename to src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java index d5cbaaf..535e2bc 100644 --- a/src/main/java/musicplayer/LibraryCallbackInterface.java +++ b/src/main/java/musicplayer/callbacks/LibraryCallbackInterface.java @@ -1,4 +1,4 @@ -package musicplayer; +package musicplayer.callbacks; public interface LibraryCallbackInterface { diff --git a/src/main/java/musicplayer/PlayerCallbackInterface.java b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java similarity index 87% rename from src/main/java/musicplayer/PlayerCallbackInterface.java rename to src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java index 2e76fda..a63b5aa 100644 --- a/src/main/java/musicplayer/PlayerCallbackInterface.java +++ b/src/main/java/musicplayer/callbacks/PlayerCallbackInterface.java @@ -1,4 +1,4 @@ -package musicplayer; +package musicplayer.callbacks; import musicplayer.model.Song;