Skip to content

Commit

Permalink
chore: add „movestart“ and "moveend" events to drag and drop (#13507)
Browse files Browse the repository at this point in the history
* chore: add „movestart“ event to drag and drop

* fix: fine tune events, add „moveend“ event

* fix(android): restore sample file

* fix: fix variable naming
  • Loading branch information
hansemannn committed Jul 17, 2022
1 parent 48208e3 commit 31d8194
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 30 deletions.
Expand Up @@ -325,6 +325,21 @@ public void onMoveItemEnded(int adapterIndex)
}
}
}
/**
* Called when starting a drag-and-drop gesture (touch start)
*/
public void onMoveGestureStarted()
{
fireEvent(TiC.EVENT_MOVE_START, null);
}

/**
* Called when starting a drag-and-drop gesture (touch end)
*/
public void onMoveGestureEnded()
{
fireEvent(TiC.EVENT_MOVE_END, null);
}

/**
* Delete row from table.
Expand Down
Expand Up @@ -39,6 +39,7 @@ public class ItemTouchHandler extends ItemTouchHelper.SimpleCallback
private int moveEndIndex = -1;
private Drawable icon;
private final ColorDrawable background;
private boolean hasMoveStarted = false;

@SuppressLint("ClickableViewAccessibility")
public ItemTouchHandler(@NonNull TiRecyclerViewAdapter adapter,
Expand All @@ -56,7 +57,13 @@ public ItemTouchHandler(@NonNull TiRecyclerViewAdapter adapter,
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getAction() == MotionEvent.ACTION_MOVE && !hasMoveStarted) {
recyclerViewProxy.onMoveGestureStarted();
hasMoveStarted = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
recyclerViewProxy.onMoveGestureEnded();
hasMoveStarted = false;

if (moveEndIndex >= 0) {
// Notify owner that item movement has ended. Will fire a "move" event.
recyclerViewProxy.onMoveItemEnded(moveEndIndex);
Expand Down
Expand Up @@ -274,6 +274,22 @@ public void onMoveItemEnded(int adapterIndex)
this.moveEventInfo.clear();
}

/**
* Called when starting a drag-and-drop gesture (touch start)
*/
public void onMoveGestureStarted()
{
fireEvent(TiC.EVENT_MOVE_START, null);
}

/**
* Called when starting a drag-and-drop gesture (touch end)
*/
public void onMoveGestureEnded()
{
fireEvent(TiC.EVENT_MOVE_END, null);
}

