Search in sources :

Example 6 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class QueuePagerFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_queue_pager, container, false);
    unbinder = ButterKnife.bind(this, rootView);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
    if (ShuttleUtils.isLandscape()) {
        textProtectionScrim.setVisibility(View.GONE);
    }
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setAdapter(viewModelAdapter);
    SnapHelper snapHelper = new PagerSnapHelper() {

        @Override
        public int findTargetSnapPosition(RecyclerView.LayoutManager layoutManager, int velocityX, int velocityY) {
            int snapPosition = super.findTargetSnapPosition(layoutManager, velocityX, velocityY);
            if (snapPosition < viewModelAdapter.items.size()) {
                Observable.defer(() -> {
                    if (MusicUtils.getQueuePosition() != snapPosition) {
                        MusicUtils.setQueuePosition(snapPosition);
                    }
                    return Observable.empty();
                }).delaySubscription(150, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io()).subscribe();
            }
            return snapPosition;
        }
    };
    snapHelper.attachToRecyclerView(recyclerView);
    recyclerView.addOnScrollListener(new RecyclerViewPreloader<>(new ListPreloader.PreloadModelProvider<QueuePagerItemView>() {

        @Override
        public List<QueuePagerItemView> getPreloadItems(int position) {
            QueuePagerItemView queuePagerItemView = (QueuePagerItemView) viewModelAdapter.items.get(position);
            return Collections.singletonList(queuePagerItemView);
        }

        @Override
        public GenericRequestBuilder getPreloadRequestBuilder(QueuePagerItemView item) {
            return requestManager.load(item.song).diskCacheStrategy(DiskCacheStrategy.SOURCE).error(PlaceholderProvider.getInstance().getPlaceHolderDrawable(item.song.name, true));
        }
    }, (item, adapterPosition, perItemPosition) -> imageSize, 3));
    recyclerView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        @Override
        public boolean onPreDraw() {
            // This null check doesn't make sense to me, but there was an NPE here..
            if (recyclerView != null) {
                imageSize = new int[] { recyclerView.getWidth(), recyclerView.getHeight() };
                recyclerView.getViewTreeObserver().removeOnPreDrawListener(this);
            }
            return false;
        }
    });
    return rootView;
}
Also used : R(com.simplecity.amp_library.R) Bundle(android.os.Bundle) SnapHelper(android.support.v7.widget.SnapHelper) ButterKnife(butterknife.ButterKnife) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) BindView(butterknife.BindView) Inject(javax.inject.Inject) GenericRequestBuilder(com.bumptech.glide.GenericRequestBuilder) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) RequestManager(com.bumptech.glide.RequestManager) ListPreloader(com.bumptech.glide.ListPreloader) DiskCacheStrategy(com.bumptech.glide.load.engine.DiskCacheStrategy) View(android.view.View) Observable(io.reactivex.Observable) Schedulers(io.reactivex.schedulers.Schedulers) Unbinder(butterknife.Unbinder) PlaceholderProvider(com.simplecity.amp_library.utils.PlaceholderProvider) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) LayoutInflater(android.view.LayoutInflater) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) RecyclerViewPreloader(com.simplecity.amp_library.glide.preloader.RecyclerViewPreloader) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) FragmentModule(com.simplecity.amp_library.dagger.module.FragmentModule) ViewTreeObserver(android.view.ViewTreeObserver) QueuePagerPresenter(com.simplecity.amp_library.ui.presenters.QueuePagerPresenter) Collections(java.util.Collections) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) BindView(butterknife.BindView) View(android.view.View) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) SnapHelper(android.support.v7.widget.SnapHelper) PagerSnapHelper(android.support.v7.widget.PagerSnapHelper) ViewTreeObserver(android.view.ViewTreeObserver)

Example 7 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class SearchFragment method onCreate.

@SuppressLint("InlinedApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    searchPresenter = new SearchPresenter(new PrefixHighlighter(), Glide.with(this));
    query = getArguments().getString(ARG_QUERY);
    loadingView = new LoadingView();
    emptyView = new EmptyView(R.string.empty_search);
    emptyView.setHeight(ResourceUtils.toPixels(96));
    adapter = new ViewModelAdapter();
}
Also used : PrefixHighlighter(com.simplecity.amp_library.format.PrefixHighlighter) LoadingView(com.simplecity.amp_library.ui.modelviews.LoadingView) EmptyView(com.simplecity.amp_library.ui.modelviews.EmptyView) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter) SuppressLint(android.annotation.SuppressLint)

Example 8 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter 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;
        }
    }
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Example 9 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter in project Shuttle by timusus.

the class QueuePagerFragment method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    viewModelAdapter = new ViewModelAdapter();
}
Also used : ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Example 10 with ViewModelAdapter

use of com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter 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;
        }
    }
}
Also used : GridLayoutManager(android.support.v7.widget.GridLayoutManager) RecyclerView(android.support.v7.widget.RecyclerView) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) ViewModelAdapter(com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)

Aggregations

ViewModelAdapter (com.simplecityapps.recycler_adapter.adapter.ViewModelAdapter)16 RecyclerView (android.support.v7.widget.RecyclerView)7 ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)7 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)5 R (com.simplecity.amp_library.R)5 List (java.util.List)5 LayoutInflater (android.view.LayoutInflater)4 View (android.view.View)4 FragmentModule (com.simplecity.amp_library.dagger.module.FragmentModule)4 Observable (io.reactivex.Observable)4 SuppressLint (android.annotation.SuppressLint)3 Context (android.content.Context)3 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)3 Stream (com.annimon.stream.Stream)3 Bundle (android.os.Bundle)2 GridLayoutManager (android.support.v7.widget.GridLayoutManager)2 PagerSnapHelper (android.support.v7.widget.PagerSnapHelper)2 SnapHelper (android.support.v7.widget.SnapHelper)2 ViewGroup (android.view.ViewGroup)2 ViewTreeObserver (android.view.ViewTreeObserver)2