Skip to content

Commit 0fb5b6e

Browse files
authored
Merge pull request #246 from surfstudio/hotfix/SPT-1546/insert_func_optimisation
Hotfix/spt 1546/insert func optimisation
2 parents c280fdc + 4944fec commit 0fb5b6e

File tree

10 files changed

+160
-129
lines changed

10 files changed

+160
-129
lines changed

Example/ReactiveDataDisplayManager/Table/AllPluginsTableViewController/AllPluginsTableViewController.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,33 @@ private extension AllPluginsTableViewController {
9797
addPrefetcherableSection()
9898

9999
// Tell adapter that we've changed generators
100-
adapter.forceRefill()
100+
adapter.forceRefill { [weak self] in
101+
self?.insertMoreSections()
102+
}
101103
}
102104

103105
func updateBarButtonItem(with title: String) {
104106
let button = UIBarButtonItem(title: title, style: .plain, target: self, action: #selector(changeTableEditing))
105107
navigationItem.rightBarButtonItem = button
106108
}
107109

110+
/// Insertion of new section with some cells
111+
func insertMoreSections() {
112+
113+
guard let existingSectionGenerator = adapter.sections.last else {
114+
return
115+
}
116+
117+
// Create generators
118+
let newHeaderGenerator = SectionTitleHeaderGenerator(model: "One more section", needSectionIndexTitle: true)
119+
let generators = Constants.titles.map { TitleTableViewCell.rddm.baseGenerator(with: $0) }
120+
121+
// Insert them
122+
adapter.insertSection(after: existingSectionGenerator,
123+
new: newHeaderGenerator,
124+
generators: generators)
125+
}
126+
108127
/// Use this method for UI stress test only.
109128
/// You can traine here usage of manual manager and different replacing and insertions.
110129
/// - Note: some combinations of functions may cause crash and it's normal, because manualBuilder is just wrapper under tableView functions.

ReactiveDataDisplayManagerTests/Mocks/AutoMockable.generated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Generated using Sourcery 1.9.2 — https://github.com/krzysztofzablocki/Sourcery
1+
// Generated using Sourcery 2.0.2 — https://github.com/krzysztofzablocki/Sourcery
22
// DO NOT EDIT
33
// swiftlint:disable all
44

Source/Collection/Manager/BaseCollectionManager.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,14 @@ private extension BaseCollectionManager {
220220

221221
elements.forEach { [weak self] element in
222222
element.generator.registerCell(in: view)
223-
self?.generators[element.sectionIndex].insert(element.generator, at: element.generatorIndex)
223+
if self?.generators.count == element.sectionIndex {
224+
self?.generators.append([element.generator])
225+
} else {
226+
self?.generators[element.sectionIndex].insert(element.generator, at: element.generatorIndex)
227+
}
224228
}
225229

226-
let indexPaths = elements.map {
227-
IndexPath(row: $0.generatorIndex, section: $0.sectionIndex)
228-
}
230+
let indexPaths = elements.map { IndexPath(item: $0.generatorIndex, section: $0.sectionIndex) }
229231

230232
modifier?.insertRows(at: indexPaths, with: .animated)
231233
}

Source/Collection/Modifier/CollectionCommonModifier.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,28 @@ class CollectionCommonModifier: Modifier<UICollectionView, CollectionItemAnimati
8383
}
8484
}
8585

