Skip to content

Commit 729e725

Browse files
author
farfromrefug
committed
fix: reorder allow to disable reording to certain positions (using itemReorderCheck event)
1 parent 968b865 commit 729e725

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/collectionview/index-common.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
105105
public static itemTapEvent = 'itemTap';
106106
public static displayItemEvent = 'displayItem';
107107
public static itemReorderedEvent = 'itemReordered';
108+
public static itemReorderCheckEvent = 'itemReorderCheck';
108109
public static itemReorderStartingEvent = 'itemReorderStarting';
109110
public static itemReorderStartedEvent = 'itemReorderStarted';
110111
public static loadMoreItemsEvent = 'loadMoreItems';
@@ -559,6 +560,19 @@ export abstract class CollectionViewBase extends View implements CollectionViewD
559560
this.notify(args);
560561
this.draggingView = null;
561562
}
563+
_canReorderToPosition(oldPosition, newPosition, item) {
564+
const args = {
565+
returnValue: true,
566+
eventName: CollectionViewBase.itemReorderCheckEvent,
567+
object: this,
568+
index: oldPosition,
569+
item,
570+
data: { targetIndex: newPosition },
571+
view: this.draggingView
572+
} as CollectionViewItemEventData & { returnValue: boolean };
573+
this.notify(args);
574+
return args.returnValue;
575+
}
562576
_reorderItemInSource(oldPosition: number, newPosition: number, callEvents = true) {
563577
this.suspendUpdates();
564578
const ownerSource = this.items as any;

src/collectionview/index.android.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ class SimpleCallback extends androidx.recyclerview.widget.ItemTouchHelper.Simple
5656
): boolean {
5757
const startPosition = viewHolder.getAdapterPosition();
5858
const endPosition = target.getAdapterPosition();
59+
const owner = this.owner?.get();
60+
if (owner && !owner._canReorderToPosition(startPosition, endPosition, owner.getItemAtIndex(endPosition))) {
61+
return false;
62+
}
5963
if (this.startPosition === -1) {
6064
this.startPosition = startPosition;
6165
}
6266
this.endPosition = endPosition;
63-
const owner = this.owner?.get();
6467
if (owner) {
6568
owner._reorderItemInSource(startPosition, endPosition);
6669
return true;

src/collectionview/index.ios.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ export class CollectionView extends CollectionViewBase {
175175
return this._map.size;
176176
}
177177
onLoaded() {
178-
super.onLoaded()
178+
super.onLoaded();
179179
// we need refreshVisibleItems
180180
// if some items were updated while unloaded they wont re layout
181181
// after this (because we are not a Layout view)
182-
this.refreshVisibleItems()
182+
this.refreshVisibleItems();
183183
}
184184
eachChild(callback: (child: ViewBase) => boolean) {
185185
// used for css updates (like theme change)
@@ -472,16 +472,15 @@ export class CollectionView extends CollectionViewBase {
472472
// this.clearCellSize();
473473

474474
const sizes: NSMutableArray<NSValue> = this._delegate instanceof UICollectionViewDelegateImpl ? this._delegate.cachedSizes : null;
475-
const performBatchUpdatesCompletion = (c) =>
476-
{
475+
const performBatchUpdatesCompletion = (c) => {
477476
// if we are not "presented" (viewController hidden) then performBatchUpdatesCompletion would crash
478-
const viewIsLoaded = !!this.page?.viewController ? !!this.page.viewController.view.window : true;
477+
const viewIsLoaded = !!this.page?.viewController ? !!this.page.viewController.view.window : true;
479478
if (viewIsLoaded) {
480479
view.performBatchUpdatesCompletion(c, null);
481480
} else {
482481
c();
483482
}
484-
}
483+
};
485484

486485
switch (event.action) {
487486
case ChangeType.Delete: {
@@ -1273,6 +1272,13 @@ class UICollectionViewDelegateImpl extends UICollectionViewCacheDelegateFlowLayo
12731272
owner.scrollViewDidEndScrollingAnimation(scrollView);
12741273
}
12751274
}
1275+
collectionViewTargetIndexPathForMoveFromItemAtIndexPathToProposedIndexPath(collectionView: UICollectionView, currentIndexPath: NSIndexPath, proposedIndexPath: NSIndexPath): NSIndexPath {
1276+
const owner = this._owner?.get();
1277+
if (owner && !owner._canReorderToPosition(currentIndexPath.row, proposedIndexPath.row, owner.getItemAtIndex(proposedIndexPath.row))) {
1278+
return currentIndexPath;
1279+
}
1280+
return proposedIndexPath;
1281+
}
12761282
}
12771283

12781284
@NativeClass

0 commit comments

Comments
 (0)