-
Notifications
You must be signed in to change notification settings - Fork 554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deprecate OnActionListener in UndoHelper #438
Comments
On item swipe I want to delete only some files belonging to item but not the item itself using
This was the code: @Override
public void onItemSwipe(int position, int direction) {
//Create list for single position (only in onItemSwipe)
List<Integer> positions = new ArrayList<>(1);
positions.add(position);
final String videoPath = ((MeasurementItem) adapter.getItem(position)).getModel().getFileName();
if (videoPath != null && new File(videoPath).exists()) {
filesToDelete.push(videoPath);
new UndoHelper(adapter, this)
.withAction(UndoHelper.ACTION_UPDATE, new UndoHelper.SimpleActionListener() {
@Override
public boolean onPreAction() {
return true;
}
})
.remove(positions, layoutMain, getString(R.string.measurements_video_deleted),
getString(R.string.undo), UndoHelper.UNDO_TIMEOUT);
} else adapter.notifyItemChanged(position);
}
@Override
public void onDeleteConfirmed(int action) {
while (!filesToDelete.isEmpty()) {
final String videoPath = filesToDelete.pop();
new File(videoPath).delete();
}
adapter.notifyDataSetChanged();
}
@Override
public void onUndoConfirmed(int action) {
filesToDelete.clear();
adapter.notifyDataSetChanged();
} What would now be the correct way for implementing custom action which does not result with item deletion and removal from adapter? |
@rmunk, the event of swipe occurred so you check and do custom action even before creating the UndoHelper, if undo SnackBar is shown, swiped item is removed from the adapter, and when action button is pressed the undo callback is still called. PS. For single element list better to use |
@davideas That's what I did in the end. I created a plain SnackBar with undo action for left swipe and for the right swipe which deletes the item I still use I figured that's why you have |
Thank you @rmunk, the issue comes from the action swipe that once done, I cannot restore the view to the original state, trust me i tried everything, but the RecyclerView has an internal X value that I cannot change even if, I update the item or I animate the view back you will see a glitch and still the view at wrong coordinates, so the only thing is to remove it and to press the undo to insert it again. This week I will work again on this component to move it to the UI package, to fix the multiple swipe and maybe to rename it or change the behavior. |
@rmunk, but if you were not removing the item, how did you manage the swipe back then? |
@davideas I didn't swipe it back, I just called I didn't use |
@rmunk, thanks, I've tried the notifyChange after the swipe and it works very well if we know the positions. So, I've modified the behavior for
List of changes (for now): // Class UndoHelper is now available in package UI, with same signature.
// More on this in the migration wiki page when ready.
implementation 'eu.davidea:flexible-adapter-ui:<version>'
package eu.davidea.flexibleadapter.helpers.UndoHelper;
// Renamed callback interface
from OnUndoListener to OnActionListener
// Use of the constants from annotation interface "Action" instead of class constants
constant ACTION_REMOVE to Action.REMOVE
constant ACTION_UPDATE to Action.UPDATE
// New parameter for method onActionCancelled
// (when the action button in Snackbar is pressed)
// providing the positions affected by the action
void onActionCanceled(@Action int action);
// to
void onActionCanceled(@Action int action, List<Integer> positions); I still need to fix the consecutive swipe. |
@davideas it looks good now. Btw. thanks for this great library! |
Despite the interface was introduced for the new
UndoHelper
, it is easy to perform same actions (preAction, postAction) by just using normal flow:Execution flow is preserved. So, no need of this callback interface.
The text was updated successfully, but these errors were encountered: