use of com.simplecity.amp_library.model.Playlist in project Shuttle by timusus.
the class SuggestedFragment method refreshAdapterItems.
void refreshAdapterItems() {
PermissionUtils.RequestStoragePermissions(() -> {
if (getActivity() != null && isAdded()) {
subscription = new CompositeSubscription();
Observable<Playlist> mostPlayedPlaylistObservable = Observable.fromCallable(Playlist::mostPlayedPlaylist).subscribeOn(Schedulers.io()).cache();
Observable<List<Song>> mostPlayedSongsObservable = mostPlayedPlaylistObservable.filter(playlist -> playlist != null).flatMap(playlist -> playlist.getSongsObservable(getContext())).cache();
Observable<List<AdaptableItem>> mostPlayedItemsObservable = mostPlayedPlaylistObservable.flatMap(playlist -> {
SuggestedHeader mostPlayedHeader = new SuggestedHeader(getString(R.string.mostplayed), getString(R.string.suggested_most_played_songs_subtitle), playlist);
SuggestedHeaderView mostPlayedHeaderView = new SuggestedHeaderView(mostPlayedHeader);
return mostPlayedSongsObservable.map(songs -> {
List<AdaptableItem> items = new ArrayList<>();
if (!songs.isEmpty()) {
items.add(mostPlayedHeaderView);
items.add(mostPlayedRecyclerView);
}
return items;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<List<AdaptableItem>> recentlyPlayedAlbums = Observable.fromCallable(Playlist::recentlyPlayedPlaylist).subscribeOn(Schedulers.io()).filter(playlist -> playlist != null).flatMap(playlist -> {
SuggestedHeader recentlyPlayedHeader = new SuggestedHeader(getString(R.string.suggested_recent_title), getString(R.string.suggested_recent_subtitle), playlist);
SuggestedHeaderView recentlyPlayedHeaderView = new SuggestedHeaderView(recentlyPlayedHeader);
return playlist.getSongsObservable(getContext()).flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMap(Observable::from).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).limit(6).flatMap(album -> album.getSongsObservable().map(songs -> {
album.numSongs = songs.size();
return album;
})).sorted((a, b) -> ComparisonUtils.compareLong(b.lastPlayed, a.lastPlayed)).filter(album -> album.numSongs > 0).map(album -> (AdaptableItem) new AlbumView(album, ViewType.ALBUM_LIST_SMALL, requestManager)).toList().map(adaptableItems -> {
if (!adaptableItems.isEmpty()) {
adaptableItems.add(0, recentlyPlayedHeaderView);
}
return adaptableItems;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<Playlist> favouritesPlaylistObservable = Observable.fromCallable(Playlist::favoritesPlaylist).subscribeOn(Schedulers.io()).cache();
Observable<List<Song>> favouritesSongsObservable = favouritesPlaylistObservable.filter(playlist -> playlist != null).flatMap(playlist -> playlist.getSongsObservable(getContext())).cache();
Observable<List<AdaptableItem>> favoriteSongsItemsObservable = favouritesPlaylistObservable.flatMap(playlist -> {
SuggestedHeader favoriteHeader = new SuggestedHeader(getString(R.string.fav_title), getString(R.string.suggested_favorite_subtitle), playlist);
SuggestedHeaderView favoriteHeaderView = new SuggestedHeaderView(favoriteHeader);
return favouritesSongsObservable.map(songs -> {
List<AdaptableItem> items = new ArrayList<>();
if (!songs.isEmpty()) {
items.add(favoriteHeaderView);
items.add(favoriteRecyclerView);
}
return items;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable<List<AdaptableItem>> recentlyAddedAlbums = Observable.fromCallable(Playlist::recentlyAddedPlaylist).subscribeOn(Schedulers.io()).filter(playlist -> playlist != null).flatMap(playlist -> {
SuggestedHeader recentlyAddedHeader = new SuggestedHeader(getString(R.string.recentlyadded), getString(R.string.suggested_recently_added_subtitle), playlist);
SuggestedHeaderView recentlyAddedHeaderView = new SuggestedHeaderView(recentlyAddedHeader);
return playlist.getSongsObservable(getContext()).flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMap(Observable::from).sorted((a, b) -> ComparisonUtils.compareLong(b.dateAdded, a.dateAdded)).limit(4).flatMap(album -> album.getSongsObservable().map(songs -> {
album.numSongs = songs.size();
return album;
})).sorted((a, b) -> ComparisonUtils.compareLong(b.dateAdded, a.dateAdded)).filter(album -> album.numSongs > 0).map(album -> (AdaptableItem) new AlbumView(album, ViewType.ALBUM_LIST_SMALL, requestManager)).toList().map(adaptableItems -> {
if (!adaptableItems.isEmpty()) {
adaptableItems.add(0, recentlyAddedHeaderView);
}
return adaptableItems;
});
}).switchIfEmpty(Observable.just(Collections.emptyList()));
Observable.merge(mostPlayedItemsObservable, recentlyPlayedAlbums, favoriteSongsItemsObservable, recentlyAddedAlbums);
subscription.add(Observable.combineLatest(mostPlayedItemsObservable, recentlyPlayedAlbums, favoriteSongsItemsObservable, recentlyAddedAlbums, (mostPlayedSongs1, recentlyPlayedAlbums1, favoriteSongs1, recentlyAddedAlbums1) -> {
List<AdaptableItem> items = new ArrayList<>();
items.addAll(mostPlayedSongs1);
items.addAll(recentlyPlayedAlbums1);
items.addAll(favoriteSongs1);
items.addAll(recentlyAddedAlbums1);
return items;
}).debounce(250, TimeUnit.MILLISECONDS).switchIfEmpty(Observable.just(new ArrayList<>())).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
if (adaptableItems.isEmpty()) {
suggestedAdapter.setEmpty(new EmptyView(R.string.empty_suggested));
} else {
suggestedAdapter.setItems(adaptableItems);
}
}));
subscription.add(mostPlayedSongsObservable.map(songs -> {
Collections.sort(songs, (a, b) -> ComparisonUtils.compareInt(b.playCount, a.playCount));
return Stream.of(songs).map(song -> (AdaptableItem) new SuggestedSongView(song, requestManager)).limit(20).collect(Collectors.toList());
}).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
mostPlayedRecyclerView.itemAdapter.setItems(adaptableItems);
}));
subscription.add(favouritesSongsObservable.map(songs -> Stream.of(songs).map(song -> (AdaptableItem) new SuggestedSongView(song, requestManager)).limit(20).collect(Collectors.toList())).observeOn(AndroidSchedulers.mainThread()).subscribe(adaptableItems -> {
favoriteRecyclerView.itemAdapter.setItems(adaptableItems);
}));
}
});
}
use of com.simplecity.amp_library.model.Playlist in project Shuttle by timusus.
the class FolderFragment method onOverflowClick.
@Override
public void onOverflowClick(View v, int position, BaseFileObject fileObject) {
PopupMenu menu = new PopupMenu(getActivity(), v);
if (fileObject.fileType == FileType.FILE) {
//Play this song next
menu.getMenu().add(FRAGMENT_GROUPID, PLAY_NEXT, 4, R.string.play_next);
//Tag editor
if (ShuttleUtils.isUpgraded()) {
menu.getMenu().add(FRAGMENT_GROUPID, TAGGER, 5, R.string.edit_tags);
}
//Set this song as the ringtone
menu.getMenu().add(FRAGMENT_GROUPID, USE_AS_RINGTONE, 6, R.string.ringtone_menu);
if (FileHelper.canReadWrite(new File(fileObject.path))) {
//Rename File
menu.getMenu().add(FRAGMENT_GROUPID, RENAME, 7, R.string.rename_file);
//Delete File
menu.getMenu().add(FRAGMENT_GROUPID, DELETE_ITEM, 8, R.string.delete_item);
}
menu.getMenu().add(FRAGMENT_GROUPID, VIEW_INFO, 9, R.string.song_info);
} else {
//Play all files in this dir
menu.getMenu().add(FRAGMENT_GROUPID, PLAY_SELECTION, 0, R.string.play_selection);
//Set this directory as initial directory
menu.getMenu().add(FRAGMENT_GROUPID, SET_INITIAL_DIR, 4, R.string.set_initial_dir);
if (FileHelper.canReadWrite(new File(fileObject.path))) {
//Rename dir
menu.getMenu().add(FRAGMENT_GROUPID, RENAME, 5, R.string.rename_folder);
//Delete dir
menu.getMenu().add(FRAGMENT_GROUPID, DELETE_ITEM, 6, R.string.delete_item);
}
}
//Bring up the add to playlist menu
SubMenu sub = menu.getMenu().addSubMenu(FRAGMENT_GROUPID, ADD_TO_PLAYLIST, 2, R.string.add_to_playlist);
PlaylistUtils.makePlaylistMenu(getActivity(), sub, FRAGMENT_GROUPID);
//Add to queue
menu.getMenu().add(FRAGMENT_GROUPID, QUEUE, 3, R.string.add_to_queue);
menu.getMenu().add(FRAGMENT_GROUPID, RESCAN, 4, R.string.scan_file);
menu.setOnMenuItemClickListener(item -> {
switch(item.getItemId()) {
case TAGGER:
FileHelper.getSong(new File(fileObject.path)).subscribeOn(Schedulers.io()).subscribe(song -> TaggerDialog.newInstance(song).show(getFragmentManager()));
return true;
case QUEUE:
FileHelper.getSongList(new File(fileObject.path), true, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> MusicUtils.addToQueue(getActivity(), songs));
return true;
case DELETE_ITEM:
MaterialDialog.Builder builder = DialogUtils.getBuilder(getActivity()).title(R.string.delete_item).icon(DrawableUtils.getBlackDrawable(getActivity(), R.drawable.ic_dialog_alert));
if (fileObject.fileType == FileType.FILE) {
builder.content(String.format(getResources().getString(R.string.delete_file_confirmation_dialog), fileObject.name));
} else {
builder.content(String.format(getResources().getString(R.string.delete_folder_confirmation_dialog), fileObject.path));
}
builder.positiveText(R.string.button_ok).onPositive((materialDialog, dialogAction) -> {
if (FileHelper.deleteFile(new File(fileObject.path))) {
mAdapter.removeItem(position);
CustomMediaScanner.scanFiles(Collections.singletonList(fileObject.path), null);
} else {
Toast.makeText(getActivity(), fileObject.fileType == FileType.FOLDER ? R.string.delete_folder_failed : R.string.delete_file_failed, Toast.LENGTH_LONG).show();
}
});
builder.negativeText(R.string.cancel).show();
return true;
case RENAME:
View customView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_rename, null);
final CustomEditText editText = (CustomEditText) customView.findViewById(R.id.editText);
ThemeUtils.themeEditText(editText);
editText.setText(fileObject.name);
builder = DialogUtils.getBuilder(getActivity());
if (fileObject.fileType == FileType.FILE) {
builder.title(R.string.rename_file);
} else {
builder.title(R.string.rename_folder);
}
builder.customView(customView, false);
builder.positiveText(R.string.save).onPositive((materialDialog, dialogAction) -> {
if (editText.getText() != null) {
if (FileHelper.renameFile(getActivity(), fileObject, editText.getText().toString())) {
mAdapter.notifyDataSetChanged();
} else {
Toast.makeText(getActivity(), fileObject.fileType == FileType.FOLDER ? R.string.rename_folder_failed : R.string.rename_file_failed, Toast.LENGTH_LONG).show();
}
}
});
builder.negativeText(R.string.cancel).show();
return true;
case USE_AS_RINGTONE:
FileHelper.getSong(new File(fileObject.path)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(song -> ShuttleUtils.setRingtone(getContext(), song));
return true;
case PLAY_NEXT:
FileHelper.getSongList(new File(fileObject.path), false, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> MusicUtils.playNext(getActivity(), songs));
return true;
case PLAY_SELECTION:
final ProgressDialog progressDialog = ProgressDialog.show(getActivity(), "", getString(R.string.gathering_songs), false);
FileHelper.getSongList(new File(fileObject.path), true, fileObject.fileType == FileType.FILE).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> {
MusicUtils.playAll(songs, 0, () -> {
final String message = getContext().getString(R.string.emptyplaylist);
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
});
if (isAdded() && progressDialog.isShowing()) {
progressDialog.dismiss();
}
});
return true;
case NEW_PLAYLIST:
List<BaseFileObject> fileObjects = new ArrayList<>();
fileObjects.add(fileObject);
PlaylistUtils.createFileObjectPlaylistDialog(getActivity(), fileObjects);
return true;
case PLAYLIST_SELECTED:
final Playlist playlist = (Playlist) item.getIntent().getSerializableExtra(ShuttleUtils.ARG_PLAYLIST);
FileHelper.getSongList(new File(fileObject.path), true, false).observeOn(AndroidSchedulers.mainThread()).subscribe(songs -> PlaylistUtils.addToPlaylist(getContext(), playlist, songs));
return true;
case SET_INITIAL_DIR:
SettingsManager.getInstance().setFolderBrowserInitialDir(fileObject.path);
Toast.makeText(getActivity(), fileObject.path + getResources().getString(R.string.initial_dir_set_message), Toast.LENGTH_SHORT).show();
return true;
case RESCAN:
if (fileObject instanceof FolderObject) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_progress, null);
TextView pathsTextView = (TextView) view.findViewById(R.id.paths);
pathsTextView.setText(fileObject.path);
ProgressBar indeterminateProgress = (ProgressBar) view.findViewById(R.id.indeterminateProgress);
DrawableCompat.setTint(DrawableCompat.wrap(indeterminateProgress.getIndeterminateDrawable()), ColorUtils.getAccentColor());
ProgressBar horizontalProgress = (ProgressBar) view.findViewById(R.id.horizontalProgress);
DrawableCompat.setTint(DrawableCompat.wrap(horizontalProgress.getProgressDrawable()), ColorUtils.getAccentColor());
MaterialDialog dialog = DialogUtils.getBuilder(getContext()).title(R.string.scanning).customView(view, false).negativeText(R.string.close).show();
FileHelper.getSongList(new File(fileObject.path), true, false).map(songs -> Stream.of(songs).map(song -> song.path).collect(Collectors.toList())).observeOn(AndroidSchedulers.mainThread()).subscribe(paths -> {
ViewUtils.fadeOut(indeterminateProgress, null);
ViewUtils.fadeIn(horizontalProgress, null);
horizontalProgress.setMax(paths.size());
CustomMediaScanner.scanFiles(paths, new CustomMediaScanner.ScanCompletionListener() {
@Override
public void onPathScanned(String path) {
horizontalProgress.setProgress(horizontalProgress.getProgress() + 1);
pathsTextView.setText(path);
}
@Override
public void onScanCompleted() {
if (isAdded() && dialog.isShowing()) {
dialog.dismiss();
}
}
});
});
} else {
CustomMediaScanner.scanFiles(Collections.singletonList(fileObject.path), new CustomMediaScanner.ScanCompletionListener() {
@Override
public void onPathScanned(String path) {
}
@Override
public void onScanCompleted() {
Toast.makeText(getContext(), R.string.scan_complete, Toast.LENGTH_LONG).show();
}
});
}
return true;
case VIEW_INFO:
DialogUtils.showFileInfoDialog(getActivity(), (FileObject) fileObject);
break;
}
return false;
});
menu.show();
}
use of com.simplecity.amp_library.model.Playlist in project Shuttle by timusus.
the class DrawerPresenter method loadData.
private void loadData(@NonNull DrawerView drawerView) {
PermissionUtils.RequestStoragePermissions(() -> addDisposable(playlistsModel.getPlaylistsObservable().map(playlists -> Stream.of(playlists).map(playlist1 -> {
DrawerChild drawerChild = new DrawerChild(playlist1);
drawerChild.setListener(new DrawerChild.ClickListener() {
@Override
public void onClick(Playlist playlist) {
onPlaylistClicked(playlist);
}
@Override
public void onOverflowClick(View v, Playlist playlist) {
PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
MenuUtils.setupPlaylistMenu(popupMenu, playlist);
popupMenu.setOnMenuItemClickListener(MenuUtils.getPlaylistPopupMenuClickListener(v.getContext(), playlist, null));
popupMenu.show();
}
});
return drawerChild;
}).toList()).observeOn(AndroidSchedulers.mainThread()).delaySubscription(Observable.timer(1500, TimeUnit.MILLISECONDS)).doFinally(() -> drawerView.setPlaylistItems(Collections.emptyList())).subscribe(drawerView::setPlaylistItems, error -> LogUtils.logException(TAG, "Error refreshing DrawerFragment adapter items", error))));
}
use of com.simplecity.amp_library.model.Playlist in project Shuttle by timusus.
the class MainActivity method handleIntent.
private void handleIntent(Intent intent) {
Single.fromCallable(() -> {
boolean handled = false;
if (MusicService.ShortcutCommands.PLAYLIST.equals(intent.getAction())) {
Playlist playlist = (Playlist) intent.getExtras().getSerializable(PlaylistUtils.ARG_PLAYLIST);
NavigationEventRelay.NavigationEvent navigationEvent = new NavigationEventRelay.NavigationEvent(NavigationEventRelay.NavigationEvent.Type.PLAYLIST_SELECTED, playlist, true);
navigationEventRelay.sendEvent(navigationEvent);
handled = true;
} else if (MusicService.ShortcutCommands.FOLDERS.equals(intent.getAction())) {
NavigationEventRelay.NavigationEvent foldersSelectedEvent = new NavigationEventRelay.NavigationEvent(NavigationEventRelay.NavigationEvent.Type.FOLDERS_SELECTED, null, true);
navigationEventRelay.sendEvent(foldersSelectedEvent);
handled = true;
}
if (!handled) {
handlePlaybackRequest(intent);
} else {
setIntent(new Intent());
}
return true;
}).delaySubscription(350, TimeUnit.MILLISECONDS).subscribe();
}
use of com.simplecity.amp_library.model.Playlist in project Shuttle by timusus.
the class MenuUtils method addToPlaylist.
public static void addToPlaylist(Context context, MenuItem item, List<Song> songs, Runnable insertCallback) {
Playlist playlist = (Playlist) item.getIntent().getSerializableExtra(PlaylistUtils.ARG_PLAYLIST);
PlaylistUtils.addToPlaylist(context, playlist, songs, insertCallback);
}
Aggregations