Skip to content
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

Added direction information to callback interfaces #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha3'
classpath 'com.android.tools.build:gradle:3.0.0-beta2'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Mon Jun 12 17:27:00 EDT 2017
#Tue Aug 15 11:55:10 EDT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-rc-1-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ public interface SWItemDelegate<TItem> {
*
* @param adapterPosition
* adapter position as a reference to remove an item
* @param direction
* The direction the item was swiped, either {@link #LEFT} or {@link #RIGHT}
*/
void removeItemAtAdapterPosition(int adapterPosition);
void removeItemAtAdapterPosition(int adapterPosition, int direction);

/**
* Add an item given an adapter position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ public interface SWItemRemovalListener<TItem> {
* The item being added back
* @param position
* The position of the item being temporarily removed
* @param direction
* The direction the item was swiped, either {@link #LEFT} or {@link #RIGHT}
*/
void onItemTemporarilyRemoved(TItem item, int position);
void onItemTemporarilyRemoved(TItem item, int position, int direction);

/**
* Method invoked when it is no longer possible to add the previously removed item back into a {@link SWRecyclerView}.
*
* This is a good place for taking the next step in the removal such as database persistence
*
* @param item The item being permanently removed
* @param direction
* The direction the item was swiped, either {@link #LEFT} or {@link #RIGHT}
*/
void onItemPermanentlyRemoved(TItem item);
void onItemPermanentlyRemoved(TItem item, int direction);

/**
* Method invoked when an item associated to the given view holder is added back to a {@link SWRecyclerView}.
Expand All @@ -35,6 +39,8 @@ public interface SWItemRemovalListener<TItem> {
* The item being added back
* @param position
* The position of the item being added back
* @param direction
Copy link
Owner

@huan-nguyen huan-nguyen Jun 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marchy You added a new direction param while the method definition is not updated. I think there is no benefit of passing direction when an item is added back to the list, so probably no change should be made here. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @huan-nguyen this was a miss on my part. The direction info would be needed for an undo to be able to know which action was taken and thus how to undo that action.

I've added this parameter & associated data in and re-submitted 👍🏻

* The direction the item was swiped, either {@link #LEFT} or {@link #RIGHT}
*/
void onItemAddedBack(TItem item, int position);
void onItemAddedBack(TItem item, int position, int direction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ public void onItemCleared(final TViewHolder viewHolder, int direction) {
displaySnackBarIfNeeded(viewHolder, item, adapterPosition, direction);

if (mItemRemovalListener != null) {
mItemRemovalListener.onItemTemporarilyRemoved(item, adapterPosition);
mItemRemovalListener.onItemTemporarilyRemoved(item, adapterPosition, direction);
}
mItemDelegate.removeItemAtAdapterPosition(adapterPosition);
mItemDelegate.removeItemAtAdapterPosition(adapterPosition, direction);
notifyItemRemoved(adapterPosition);
}

private void displaySnackBarIfNeeded(TViewHolder viewHolder, final TItem item, final int adapterPosition,
int direction) {
final int direction) {
if(mSnackBarDataProvider != null && mSnackBarDataProvider.isUndoEnabled()) {
final Snackbar snackbar = Snackbar
.make(mSnackBarDataProvider
Expand All @@ -70,15 +70,15 @@ public void onClick(View v) {
mItemDelegate.addItemWithAdapterPosition(item, adapterPosition);
notifyItemInserted(adapterPosition);
if (mItemRemovalListener != null) {
mItemRemovalListener.onItemAddedBack(item, adapterPosition);
mItemRemovalListener.onItemAddedBack(item, adapterPosition, direction);
}
}
});
snackbar.setCallback(new Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
if (event != DISMISS_EVENT_ACTION && mItemRemovalListener != null) {
mItemRemovalListener.onItemPermanentlyRemoved(item);
mItemRemovalListener.onItemPermanentlyRemoved(item, direction);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public void onItemCleared(final TViewHolder viewHolder, final int direction) {
displaySnackBarIfNeeded(viewHolder, item, adapterPosition, direction);

if (mItemRemovalListener != null) {
mItemRemovalListener.onItemTemporarilyRemoved(item, adapterPosition);
mItemRemovalListener.onItemTemporarilyRemoved(item, adapterPosition, direction);
}
mItems.remove(adapterPosition);
notifyItemRemoved(adapterPosition);
}

private void displaySnackBarIfNeeded(TViewHolder viewHolder, final TItem item, final int adapterPosition, int direction) {
private void displaySnackBarIfNeeded(TViewHolder viewHolder, final TItem item, final int adapterPosition, final int direction) {
if (mSnackBarDataProvider != null && mSnackBarDataProvider.isUndoEnabled()) {
final Snackbar snackbar = Snackbar
.make(mSnackBarDataProvider.getView(), getSnackBarMessage(viewHolder, direction),
Expand All @@ -62,7 +62,7 @@ public void onClick(View v) {
mItems.add(adapterPosition, item);
notifyItemInserted(adapterPosition);
if (mItemRemovalListener != null) {
mItemRemovalListener.onItemAddedBack(item, adapterPosition);
mItemRemovalListener.onItemAddedBack(item, adapterPosition, direction);
}
}
});
Expand All @@ -71,7 +71,7 @@ public void onClick(View v) {
@Override
public void onDismissed(Snackbar snackbar, int event) {
if (event != DISMISS_EVENT_ACTION && mItemRemovalListener != null) {
mItemRemovalListener.onItemPermanentlyRemoved(item);
mItemRemovalListener.onItemPermanentlyRemoved(item, direction);
}
}
});
Expand Down