Skip to content

Commit eb4543d

Browse files
committed
add batch updates
1 parent b988a9a commit eb4543d

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

Ark.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
B42875CA24EE7A7700933413 /* Ark.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B4256DC224ED1715000DF13C /* Ark.framework */; };
2828
B42875CB24EE7A7700933413 /* Ark.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = B4256DC224ED1715000DF13C /* Ark.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
2929
B44FACDA24FE2B4D0027D5A5 /* AnyNodable.swift in Sources */ = {isa = PBXBuildFile; fileRef = B44FACD924FE2B4D0027D5A5 /* AnyNodable.swift */; };
30+
B44FACDC24FE5CAE0027D5A5 /* BatchUpdates.swift in Sources */ = {isa = PBXBuildFile; fileRef = B44FACDB24FE5CAE0027D5A5 /* BatchUpdates.swift */; };
3031
B4E5581B24FCA9DA004C2477 /* DateFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E81FA224F7A8C600AAE8B0 /* DateFormat.swift */; };
3132
B4E5581E24FCC530004C2477 /* NodeEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4E5581D24FCC530004C2477 /* NodeEvent.swift */; };
3233
/* End PBXBuildFile section */
@@ -92,6 +93,7 @@
9293
B42875C024EE138100933413 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
9394
B42875C524EE2CBB00933413 /* DiffableDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DiffableDataSource.swift; sourceTree = "<group>"; };
9495
B44FACD924FE2B4D0027D5A5 /* AnyNodable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyNodable.swift; sourceTree = "<group>"; };
96+
B44FACDB24FE5CAE0027D5A5 /* BatchUpdates.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchUpdates.swift; sourceTree = "<group>"; };
9597
B4E5581D24FCC530004C2477 /* NodeEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodeEvent.swift; sourceTree = "<group>"; };
9698
F0C766C3C5A77FD675545DA9 /* Pods_ArkDemo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ArkDemo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
9799
F61531B872BE69FE4C5901BD /* Pods-Ark.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Ark.release.xcconfig"; path = "Target Support Files/Pods-Ark/Pods-Ark.release.xcconfig"; sourceTree = "<group>"; };
@@ -158,6 +160,7 @@
158160
children = (
159161
B4256DC524ED1715000DF13C /* Ark.h */,
160162
B42875AA24EE10ED00933413 /* ListDiff.swift */,
163+
B44FACDB24FE5CAE0027D5A5 /* BatchUpdates.swift */,
161164
B42875C524EE2CBB00933413 /* DiffableDataSource.swift */,
162165
A64FD07724F681CF00EB0D38 /* Nodable.swift */,
163166
B44FACD924FE2B4D0027D5A5 /* AnyNodable.swift */,
@@ -399,6 +402,7 @@
399402
files = (
400403
B4E5581E24FCC530004C2477 /* NodeEvent.swift in Sources */,
401404
B42875AB24EE10ED00933413 /* ListDiff.swift in Sources */,
405+
B44FACDC24FE5CAE0027D5A5 /* BatchUpdates.swift in Sources */,
402406
A6E81FA624F7E99E00AAE8B0 /* NodeContext.swift in Sources */,
403407
B42875C624EE2CBB00933413 /* DiffableDataSource.swift in Sources */,
404408
B44FACDA24FE2B4D0027D5A5 /* AnyNodable.swift in Sources */,

Ark/BatchUpdates.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// BatchUpdates.swift
3+
// Ark
4+
//
5+
// Created by Dwight CHENG on 2020/9/1.
6+
// Copyright © 2020 Glow. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
struct BatchUpdates {
12+
var itemInserts: [IndexPath] = []
13+
var itemDeletes: [IndexPath] = []
14+
var itemMoves: [MoveIndexPath] = []
15+
}
16+
17+
struct MoveIndexPath {
18+
let from: IndexPath
19+
let to: IndexPath
20+
}
21+
22+
extension IndexSet {
23+
func toIndexPaths(section: Int) -> [IndexPath] {
24+
map({ IndexPath(item: $0, section: section) })
25+
}
26+
}
27+
28+
extension Array where Element == List.MoveIndex {
29+
func toIndexPaths(section: Int) -> [MoveIndexPath] {
30+
map({
31+
MoveIndexPath(
32+
from: IndexPath(item: $0.from, section: section),
33+
to: IndexPath(item: $0.to, section: section))
34+
})
35+
}
36+
}

Ark/DiffableDataSource.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,31 @@ public class DiffableDataSource<Target: SectionInflator>: NSObject,
6969
return
7070
}
7171
self.snapshot = snapshot
72-
let result = List.diffing(oldArray: oldSnapshot.sections, newArray: snapshot.sections).forBatchUpdates()
72+
let result = List.diffing(oldArray: oldSnapshot.sections, newArray: snapshot.sections)
7373
print(result)
74+
75+
func createBactchUpdates(from updates: IndexSet) -> BatchUpdates {
76+
var result = BatchUpdates()
77+
for section in updates {
78+
let oldItems = oldSnapshot.sections[section].items
79+
let newItems = snapshot.sections[section].items
80+
let diffResult = List.diffing(oldArray: oldItems, newArray: newItems).forBatchUpdates()
81+
result.itemDeletes += diffResult.deletes.toIndexPaths(section: section)
82+
result.itemInserts += diffResult.inserts.toIndexPaths(section: section)
83+
result.itemMoves += diffResult.moves.toIndexPaths(section: section)
84+
}
85+
return result
86+
}
87+
let batchUpdates = createBactchUpdates(from: result.updates)
88+
7489
collectionNode.performBatch(animated: animatingDifferences, updates: { [node = self.collectionNode] in
90+
91+
node?.deleteItems(at: batchUpdates.itemDeletes)
92+
node?.insertItems(at: batchUpdates.itemInserts)
93+
94+
for move in batchUpdates.itemMoves {
95+
node?.moveItem(at: move.from, to: move.to)
96+
}
7597

7698
for move in result.moves {
7799
node?.moveSection(move.from, toSection: move.to)

ArkDemo/ViewController.swift

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,19 @@ final class ViewController: ASDKViewController<ASCollectionNode> {
8585
}
8686

8787
private func handleArticleEvent(_ event: GenericNodeEvent<ArticleFeed.Subject>) {
88-
func findSectionAndIndex(of subject: ArticleFeed.Subject) -> (ArticleFeed, IndexPath)? {
89-
for (section, model) in self.sections.enumerated() {
90-
guard case .articleFeed(let feed) = model,
91-
let index = feed.subjects.firstIndex(of: subject) else {
92-
continue
93-
}
94-
return (feed, IndexPath(item: index, section: section))
95-
}
96-
return nil
88+
guard case .articleFeed(let feed) = sections[event.indexPath.section],
89+
let index = feed.subjects.firstIndex(of: event.model) else {
90+
return
9791
}
98-
if let (feed, indexPath) = findSectionAndIndex(of: event.model) {
99-
switch event.model {
100-
case .article(var article):
101-
article.read.toggle()
102-
var subjects = feed.subjects
103-
subjects[indexPath.item] = .article(article)
104-
let newFeed = HomeFeed.articleFeed(.init(date: feed.date, subjects: subjects))
105-
sections[indexPath.section] = newFeed
106-
case .poll(let poll):
107-
break
108-
}
92+
switch event.model {
93+
case .article(var article):
94+
var subjects = feed.subjects
95+
article.read.toggle()
96+
subjects[index] = .article(article)
97+
let newFeed = HomeFeed.articleFeed(.init(date: feed.date, subjects: subjects))
98+
sections[event.indexPath.section] = newFeed
99+
case .poll(let poll):
100+
break
109101
}
110102
}
111103

0 commit comments

Comments
 (0)