RXSwift - RxCollectionViewSectionedReloadDataSource - Drag & drop move (Reorder)

Viewed 251

I have used RxCollectionViewSectionedReloadDataSource to load my data into UICollectionView.

let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, WorkGroup>>(
            configureCell: { (_, collectionView, indexPath, item) in
                guard let cell = collectionView
                        .dequeueReusableCell(withReuseIdentifier: WorkGroupCell.identifier, for: indexPath) as? WorkGroupCell else {
                            return WorkGroupCell()
                        }
                cell.viewModel = item
                return cell
            }
        )
        
        viewModel.items.bind(to: tileCollectonView.rx.items(dataSource: dataSource)).disposed(by: disposeBag)
 
        tileCollectonView.rx.setDelegate(self).disposed(by: disposeBag)

Using above code i can display the data. But i want to drag and drop(Reorder) the cell.

Please let me know how can i do that reorder using RxSwift.

Help will be highly appreciated.

1 Answers

As is often the case, you need a state machine.

The logic involved is quite simple:

struct State<Item> {
    var items: [Item] = []
}

func state<Item>(initialState: State<Item>, itemMoved: Observable<ItemMovedEvent>) -> Observable<State<Item>> {
    itemMoved
        .scan(into: initialState) { (state, itemMoved) in
            state.items.move(from: itemMoved.sourceIndex.row, to: itemMoved.destinationIndex.row)
        }
        .startWith(initialState)
}

extension Array
{
    mutating func move(from oldIndex: Index, to newIndex: Index) {
        if oldIndex == newIndex { return }
        if abs(newIndex - oldIndex) == 1 { return self.swapAt(oldIndex, newIndex) }
        self.insert(self.remove(at: oldIndex), at: newIndex)
    }
}

The above uses the ItemMovedEvent provided by RxCocoa for table views. In order to create something like that for a collection view, you will need to wrap the UICollectionViewDragDelegate in a delegate proxy. An article on how to do that can be found here: Convert a Swift Delegate to RxSwift Observables

Related