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