-
Notifications
You must be signed in to change notification settings - Fork 553
5.x | UndoHelper
- Introduction
-
Configuration
- Basic usage
- Advanced usage
-
Callbacks
- onUndoConfirmed
- onDeleteConfirmed
- 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, theonUndoConfirmed
callback will be invoked, otherwise theonDeleteConfirmed
callback will be invoked.
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);
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);
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(@UndoHelper.Action int action) {
// Restore deleted items
mAdapter.restoreDeletedItems();
// Check also selection restoration
if (mAdapter.isRestoreWithSelection()) {
mActionModeHelper.restoreSelection(this);
}
}
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());
}
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!