Search in sources :

Example 1 with PlaylistView

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));
        }
    });
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) Bundle(android.os.Bundle) Stream(com.annimon.stream.Stream) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) ArrayList(java.util.ArrayList) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) PlaylistView(com.simplecity.amp_library.ui.modelviews.PlaylistView) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) LogUtils(com.simplecity.amp_library.utils.LogUtils) LayoutInflater(android.view.LayoutInflater) Playlist(com.simplecity.amp_library.model.Playlist) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PopupMenu(android.support.v7.widget.PopupMenu) ViewGroup(android.view.ViewGroup) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) ComparisonUtils(com.simplecity.amp_library.utils.ComparisonUtils) DataManager(com.simplecity.amp_library.utils.DataManager) Nullable(android.support.annotation.Nullable) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PlaylistView(com.simplecity.amp_library.ui.modelviews.PlaylistView) View(android.view.View) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) Playlist(com.simplecity.amp_library.model.Playlist) PlaylistView(com.simplecity.amp_library.ui.modelviews.PlaylistView) ArrayList(java.util.ArrayList) List(java.util.List) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PopupMenu(android.support.v7.widget.PopupMenu)

Aggregations

Context (android.content.Context)1 Bundle (android.os.Bundle)1 Nullable (android.support.annotation.Nullable)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 PopupMenu (android.support.v7.widget.PopupMenu)1 LayoutInflater (android.view.LayoutInflater)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 Stream (com.annimon.stream.Stream)1 R (com.simplecity.amp_library.R)1 Playlist (com.simplecity.amp_library.model.Playlist)1 EmptyView (com.simplecity.amp_library.ui.modelviews.EmptyView)1 PlaylistView (com.simplecity.amp_library.ui.modelviews.PlaylistView)1 ComparisonUtils (com.simplecity.amp_library.utils.ComparisonUtils)1 DataManager (com.simplecity.amp_library.utils.DataManager)1 LogUtils (com.simplecity.amp_library.utils.LogUtils)1 MenuUtils (com.simplecity.amp_library.utils.MenuUtils)1 PermissionUtils (com.simplecity.amp_library.utils.PermissionUtils)1 ViewModelAdapter (com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)1 ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)1