Skip to content

Commit 1daed1a

Browse files
committed
Makes configureSupplementaryView optional for reload data source. #186
1 parent 1061a38 commit 1daed1a

File tree

4 files changed

+131
-5
lines changed

4 files changed

+131
-5
lines changed

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
C8BBFBE01F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8BBFBD51F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift */; };
7575
C8BBFBE11F3B8F8D00A225F7 /* UI+SectionedViewType.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8BBFBD61F3B8F8D00A225F7 /* UI+SectionedViewType.swift */; };
7676
C8E3905A1F36625B004FC993 /* RxDataSources.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8984C5A1C36AF35001E4272 /* RxDataSources.framework */; };
77+
C8E643E01FAE45710080BD2C /* RxCollectionViewSectionedDataSource+Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8E643DF1FAE45710080BD2C /* RxCollectionViewSectionedDataSource+Test.swift */; };
7778
/* End PBXBuildFile section */
7879

7980
/* Begin PBXContainerItemProxy section */
@@ -394,6 +395,7 @@
394395
C8BBFBD41F3B8F8D00A225F7 /* RxTableViewSectionedReloadDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RxTableViewSectionedReloadDataSource.swift; sourceTree = "<group>"; };
395396
C8BBFBD51F3B8F8D00A225F7 /* TableViewSectionedDataSource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TableViewSectionedDataSource.swift; sourceTree = "<group>"; };
396397
C8BBFBD61F3B8F8D00A225F7 /* UI+SectionedViewType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UI+SectionedViewType.swift"; sourceTree = "<group>"; };
398+
C8E643DF1FAE45710080BD2C /* RxCollectionViewSectionedDataSource+Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RxCollectionViewSectionedDataSource+Test.swift"; sourceTree = "<group>"; };
397399
E39CF67E2A16306B47BCE935 /* Pods_RxDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RxDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; };
398400
E3CB312567712E0443B47BE1 /* Pods_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
399401
/* End PBXFileReference section */
@@ -464,6 +466,7 @@
464466
03EEEE851F5B7D5C006068BC /* Randomizer.swift */,
465467
03EEEE861F5B7D5C006068BC /* s.swift */,
466468
03EEEE871F5B7D5C006068BC /* XCTest+Extensions.swift */,
469+
C8E643DF1FAE45710080BD2C /* RxCollectionViewSectionedDataSource+Test.swift */,
467470
);
468471
path = RxDataSourcesTests;
469472
sourceTree = "<group>";
@@ -1095,6 +1098,7 @@
10951098
03EEEE911F5B7D71006068BC /* AlgorithmTests.swift in Sources */,
10961099
03EEEE921F5B7D71006068BC /* Array+Extensions.swift in Sources */,
10971100
03EEEE931F5B7D71006068BC /* ChangeSet+TestExtensions.swift in Sources */,
1101+
C8E643E01FAE45710080BD2C /* RxCollectionViewSectionedDataSource+Test.swift in Sources */,
10981102
03EEEE941F5B7D71006068BC /* i.swift in Sources */,
10991103
03EEEE951F5B7D71006068BC /* NumberSection.swift in Sources */,
11001104
03EEEE961F5B7D71006068BC /* Randomizer.swift in Sources */,

Sources/RxDataSources/CollectionViewSectionedDataSource.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ open class CollectionViewSectionedDataSource<S: SectionModelType>
2828

2929
public init(
3030
configureCell: @escaping ConfigureCell,
31-
configureSupplementaryView: @escaping ConfigureSupplementaryView,
31+
configureSupplementaryView: ConfigureSupplementaryView? = nil,
3232
moveItem: @escaping MoveItem = { _, _, _ in () },
3333
canMoveItemAtIndexPath: @escaping CanMoveItemAtIndexPath = { _, _ in false }
3434
) {
@@ -96,7 +96,7 @@ open class CollectionViewSectionedDataSource<S: SectionModelType>
9696
}
9797
}
9898

99-
open var configureSupplementaryView: ConfigureSupplementaryView {
99+
open var configureSupplementaryView: ConfigureSupplementaryView? {
100100
didSet {
101101
#if DEBUG
102102
ensureNotMutatedAfterBinding()
@@ -136,7 +136,7 @@ open class CollectionViewSectionedDataSource<S: SectionModelType>
136136
}
137137

138138
open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
139-
return configureSupplementaryView(self, collectionView, kind, indexPath)
139+
return configureSupplementaryView!(self, collectionView, kind, indexPath)
140140
}
141141

142142
open func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
@@ -151,6 +151,14 @@ open class CollectionViewSectionedDataSource<S: SectionModelType>
151151
self._sectionModels.moveFromSourceIndexPath(sourceIndexPath, destinationIndexPath: destinationIndexPath)
152152
self.moveItem(self, sourceIndexPath, destinationIndexPath)
153153
}
154-
154+
155+
override open func responds(to aSelector: Selector!) -> Bool {
156+
if aSelector == #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:)) {
157+
return configureSupplementaryView != nil
158+
}
159+
else {
160+
return super.responds(to: aSelector)
161+
}
162+
}
155163
}
156164
#endif

