From dbd424571be654c2bcf37d64e24184bd76cbcfb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20R=C3=B6ssler?= Date: Fri, 8 Apr 2016 11:06:37 +0200 Subject: [PATCH 1/2] pass the token to the listener onHide as well (not just in onUndo). this is useful for doing stuff that is not actually undoable and should only be executed after the user had his chance to undo --- .../java/com/jensdriller/libs/undobar/UndoBar.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java b/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java index 5aff274..3f26ecc 100644 --- a/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java +++ b/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java @@ -67,7 +67,7 @@ public interface Listener { /** * Will be fired when the undo bar disappears without being actioned. */ - void onHide(); + void onHide(Parcelable token); /** * Will be fired when the undo button is pressed. @@ -392,8 +392,10 @@ public void onAnimationEnd() { * Hides the undo bar and notifies potential listener. */ protected void onHide() { + // hide(true) will set mUndoToken to null, so we grab a reference and pass it to safelyNotifyOnHide() + Parcelable token = mUndoToken; hide(true); - safelyNotifyOnHide(); + safelyNotifyOnHide(token); mUndoListener = null; } @@ -409,9 +411,9 @@ protected void onUndo() { /** * Notifies listener if available. */ - protected void safelyNotifyOnHide() { + protected void safelyNotifyOnHide(Parcelable token) { if (mUndoListener != null) { - mUndoListener.onHide(); + mUndoListener.onHide(token); } } From 63808e12c54fb023172d610eda8e98a9d29e5aa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20R=C3=B6ssler?= Date: Fri, 8 Apr 2016 11:10:12 +0200 Subject: [PATCH 2/2] mUndoToken might be set to null in hide(true) before it can be picked up in safelyNotifyOnUndo. Although it's super unlikely that this actually happens I think it's cleaner to first grab a reference --- .../main/java/com/jensdriller/libs/undobar/UndoBar.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java b/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java index 3f26ecc..7e23bf8 100644 --- a/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java +++ b/library/src/main/java/com/jensdriller/libs/undobar/UndoBar.java @@ -404,8 +404,10 @@ protected void onHide() { * Hides the undo bar and notifies potential listener. */ protected void onUndo() { + // hide(true) will set mUndoToken to null, so we grab a reference and pass it to safelyNotifyOnHide() + Parcelable token = mUndoToken; hide(true); - safelyNotifyOnUndo(); + safelyNotifyOnUndo(token); } /** @@ -420,9 +422,9 @@ protected void safelyNotifyOnHide(Parcelable token) { /** * Notifies listener if available. */ - protected void safelyNotifyOnUndo() { + protected void safelyNotifyOnUndo(Parcelable token) { if (mUndoListener != null) { - mUndoListener.onUndo(mUndoToken); + mUndoListener.onUndo(token); } }