/
CustomDiffableDataSource.swift
40 lines (30 loc) · 1.11 KB
/
CustomDiffableDataSource.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// CustomDiffableDataSource.swift
// GitHubUsers
//
// Created by Cathal Farrell on 04/09/2020.
// Copyright © 2020 Cathal Farrell. All rights reserved.
//
import UIKit
enum CollectionSection {
case main
}
class CustomDiffableDataSource: UICollectionViewDiffableDataSource<CollectionSection, User> {
var reorderedItems = [User]()
override func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath,
to destinationIndexPath: IndexPath) {
let sourceIndex = sourceIndexPath.row
let destinationIndex = destinationIndexPath.row
guard sourceIndex != destinationIndex else { return }
reorderedItems = self.snapshot().itemIdentifiers
let userToMove = reorderedItems[sourceIndex]
reorderedItems.remove(at: sourceIndex)
reorderedItems.insert(userToMove, at: destinationIndex)
}
override func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
return true
}
func getReorderedItems() -> [User] {
return reorderedItems
}
}