From 8dd39398b9871afff64db7b68533ec60e83fa8f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Thu, 1 Sep 2022 10:27:50 +0800 Subject: [PATCH] iOS: update list beforeDragging & add itemCanDrag --- doric-demo/src/ListDragDemo.ts | 9 ++++++- doric-iOS/Pod/Classes/Shader/DoricListNode.m | 28 +++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/doric-demo/src/ListDragDemo.ts b/doric-demo/src/ListDragDemo.ts index 1fd3ba41..0f3a24d0 100644 --- a/doric-demo/src/ListDragDemo.ts +++ b/doric-demo/src/ListDragDemo.ts @@ -27,8 +27,15 @@ class ListDragDemo extends Panel { }, layoutConfig: layoutConfig().most(), canDrag: true, + itemCanDrag: (from) => { + if (from === 0) { + return false; + } else { + return true; + } + }, beforeDragging: (from) => { - loge(`beforeDragging from: ${from}`) + return [0, 1, 2] }, onDragging: (from, to) => { loge(`onDragging from: ${from}, to: ${to}`) diff --git a/doric-iOS/Pod/Classes/Shader/DoricListNode.m b/doric-iOS/Pod/Classes/Shader/DoricListNode.m index ffe75d72..347ad639 100644 --- a/doric-iOS/Pod/Classes/Shader/DoricListNode.m +++ b/doric-iOS/Pod/Classes/Shader/DoricListNode.m @@ -67,9 +67,11 @@ @interface DoricListNode () @property(nonatomic, strong) UILongPressGestureRecognizer *longPress; @property(nonatomic, strong) NSIndexPath *initialDragIndexPath; @property(nonatomic, strong) NSIndexPath *currentDragIndexPath; +@property(nonatomic, copy) NSString *itemCanDragFuncId; @property(nonatomic, copy) NSString *beforeDraggingFuncId; @property(nonatomic, copy) NSString *onDraggingFuncId; @property(nonatomic, copy) NSString *onDraggedFuncId; +@property(nonatomic, strong) NSArray *swapDisabled; @property(nonatomic, assign) NSUInteger rowCount; @property(nonatomic, assign) BOOL needReload; @@ -119,11 +121,21 @@ - (void)longPressAction:(UILongPressGestureRecognizer *)sender { self.initialDragIndexPath = indexPath; self.currentDragIndexPath = indexPath; if (self.beforeDraggingFuncId != nil) { - [self callJSResponse:self.beforeDraggingFuncId, @(indexPath.row), nil]; + DoricAsyncResult *asyncResult = [self callJSResponse:self.beforeDraggingFuncId, @(indexPath.row), nil]; + JSValue *model = [asyncResult waitUntilResult]; + if (model.isArray) { + self.swapDisabled = [model toArray]; + } } } } else if (sender.state == UIGestureRecognizerStateChanged) { if ((indexPath != nil) && (indexPath != self.currentDragIndexPath)) { + for (int i = 0; i < self.swapDisabled.count; i++) { + if (indexPath.row == [self.swapDisabled[i] intValue]) { + return; + } + } + NSString *fromValue = self.itemViewIds[@(self.currentDragIndexPath.row)]; NSString *toValue = self.itemViewIds[@(indexPath.row)]; self.itemViewIds[@(self.currentDragIndexPath.row)] = toValue; @@ -186,6 +198,8 @@ - (void)blendView:(UITableView *)view forPropName:(NSString *)name propValue:(id } else if ([@"canDrag" isEqualToString:name]) { bool canDrag = [prop boolValue]; [self.longPress setEnabled:canDrag]; + } else if ([@"itemCanDrag" isEqualToString:name]) { + self.itemCanDragFuncId = prop; } else if ([@"beforeDragging" isEqualToString:name]) { self.beforeDraggingFuncId = prop; } else if ([@"onDragging" isEqualToString:name]) { @@ -535,6 +549,17 @@ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL } } +- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath { + if (self.itemCanDragFuncId != nil) { + DoricAsyncResult *asyncResult = [self callJSResponse:self.itemCanDragFuncId, @(indexPath.row), nil]; + JSValue *model = [asyncResult waitUntilResult]; + if (model.isBoolean) { + return [model toBool]; + } + } + return true; +} + - (NSMutableSet *)didScrollBlocks { if (!_didScrollBlocks) { _didScrollBlocks = [NSMutableSet new]; @@ -591,6 +616,7 @@ - (void)reset { self.loadMoreViewId = nil; self.onScrollFuncId = nil; self.onScrollEndFuncId = nil; + self.itemCanDragFuncId = nil; self.beforeDraggingFuncId = nil; self.onDraggingFuncId = nil; self.onDraggedFuncId = nil;