-
Notifications
You must be signed in to change notification settings - Fork 554
5.x | UndoHelper
- Introduction
-
Configuration
- Basic usage
- Advanced usage
-
Callbacks
- onActionCanceled
- onActionConfirmed
- Bin use case
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.
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 byFlexibleAdapter.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, theonActionCanceled
callback will be invoked, otherwise theonActionConfirmed
callback will be invoked.
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);
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);
In order to finalize the user's action and to restore/update thirds components 2 callbacks are foreseen:
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(@Action int action) {
// 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
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
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.
- Update Data Set
- Selection modes
- Headers and Sections
- Scrollable Headers and Footers
- Expandable items
- Drag&Drop and Swipe
- EndlessScroll / On Load More
- Search Filter
- FastScroller
- Adapter Animations
- Third party Layout Managers
- Payload
- Smooth Layout Managers
- Flexible Item Decoration
- Utils
- ActionModeHelper
- AnimatorHelper
- EmptyViewHelper
- UndoHelper
* = Under revision!