use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class AddCommand method addPlayables.
private void addPlayables(Playlist playlist, List<Playable> playables) {
if ((getTask() != null && getTask().isTerminated()) || Thread.currentThread().isInterrupted()) {
return;
}
Session session = getContext().getSession();
List<PlaylistItem> items = Lists.newArrayList();
invoke(() -> {
playables.forEach(playable -> {
if (playable instanceof HollowYouTubeVideo && ((HollowYouTubeVideo) playable).isCanceled()) {
return;
}
items.add(playable.export(playlist, getContext().getUser(), session));
});
addToList(playlist, items);
});
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class AddCommand method addToList.
protected void addToList(Playlist playlist, List<PlaylistItem> items) {
if (items.isEmpty()) {
throw new NoResultsFoundException("Result is empty!");
}
Session session = getContext().getSession();
invoke(() -> items.forEach(item -> {
item.add();
session.persist(item);
}));
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class ExportCommand method doRun.
@Override
public void doRun() {
Session session = getContext().getSession();
Guild guild = getContext().getGuild();
AudioQueue queue = Aiode.get().getAudioManager().getQueue(guild);
if (queue.isEmpty()) {
throw new InvalidCommandException("Queue is empty");
}
Playlist existingPlaylist = SearchEngine.searchLocalList(session, getCommandInput());
if (existingPlaylist != null) {
throw new InvalidCommandException("Playlist " + getCommandInput() + " already exists");
}
List<Playable> tracks = queue.getTracks();
User createUser = getContext().getUser();
Playlist playlist = new Playlist(getCommandInput(), createUser, guild);
invoke(() -> {
session.persist(playlist);
for (Playable track : tracks) {
if (track instanceof HollowYouTubeVideo) {
HollowYouTubeVideo video = (HollowYouTubeVideo) track;
video.awaitCompletion();
if (video.isCanceled()) {
continue;
}
}
PlaylistItem export = track.export(playlist, getContext().getUser(), session);
export.add();
session.persist(export);
}
});
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class InsertCommand method addToList.
@Override
protected void addToList(Playlist playlist, List<PlaylistItem> items) {
if (!(targetIndex > 0 && targetIndex <= playlist.getSize())) {
throw new InvalidCommandException(String.format("Invalid index: %d. Index is not within playlist of size %d. Use the add command to add items at the end of the list instead.", targetIndex, playlist.getSize()));
}
int actualIndex = targetIndex - 1;
// move back all items that are at or after the target index
List<PlaylistItem> itemsSorted = playlist.getItemsSorted(true);
for (int i = actualIndex; i < itemsSorted.size(); i++) {
PlaylistItem item = itemsSorted.get(i);
item.setIndex(item.getIndex() + items.size());
}
// assign target indices
for (int i = 0; i < items.size(); i++) {
PlaylistItem item = items.get(i);
item.setIndex(actualIndex + i);
}
super.addToList(playlist, items);
}
use of net.robinfriedli.aiode.entities.PlaylistItem in project aiode by robinfriedli.
the class UpdatePlaylistItemIndicesTask method perform.
@Override
public Void perform() {
for (Playlist playlist : playlistsToUpdate) {
List<PlaylistItem> items = playlist.getItems();
List<PlaylistItem> itemsOrdered = items.stream().filter(item -> item.getIndex() != null).sorted(Comparator.comparing(PlaylistItem::getIndex)).collect(Collectors.toList());
List<PlaylistItem> addedItems = items.stream().filter(item -> item.getIndex() == null).sorted(sorter).collect(Collectors.toList());
itemsOrdered.addAll(addedItems);
for (int i = 0; i < itemsOrdered.size(); i++) {
PlaylistItem item = itemsOrdered.get(i);
if (item.getIndex() == null || item.getIndex() != i) {
item.setIndex(i);
}
}
}
return null;
}
Aggregations