Search in sources :

Example 16 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class ViewModelAdapter method removeItem.

/**
 * Remove & return the item at items[position]
 *
 * @param position int
 * @return the {@link ViewModel} that was removed, or null if it couldn't be removed
 */
@Nullable
public ViewModel removeItem(int position) {
    if (getItemCount() == 0 || position < 0 || position >= items.size()) {
        return null;
    }
    final ViewModel model = items.remove(position);
    notifyItemRemoved(position);
    return model;
}
Also used : ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) Nullable(android.support.annotation.Nullable)

Example 17 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class AlbumArtistFragment method refreshAdapterItems.

void refreshAdapterItems(boolean force) {
    PermissionUtils.RequestStoragePermissions(() -> {
        if (getActivity() != null && isAdded()) {
            int artistDisplayType = SettingsManager.getInstance().getArtistDisplayType();
            boolean ascending = SortManager.getInstance().getArtistsAscending();
            disposable = DataManager.getInstance().getAlbumArtistsRelay().skipWhile(albumArtists -> !force && adapter.items.size() == albumArtists.size()).debounce(150, TimeUnit.MILLISECONDS).flatMapSingle(albumArtists -> {
                // Sort
                SortManager.getInstance().sortAlbumArtists(albumArtists);
                // Reverse if required
                if (!ascending) {
                    Collections.reverse(albumArtists);
                }
                return Observable.fromIterable(albumArtists).map(albumArtist -> {
                    // Look for an existing AlbumArtistView wrapping the song, we'll reuse it if it exists.
                    AlbumArtistView albumArtistView = (AlbumArtistView) Stream.of(adapter.items).filter(viewModel -> viewModel instanceof AlbumArtistView && (((AlbumArtistView) viewModel).albumArtist.equals(albumArtist))).findFirst().orElse(null);
                    if (albumArtistView == null) {
                        albumArtistView = new AlbumArtistView(albumArtist, artistDisplayType, requestManager);
                        albumArtistView.setClickListener(this);
                    }
                    return (ViewModel) albumArtistView;
                }).toList();
            }).observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
                if (items.isEmpty()) {
                    adapter.setItems(Collections.singletonList(new EmptyView(R.string.empty_artists)));
                } else {
                    adapter.setItems(items);
                }
                // Move the RV back to the top if we've had a sort order change.
                if (sortOrderChanged) {
                    recyclerView.scrollToPosition(0);
                }
                sortOrderChanged = false;
            });
        }
    });
}
Also used : R(com.simplecity.amp_library.R) Context(android.content.Context) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) Stream(com.annimon.stream.Stream) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) ViewType(com.simplecity.amp_library.ui.adapters.ViewType) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) AlbumArtistView(com.simplecity.amp_library.ui.modelviews.AlbumArtistView) RequestManager(com.bumptech.glide.RequestManager) MenuInflater(android.view.MenuInflater) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) Menu(android.view.Menu) View(android.view.View) Observable(io.reactivex.Observable) SectionedAdapter(com.simplecity.amp_library.ui.adapters.SectionedAdapter) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) LayoutInflater(android.view.LayoutInflater) Fragment(android.support.v4.app.Fragment) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Disposable(io.reactivex.disposables.Disposable) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) GridDividerDecoration(com.simplecity.amp_library.ui.recyclerview.GridDividerDecoration) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) DataManager(com.simplecity.amp_library.utils.DataManager) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Nullable(android.support.annotation.Nullable) SpanSizeLookup(com.simplecityapps.recycler_adapter.recyclerview.SpanSizeLookup) Collections(java.util.Collections) AlbumArtistView(com.simplecity.amp_library.ui.modelviews.AlbumArtistView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView)

Example 18 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class AlbumArtistFragment method setupContextualToolbar.

