Skip to content

Commit ea762ad

Browse files
authored
Merge pull request #3 from kishikawakatsumi/class-to-struct
Make `DiffableDataSourceSnapshot` struct
2 parents 3e216e1 + d717904 commit ea762ad

7 files changed

+155
-146
lines changed

Sources/DiffableDataSourceSnapshot.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// A class for backporting `NSDiffableDataSourceSnapshot` introduced in iOS 13.0+, macOS 10.15+, tvOS 13.0+.
22
/// Represents the mutable state of diffable data source of UI.
3-
public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIdentifierType: Hashable> {
4-
internal let structure = SnapshotStructure<SectionIdentifierType, ItemIdentifierType>()
3+
public struct DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType> where SectionIdentifierType : Hashable, ItemIdentifierType : Hashable {
4+
internal var structure = SnapshotStructure<SectionIdentifierType, ItemIdentifierType>()
55

66
/// Creates a new empty snapshot object.
77
public init() {}
@@ -81,7 +81,7 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
8181
/// - Parameters:
8282
/// - identifiers: The item identifiers to be appended.
8383
/// - sectionIdentifier: An identifier of section to append the given identiciers.
84-
public func appendItems(_ identifiers: [ItemIdentifierType], toSection sectionIdentifier: SectionIdentifierType? = nil) {
84+
public mutating func appendItems(_ identifiers: [ItemIdentifierType], toSection sectionIdentifier: SectionIdentifierType? = nil) {
8585
structure.append(itemIDs: identifiers, to: sectionIdentifier)
8686
}
8787

@@ -90,7 +90,7 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
9090
/// - Parameters:
9191
/// - identifiers: The item identifiers to be inserted.
9292
/// - beforeIdentifier: An identifier of item.
93-
public func insertItems(_ identifiers: [ItemIdentifierType], beforeItem beforeIdentifier: ItemIdentifierType) {
93+
public mutating func insertItems(_ identifiers: [ItemIdentifierType], beforeItem beforeIdentifier: ItemIdentifierType) {
9494
structure.insert(itemIDs: identifiers, before: beforeIdentifier)
9595
}
9696

@@ -99,20 +99,20 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
9999
/// - Parameters:
100100
/// - identifiers: The item identifiers to be inserted.
101101
/// - afterIdentifier: An identifier of item.
102-
public func insertItems(_ identifiers: [ItemIdentifierType], afterItem afterIdentifier: ItemIdentifierType) {
102+
public mutating func insertItems(_ identifiers: [ItemIdentifierType], afterItem afterIdentifier: ItemIdentifierType) {
103103
structure.insert(itemIDs: identifiers, after: afterIdentifier)
104104
}
105105

106106
/// Deletes the specified items.
107107
///
108108
/// - Parameters:
109109
/// - identifiers: The item identifiers to be deleted.
110-
public func deleteItems(_ identifiers: [ItemIdentifierType]) {
110+
public mutating func deleteItems(_ identifiers: [ItemIdentifierType]) {
111111
structure.remove(itemIDs: identifiers)
112112
}
113113

114114
/// Deletes the all items in the snapshot.
115-
public func deleteAllItems() {
115+
public mutating func deleteAllItems() {
116116
structure.removeAllItems()
117117
}
118118

@@ -121,7 +121,7 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
121121
/// - Parameters:
122122
/// - identifier: An item identifier to be moved.
123123
/// - toIdentifier: An identifier of item.
124-
public func moveItem(_ identifier: ItemIdentifierType, beforeItem toIdentifier: ItemIdentifierType) {
124+
public mutating func moveItem(_ identifier: ItemIdentifierType, beforeItem toIdentifier: ItemIdentifierType) {
125125
structure.move(itemID: identifier, before: toIdentifier)
126126
}
127127

@@ -130,23 +130,23 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
130130
/// - Parameters:
131131
/// - identifier: An item identifier to be moved.
132132
/// - toIdentifier: An identifier of item.
133-
public func moveItem(_ identifier: ItemIdentifierType, afterItem toIdentifier: ItemIdentifierType) {
133+
public mutating func moveItem(_ identifier: ItemIdentifierType, afterItem toIdentifier: ItemIdentifierType) {
134134
structure.move(itemID: identifier, after: toIdentifier)
135135
}
136136

137137
/// Reloads the specified items.
138138
///
139139
/// - Parameters:
140140
/// - identifiers: The item identifiers to be reloaded.
141-
public func reloadItems(_ identifiers: [ItemIdentifierType]) {
141+
public mutating func reloadItems(_ identifiers: [ItemIdentifierType]) {
142142
structure.update(itemIDs: identifiers)
143143
}
144144

145145
/// Appends the given section identifiers to the end of sections.
146146
///
147147
/// - Parameters:
148148
/// - identifiers: The section identifiers to be appended.
149-
public func appendSections(_ identifiers: [SectionIdentifierType]) {
149+
public mutating func appendSections(_ identifiers: [SectionIdentifierType]) {
150150
structure.append(sectionIDs: identifiers)
151151
}
152152

@@ -155,7 +155,7 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
155155
/// - Parameters:
156156
/// - identifiers: The section identifiers to be inserted.
157157
/// - toIdentifier: An identifier of setion.
158-
public func insertSections(_ identifiers: [SectionIdentifierType], beforeSection toIdentifier: SectionIdentifierType) {
158+
public mutating func insertSections(_ identifiers: [SectionIdentifierType], beforeSection toIdentifier: SectionIdentifierType) {
159159
structure.insert(sectionIDs: identifiers, before: toIdentifier)
160160
}
161161

@@ -164,15 +164,15 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
164164
/// - Parameters:
165165
/// - identifiers: The section identifiers to be inserted.
166166
/// - toIdentifier: An identifier of setion.
167-
public func insertSections(_ identifiers: [SectionIdentifierType], afterSection toIdentifier: SectionIdentifierType) {
167+
public mutating func insertSections(_ identifiers: [SectionIdentifierType], afterSection toIdentifier: SectionIdentifierType) {
168168
structure.insert(sectionIDs: identifiers, after: toIdentifier)
169169
}
170170

171171
/// Deletes the specified sections.
172172
///
173173
/// - Parameters:
174174
/// - identifiers: The section identifiers to be deleted.
175-
public func deleteSections(_ identifiers: [SectionIdentifierType]) {
175+
public mutating func deleteSections(_ identifiers: [SectionIdentifierType]) {
176176
structure.remove(sectionIDs: identifiers)
177177
}
178178

@@ -181,7 +181,7 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
181181
/// - Parameters:
182182
/// - identifier: A section identifier to be moved.
183183
/// - toIdentifier: An identifier of section.
184-
public func moveSection(_ identifier: SectionIdentifierType, beforeSection toIdentifier: SectionIdentifierType) {
184+
public mutating func moveSection(_ identifier: SectionIdentifierType, beforeSection toIdentifier: SectionIdentifierType) {
185185
structure.move(sectionID: identifier, before: toIdentifier)
186186
}
187187

@@ -190,15 +190,15 @@ public class DiffableDataSourceSnapshot<SectionIdentifierType: Hashable, ItemIde
190190
/// - Parameters:
191191
/// - identifier: A section identifier to be moved.
192192
/// - toIdentifier: An identifier of section.
193-
public func moveSection(_ identifier: SectionIdentifierType, afterSection toIdentifier: SectionIdentifierType) {
193+
public mutating func moveSection(_ identifier: SectionIdentifierType, afterSection toIdentifier: SectionIdentifierType) {
194194
structure.move(sectionID: identifier, after: toIdentifier)
195195
}
196196

197197
/// Reloads the specified sections.
198198
///
199199
/// - Parameters:
200200
/// - identifiers: The section identifiers to be reloaded.
201-
public func reloadSections(_ identifiers: [SectionIdentifierType]) {
201+
public mutating func reloadSections(_ identifiers: [SectionIdentifierType]) {
202202
structure.update(sectionIDs: identifiers)
203203
}
204204
}

Sources/Internal/DiffableDataSourceCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ final class DiffableDataSourceCore<SectionIdentifierType: Hashable, ItemIdentifi
4848
}
4949

5050
func snapshot() -> DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType> {
51-
let snapshot = DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>()
51+
var snapshot = DiffableDataSourceSnapshot<SectionIdentifierType, ItemIdentifierType>()
5252
snapshot.structure.sections = currentSnapshot.structure.sections
5353
return snapshot
5454
}

Sources/Internal/SnapshotStructure.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Foundation
22
import DifferenceKit
33

4-
final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
4+
struct SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
55
struct Item: Differentiable, Equatable {
66
var differenceIdentifier: ItemID
77
var isReloaded: Bool
@@ -68,7 +68,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
6868
return itemPositionMap()[itemID]?.section.differenceIdentifier
6969
}
7070

71-
func append(itemIDs: [ItemID], to sectionID: SectionID? = nil, file: StaticString = #file, line: UInt = #line) {
71+
mutating func append(itemIDs: [ItemID], to sectionID: SectionID? = nil, file: StaticString = #file, line: UInt = #line) {
7272
let index: Array<Section>.Index
7373

7474
if let sectionID = sectionID {
@@ -90,7 +90,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
9090
sections[index].elements.append(contentsOf: items)
9191
}
9292

93-
func insert(itemIDs: [ItemID], before beforeItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
93+
mutating func insert(itemIDs: [ItemID], before beforeItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
9494
guard let itemPosition = itemPositionMap()[beforeItemID] else {
9595
specifiedItemIsNotFound(beforeItemID, file: file, line: line)
9696
}
@@ -99,7 +99,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
9999
sections[itemPosition.sectionIndex].elements.insert(contentsOf: items, at: itemPosition.itemRelativeIndex)
100100
}
101101

102-
func insert(itemIDs: [ItemID], after afterItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
102+
mutating func insert(itemIDs: [ItemID], after afterItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
103103
guard let itemPosition = itemPositionMap()[afterItemID] else {
104104
specifiedItemIsNotFound(afterItemID, file: file, line: line)
105105
}
@@ -109,7 +109,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
109109
sections[itemPosition.sectionIndex].elements.insert(contentsOf: items, at: itemIndex)
110110
}
111111

112-
func remove(itemIDs: [ItemID]) {
112+
mutating func remove(itemIDs: [ItemID]) {
113113
let itemPositionMap = self.itemPositionMap()
114114
var removeIndexSetMap = [Int: IndexSet]()
115115

@@ -128,13 +128,13 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
128128
}
129129
}
130130

131-
func removeAllItems() {
131+
mutating func removeAllItems() {
132132
for sectionIndex in sections.indices {
133133
sections[sectionIndex].elements.removeAll()
134134
}
135135
}
136136

137-
func move(itemID: ItemID, before beforeItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
137+
mutating func move(itemID: ItemID, before beforeItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
138138
guard let removed = remove(itemID: itemID) else {
139139
specifiedItemIsNotFound(itemID, file: file, line: line)
140140
}
@@ -146,7 +146,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
146146
sections[itemPosition.sectionIndex].elements.insert(removed, at: itemPosition.itemRelativeIndex)
147147
}
148148

149-
func move(itemID: ItemID, after afterItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
149+
mutating func move(itemID: ItemID, after afterItemID: ItemID, file: StaticString = #file, line: UInt = #line) {
150150
guard let removed = remove(itemID: itemID) else {
151151
specifiedItemIsNotFound(itemID, file: file, line: line)
152152
}
@@ -159,7 +159,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
159159
sections[itemPosition.sectionIndex].elements.insert(removed, at: itemIndex)
160160
}
161161

162-
func update(itemIDs: [ItemID], file: StaticString = #file, line: UInt = #line) {
162+
mutating func update(itemIDs: [ItemID], file: StaticString = #file, line: UInt = #line) {
163163
let itemPositionMap = self.itemPositionMap()
164164

165165
for itemID in itemIDs {
@@ -171,12 +171,12 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
171171
}
172172
}
173173

174-
func append(sectionIDs: [SectionID]) {
174+
mutating func append(sectionIDs: [SectionID]) {
175175
let newSections = sectionIDs.lazy.map(Section.init)
176176
sections.append(contentsOf: newSections)
177177
}
178178

179-
func insert(sectionIDs: [SectionID], before beforeSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
179+
mutating func insert(sectionIDs: [SectionID], before beforeSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
180180
guard let sectionIndex = sectionIndex(of: beforeSectionID) else {
181181
specifiedSectionIsNotFound(beforeSectionID, file: file, line: line)
182182
}
@@ -185,7 +185,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
185185
sections.insert(contentsOf: newSections, at: sectionIndex)
186186
}
187187

188-
func insert(sectionIDs: [SectionID], after afterSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
188+
mutating func insert(sectionIDs: [SectionID], after afterSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
189189
guard let beforeIndex = sectionIndex(of: afterSectionID) else {
190190
specifiedSectionIsNotFound(afterSectionID, file: file, line: line)
191191
}
@@ -195,13 +195,13 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
195195
sections.insert(contentsOf: newSections, at: sectionIndex)
196196
}
197197

198-
func remove(sectionIDs: [SectionID]) {
198+
mutating func remove(sectionIDs: [SectionID]) {
199199
for sectionID in sectionIDs {
200200
remove(sectionID: sectionID)
201201
}
202202
}
203203

204-
func move(sectionID: SectionID, before beforeSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
204+
mutating func move(sectionID: SectionID, before beforeSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
205205
guard let removed = remove(sectionID: sectionID) else {
206206
specifiedSectionIsNotFound(sectionID, file: file, line: line)
207207
}
@@ -213,7 +213,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
213213
sections.insert(removed, at: sectionIndex)
214214
}
215215

216-
func move(sectionID: SectionID, after afterSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
216+
mutating func move(sectionID: SectionID, after afterSectionID: SectionID, file: StaticString = #file, line: UInt = #line) {
217217
guard let removed = remove(sectionID: sectionID) else {
218218
specifiedSectionIsNotFound(sectionID, file: file, line: line)
219219
}
@@ -226,7 +226,7 @@ final class SnapshotStructure<SectionID: Hashable, ItemID: Hashable> {
226226
sections.insert(removed, at: sectionIndex)
227227
}
228228

229-
func update(sectionIDs: [SectionID]) {
229+
mutating func update(sectionIDs: [SectionID]) {
230230
for sectionID in sectionIDs {
231231
guard let sectionIndex = sectionIndex(of: sectionID) else {
232232
continue
@@ -250,7 +250,7 @@ private extension SnapshotStructure {
250250
}
251251

252252
@discardableResult
253-
func remove(itemID: ItemID) -> Item? {
253+
mutating func remove(itemID: ItemID) -> Item? {
254254
guard let itemPosition = itemPositionMap()[itemID] else {
255255
return nil
256256
}
@@ -259,7 +259,7 @@ private extension SnapshotStructure {
259259
}
260260

261261
@discardableResult
262-
func remove(sectionID: SectionID) -> Section? {
262+
mutating func remove(sectionID: SectionID) -> Section? {
263263
guard let sectionIndex = sectionIndex(of: sectionID) else {
264264
return nil
265265
}

0 commit comments

Comments
 (0)