use of com.simplecityapps.recycler_adapter.model.ViewModel 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));
}
});
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class QueueFragment method setupContextualToolbar.
private void setupContextualToolbar() {
cabToolbar.getMenu().clear();
cabToolbar.inflateMenu(R.menu.context_menu_queue);
final SubMenu sub = cabToolbar.getMenu().findItem(R.id.queue_add_to_playlist).getSubMenu();
disposables.add(PlaylistUtils.createUpdatingPlaylistMenu(sub).subscribe());
cabToolbar.setOnMenuItemClickListener(MenuUtils.getQueueMenuClickListener(getContext(), Single.fromCallable(() -> cabHelper.getItems()), deleteDialog -> deleteDialog.show(getChildFragmentManager()), () -> {
queuePresenter.removeFromQueue(cabHelper.getItems());
cabHelper.finish();
}, () -> cabHelper.finish()));
cabHelper = new ContextualToolbarHelper<>(cabToolbar, new Callback() {
@Override
public void notifyItemChanged(int position, SelectableViewModel viewModel) {
adapter.notifyItemChanged(position, 0);
}
@Override
public void notifyDatasetChanged() {
adapter.notifyItemRangeChanged(0, adapter.items.size(), 0);
}
});
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class QueueFragment method updateQueuePosition.
@Override
public void updateQueuePosition(int position, boolean fromUser) {
if (adapter.items.isEmpty() || position >= adapter.items.size() || position < 0) {
return;
}
if (recyclerView == null) {
return;
}
if (!fromUser) {
recyclerView.scrollToPosition(position);
}
int prevPosition = -1;
int len = adapter.items.size();
for (int i = 0; i < len; i++) {
ViewModel viewModel = adapter.items.get(i);
if (viewModel instanceof SongView) {
if (((SongView) viewModel).isCurrentTrack()) {
prevPosition = i;
}
((SongView) viewModel).setCurrentTrack(i == position);
}
}
((SongView) adapter.items.get(position)).setCurrentTrack(true);
adapter.notifyItemChanged(prevPosition, 1);
adapter.notifyItemChanged(position, 1);
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class SuggestedFragment method getRecentlyAddedViewModels.
Observable<List<ViewModel>> getRecentlyAddedViewModels() {
return Playlist.recentlyAddedPlaylist.getSongsObservable().flatMap(songs -> Observable.just(Operators.songsToAlbums(songs))).flatMapSingle(source -> Observable.fromIterable(source).sorted((a, b) -> ComparisonUtils.compareLong(b.songPlayCount, a.songPlayCount)).take(20).toList()).map(albums -> {
if (!albums.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader recentlyAddedHeader = new SuggestedHeader(getString(R.string.recentlyadded), getString(R.string.suggested_recently_added_subtitle), Playlist.recentlyAddedPlaylist);
SuggestedHeaderView recentlyAddedHeaderView = new SuggestedHeaderView(recentlyAddedHeader);
recentlyAddedHeaderView.setClickListener(this);
viewModels.add(recentlyAddedHeaderView);
viewModels.addAll(Stream.of(albums).map(album -> {
AlbumView albumView = new AlbumView(album, ViewType.ALBUM_CARD, requestManager);
albumView.setClickListener(this);
return albumView;
}).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class SuggestedFragment method getFavoriteSongViewModels.
@SuppressLint("CheckResult")
Observable<List<ViewModel>> getFavoriteSongViewModels() {
Observable<List<Song>> favoritesSongs = DataManager.getInstance().getFavoriteSongsRelay().take(20);
return Observable.combineLatest(favoritesSongs, Playlist.favoritesPlaylist().filter(Optional::isPresent).map(Optional::get).toObservable(), (songs, playlist) -> {
if (!songs.isEmpty()) {
List<ViewModel> viewModels = new ArrayList<>();
SuggestedHeader favoriteHeader = new SuggestedHeader(getString(R.string.fav_title), getString(R.string.suggested_favorite_subtitle), playlist);
SuggestedHeaderView favoriteHeaderView = new SuggestedHeaderView(favoriteHeader);
favoriteHeaderView.setClickListener(SuggestedFragment.this);
viewModels.add(favoriteHeaderView);
viewModels.add(favoriteRecyclerView);
SongClickListener songClickListener = new SongClickListener(songs);
favoriteRecyclerView.viewModelAdapter.setItems(Stream.of(songs).map(song -> {
SuggestedSongView suggestedSongView = new SuggestedSongView(song, requestManager);
suggestedSongView.setClickListener(songClickListener);
return (ViewModel) suggestedSongView;
}).toList());
return viewModels;
} else {
return Collections.emptyList();
}
});
}
Aggregations