Skip to content
Davide Steduto edited this page May 1, 2018 · 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.

Class location 👓

eu.davidea.flexibleadapter.helpers
  |_ UndoHelper

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 onActionCanceled callback will be invoked, otherwise the onActionConfirmed callback will be invoked.

Basic usage

new UndoHelper(Adapter, UndoHelper.OnUndoListener)
        .start(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
        .withConsecutive(true/false) // Default value is false (accumulate items to undo)
        .withAction(@Action UndoHelper.ACTION_REMOVE / UndoHelper.ACTION_UPDATE)
        .withActionTextColor(@ColorInt actionTextColor)
        .start(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:

onActionCanceled

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

@Override
public void onActionCanceled(@Action int action, @NonNull List<Integer> positions) {
    // Restore deleted items
    mAdapter.restoreDeletedItems();
    // Check also selection restoration
    if (mAdapter.isRestoreWithSelection()) {
        mActionModeHelper.restoreSelection(this);
    }
}

Triggered when user presses Snackbar action button:
- DISMISS_EVENT_ACTION = 1;      // Snackbar dismissed via an action click

onActionConfirmed

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 onActionConfirmed(@Action int action, int event) {
    repository.removeItems(mAdapter.getDeletedItems());
}

Triggered by User/Snackbar events:
- DISMISS_EVENT_SWIPE = 0;       // Snackbar dismissed via a swipe
- DISMISS_EVENT_TIMEOUT = 2;     // Snackbar dismissed via a timeout
- DISMISS_EVENT_MANUAL = 3;      // Snackbar dismissed via a call to dismiss()
- DISMISS_EVENT_CONSECUTIVE = 4; // Snackbar dismissed from a new Snackbar being shown

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