apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.
MIT License
1.36k stars 161 forks source link

Add `dragDroppingItems` to accumulate changes & update `dragDropConfig.dataBinding` only once per drag-drop reorder #206

Open inamiy opened 3 years ago

inamiy commented 3 years ago

Hi there πŸ‘‹

This PR adds an improvement in CollectionView (and also TableView)'s drag-drop reordering which reduces "removed -> inserted" dance (2 calls) into 1 move only (1 call).

By applying this, I think it will be more SwiftUI-engine-friendly to only have 1 update per @Binding change.

Also, this fix is needed when CollectionView's items are derived from other single-source-of-truth via its state's binding transformation e.g. this code.

For example, if domain's @ObservableObject owns an array of N elements, and there's ViewModel that owns N + 1 elements (N comes from domain, and additional 1 element is for view-specific reason), we will eventually map $observableObject.item as Binding<[Domain.Item]> to Binding<[CollectionView.Item]> by using the above Binding.transform (or similar) function.

But if "removed -> inserted" occurs inside ASCollectionView, it will be difficult for domain (user-side) to recover the inserted view-item back to domain-item because view-item is usually only a part of domain-item. On the other side, if view-items are just reordered without item loss, it's easy for domain to reorder its domain-items since the order of view-item's ID only matters.

What do you think?