Search in sources :

Example 1 with ExpandableExtension

use of com.mikepenz.fastadapter.expandable.ExpandableExtension in project Hentoid by avluis.

the class ViewerGalleryFragment method updateListAdapter.

private void updateListAdapter(boolean isChapterEditMode) {
    if (isChapterEditMode) {
        if (!fastAdapter2.hasObservers())
            fastAdapter2.setHasStableIds(true);
        itemAdapter2.clear();
        ExpandableExtension<INestedItem<SubExpandableItem.ViewHolder>> expandableExtension = fastAdapter2.getOrCreateExtension(ExpandableExtension.class);
        GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
        if (glm != null) {
            int spanCount = Preferences.getViewerGalleryColumns();
            glm.setSpanCount(spanCount);
            // Use the correct size to display chapter separators, if any
            GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    if (fastAdapter2.getItemViewType(position) == R.id.expandable_item) {
                        return spanCount;
                    }
                    return 1;
                }
            };
            glm.setSpanSizeLookup(spanSizeLookup);
        }
        // Activate drag & drop
        SimpleDragCallback dragCallback = new SimpleDragCallback(this);
        dragCallback.setNotifyAllDrops(true);
        touchHelper = new ItemTouchHelper(dragCallback);
        touchHelper.attachToRecyclerView(recyclerView);
        recyclerView.setAdapter(fastAdapter2);
        fastAdapter2.addEventHook(new SubExpandableItem.DragHandlerTouchEvent(position -> {
            RecyclerView.ViewHolder vh = recyclerView.findViewHolderForAdapterPosition(position);
            if (vh != null)
                touchHelper.startDrag(vh);
            return null;
        }));
        // Item click listener
        fastAdapter2.setOnClickListener((v, a, i, p) -> onNestedItemClick(i));
        // Select on swipe
        if (mDragSelectTouchListener != null) {
            recyclerView.removeOnItemTouchListener(mDragSelectTouchListener);
            mDragSelectTouchListener = null;
        }
    } else {
        if (!fastAdapter.hasObservers())
            fastAdapter.setHasStableIds(true);
        itemAdapter.clear();
        // Gets (or creates and attaches if not yet existing) the extension from the given `FastAdapter`
        selectExtension = fastAdapter.getOrCreateExtension(SelectExtension.class);
        if (selectExtension != null) {
            selectExtension.setSelectable(true);
            selectExtension.setMultiSelect(true);
            selectExtension.setSelectOnLongClick(true);
            selectExtension.setSelectWithItemUpdate(true);
            selectExtension.setSelectionListener((i, b) -> this.onSelectionChanged());
            FastAdapterPreClickSelectHelper<ImageFileItem> helper = new FastAdapterPreClickSelectHelper<>(selectExtension);
            fastAdapter.setOnPreClickListener(helper::onPreClickListener);
            fastAdapter.setOnPreLongClickListener((v, a, i, p) -> {
                // Warning : specific code for drag selection
                mDragSelectTouchListener.startDragSelection(p);
                return helper.onPreLongClickListener(v, a, i, p);
            });
        }
        // Item click listener
        fastAdapter.setOnClickListener((v, a, i, p) -> onItemClick(i));
        GridLayoutManager glm = (GridLayoutManager) recyclerView.getLayoutManager();
        if (glm != null) {
            int spanCount = Preferences.getViewerGalleryColumns();
            glm.setSpanCount(spanCount);
            // Use the correct size to display chapter separators, if any
            GridLayoutManager.SpanSizeLookup spanSizeLookup = new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    if (fastAdapter.getItemViewType(position) == R.id.expandable_item) {
                        return spanCount;
                    }
                    return 1;
                }
            };
            glm.setSpanSizeLookup(spanSizeLookup);
        }
        if (touchHelper != null)
            touchHelper.attachToRecyclerView(null);
        recyclerView.setAdapter(fastAdapter);
        // Select on swipe
        DragSelectTouchListener.OnDragSelectListener onDragSelectionListener = (start, end, isSelected) -> selectExtension.select(IntStream.rangeClosed(start, end).boxed().toList());
        mDragSelectTouchListener = new DragSelectTouchListener().withSelectListener(onDragSelectionListener);
        if (mDragSelectTouchListener2 != null) {
            recyclerView.removeOnItemTouchListener(mDragSelectTouchListener2);
            mDragSelectTouchListener2 = null;
        }
        recyclerView.addOnItemTouchListener(mDragSelectTouchListener);
    }
}
Also used : Content(me.devsaki.hentoid.database.domains.Content) Bundle(android.os.Bundle) NonNull(androidx.annotation.NonNull) FastAdapterDiffUtil(com.mikepenz.fastadapter.diff.FastAdapterDiffUtil) MaterialAlertDialogBuilder(com.google.android.material.dialog.MaterialAlertDialogBuilder) Chapter(me.devsaki.hentoid.database.domains.Chapter) Optional(com.annimon.stream.Optional) ItemAdapter(com.mikepenz.fastadapter.adapters.ItemAdapter) Handler(android.os.Handler) Looper(android.os.Looper) ImageFile(me.devsaki.hentoid.database.domains.ImageFile) Fragment(androidx.fragment.app.Fragment) ImageViewerActivity(me.devsaki.hentoid.activities.ImageViewerActivity) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) ItemTouchCallback(com.mikepenz.fastadapter.drag.ItemTouchCallback) PowerSpinnerView(com.skydoves.powerspinner.PowerSpinnerView) ExpandableExtension(com.mikepenz.fastadapter.expandable.ExpandableExtension) DragSelectTouchListener(me.devsaki.hentoid.widget.DragSelectTouchListener) Set(java.util.Set) ToastHelper(me.devsaki.hentoid.util.ToastHelper) ViewGroup(android.view.ViewGroup) Timber(timber.log.Timber) ViewModelFactory(me.devsaki.hentoid.viewmodels.ViewModelFactory) Nullable(org.jetbrains.annotations.Nullable) ViewCompat.requireViewById(androidx.core.view.ViewCompat.requireViewById) List(java.util.List) ImageItemBundle(me.devsaki.hentoid.activities.bundles.ImageItemBundle) ContentNotProcessedException(me.devsaki.hentoid.util.exception.ContentNotProcessedException) ImageFileItem(me.devsaki.hentoid.viewholders.ImageFileItem) SubExpandableItem(me.devsaki.hentoid.viewholders.SubExpandableItem) FastAdapter(com.mikepenz.fastadapter.FastAdapter) Toolbar(androidx.appcompat.widget.Toolbar) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) Snackbar(com.google.android.material.snackbar.Snackbar) IntStream(com.annimon.stream.IntStream) Context(android.content.Context) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) Preferences(me.devsaki.hentoid.util.Preferences) Stream(com.annimon.stream.Stream) DiffCallback(com.mikepenz.fastadapter.diff.DiffCallback) SimpleDragCallback(com.mikepenz.fastadapter.drag.SimpleDragCallback) BaseTransientBottomBar(com.google.android.material.snackbar.BaseTransientBottomBar) IntDef(androidx.annotation.IntDef) MenuItem(android.view.MenuItem) Retention(java.lang.annotation.Retention) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) FastAdapterPreClickSelectHelper(me.devsaki.hentoid.widget.FastAdapterPreClickSelectHelper) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) SelectExtension(com.mikepenz.fastadapter.select.SelectExtension) WeakReference(java.lang.ref.WeakReference) ImageViewerViewModel(me.devsaki.hentoid.viewmodels.ImageViewerViewModel) FragmentManager(androidx.fragment.app.FragmentManager) ViewerKeyListener(me.devsaki.hentoid.widget.ViewerKeyListener) ViewModelProvider(androidx.lifecycle.ViewModelProvider) LayoutInflater(android.view.LayoutInflater) INestedItem(me.devsaki.hentoid.viewholders.INestedItem) DragDropUtil(com.mikepenz.fastadapter.utils.DragDropUtil) FastScrollerBuilder(me.zhanghai.android.fastscroll.FastScrollerBuilder) R(me.devsaki.hentoid.R) ProgressDialogFragment(me.devsaki.hentoid.fragments.ProgressDialogFragment) RetentionPolicy(java.lang.annotation.RetentionPolicy) SimpleDragCallback(com.mikepenz.fastadapter.drag.SimpleDragCallback) SelectExtension(com.mikepenz.fastadapter.select.SelectExtension) SubExpandableItem(me.devsaki.hentoid.viewholders.SubExpandableItem) SuppressLint(android.annotation.SuppressLint) ItemTouchHelper(androidx.recyclerview.widget.ItemTouchHelper) GridLayoutManager(androidx.recyclerview.widget.GridLayoutManager) ImageFileItem(me.devsaki.hentoid.viewholders.ImageFileItem) FastAdapterPreClickSelectHelper(me.devsaki.hentoid.widget.FastAdapterPreClickSelectHelper) INestedItem(me.devsaki.hentoid.viewholders.INestedItem) DragSelectTouchListener(me.devsaki.hentoid.widget.DragSelectTouchListener)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 Context (android.content.Context)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 Looper (android.os.Looper)1 LayoutInflater (android.view.LayoutInflater)1 MenuItem (android.view.MenuItem)1 View (android.view.View)1 ViewGroup (android.view.ViewGroup)1 IntDef (androidx.annotation.IntDef)1 NonNull (androidx.annotation.NonNull)1 Toolbar (androidx.appcompat.widget.Toolbar)1 ViewCompat.requireViewById (androidx.core.view.ViewCompat.requireViewById)1 Fragment (androidx.fragment.app.Fragment)1 FragmentManager (androidx.fragment.app.FragmentManager)1 ViewModelProvider (androidx.lifecycle.ViewModelProvider)1 GridLayoutManager (androidx.recyclerview.widget.GridLayoutManager)1 ItemTouchHelper (androidx.recyclerview.widget.ItemTouchHelper)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1