use of se.light.assembly64.model.NonReturningTask in project assembly64fx by freabemania.
the class SidifyExportPlaylistController method export.
public void export() throws Exception {
int idx = locations.getSelectionModel().getSelectedIndex();
List<String> exportLocations = new ArrayList<>();
if (idx != 0) {
exportLocations.addAll(Collections.singletonList(locations.getSelectionModel().getSelectedItem()));
} else {
exportLocations.addAll(locations.getItems().stream().filter(item -> !item.equals(ALL_LOCATIONS)).collect(Collectors.toList()));
}
int items = playlists.stream().map(item -> playlistService.getSongsForPlaylist(item)).map(item -> item.size()).mapToInt(i -> i.intValue()).sum() * exportLocations.size();
CancelableTask cancelTask = CancelableTask.of();
ProgressDBController controller = GuiUtils.showDialogOwnerNoWait("progressBarDbUpdate.fxml", "Progress", true, NullWindowOwner.of(), new Object[] { items, cancelTask, exportLocations.get(0) + MUSIC_SIDIFY, "Playlist exported to <location>/Music/Sidify" });
NonReturningTask t = () -> {
try {
for (PlaylistInfo playlist : playlists) {
List<PlaylistEntry> songsForPlaylist = playlistService.getSongsForPlaylist(playlist);
int padding = Integer.valueOf(String.valueOf(songsForPlaylist.size()).length());
for (String dir : exportLocations) {
File exportDir = new File(dir + MUSIC_SIDIFY + playlist.getName());
FileUtils.deleteQuietly(exportDir);
FileUtils.forceMkdir(exportDir);
int ctr = 1;
for (PlaylistEntry entry : playlistService.getSongsForPlaylist(playlist)) {
String pos = String.format("%0" + padding + "d", ctr);
try {
controller.increaseProgress();
controller.setProgressLabel("Exporting " + entry.getNameMasked());
FileUtils.copyFile(downloadService.getSid(entry), new File(exportDir + "/" + pos + "_" + entry.getNameMasked() + ".sid"));
ctr++;
} catch (Exception e) {
e.printStackTrace();
}
if (cancelTask.isCancelled()) {
FileUtils.deleteQuietly(exportDir);
break;
}
}
}
if (cancelTask.isCancelled()) {
break;
}
}
} finally {
controller.progressDone();
}
};
ExecutorUtil.executeAsync(t);
}
Aggregations