/**
* Remove section from list at specified index.
*
Expand Down
Expand Up @@ -19,4 +19,8 @@ public abstract class RecyclerViewProxy extends TiViewProxy
public abstract boolean onMoveItemStarting(int index);

public abstract void onMoveItemEnded(int index);

public abstract void onMoveGestureStarted();

public abstract void onMoveGestureEnded();
}
2 changes: 2 additions & 0 deletions android/titanium/src/java/org/appcelerator/titanium/TiC.java
Expand Up @@ -147,6 +147,8 @@ public class TiC
public static final String EVENT_PROPERTY_FORCE = "force";
public static final String EVENT_PROPERTY_SIZE = "size";
public static final String EVENT_MOVE = "move";
public static final String EVENT_MOVE_START = "movestart";
public static final String EVENT_MOVE_END = "moveend";
public static final String EVENT_REFRESH_END = "refreshend";
public static final String EVENT_REFRESH_START = "refreshstart";
public static final String EVENT_REGION_CHANGED = "regionchanged";
Expand Down
14 changes: 14 additions & 0 deletions apidoc/Titanium/UI/ListView.yml
Expand Up @@ -384,6 +384,20 @@ events:
summary: section item index of the reference item.
type: Number

- name: movestart
summary: Fired when a list row has started moving.
description: |
This property can be used to change the UI once a new drag-and-drop interaction starts.
since: "11.1.0"
platforms: [android, iphone, ipad, macos]

- name: moveend
summary: Fired when a list row has ended moving.
description: |
This property can be used to change the UI once a drag-and-drop interaction ends.
since: "11.1.0"
platforms: [android, iphone, ipad, macos]

- name: move
summary: Fired when a list row is moved to a different location by the user.
description: |
Expand Down
66 changes: 37 additions & 29 deletions iphone/Classes/TiUIListView.m
Expand Up @@ -1079,17 +1079,52 @@ - (BOOL)canEditRowAtIndexPath:(NSIndexPath *)indexPath
- (BOOL)canInsertRowAtIndexPath:(NSIndexPath *)indexPath
{
id insertValue = [self valueWithKey:@"canInsert" atIndexPath:indexPath];
//canInsert if undefined is false
return [TiUtils boolValue:insertValue def:NO];
}

- (BOOL)canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
id moveValue = [self valueWithKey:@"canMove" atIndexPath:indexPath];
//canMove if undefined is false
return [TiUtils boolValue:moveValue def:NO];
}

- (nonnull NSArray<UIDragItem *> *)tableView:(nonnull UITableView *)tableView itemsForBeginningDragSession:(nonnull id<UIDragSession>)session atIndexPath:(nonnull NSIndexPath *)indexPath
{
NSItemProvider *itemProvider = [NSItemProvider new];
NSString *identifier = [NSString stringWithFormat:@"%lu_%lu", indexPath.section, indexPath.row];

[itemProvider registerDataRepresentationForTypeIdentifier:(NSString *)kUTTypePlainText
visibility:NSItemProviderRepresentationVisibilityAll
loadHandler:^NSProgress *_Nullable(void (^_Nonnull completionHandler)(NSData *_Nullable, NSError *_Nullable)) {
return nil;
}];

UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:itemProvider];
dragItem.localObject = identifier;

return @[ dragItem ];
}

- (void)tableView:(UITableView *)tableView dropSessionDidEnter:(id<UIDropSession>)session
{
[[self proxy] fireEvent:@"movestart"];
}

- (void)tableView:(UITableView *)tableView dropSessionDidEnd:(id<UIDropSession>)session
{
[[self proxy] fireEvent:@"moveend"];
}

- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator
{
// NO-OP right now
}

- (BOOL)tableView:(UITableView *)tableView canHandleDropSession:(id<UIDropSession>)session
{
return [session canLoadObjectsOfClass:[NSString class]];
}

- (NSArray *)editActionsFromValue:(id)value
{
ENSURE_ARRAY(value);
Expand Down Expand Up @@ -2561,33 +2596,6 @@ + (UITableViewRowAnimation)animationStyleForProperties:(NSDictionary *)propertie
return animate ? UITableViewRowAnimationFade : UITableViewRowAnimationNone;
}

- (nonnull NSArray<UIDragItem *> *)tableView:(nonnull UITableView *)tableView itemsForBeginningDragSession:(nonnull id<UIDragSession>)session atIndexPath:(nonnull NSIndexPath *)indexPath
{
NSItemProvider *itemProvider = [NSItemProvider new];
NSString *identifier = [NSString stringWithFormat:@"%lu_%lu", indexPath.section, indexPath.row];

[itemProvider registerDataRepresentationForTypeIdentifier:(NSString *)kUTTypePlainText
visibility:NSItemProviderRepresentationVisibilityAll
loadHandler:^NSProgress *_Nullable(void (^_Nonnull completionHandler)(NSData *_Nullable, NSError *_Nullable)) {
return nil;
}];

UIDragItem *dragItem = [[UIDragItem alloc] initWithItemProvider:itemProvider];
dragItem.localObject = identifier;

return @[ dragItem ];
}

- (void)tableView:(UITableView *)tableView performDropWithCoordinator:(id<UITableViewDropCoordinator>)coordinator
{
// NO-OP right now
}

- (BOOL)tableView:(UITableView *)tableView canHandleDropSession:(id<UIDropSession>)session
{
return [session canLoadObjectsOfClass:[NSString class]];
}

@end

static TiViewProxy *FindViewProxyWithBindIdContainingPoint(UIView *view, CGPoint point)
Expand Down

0 comments on commit 31d8194

Please sign in to comment.