Sources/RxDataSources/Deprecated.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#if os(iOS) || os(tvOS)
1010
extension CollectionViewSectionedDataSource {
1111
@available(*, deprecated, renamed: "configureSupplementaryView")
12-
public var supplementaryViewFactory: ConfigureSupplementaryView {
12+
public var supplementaryViewFactory: ConfigureSupplementaryView? {
1313
get {
1414
return self.configureSupplementaryView
1515
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
//
2+
// RxCollectionViewSectionedDataSource+Test.swift
3+
// Tests
4+
//
5+
// Created by Krunoslav Zaher on 11/4/17.
6+
// Copyright © 2017 kzaher. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import RxDataSources
11+
import XCTest
12+
import UIKit
13+
14+
class RxCollectionViewSectionedDataSourceTest: XCTestCase {
15+
}
16+
17+
// configureSupplementaryView not passed through init
18+
extension RxCollectionViewSectionedDataSourceTest {
19+
func testCollectionViewSectionedReloadDataSource_optionalConfigureSupplementaryView() {
20+
let dataSource = RxCollectionViewSectionedReloadDataSource<AnimatableSectionModel<String, String>>(configureCell: { _, _, _, _ in UICollectionViewCell() })
21+
let layout = UICollectionViewFlowLayout()
22+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
23+
24+
XCTAssertFalse(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
25+
26+
let sentinel = UICollectionReusableView()
27+
dataSource.configureSupplementaryView = { _, _, _, _ in return sentinel }
28+
29+
let returnValue = dataSource.collectionView(
30+
collectionView,
31+
viewForSupplementaryElementOfKind: UICollectionElementKindSectionHeader,
32+
at: IndexPath(item: 0, section: 0)
33+
)
34+
XCTAssertEqual(returnValue, sentinel)
35+
XCTAssertTrue(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
36+
}
37+
38+
func testCollectionViewSectionedDataSource_optionalConfigureSupplementaryView() {
39+
let dataSource = CollectionViewSectionedDataSource<AnimatableSectionModel<String, String>>(configureCell: { _, _, _, _ in UICollectionViewCell() })
40+
let layout = UICollectionViewFlowLayout()
41+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
42+
43+
XCTAssertFalse(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
44+
45+
let sentinel = UICollectionReusableView()
46+
dataSource.configureSupplementaryView = { _, _, _, _ in return sentinel }
47+
48+
let returnValue = dataSource.collectionView(
49+
collectionView,
50+
viewForSupplementaryElementOfKind: UICollectionElementKindSectionHeader,
51+
at: IndexPath(item: 0, section: 0)
52+
)
53+
XCTAssertEqual(returnValue, sentinel)
54+
XCTAssertTrue(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
55+
}
56+
}
57+
58+
// configureSupplementaryView passed through init
59+
extension RxCollectionViewSectionedDataSourceTest {
60+
func testCollectionViewSectionedAnimatedDataSource_optionalConfigureSupplementaryView_initializer() {
61+
let sentinel = UICollectionReusableView()
62+
let dataSource = RxCollectionViewSectionedAnimatedDataSource<AnimatableSectionModel<String, String>>(
63+
configureCell: { _, _, _, _ in UICollectionViewCell() },
64+
configureSupplementaryView: { _, _, _, _ in return sentinel }
65+
)
66+
let layout = UICollectionViewFlowLayout()
67+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
68+
69+
let returnValue = dataSource.collectionView(
70+
collectionView,
71+
viewForSupplementaryElementOfKind: UICollectionElementKindSectionHeader,
72+
at: IndexPath(item: 0, section: 0)
73+
)
74+
XCTAssertEqual(returnValue, sentinel)
75+
XCTAssertTrue(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
76+
}
77+
78+
func testCollectionViewSectionedReloadDataSource_optionalConfigureSupplementaryView_initializer() {
79+
let sentinel = UICollectionReusableView()
80+
let dataSource = RxCollectionViewSectionedReloadDataSource<AnimatableSectionModel<String, String>>(
81+
configureCell: { _, _, _, _ in UICollectionViewCell() },
82+
configureSupplementaryView: { _, _, _, _ in return sentinel }
83+
)
84+
let layout = UICollectionViewFlowLayout()
85+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
86+
87+
let returnValue = dataSource.collectionView(
88+
collectionView,
89+
viewForSupplementaryElementOfKind: UICollectionElementKindSectionHeader,
90+
at: IndexPath(item: 0, section: 0)
91+
)
92+
XCTAssertEqual(returnValue, sentinel)
93+
XCTAssertTrue(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
94+
}
95+
96+
func testCollectionViewSectionedDataSource_optionalConfigureSupplementaryView_initializer() {
97+
let sentinel = UICollectionReusableView()
98+
let dataSource = CollectionViewSectionedDataSource<AnimatableSectionModel<String, String>>(
99+
configureCell: { _, _, _, _ in UICollectionViewCell() },
100+
configureSupplementaryView: { _, _, _, _ in return sentinel }
101+
)
102+
let layout = UICollectionViewFlowLayout()
103+
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
104+
105+
let returnValue = dataSource.collectionView(
106+
collectionView,
107+
viewForSupplementaryElementOfKind: UICollectionElementKindSectionHeader,
108+
at: IndexPath(item: 0, section: 0)
109+
)
110+
XCTAssertEqual(returnValue, sentinel)
111+
XCTAssertTrue(dataSource.responds(to: #selector(UICollectionViewDataSource.collectionView(_:viewForSupplementaryElementOfKind:at:))))
112+
}
113+
}
114+

0 commit comments

Comments
 (0)