use of com.simplecity.amp_library.ui.modelviews.PlaylistView in project Shuttle by timusus.
the class PlaylistFragment method refreshAdapterItems.
private void refreshAdapterItems() {
PermissionUtils.RequestStoragePermissions(() -> {
if (getActivity() != null && isAdded()) {
Observable<List<Playlist>> defaultPlaylistsObservable = Observable.defer(() -> {
List<Playlist> playlists = new ArrayList<>();
Playlist podcastPlaylist = Playlist.podcastPlaylist();
if (podcastPlaylist != null) {
playlists.add(podcastPlaylist);
}
playlists.add(Playlist.recentlyAddedPlaylist);
playlists.add(Playlist.mostPlayedPlaylist);
return Observable.just(playlists);
});
Observable<List<Playlist>> playlistsObservable = DataManager.getInstance().getPlaylistsRelay();
disposable = Observable.combineLatest(defaultPlaylistsObservable, playlistsObservable, (defaultPlaylists, playlists) -> {
List<Playlist> list = new ArrayList<>();
list.addAll(defaultPlaylists);
list.addAll(playlists);
return list;
}).debounce(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).map(playlists -> {
PlaylistView.OnClickListener listener = new PlaylistView.OnClickListener() {
@Override
public void onPlaylistClick(int position, PlaylistView playlistView) {
if (playlistClickListener != null) {
playlistClickListener.onPlaylistClicked(playlistView.playlist);
}
}
@Override
public void onPlaylistOverflowClick(int position, View v, Playlist playlist) {
PopupMenu menu = new PopupMenu(PlaylistFragment.this.getActivity(), v);
MenuUtils.setupPlaylistMenu(menu, playlist);
menu.setOnMenuItemClickListener(MenuUtils.getPlaylistPopupMenuClickListener(getContext(), playlist, null));
menu.show();
}
};
return Stream.of(playlists).sorted((a, b) -> ComparisonUtils.compare(a.name, b.name)).sorted((a, b) -> ComparisonUtils.compareInt(a.type, b.type)).map(playlist -> {
PlaylistView playlistView = new PlaylistView(playlist);
playlistView.setListener(listener);
return (ViewModel) playlistView;
}).toList();
}).observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
if (items.isEmpty()) {
adapter.setItems(Collections.singletonList(new EmptyView(R.string.empty_playlist)));
} else {
adapter.setItems(items);
}
}, error -> LogUtils.logException(TAG, "Error refreshing adapter", error));
}
});
}
Aggregations