Library update callbacks can now be disabled by providing a null LibraryCallbackInterface.

This commit is contained in:
neviyn 2016-02-29 02:27:11 +00:00
parent bc53f088cf
commit fb611c3062

View File

@ -22,24 +22,42 @@ import java.util.stream.Collectors;
public final class LibraryUtils {
static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)";
static Thread updaterThread;
/**
* Add all songs contained with all paths in rootDirectory to the database.
* @param rootDirectory Folder(s) containing music files to index.
* @param callbackInterface Object to send callback data to, set to null if the application doesn't require update data.
*/
public static void processSongsWithCallback(List<Path> rootDirectory, LibraryCallbackInterface callbackInterface){
Thread thread = new Thread(() -> {
boolean callbacksEnabled = callbackInterface != null;
updaterThread = new Thread(() -> {
rootDirectory.forEach(dir -> {
try {
Files.walk(dir)
.filter(f -> f.toString().matches(musicFileExtensionRegex)).map(LibraryUtils::autoParse)
.forEach(x -> x.ifPresent(i -> {
Gateway.addSong(i);
if(callbacksEnabled)
callbackInterface.currentlyUpdating(i.toString());
}));
} catch (IOException e) {
if(callbacksEnabled)
callbackInterface.libraryUpdated(false);
}
});
if(callbacksEnabled)
callbackInterface.libraryUpdated(true);
});
thread.start();
updaterThread.start();
}
/**
* Halt execution of the library updater thread (if running)
*/
public static void cancelProcessing(){
if(updaterThread.isAlive())
updaterThread.interrupt();
}
/**