Library update callbacks can now be disabled by providing a null LibraryCallbackInterface.
This commit is contained in:
parent
bc53f088cf
commit
fb611c3062
@ -22,24 +22,42 @@ import java.util.stream.Collectors;
|
|||||||
public final class LibraryUtils {
|
public final class LibraryUtils {
|
||||||
|
|
||||||
static final String musicFileExtensionRegex = "(?iu).*\\.(mp3|mp4|flac|ogg)";
|
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){
|
public static void processSongsWithCallback(List<Path> rootDirectory, LibraryCallbackInterface callbackInterface){
|
||||||
Thread thread = new Thread(() -> {
|
boolean callbacksEnabled = callbackInterface != null;
|
||||||
|
updaterThread = new Thread(() -> {
|
||||||
rootDirectory.forEach(dir -> {
|
rootDirectory.forEach(dir -> {
|
||||||
try {
|
try {
|
||||||
Files.walk(dir)
|
Files.walk(dir)
|
||||||
.filter(f -> f.toString().matches(musicFileExtensionRegex)).map(LibraryUtils::autoParse)
|
.filter(f -> f.toString().matches(musicFileExtensionRegex)).map(LibraryUtils::autoParse)
|
||||||
.forEach(x -> x.ifPresent(i -> {
|
.forEach(x -> x.ifPresent(i -> {
|
||||||
Gateway.addSong(i);
|
Gateway.addSong(i);
|
||||||
callbackInterface.currentlyUpdating(i.toString());
|
if(callbacksEnabled)
|
||||||
|
callbackInterface.currentlyUpdating(i.toString());
|
||||||
}));
|
}));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
callbackInterface.libraryUpdated(false);
|
if(callbacksEnabled)
|
||||||
|
callbackInterface.libraryUpdated(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
callbackInterface.libraryUpdated(true);
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user