86+
/// Insert new sections with items at specific position with animation
87+
///
88+
/// - parameter indexDictionary: dictionary where **key** is new section index and value is location of items to insert
89+
/// - parameter insertAnimation: animation of insert operation
90+
/// - Warning: make sure that you do not have mistake in indexes inside `indexDictionary`.
91+
/// For example, if you are inserting **many sections** using this method you should notice that **index** cannot be greater than **final number of sections**.
92+
override func insertSectionsAndRows(at indexDictionary: [Int: [IndexPath]],
93+
with insertAnimation: CollectionItemAnimation?) {
94+
guard let view = view else { return }
95+
animator?.perform(in: view, animated: insertAnimation != nil) { [weak view] in
96+
let setOfKeys = IndexSet(indexDictionary.keys)
97+
let allValues = indexDictionary.values.flatMap { $0 }
98+
view?.insertSections(setOfKeys)
99+
view?.insertItems(at: allValues)
100+
}
101+
}
102+
86103
/// Insert sections with animation
87104
///
88105
/// - parameter indexPaths: indexes of inserted sections
89106
/// - parameter insertAnimation: animation of inserted sections
107+
/// - Warning: This method will insert an **empty** section only. If you need to insert section with items use `insertSectionsAndRows` instead.
90108
override func insertSections(at indexPaths: IndexSet, with insertAnimation: CollectionItemAnimation?) {
91109
guard let view = view else { return }
92110
animator?.perform(in: view, animated: insertAnimation != nil) { [weak view] in

Source/Collection/Modifier/CollectionDiffableModifier.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,17 @@ class CollectionDiffableModifier: Modifier<UICollectionView, CollectionItemAnima
8686
apply(animated: animation != nil)
8787
}
8888

89+
/// Insert new sections with items at specific position with animation
90+
///
91+
/// - parameter indexDictionary: **ignored**, automatically calculated using `DiffableSnapshot`
92+
/// - parameter insertAnimation:
93+
/// - **allowed** nil to disable animation
94+
/// - **ignored** any other, because automatically selected by `UICollectionViewDiffableDataSource`
95+
override func insertSectionsAndRows(at indexDictionary: [Int: [IndexPath]],
96+
with insertAnimation: CollectionItemAnimation?) {
97+
apply(animated: insertAnimation != nil)
98+
}
99+
89100
/// Update snapshot after sections inserted
90101
///
91102
/// - parameter indexPaths: **ignored**, automatically calculated using `DiffableSnapshot`

Source/Protocols/Modifier/Modifier.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,21 @@ open class Modifier<View: UIView, Animation> {
6060
preconditionFailure("\(#function) must be overriden in child")
6161
}
6262

63+
/// Insert new sections with rows at specific position with animation
64+
///
65+
/// - parameter indexDictionary: dictionary where **key** is new section index and value is location of subviews to insert
66+
/// - parameter insertAnimation: animation of insert operation
67+
/// - Warning: make sure that you do not have mistake in indexes inside `indexDictionary`.
68+
/// For example, if you are inserting **many sections** using this method you should notice that **index** cannot be greater than **final number of sections**.
69+
open func insertSectionsAndRows(at indexDictionary: [Int: [IndexPath]], with insertAnimation: Animation?) {
70+
preconditionFailure("\(#function) must be overriden in child")
71+
}
72+
6373
/// Insert new sections at specific position with animation
6474
///
6575
/// - parameter indexPaths: location of sections to insert
6676
/// - parameter insertAnimation: animation of insert operation
77+
/// - Warning: This method will insert an **empty** section only. If you need to insert section with rows use `insertSectionsAndRows: [Int : [IndexPath]]` instead.
6778
open func insertSections(at indexPaths: IndexSet, with insertAnimation: Animation?) {
6879
preconditionFailure("\(#function) must be overriden in child")
6980
}

Source/Table/Manager/BaseTableManager.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ extension BaseTableManager {
195195
self?.generators[element.sectionIndex].insert(element.generator, at: element.generatorIndex)
196196
}
197197

198-
let indexPaths = elements.map {
199-
IndexPath(row: $0.generatorIndex, section: $0.sectionIndex)
200-
}
198+
let indexPaths = elements.map { IndexPath(row: $0.generatorIndex, section: $0.sectionIndex) }
201199

202200
modifier?.insertRows(at: indexPaths, with: animation.value)
203201
}

0 commit comments

Comments
 (0)