private void setupContextualToolbar() {
    ContextualToolbar contextualToolbar = ContextualToolbar.findContextualToolbar(this);
    if (contextualToolbar != null) {
        contextualToolbar.getMenu().clear();
        contextualToolbar.inflateMenu(R.menu.context_menu_general);
        SubMenu sub = contextualToolbar.getMenu().findItem(R.id.addToPlaylist).getSubMenu();
        if (playlistMenuDisposable != null) {
            playlistMenuDisposable.dispose();
        }
        playlistMenuDisposable = PlaylistUtils.createUpdatingPlaylistMenu(sub).subscribe();
        contextualToolbar.setOnMenuItemClickListener(MenuUtils.getAlbumArtistMenuClickListener(getContext(), () -> contextualToolbarHelper.getItems(), deleteDialog -> deleteDialog.show(getChildFragmentManager()), () -> contextualToolbarHelper.finish()));
        contextualToolbarHelper = new ContextualToolbarHelper<>(contextualToolbar, new ContextualToolbarHelper.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) Context(android.content.Context) GridLayoutManager(android.support.v7.widget.GridLayoutManager) Bundle(android.os.Bundle) Stream(com.annimon.stream.Stream) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) ViewType(com.simplecity.amp_library.ui.adapters.ViewType) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Inject(javax.inject.Inject) AlbumArtistView(com.simplecity.amp_library.ui.modelviews.AlbumArtistView) RequestManager(com.bumptech.glide.RequestManager) MenuInflater(android.view.MenuInflater) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) Menu(android.view.Menu) View(android.view.View) Observable(io.reactivex.Observable) SectionedAdapter(com.simplecity.amp_library.ui.adapters.SectionedAdapter) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) LayoutInflater(android.view.LayoutInflater) Fragment(android.support.v4.app.Fragment) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) Disposable(io.reactivex.disposables.Disposable) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) GridDividerDecoration(com.simplecity.amp_library.ui.recyclerview.GridDividerDecoration) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) DataManager(com.simplecity.amp_library.utils.DataManager) AlbumArtist(com.simplecity.amp_library.model.AlbumArtist) Nullable(android.support.annotation.Nullable) SpanSizeLookup(com.simplecityapps.recycler_adapter.recyclerview.SpanSizeLookup) Collections(java.util.Collections) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) SubMenu(android.view.SubMenu) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar)

Example 19 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class SongFragment method refreshAdapterItems.

void refreshAdapterItems(boolean force) {
    PermissionUtils.RequestStoragePermissions(() -> {
        if (getActivity() != null && isAdded()) {
            boolean ascending = SortManager.getInstance().getSongsAscending();
            disposable = DataManager.getInstance().getSongsRelay().skipWhile(songs -> !force && Stream.of(adapter.items).filter(viewModel -> viewModel instanceof SongView).count() == songs.size()).debounce(150, TimeUnit.MILLISECONDS).flatMapSingle(songs -> {
                // Sort
                SortManager.getInstance().sortSongs(songs);
                // Reverse if required
                if (!ascending) {
                    Collections.reverse(songs);
                }
                return Observable.fromIterable(songs).map(song -> {
                    // Look for an existing SongView wrapping the song, we'll reuse it if it exists.
                    SongView songView = (SongView) Stream.of(adapter.items).filter(viewModel -> viewModel instanceof SongView && (((SongView) viewModel).song.equals(song))).findFirst().orElse(null);
                    if (songView == null) {
                        songView = new SongView(song, null);
                        songView.setClickListener(this);
                    }
                    return (ViewModel) songView;
                }).toList();
            }).observeOn(AndroidSchedulers.mainThread()).subscribe(items -> {
                if (items.isEmpty()) {
                    adapter.setItems(Collections.singletonList(new EmptyView(R.string.empty_songlist)));
                } else {
                    items.add(0, shuffleView);
                    adapter.setItems(items);
                }
                // Move the RV back to the top if we've had a sort order change.
                if (sortOrderChanged) {
                    recyclerView.scrollToPosition(0);
                }
                sortOrderChanged = false;
            }, error -> LogUtils.logException(TAG, "Error refreshing adapter items", error));
        }
    });
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) Stream(com.annimon.stream.Stream) ShuffleView(com.simplecity.amp_library.ui.modelviews.ShuffleView) SongView(com.simplecity.amp_library.ui.modelviews.SongView) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Song(com.simplecity.amp_library.model.Song) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) Menu(android.view.Menu) View(android.view.View) Observable(io.reactivex.Observable) SectionedAdapter(com.simplecity.amp_library.ui.adapters.SectionedAdapter) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) LogUtils(com.simplecity.amp_library.utils.LogUtils) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) DataManager(com.simplecity.amp_library.utils.DataManager) Nullable(android.support.annotation.Nullable) Collections(java.util.Collections) SongView(com.simplecity.amp_library.ui.modelviews.SongView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView)

