This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathDataSource.swift
More file actions
166 lines (144 loc) · 4.89 KB
/
DataSource.swift
File metadata and controls
166 lines (144 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Created by Jesse Squires
// https://www.jessesquires.com
//
//
// Documentation
// https://jessesquires.github.io/JSQDataSourcesKit
//
//
// GitHub
// https://github.com/jessesquires/JSQDataSourcesKit
//
//
// License
// Copyright © 2015-present Jesse Squires
// Released under an MIT license: https://opensource.org/licenses/MIT
//
import Foundation
/// A `DataSource` is a concrete `DataSourceProtocol`.
/// It represents a collection of section-based data.
public struct DataSource<Item> {
// MARK: Properties
/// The sections in the data source.
public var sections: [Section<Item>]
// MARK: Initialization
/// Constructs a new `DataSource`.
///
/// - Parameter sections: The sections for the data source.
public init(_ sections: [Section<Item>]) {
self.sections = sections
}
/// Constructs a new `DataSource`.
///
/// - Parameter sections: The sections for the data source.
public init(sections: Section<Item>...) {
self.sections = sections
}
// MARK: Methods
/**
Inserts the item at the specified index path.
- parameter item: The item to be inserted.
- parameter indexPath: The index path specifying the location for the item.
*/
public mutating func insert(item: Item, at indexPath: IndexPath) {
insert(item: item, atRow: indexPath.row, inSection: indexPath.section)
}
/**
Inserts the item at the specified row and section.
- parameter item: The item to be inserted.
- parameter row: The row location for the item.
- parameter section: The section location for the item.
*/
public mutating func insert(item: Item, atRow row: Int, inSection section: Int) {
guard section < numberOfSections() else { return }
guard row <= numberOfItems(inSection: section) else { return }
sections[section].items.insert(item, at: row)
}
/**
Appends the item at the specified section.
- parameter item: The item to be appended.
- parameter section: The section location for the item.
*/
public mutating func append(_ item: Item, inSection section: Int) {
guard let items = items(inSection: section) else { return }
insert(item: item, atRow: items.endIndex, inSection: section)
}
/**
Removes the item at the specified row and section.
- parameter row: The row location of the item.
- parameter section: The section location of the item.
- returns: The item removed, or `nil` if it does not exist.
*/
@discardableResult
public mutating func remove(atRow row: Int, inSection section: Int) -> Item? {
guard item(atRow: row, inSection: section) != nil else { return nil }
return sections[section].items.remove(at: row)
}
/**
Removes the item at the specified index path.
- parameter indexPath: The index path specifying the location of the item.
- returns: The item at `indexPath`, or `nil` if it does not exist.
*/
@discardableResult
public mutating func remove(at indexPath: IndexPath) -> Item? {
remove(atRow: indexPath.row, inSection: indexPath.section)
}
// MARK: Subscripts
/**
- parameter index: The index of a section.
- returns: The section at `index`.
*/
public subscript (index: Int) -> Section<Item> {
get {
sections[index]
}
set {
sections[index] = newValue
}
}
/**
- parameter indexPath: The index path of an item.
- returns: The item at `indexPath`.
*/
public subscript (indexPath: IndexPath) -> Item {
get {
sections[indexPath.section].items[indexPath.row]
}
set {
sections[indexPath.section].items[indexPath.row] = newValue
}
}
}
extension DataSource: DataSourceProtocol {
/// :nodoc:
public func numberOfSections() -> Int {
sections.count
}
/// :nodoc:
public func numberOfItems(inSection section: Int) -> Int {
guard section < sections.count else { return 0 }
return sections[section].items.count
}
/// :nodoc:
public func items(inSection section: Int) -> [Item]? {
guard section < sections.count else { return nil }
return sections[section].items
}
/// :nodoc:
public func item(atRow row: Int, inSection section: Int) -> Item? {
guard let items = items(inSection: section) else { return nil }
guard row < items.count else { return nil }
return items[row]
}
/// :nodoc:
public func headerTitle(inSection section: Int) -> String? {
guard section < sections.count else { return nil }
return sections[section].headerTitle
}
/// :nodoc:
public func footerTitle(inSection section: Int) -> String? {
guard section < sections.count else { return nil }
return sections[section].footerTitle
}
}