Search in sources :

Example 1 with QueuePagerItemView

use of com.simplecity.amp_library.ui.modelviews.QueuePagerItemView 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 2 with QueuePagerItemView

use of com.simplecity.amp_library.ui.modelviews.QueuePagerItemView 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;
            }
        }
    }));
}
Also used : QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) BackpressureStrategy(io.reactivex.BackpressureStrategy) Collectors(com.annimon.stream.Collectors) Stream(com.annimon.stream.Stream) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) NonNull(android.support.annotation.NonNull) RxBroadcast(com.cantrowitz.rxbroadcast.RxBroadcast) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) MusicUtils(com.simplecity.amp_library.utils.MusicUtils) Inject(javax.inject.Inject) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) RequestManager(com.bumptech.glide.RequestManager) MusicService(com.simplecity.amp_library.playback.MusicService) IntentFilter(android.content.IntentFilter) QueuePagerView(com.simplecity.amp_library.ui.views.QueuePagerView) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) Intent(android.content.Intent) List(java.util.List)

Example 3 with QueuePagerItemView

use of com.simplecity.amp_library.ui.modelviews.QueuePagerItemView 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(InternalIntents.META_CHANGED);
    filter.addAction(InternalIntents.REPEAT_CHANGED);
    filter.addAction(InternalIntents.SHUFFLE_CHANGED);
    filter.addAction(InternalIntents.QUEUE_CHANGED);
    filter.addAction(InternalIntents.SERVICE_CONNECTED);
    addDisposable(RxBroadcast.fromBroadcast(application, filter).startWith(new Intent(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 InternalIntents.META_CHANGED:
                    queuePagerView.updateQueuePosition(mediaManager.getQueuePosition());
                    break;
                case InternalIntents.REPEAT_CHANGED:
                case InternalIntents.SHUFFLE_CHANGED:
                case InternalIntents.QUEUE_CHANGED:
                case InternalIntents.SERVICE_CONNECTED:
                    List<ViewModel> items = Stream.of(mediaManager.getQueue()).map(queueItem -> new QueuePagerItemView(queueItem.getSong(), requestManager, settingsManager)).collect(Collectors.toList());
                    queuePagerView.loadData(items, mediaManager.getQueuePosition());
                    break;
            }
        }
    }));
}
Also used : QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) BackpressureStrategy(io.reactivex.BackpressureStrategy) Collectors(com.annimon.stream.Collectors) Stream(com.annimon.stream.Stream) MediaManager(com.simplecity.amp_library.playback.MediaManager) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) ViewModel(com.simplecityapps.recycler_adapter.model.ViewModel) NonNull(android.support.annotation.NonNull) RxBroadcast(com.cantrowitz.rxbroadcast.RxBroadcast) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) Inject(javax.inject.Inject) ShuttleApplication(com.simplecity.amp_library.ShuttleApplication) List(java.util.List) InternalIntents(com.simplecity.amp_library.playback.constants.InternalIntents) RequestManager(com.bumptech.glide.RequestManager) Presenter(com.simplecity.amp_library.ui.common.Presenter) IntentFilter(android.content.IntentFilter) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) Intent(android.content.Intent) List(java.util.List)

Example 4 with QueuePagerItemView

use of com.simplecity.amp_library.ui.modelviews.QueuePagerItemView in project Shuttle by timusus.

the class QueuePagerFragment method onCreateView.

@Override
public View onCreateView(@NonNull 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(getContext())) {
        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.timer(200, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(o -> {
                    if (mediaManager.getQueuePosition() != snapPosition) {
                        mediaManager.setQueuePosition(snapPosition);
                    }
                }, throwable -> LogUtils.logException(TAG, "Error setting queue position", throwable));
            }
            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(getContext()).getPlaceHolderDrawable(item.song.name, true, settingsManager));
        }
    }, (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) Context(android.content.Context) 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) NonNull(android.support.annotation.NonNull) RequestManagerProvider(com.simplecity.amp_library.ui.common.RequestManagerProvider) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) 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) Unbinder(butterknife.Unbinder) PlaceholderProvider(com.simplecity.amp_library.utils.PlaceholderProvider) ShuttleUtils(com.simplecity.amp_library.utils.ShuttleUtils) LogUtils(com.simplecity.amp_library.utils.LogUtils) QueuePagerItemView(com.simplecity.amp_library.ui.modelviews.QueuePagerItemView) LayoutInflater(android.view.LayoutInflater) AndroidSupportInjection(dagger.android.support.AndroidSupportInjection) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) ViewGroup(android.view.ViewGroup) RecyclerViewPreloader(com.simplecity.amp_library.glide.preloader.RecyclerViewPreloader) BaseFragment(com.simplecity.amp_library.ui.common.BaseFragment) SettingsManager(com.simplecity.amp_library.utils.SettingsManager) TimeUnit(java.util.concurrent.TimeUnit) RecyclerView(android.support.v7.widget.RecyclerView) List(java.util.List) ViewTreeObserver(android.view.ViewTreeObserver) 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) 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)

Aggregations

RequestManager (com.bumptech.glide.RequestManager)4 QueuePagerItemView (com.simplecity.amp_library.ui.modelviews.QueuePagerItemView)4 ViewModel (com.simplecityapps.recycler_adapter.model.ViewModel)4 List (java.util.List)4 Inject (javax.inject.Inject)4 NonNull (android.support.annotation.NonNull)3 ShuttleApplication (com.simplecity.amp_library.ShuttleApplication)3 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)3 Intent (android.content.Intent)2 IntentFilter (android.content.IntentFilter)2 Bundle (android.os.Bundle)2 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)2 PagerSnapHelper (android.support.v7.widget.PagerSnapHelper)2 RecyclerView (android.support.v7.widget.RecyclerView)2 SnapHelper (android.support.v7.widget.SnapHelper)2 LayoutInflater (android.view.LayoutInflater)2 View (android.view.View)2 ViewGroup (android.view.ViewGroup)2 ViewTreeObserver (android.view.ViewTreeObserver)2 BindView (butterknife.BindView)2