use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class SuggestedFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (recyclerView == null) {
recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_suggested, container, false);
recyclerView.addItemDecoration(new SuggestedDividerDecoration(getResources()));
recyclerView.setRecyclerListener(new RecyclerListener());
int spanCount = ShuttleUtils.isTablet() ? 12 : 6;
GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), spanCount);
gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
if (!adapter.items.isEmpty() && position >= 0) {
ViewModel item = adapter.items.get(position);
if (item instanceof HorizontalRecyclerView || item instanceof SuggestedHeaderView || (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_LIST) || (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_LIST_SMALL) || item instanceof EmptyView) {
return spanCount;
}
if (item instanceof AlbumView && item.getViewType() == ViewType.ALBUM_CARD_LARGE) {
return 3;
}
}
return 2;
}
});
recyclerView.setLayoutManager(gridLayoutManager);
}
if (recyclerView.getAdapter() != adapter) {
recyclerView.setAdapter(adapter);
}
return recyclerView;
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class BaseDetailFragment method getAlbumViewModels.
@NonNull
@Override
public List<ViewModel> getAlbumViewModels(List<Album> albums) {
if (albums.isEmpty()) {
return Collections.emptyList();
}
List<ViewModel> items = new ArrayList<>();
if (setHorizontalItemsDisposable != null) {
setHorizontalItemsDisposable.dispose();
}
setHorizontalItemsDisposable = horizontalRecyclerView.setItems(Stream.of(albums).map(album -> {
HorizontalAlbumView horizontalAlbumView = new HorizontalAlbumView(album, requestManager);
horizontalAlbumView.setClickListener(BaseDetailFragment.this);
horizontalAlbumView.showYear(true);
return horizontalAlbumView;
}).collect(Collectors.toList()));
items.add(new SubheaderView(StringUtils.makeAlbumsLabel(getContext(), albums.size())));
items.add(horizontalRecyclerView);
return items;
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class QueuePagerPresenter method bindView.
@Override
public void bindView(@NonNull QueuePagerView view) {
super.bindView(view);
IntentFilter filter = new IntentFilter();
filter.addAction(MusicService.InternalIntents.META_CHANGED);
filter.addAction(MusicService.InternalIntents.REPEAT_CHANGED);
filter.addAction(MusicService.InternalIntents.SHUFFLE_CHANGED);
filter.addAction(MusicService.InternalIntents.QUEUE_CHANGED);
filter.addAction(MusicService.InternalIntents.SERVICE_CONNECTED);
addDisposable(RxBroadcast.fromBroadcast(ShuttleApplication.getInstance(), filter).startWith(new Intent(MusicService.InternalIntents.QUEUE_CHANGED)).toFlowable(BackpressureStrategy.LATEST).observeOn(AndroidSchedulers.mainThread()).subscribe(intent -> {
final String action = intent.getAction();
QueuePagerView queuePagerView = getView();
if (queuePagerView == null) {
return;
}
if (action != null) {
switch(action) {
case MusicService.InternalIntents.META_CHANGED:
queuePagerView.updateQueuePosition(MusicUtils.getQueuePosition());
break;
case MusicService.InternalIntents.REPEAT_CHANGED:
case MusicService.InternalIntents.SHUFFLE_CHANGED:
case MusicService.InternalIntents.QUEUE_CHANGED:
case MusicService.InternalIntents.SERVICE_CONNECTED:
List<ViewModel> items = Stream.of(MusicUtils.getQueue()).map(song -> new QueuePagerItemView(song, requestManager)).collect(Collectors.toList());
queuePagerView.loadData(items, MusicUtils.getQueuePosition());
break;
}
}
}));
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class GridDividerDecoration method getItemOffsets.
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
if (!(parent.getLayoutManager() instanceof GridLayoutManager)) {
throw new IllegalStateException("GridDividerDecoration can only be used with GridLayoutManager");
}
int spanCount = ((GridLayoutManager) parent.getLayoutManager()).getSpanCount();
int position = parent.getChildAdapterPosition(view);
if (position < 0) {
return;
}
int spanIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanIndex(position, spanCount);
int groupIndex = ((GridLayoutManager) parent.getLayoutManager()).getSpanSizeLookup().getSpanGroupIndex(position, spanCount);
RecyclerView.Adapter adapter = parent.getAdapter();
if (adapter instanceof ViewModelAdapter) {
ViewModel viewModel = ((ViewModelAdapter) adapter).items.get(position);
// in which case we add bottom spacing if includeEdge is true)
if (viewModel.getSpanSize(spanCount) == spanCount) {
if (includeEdge && groupIndex == 0) {
outRect.bottom = spacing;
}
return;
}
}
if (includeEdge) {
outRect.left = spacing - spanIndex * spacing / spanCount;
outRect.right = (spanIndex + 1) * spacing / spanCount;
if (groupIndex == 0) {
outRect.top = spacing;
}
outRect.bottom = spacing;
} else {
outRect.left = spanIndex * spacing / spanCount;
outRect.right = spacing - (spanIndex + 1) * spacing / spanCount;
if (groupIndex > 0) {
outRect.top = spacing;
}
}
}
use of com.simplecityapps.recycler_adapter.model.ViewModel in project Shuttle by timusus.
the class ViewModelAdapter method moveItem.
/**
* Moves an item from {@param fromPosition} to {@param toPosition}
*
* @param fromPosition int
* @param toPosition int
*/
public void moveItem(int fromPosition, int toPosition) {
final ViewModel model = items.remove(fromPosition);
items.add(toPosition, model);
notifyItemMoved(fromPosition, toPosition);
}
Aggregations