Search in sources :

Example 1 with UndoHelper

use of eu.davidea.flexibleadapter.helpers.UndoHelper 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 2 with UndoHelper

use of eu.davidea.flexibleadapter.helpers.UndoHelper in project FlexibleAdapter by davideas.

the class MainActivity method onItemSwipe.

@Override
public void onItemSwipe(final int position, int direction) {
    Log.i(TAG, "onItemSwipe position=" + position + " direction=" + (direction == ItemTouchHelper.LEFT ? "LEFT" : "RIGHT"));
    // Option 1 FULL_SWIPE: Direct action no Undo Action
    // Do something based on direction when item has been swiped:
    //   A) update item, set "read" if an email etc.
    //   B) remove the item from the adapter;
    // Option 2 FULL_SWIPE: Delayed action with Undo Action
    // Show action button and start a new Handler:
    //   A) on time out do something based on direction (open dialog with options);
    // Create list for single position (only in onItemSwipe)
    List<Integer> positions = new ArrayList<>(1);
    positions.add(position);
    // Build the message
    IFlexible abstractItem = mAdapter.getItem(position);
    StringBuilder message = new StringBuilder();
    message.append(extractTitleFrom(abstractItem)).append(" ");
    // Experimenting NEW feature
    if (abstractItem.isSelectable())
        mAdapter.setRestoreSelectionOnUndo(false);
    // Here, option 2A) is implemented
    if (direction == ItemTouchHelper.LEFT) {
        message.append(getString(R.string.action_archived));
        // Example of UNDO color
        int actionTextColor;
        if (Utils.hasMarshmallow()) {
            actionTextColor = getColor(R.color.material_color_orange_500);
        } else {
            //noinspection deprecation
            actionTextColor = getResources().getColor(R.color.material_color_orange_500);
        }
        new UndoHelper(mAdapter, this).withPayload(//You can pass any custom object (in this case Boolean is enough)
        null).withAction(UndoHelper.ACTION_UPDATE, new UndoHelper.SimpleActionListener() {

            @Override
            public boolean onPreAction() {
                // remove the item from Adapter list as usual.
                return true;
            }
        }).withActionTextColor(actionTextColor).remove(positions, findViewById(R.id.main_view), message, getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);
    //Here, option 1B) is implemented
    } else if (direction == ItemTouchHelper.RIGHT) {
        message.append(getString(R.string.action_deleted));
        mSwipeRefreshLayout.setRefreshing(true);
        new UndoHelper(mAdapter, this).withPayload(//You can pass any custom object (in this case Boolean is enough)
        null).withAction(UndoHelper.ACTION_REMOVE, new UndoHelper.SimpleActionListener() {

            @Override
            public void onPostAction() {
                // Handle ActionMode title
                if (mAdapter.getSelectedItemCount() == 0)
                    mActionModeHelper.destroyActionModeIfCan();
                else
                    mActionModeHelper.updateContextTitle(mAdapter.getSelectedItemCount());
            }
        }).remove(positions, findViewById(R.id.main_view), message, getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);
    }
}
Also used : UndoHelper(eu.davidea.flexibleadapter.helpers.UndoHelper) ArrayList(java.util.ArrayList) IFlexible(eu.davidea.flexibleadapter.items.IFlexible)

Aggregations

UndoHelper (eu.davidea.flexibleadapter.helpers.UndoHelper)2 ArrayList (java.util.ArrayList)2 IFlexible (eu.davidea.flexibleadapter.items.IFlexible)1 StaggeredItem (eu.davidea.samples.flexibleadapter.items.StaggeredItem)1 DatabaseService (eu.davidea.samples.flexibleadapter.services.DatabaseService)1