Search in sources :

Example 6 with ViewModel

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));
        }
    });
}
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)

Example 7 with ViewModel

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);
        }
    });
}
Also used : R(com.simplecity.amp_library.R) CompletionListUpdateCallbackAdapter(com.simplecityapps.recycler_adapter.adapter.CompletionListUpdateCallbackAdapter) Bundle(android.os.Bundle) SongView(com.simplecity.amp_library.ui.modelviews.SongView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) BindView(butterknife.BindView) Song(com.simplecity.amp_library.model.Song) Aesthetic(com.afollestad.aesthetic.Aesthetic) RequestManager(com.bumptech.glide.RequestManager) ItemTouchHelper(android.support.v7.widget.helper.ItemTouchHelper) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) ResourceUtils(com.simplecity.amp_library.utils.ResourceUtils) Unbinder(butterknife.Unbinder) ItemTouchHelperCallback(com.simplecity.amp_library.ui.recyclerview.ItemTouchHelperCallback) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) QueueView(com.simplecity.amp_library.ui.views.QueueView) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) Util(com.afollestad.aesthetic.Util) PlayerViewAdapter(com.simplecity.amp_library.ui.views.PlayerViewAdapter) Nullable(android.support.annotation.Nullable) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) ButterKnife(butterknife.ButterKnife) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) Single(io.reactivex.Single) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) Toast(android.widget.Toast) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) DeleteDialog(com.simplecity.amp_library.ui.dialog.DeleteDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) TaggerDialog(com.simplecity.amp_library.tagger.TaggerDialog) MultiSheetView(com.simplecity.multisheetview.ui.view.MultiSheetView) LayoutInflater(android.view.LayoutInflater) ThemedStatusBarView(com.simplecity.amp_library.ui.views.ThemedStatusBarView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Color(android.graphics.Color) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) MultiSheetSlideEventRelay(com.simplecity.amp_library.ui.views.multisheet.MultiSheetSlideEventRelay) Toolbar(android.support.v7.widget.Toolbar) PlayerPresenter(com.simplecity.amp_library.ui.presenters.PlayerPresenter) QueuePresenter(com.simplecity.amp_library.ui.presenters.QueuePresenter) Callback(com.simplecity.amp_library.utils.ContextualToolbarHelper.Callback) ItemTouchHelperCallback(com.simplecity.amp_library.ui.recyclerview.ItemTouchHelperCallback) Callback(com.simplecity.amp_library.utils.ContextualToolbarHelper.Callback) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) SubMenu(android.view.SubMenu)

Example 8 with ViewModel

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);
}
Also used : SongView(com.simplecity.amp_library.ui.modelviews.SongView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel)

Example 9 with ViewModel

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();
        }
    });
}
Also used : R(com.simplecity.amp_library.R) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedDividerDecoration(com.simplecity.amp_library.ui.views.SuggestedDividerDecoration) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) Song(com.simplecity.amp_library.model.Song) Optional(com.annimon.stream.Optional) RequestManager(com.bumptech.glide.RequestManager) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) View(android.view.View) LogUtils(com.simplecity.amp_library.utils.LogUtils) Playlist(com.simplecity.amp_library.model.Playlist) PopupMenu(android.support.v7.widget.PopupMenu) ViewGroup(android.view.ViewGroup) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) List(java.util.List) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) Nullable(android.support.annotation.Nullable) Context(android.content.Context) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) Album(com.simplecity.amp_library.model.Album) 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) ViewType(com.simplecity.amp_library.ui.adapters.ViewType) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) SuppressLint(android.annotation.SuppressLint) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) Toast(android.widget.Toast) Observable(io.reactivex.Observable) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) LayoutInflater(android.view.LayoutInflater) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) HorizontalRecyclerView(com.simplecity.amp_library.ui.modelviews.HorizontalRecyclerView) PlaylistDetailFragment(com.simplecity.amp_library.ui.detail.PlaylistDetailFragment) ComparisonUtils(com.simplecity.amp_library.utils.ComparisonUtils) DataManager(com.simplecity.amp_library.utils.DataManager) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Operators(com.simplecity.amp_library.utils.Operators) Collections(java.util.Collections) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) AlbumView(com.simplecity.amp_library.ui.modelviews.AlbumView) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader)

Example 10 with ViewModel

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();
        }
    });
}
Also used : Optional(com.annimon.stream.Optional) SuggestedHeaderView(com.simplecity.amp_library.ui.modelviews.SuggestedHeaderView) ArrayList(java.util.ArrayList) SuggestedSongView(com.simplecity.amp_library.ui.modelviews.SuggestedSongView) List(java.util.List) ArrayList(java.util.ArrayList) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SuggestedHeader(com.simplecity.amp_library.model.SuggestedHeader) SuppressLint(android.annotation.SuppressLint)

Aggregations

ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)28 Stream (com.annimon.stream.Stream)18 R (com.simplecity.amp_library.R)18 List (java.util.List)18 Nullable (android.support.annotation.Nullable)17 View (android.view.View)17 Bundle (android.os.Bundle)16 LayoutInflater (android.view.LayoutInflater)16 MenuUtils (com.simplecity.amp_library.utils.MenuUtils)16 RecyclerListener (com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener)16 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)16 ViewGroup (android.view.ViewGroup)15 MusicUtils (com.simplecity.amp_library.utils.MusicUtils)15 Disposable (io.reactivex.disposables.Disposable)15 PopupMenu (android.support.v7.widget.PopupMenu)14 UpgradeDialog (com.simplecity.amp_library.ui.dialog.UpgradeDialog)14 EmptyView (com.simplecity.amp_library.ui.modelviews.EmptyView)14 Observable (io.reactivex.Observable)14 Collections (java.util.Collections)14 TimeUnit (java.util.concurrent.TimeUnit)14