Skip to content

Commit e145eaf

Browse files
committed
Create DiffableSectionSnapshot
1 parent 450c49a commit e145eaf

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Created by Jesse Squires
3+
// https://www.jessesquires.com
4+
//
5+
// Documentation
6+
// https://jessesquires.github.io/ReactiveCollectionsKit
7+
//
8+
// GitHub
9+
// https://github.com/jessesquires/ReactiveCollectionsKit
10+
//
11+
// Copyright © 2019-present Jesse Squires
12+
//
13+
14+
import Foundation
15+
import UIKit
16+
17+
typealias DiffableSectionSnapshot = NSDiffableDataSourceSectionSnapshot<AnyHashable>
18+
19+
extension DiffableSectionSnapshot {
20+
init(viewModel: SectionViewModel) {
21+
self.init()
22+
23+
let allCellIdentifiers = viewModel.cells.map(\.id)
24+
self.append(allCellIdentifiers)
25+
26+
viewModel.cells.forEach { cell in
27+
appendAllChildren(from: cell, to: cell.id)
28+
}
29+
}
30+
31+
private mutating func appendAllChildren(from cell: AnyCellViewModel, to parentId: AnyHashable) {
32+
let childIdentifiers = cell.children.map(\.id)
33+
34+
guard childIdentifiers.isNotEmpty else { return }
35+
36+
self.append(childIdentifiers, to: parentId)
37+
38+
for child in cell.children {
39+
appendAllChildren(from: child, to: child.id)
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)