Example 20 with ViewModel

use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.

the class SongFragment method setupContextualToolbar.

private void setupContextualToolbar() {
    ContextualToolbar contextualToolbar = ContextualToolbar.findContextualToolbar(this);
    if (contextualToolbar != null) {
        contextualToolbar.getMenu().clear();
        contextualToolbar.inflateMenu(R.menu.context_menu_general);
        SubMenu sub = contextualToolbar.getMenu().findItem(R.id.addToPlaylist).getSubMenu();
        if (playlistMenuDisposable != null) {
            playlistMenuDisposable.dispose();
        }
        playlistMenuDisposable = PlaylistUtils.createUpdatingPlaylistMenu(sub).subscribe();
        contextualToolbarHelper = new ContextualToolbarHelper<>(contextualToolbar, new ContextualToolbarHelper.Callback() {

            @Override
            public void notifyItemChanged(int position, SelectableViewModel viewModel) {
                adapter.notifyItemChanged(position, 0);
            }

            @Override
            public void notifyDatasetChanged() {
                adapter.notifyItemRangeChanged(0, adapter.items.size(), 0);
            }
        });
        contextualToolbar.setOnMenuItemClickListener(MenuUtils.getSongMenuClickListener(getContext(), Single.fromCallable(() -> contextualToolbarHelper.getItems()), deleteDialog -> deleteDialog.show(getChildFragmentManager()), () -> contextualToolbarHelper.finish()));
    }
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) Stream(com.annimon.stream.Stream) ShuffleView(com.simplecity.amp_library.ui.modelviews.ShuffleView) SongView(com.simplecity.amp_library.ui.modelviews.SongView) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar) RecyclerListener(com.simplecityapps.recycler_adapter.recyclerview.RecyclerListener) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) PlaylistUtils(com.simplecity.amp_library.utils.PlaylistUtils) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MenuItem(android.view.MenuItem) Song(com.simplecity.amp_library.model.Song) MenuInflater(android.view.MenuInflater) Toast(android.widget.Toast) MenuUtils(com.simplecity.amp_library.utils.MenuUtils) Menu(android.view.Menu) View(android.view.View) Observable(io.reactivex.Observable) SectionedAdapter(com.simplecity.amp_library.ui.adapters.SectionedAdapter) UpgradeDialog(com.simplecity.amp_library.ui.dialog.UpgradeDialog) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) FastScrollRecyclerView(com.simplecityapps.recyclerview_fastscroll.views.FastScrollRecyclerView) LogUtils(com.simplecity.amp_library.utils.LogUtils) ContextualToolbarHelper(com.simplecity.amp_library.utils.ContextualToolbarHelper) SortManager(com.simplecity.amp_library.utils.SortManager) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PopupMenu(android.support.v7.widget.PopupMenu) SubMenu(android.view.SubMenu) ViewGroup(android.view.ViewGroup) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Disposable(io.reactivex.disposables.Disposable) PermissionUtils(com.simplecity.amp_library.utils.PermissionUtils) DataManager(com.simplecity.amp_library.utils.DataManager) Nullable(android.support.annotation.Nullable) Collections(java.util.Collections) SelectableViewModel(com.simplecity.amp_library.ui.modelviews.SelectableViewModel) SubMenu(android.view.SubMenu) ContextualToolbar(com.simplecity.amp_library.ui.views.ContextualToolbar)

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