|
| 1 | +import UIKit |
| 2 | +import DifferenceKit |
| 3 | + |
| 4 | +private struct RandomModel: Differentiable { |
| 5 | + var id: UUID |
| 6 | + var isUpdated: Bool |
| 7 | + |
| 8 | + var differenceIdentifier: UUID { |
| 9 | + return id |
| 10 | + } |
| 11 | + |
| 12 | + init(_ id: UUID = UUID(), _ isUpdated: Bool = false) { |
| 13 | + self.id = id |
| 14 | + self.isUpdated = isUpdated |
| 15 | + } |
| 16 | + |
| 17 | + func isContentEqual(to source: RandomModel) -> Bool { |
| 18 | + return isUpdated == source.isUpdated |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +private typealias RandomSection = ArraySection<RandomModel, RandomModel> |
| 23 | + |
| 24 | +final class RandomViewController: UIViewController { |
| 25 | + private let collectionView: UICollectionView |
| 26 | + private var data = [RandomSection]() |
| 27 | + |
| 28 | + private var dataInput: [RandomSection] { |
| 29 | + get { return data } |
| 30 | + set { |
| 31 | + let changeset = StagedChangeset(source: data, target: newValue) |
| 32 | + collectionView.reload(using: changeset) { data in |
| 33 | + self.data = data |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + @objc private func refresh() { |
| 39 | + let defaultSourceSectionCount = 20 |
| 40 | + let defaultSourceElementCount = 20 |
| 41 | + |
| 42 | + func randomSection() -> ArraySection<RandomModel, RandomModel> { |
| 43 | + let elements = (0..<defaultSourceElementCount).map { _ in RandomModel() } |
| 44 | + return ArraySection(model: RandomModel(), elements: elements) |
| 45 | + } |
| 46 | + |
| 47 | + guard !data.isEmpty else { |
| 48 | + dataInput = (0..<defaultSourceSectionCount).map { _ in randomSection() } |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + let source = data |
| 53 | + var target = source |
| 54 | + |
| 55 | + let sourceSectionCount = source.count |
| 56 | + let deleteSectionCount = Int.random(in: 0..<sourceSectionCount / 4) |
| 57 | + let deletedSourceSectionCount = sourceSectionCount - deleteSectionCount |
| 58 | + let updateSectionCount = Int.random(in: 0..<deletedSourceSectionCount / 4) |
| 59 | + let moveSectionCount = Int.random(in: 0..<deletedSourceSectionCount / 4) |
| 60 | + let minInsertCount = defaultSourceSectionCount > sourceSectionCount ? deleteSectionCount : 0 |
| 61 | + let insertSectionCount = Int.random(in: minInsertCount..<sourceSectionCount / 4) |
| 62 | + let targetSectionCount = deletedSourceSectionCount + insertSectionCount |
| 63 | + |
| 64 | + let deleteSectionIndices = (0..<deleteSectionCount).map { i in Int.random(in: 0..<sourceSectionCount - i) } |
| 65 | + let updateSectionIndices = (0..<updateSectionCount).map { _ in Int.random(in: 0..<deletedSourceSectionCount) } |
| 66 | + let moveSectionIndexPairs = (0..<moveSectionCount).map { _ in (source: Int.random(in: 0..<deletedSourceSectionCount), target: Int.random(in: 0..<deletedSourceSectionCount)) } |
| 67 | + let insertSectionIndices = (0..<insertSectionCount).map { i in Int.random(in: 0..<deletedSourceSectionCount + i) } |
| 68 | + |
| 69 | + for index in deleteSectionIndices { |
| 70 | + target.remove(at: index) |
| 71 | + } |
| 72 | + |
| 73 | + for index in target.indices { |
| 74 | + let sourceCount = target[index].elements.count |
| 75 | + let deleteCount = Int.random(in: 0..<sourceCount / 4) |
| 76 | + let deletedSourceCount = sourceCount - deleteCount |
| 77 | + let updateCount = Int.random(in: 0..<deletedSourceCount / 4) |
| 78 | + let moveCount = Int.random(in: 0..<deletedSourceCount / 4) |
| 79 | + let insertCount = Int.random(in: 0..<sourceCount / 4) |
| 80 | + |
| 81 | + let deleteIndices = (0..<deleteCount).map { i in Int.random(in: 0..<sourceCount - i) } |
| 82 | + let updateIndices = (0..<updateCount).map { _ in Int.random(in: 0..<deletedSourceCount) } |
| 83 | + let moveIndexPairs = (0..<moveCount).map { _ in (source: Int.random(in: 0..<deletedSourceCount), target: Int.random(in: 0..<deletedSourceCount)) } |
| 84 | + let insertIndices = (0..<insertCount).map { i in Int.random(in: 0..<deletedSourceCount + i) } |
| 85 | + |
| 86 | + for elementIndex in deleteIndices { |
| 87 | + target[index].elements.remove(at: elementIndex) |
| 88 | + } |
| 89 | + |
| 90 | + for elementIndex in updateIndices { |
| 91 | + target[index].elements[elementIndex].isUpdated.toggle() |
| 92 | + } |
| 93 | + |
| 94 | + for pair in moveIndexPairs { |
| 95 | + target[index].elements.swapAt(pair.source, pair.target) |
| 96 | + } |
| 97 | + |
| 98 | + for elementIndex in insertIndices { |
| 99 | + target[index].elements.insert(RandomModel(), at: elementIndex) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + for index in updateSectionIndices { |
| 104 | + target[index].model.isUpdated.toggle() |
| 105 | + } |
| 106 | + |
| 107 | + for pair in moveSectionIndexPairs { |
| 108 | + target.swapAt(pair.source, pair.target) |
| 109 | + } |
| 110 | + |
| 111 | + for index in insertSectionIndices { |
| 112 | + target.insert(randomSection(), at: index) |
| 113 | + } |
| 114 | + |
| 115 | + let elementMoveAcrossSectionCount = Int.random(in: 0..<targetSectionCount * 2) |
| 116 | + for _ in (0..<elementMoveAcrossSectionCount) { |
| 117 | + func randomIndexPath() -> IndexPath { |
| 118 | + let sectionIndex = Int.random(in: 0..<targetSectionCount) |
| 119 | + let elementIndex = Int.random(in: 0..<target[sectionIndex].elements.count) |
| 120 | + return IndexPath(item: elementIndex, section: sectionIndex) |
| 121 | + } |
| 122 | + let sourceIndexPath = randomIndexPath() |
| 123 | + let targetIndexPath = randomIndexPath() |
| 124 | + target[sourceIndexPath.section].elements[sourceIndexPath.item] = target[targetIndexPath.section].elements[targetIndexPath.item] |
| 125 | + target[targetIndexPath.section].elements[targetIndexPath.item] = target[sourceIndexPath.section].elements[sourceIndexPath.item] |
| 126 | + } |
| 127 | + |
| 128 | + dataInput = target |
| 129 | + } |
| 130 | + |
| 131 | + override func viewWillAppear(_ animated: Bool) { |
| 132 | + super.viewWillAppear(animated) |
| 133 | + refresh() |
| 134 | + } |
| 135 | + |
| 136 | + init() { |
| 137 | + let flowLayout = UICollectionViewFlowLayout() |
| 138 | + flowLayout.sectionInset = UIEdgeInsets(top: 8, left: 0, bottom: 8, right: 0) |
| 139 | + flowLayout.headerReferenceSize = CGSize(width: UIScreen.main.bounds.size.width, height: 30) |
| 140 | + collectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout) |
| 141 | + |
| 142 | + super.init(nibName: nil, bundle: nil) |
| 143 | + |
| 144 | + title = "Random" |
| 145 | + view.backgroundColor = .white |
| 146 | + navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: #selector(refresh)) |
| 147 | + |
| 148 | + collectionView.backgroundColor = .clear |
| 149 | + collectionView.dataSource = self |
| 150 | + collectionView.delegate = self |
| 151 | + collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: UICollectionViewCell.reuseIdentifier) |
| 152 | + collectionView.register(TextCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: TextCollectionReusableView.reuseIdentifier) |
| 153 | + collectionView.translatesAutoresizingMaskIntoConstraints = false |
| 154 | + view.addSubview(collectionView) |
| 155 | + |
| 156 | + let constraints = [ |
| 157 | + collectionView.topAnchor.constraint(equalTo: view.topAnchor), |
| 158 | + collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor), |
| 159 | + collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 160 | + collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor) |
| 161 | + ] |
| 162 | + |
| 163 | + NSLayoutConstraint.activate(constraints) |
| 164 | + } |
| 165 | + |
| 166 | + @available(*, unavailable) |
| 167 | + required init?(coder aDecoder: NSCoder) { |
| 168 | + fatalError("init(coder:) has not been implemented") |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +extension RandomViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { |
| 173 | + func numberOfSections(in collectionView: UICollectionView) -> Int { |
| 174 | + return data.count |
| 175 | + } |
| 176 | + |
| 177 | + func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| 178 | + return data[section].elements.count |
| 179 | + } |
| 180 | + |
| 181 | + func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| 182 | + let cell = collectionView.dequeueReusableCell(withReuseIdentifier: UICollectionViewCell.reuseIdentifier, for: indexPath) |
| 183 | + let model = data[indexPath.section].elements[indexPath.item] |
| 184 | + cell.contentView.backgroundColor = model.isUpdated ? .yellow : .red |
| 185 | + return cell |
| 186 | + } |
| 187 | + |
| 188 | + func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { |
| 189 | + guard kind == UICollectionElementKindSectionHeader else { |
| 190 | + return UICollectionReusableView() |
| 191 | + } |
| 192 | + |
| 193 | + let model = data[indexPath.section].model |
| 194 | + let view = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: TextCollectionReusableView.reuseIdentifier, for: indexPath) as! TextCollectionReusableView |
| 195 | + view.text = "Section \(model.id)" + (model.isUpdated ? "+" : "") |
| 196 | + return view |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +private extension UICollectionViewCell { |
| 201 | + static let reuseIdentifier = String(describing: self) |
| 202 | +} |
0 commit comments