Search in sources :

Example 1 with StaggeredItem

use of eu.davidea.samples.flexibleadapter.items.StaggeredItem in project FlexibleAdapter by davideas.

the class FragmentStaggeredLayout method moveItem.

private int moveItem(StaggeredItemStatus status, StaggeredHeaderItem headerItem) {
    StaggeredItem staggeredItem = DatabaseService.getInstance().getRandomStaggeredItem();
    if (!staggeredItem.getHeader().equals(headerItem)) {
        // Before calculate the position, change header/section
        //!!!
        staggeredItem.setStatus(status);
        staggeredItem.setHeader(headerItem);
        int toPosition = mAdapter.calculatePositionFor(staggeredItem, new DatabaseService.ItemComparatorByGroup());
        // Move item to just calculated position under the correct section
        mAdapter.moveItem(mAdapter.getGlobalPositionOf(staggeredItem), toPosition, Payload.MOVE);
    }
    // Retrieve the final position due to a possible hidden header became now visible!
    int scrollTo = mAdapter.getGlobalPositionOf(staggeredItem);
    Log.d(TAG, "Moving Item to position" + scrollTo);
    return scrollTo;
}
Also used : DatabaseService(eu.davidea.samples.flexibleadapter.services.DatabaseService) StaggeredItem(eu.davidea.samples.flexibleadapter.items.StaggeredItem)

Example 2 with StaggeredItem

use of eu.davidea.samples.flexibleadapter.items.StaggeredItem in project FlexibleAdapter by davideas.

the class MainActivity method onActionItemClicked.

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_select_all:
            mAdapter.selectAll();
            mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
            // We consume the event
            return true;
        case R.id.action_delete:
            // Build message before delete, for the SnackBar
            StringBuilder message = new StringBuilder();
            message.append(getString(R.string.action_deleted)).append(" ");
            for (Integer pos : mAdapter.getSelectedPositions()) {
                message.append(extractTitleFrom(mAdapter.getItem(pos)));
                if (mAdapter.getSelectedItemCount() > 1)
                    message.append(", ");
            }
            // Experimenting NEW feature
            mAdapter.setRestoreSelectionOnUndo(true);
            // New Undo Helper
            new UndoHelper(mAdapter, this).withPayload(Payload.CHANGE).withAction(UndoHelper.ACTION_REMOVE, new UndoHelper.OnActionListener() {

                @Override
                public boolean onPreAction() {
                    // OR use UndoHelper.SimpleActionListener and Override only onPostAction()
                    return false;
                }

                @Override
                public void onPostAction() {
                    // Enable Refreshing
                    mRefreshHandler.sendEmptyMessage(1);
                    mRefreshHandler.sendEmptyMessageDelayed(0, 7000);
                    // Finish the action mode
                    mActionModeHelper.destroyActionModeIfCan();
                }
            }).remove(mAdapter.getSelectedPositions(), findViewById(R.id.main_view), message, getString(R.string.undo), 7000);
            // We consume the event
            return true;
        case R.id.action_merge:
            if (mAdapter.getSelectedItemCount() > 1) {
                // Selected positions are sorted by default, we take the first item of the set
                int mainPosition = mAdapter.getSelectedPositions().get(0);
                mAdapter.removeSelection(mainPosition);
                StaggeredItem mainItem = (StaggeredItem) mAdapter.getItem(mainPosition);
                for (Integer position : mAdapter.getSelectedPositions()) {
                    // Merge item - Save the modification in the memory for next refresh
                    DatabaseService.getInstance().mergeItem(mainItem, (StaggeredItem) mAdapter.getItem(position));
                }
                // Remove merged item from the list
                mAdapter.removeAllSelectedItems();
                // Keep selection on mainItem & Skip default notification by calling addSelection
                mAdapter.addSelection(mainPosition);
                // Custom notification to bind again (ripple only)
                mAdapter.notifyItemChanged(mainPosition, "blink");
                // New title for context
                mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
            }
            // We consume always the event, never finish the ActionMode
            return true;
        case R.id.action_split:
            if (mAdapter.getSelectedItemCount() == 1) {
                StaggeredItem mainItem = (StaggeredItem) mAdapter.getItem(mAdapter.getSelectedPositions().get(0));
                if (mainItem.getMergedItems() != null) {
                    List<StaggeredItem> itemsToSplit = new ArrayList<>(mainItem.getMergedItems());
                    for (StaggeredItem itemToSplit : itemsToSplit) {
                        // Split item - Save the modification in the memory for next refresh
                        DatabaseService.getInstance().splitItem(mainItem, itemToSplit);
                        // We know the section object, so we can insert directly the item at the right position
                        // The calculated position is then returned
                        int position = mAdapter.addItemToSection(itemToSplit, mainItem.getHeader(), new DatabaseService.ItemComparatorById());
                        //Execute default notification
                        mAdapter.toggleSelection(position);
                        mAdapter.notifyItemChanged(position, "blink");
                    }
                    // Custom notification to bind again (ripple only)
                    mAdapter.notifyItemChanged(mAdapter.getGlobalPositionOf(mainItem), "blink");
                    // New title for context
                    mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
                }
            }
            // We consume always the event, never finish the ActionMode
            return true;
        default:
            // If an item is not implemented we don't consume the event, so we finish the ActionMode
            return false;
    }
}
Also used : UndoHelper(eu.davidea.flexibleadapter.helpers.UndoHelper) ArrayList(java.util.ArrayList) DatabaseService(eu.davidea.samples.flexibleadapter.services.DatabaseService) StaggeredItem(eu.davidea.samples.flexibleadapter.items.StaggeredItem)

