iOS: update list beforeDragging & add itemCanDrag

This commit is contained in:
王劲鹏 2022-09-01 10:27:50 +08:00 committed by osborn
parent ef4eb4d517
commit 8dd39398b9
2 changed files with 35 additions and 2 deletions

View File

@ -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}`)

View File

@ -67,9 +67,11 @@ @interface DoricListNode () <UITableViewDataSource, UITableViewDelegate>
@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<DoricDidScrollBlock> *)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;