Skip to content

Commit

Permalink
Fixed #265 - UndoHelper dismissed quickly with SnackBar predefined le…
Browse files Browse the repository at this point in the history
…ngths
  • Loading branch information
davideas committed Jan 13, 2017
1 parent 61ab9e9 commit 37a304b
Showing 1 changed file with 26 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public class UndoHelper extends Snackbar.Callback {

/**
* Default constructor.
* <p>Only from version 5.0.0-b8, by calling this constructor,
* {@link FlexibleAdapter#setPermanentDelete(boolean)} is set {@code false} automatically.
* <p>By calling this constructor, {@link FlexibleAdapter#setPermanentDelete(boolean)}
* is set {@code false} automatically.
*
* @param adapter the instance of {@code FlexibleAdapter}
* @param undoListener the callback for the Undo and Delete confirmation
Expand Down Expand Up @@ -111,26 +111,26 @@ public UndoHelper withAction(@Action int action, @NonNull OnActionListener actio
return this;
}

/**
* Sets the text color of the action.
*
* @param color the color for the action button
* @return this object, so it can be chained
*/
public UndoHelper withActionTextColor(@ColorInt int color) {
this.mActionTextColor = color;
return this;
}
/**
* Sets the text color of the action.
*
* @param color the color for the action button
* @return this object, so it can be chained
*/
public UndoHelper withActionTextColor(@ColorInt int color) {
this.mActionTextColor = color;
return this;
}

/**
* As {@link #remove(List, View, CharSequence, CharSequence, int)} but with String
* resources instead of CharSequence.
*/
public Snackbar remove(List<Integer> positions, @NonNull View mainView,
@StringRes int messageStringResId, @StringRes int actionStringResId,
@IntRange(from = 0) int undoTime) {
public void remove(List<Integer> positions, @NonNull View mainView,
@StringRes int messageStringResId, @StringRes int actionStringResId,
@IntRange(from = -1) int undoTime) {
Context context = mainView.getContext();
return remove(positions, mainView, context.getString(messageStringResId),
remove(positions, mainView, context.getString(messageStringResId),
context.getString(actionStringResId), undoTime);
}

Expand All @@ -146,17 +146,16 @@ public Snackbar remove(List<Integer> positions, @NonNull View mainView,
* @param actionText the action text to display
* @param undoTime How long to display the message. Either {@link Snackbar#LENGTH_SHORT} or
* {@link Snackbar#LENGTH_LONG} or any custom Integer.
* @return The SnackBar instance to be customized again
* @see #remove(List, View, int, int, int)
*/
@SuppressWarnings("WrongConstant")
public Snackbar remove(List<Integer> positions, @NonNull View mainView,
CharSequence message, CharSequence actionText,
@IntRange(from = 0) int undoTime) {
public void remove(List<Integer> positions, @NonNull View mainView,
CharSequence message, CharSequence actionText,
@IntRange(from = -1) int undoTime) {
this.mPositions = positions;
Snackbar snackbar;
if (!mAdapter.isPermanentDelete()) {
snackbar = Snackbar.make(mainView, message, undoTime + 400)//More time due to the animation
snackbar = Snackbar.make(mainView, message, undoTime > 0 ? undoTime + 400 : undoTime)
.setAction(actionText, new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -170,9 +169,8 @@ public void onClick(View v) {
if (mActionTextColor != Color.TRANSPARENT) {
snackbar.setActionTextColor(mActionTextColor);
}
snackbar.setCallback(this);
snackbar.addCallback(this);
snackbar.show();
return snackbar;
}

/**
Expand Down Expand Up @@ -201,13 +199,13 @@ public void onDismissed(Snackbar snackbar, int event) {
@Override
public void onShown(Snackbar snackbar) {
boolean consumed = false;
//Perform the action before deletion
// Perform the action before deletion
if (mActionListener != null) consumed = mActionListener.onPreAction();
//Remove selected items from Adapter list after SnackBar is shown
// Remove selected items from Adapter list after SnackBar is shown
if (!consumed) mAdapter.removeItems(mPositions, mPayload);
//Perform the action after the deletion
// Perform the action after the deletion
if (mActionListener != null) mActionListener.onPostAction();
//Here, we can notify the callback only in case of permanent deletion
// Here, we can notify the callback only in case of permanent deletion
if (mAdapter.isPermanentDelete() && mUndoListener != null)
mUndoListener.onDeleteConfirmed(mAction);
}
Expand Down Expand Up @@ -261,7 +259,7 @@ public interface OnUndoListener {
* Called when Undo timeout is over and action must be committed in the user Database.
* <p>Due to Java Generic, it's too complicated and not well manageable if we pass the
* List&lt;T&gt; object.<br/>
* To get deleted items, use {@link FlexibleAdapter#getDeletedItems()} from the
* So, to get deleted items, use {@link FlexibleAdapter#getDeletedItems()} from the
* implementation of this method.</p>
*
* @param action one of {@link UndoHelper#ACTION_REMOVE}, {@link UndoHelper#ACTION_UPDATE}
Expand Down

0 comments on commit 37a304b

Please sign in to comment.