Example 3 with StaggeredItem

use of eu.davidea.samples.flexibleadapter.items.StaggeredItem in project FlexibleAdapter by davideas.

the class FragmentStaggeredLayout method addItem.

private int addItem(StaggeredItemStatus status, StaggeredHeaderItem headerItem) {
    StaggeredItem staggeredItem = DatabaseService.newStaggeredItem(DatabaseService.getInstance().getMaxStaggeredId(), headerItem);
    //!!!
    staggeredItem.setStatus(status);
    // The section object is known
    mAdapter.addItemToSection(staggeredItem, staggeredItem.getHeader(), new DatabaseService.ItemComparatorByGroup());
    // Add Item to the Database as well for next refresh
    DatabaseService.getInstance().addItem(staggeredItem, new DatabaseService.ItemComparatorById());
    // Change fab action (MOVE ITEM)
    if (mAdapter.getItemCountOfTypes(R.layout.recycler_staggered_item) >= 15) {
        FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
        fab.setImageResource(R.drawable.ic_sort_white_24dp);
    }
    // Retrieve the final position due to a possible hidden header became now visible!
    int scrollTo = mAdapter.getGlobalPositionOf(staggeredItem);
    Log.d(TAG, "Creating New Item " + staggeredItem + " at position " + scrollTo);
    return scrollTo;
}
Also used : FloatingActionButton(android.support.design.widget.FloatingActionButton) DatabaseService(eu.davidea.samples.flexibleadapter.services.DatabaseService) StaggeredItem(eu.davidea.samples.flexibleadapter.items.StaggeredItem)

Example 4 with StaggeredItem

use of eu.davidea.samples.flexibleadapter.items.StaggeredItem in project FlexibleAdapter by davideas.

the class DatabaseService method resetItems.

public void resetItems() {
    List<StaggeredItem> mergedItems = new ArrayList<>();
    for (AbstractFlexibleItem item : mItems) {
        if (item instanceof StaggeredItem) {
            StaggeredItem staggeredItem = (StaggeredItem) item;
            staggeredItem.setStatus(StaggeredItemStatus.C);
            staggeredItem.setHeader(headers.get(StaggeredItemStatus.C));
            mergedItems.addAll(staggeredItem.splitAllItems());
        }
    }
    for (StaggeredHeaderItem header : headers.values()) {
        header.setHidden(true);
    }
    mItems.addAll(mergedItems);
    Collections.sort(mItems, new ItemComparatorById());
    createMergedItems();
}
Also used : StaggeredHeaderItem(eu.davidea.samples.flexibleadapter.items.StaggeredHeaderItem) AbstractFlexibleItem(eu.davidea.flexibleadapter.items.AbstractFlexibleItem) ArrayList(java.util.ArrayList) StaggeredItem(eu.davidea.samples.flexibleadapter.items.StaggeredItem)

Example 5 with StaggeredItem

use of eu.davidea.samples.flexibleadapter.items.StaggeredItem in project FlexibleAdapter by davideas.

the class DatabaseService method mergeItem.

public void mergeItem(StaggeredItem mainItem, StaggeredItem itemToMerge) {
    mainItem.mergeItem(itemToMerge);
    itemToMerge.setStatus(mainItem.getStatus());
    //Add more items already merged in itemsToMerge
    if (itemToMerge.getMergedItems() != null) {
        for (StaggeredItem subItem : itemToMerge.getMergedItems()) {
            mainItem.mergeItem(subItem);
            subItem.setStatus(mainItem.getStatus());
            mItems.remove(subItem);
        }
        itemToMerge.setMergedItems(null);
    }
    mItems.remove(itemToMerge);
}
Also used : StaggeredItem(eu.davidea.samples.flexibleadapter.items.StaggeredItem)

Aggregations

StaggeredItem (eu.davidea.samples.flexibleadapter.items.StaggeredItem)5 DatabaseService (eu.davidea.samples.flexibleadapter.services.DatabaseService)3 ArrayList (java.util.ArrayList)2 FloatingActionButton (android.support.design.widget.FloatingActionButton)1 UndoHelper (eu.davidea.flexibleadapter.helpers.UndoHelper)1 AbstractFlexibleItem (eu.davidea.flexibleadapter.items.AbstractFlexibleItem)1 StaggeredHeaderItem (eu.davidea.samples.flexibleadapter.items.StaggeredHeaderItem)1