Skip to content
Davide Steduto edited this page Sep 2, 2017 · 7 revisions

In this page


Introduction

This Helper class is designed to undo operations of deleted items and archived items. The Helper extends Snackbar.Callback and implements FlexibleAdapter.OnDeleteCompleteListener. This means that an action bar will be shown and the repository commit is performed only when time out is over!

The Helper is ideal to be used after a Swipe or to complete the ActionMode MULTI.

Configuration

Available options are:

  • You can give a custom payload object to inform other linked items about the change in action under their control. The linked items will be rebound with that payload.
  • An action REMOVE (default).
  • An action UPDATE, for instance: to archive item.
  • A custom text and color for the Snackbar button.

ℹ️ Note: Based on the permanentDelete option provided by FlexibleAdapter.setPermanentDelete(true/false), the Snackbar will be shown without or with action button. If the action button is available and is pressed by the user, the onUndoConfirmed callback will be invoked, otherwise the onDeleteConfirmed callback will be invoked.

Basic usage
new UndoHelper(Adapter, UndoHelper.OnUndoListener)
        .remove(positionsToRemove,
                // Suitable ViewGroup parent to attach the Snackbar
                findViewById(R.id.main_view),
                // Custom message and action button text
                // Overload function with Strings is available
                R.string.action_archived, R.string.undo,
                // Default undo timeout of 5''
                UndoHelper.UNDO_TIMEOUT);
Advanced usage
Snackbar snackbar = new UndoHelper(Adapter, UndoHelper.OnUndoListener)
        .withPayload(Payload.CHANGE) // You can provide any custom object
        .withAction(@Action UndoHelper.ACTION_REMOVE / UndoHelper.ACTION_UPDATE)
        .withActionTextColor(@ColorInt actionTextColor)
        .remove(mAdapter.getSelectedPositions(),
                findViewById(R.id.main_view), message,
                getString(R.string.undo), 20000);

Callbacks

In order to finalize the user's action and to restore/update thirds components 2 callbacks are foreseen:

onUndoConfirmed

Called when Undo event is triggered. Perform custom action after restoration. Usually for a delete restoration you should call FlexibleAdapter.restoreDeletedItems().

@Override
public void onUndoConfirmed(@UndoHelper.Action int action) {
    // Restore deleted items
    mAdapter.restoreDeletedItems();
    // Check also selection restoration
    if (mAdapter.isRestoreWithSelection()) {
        mActionModeHelper.restoreSelection(this);
    }
}
onDeleteConfirmed

Called when Undo timeout is over and action must be committed in the user Repository. To get deleted items, use FlexibleAdapter.getDeletedItems() from the implementation of this method.

@Override
public void onDeleteConfirmed(@UndoHelper.Action int action) {
    repository.removeItems(mAdapter.getDeletedItems());
}

Bin use case

Another approach in complement or in substitution of the Undo feature, is the Bin folder use case. It can be implemented to be at disposal of the user any time in the future, so he can undo any deleted item when he wants.

Clone this wiki locally