Skip to content

Commit 3928d62

Browse files
committed
Fixes problem with collection view item moving.
1 parent 9a67932 commit 3928d62

File tree

4 files changed

+83
-61
lines changed

4 files changed

+83
-61
lines changed

RxDataSources.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
C8BB76781C4C3E53002B21C8 /* RxCocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9FF3014C1C4AA6DA007376BD /* RxCocoa.framework */; };
106106
C8BB767B1C4C3E53002B21C8 /* RxDataSources.h in Headers */ = {isa = PBXBuildFile; fileRef = C8984C5D1C36AF35001E4272 /* RxDataSources.h */; settings = {ATTRIBUTES = (Public, ); }; };
107107
C8BB76831C4C3EC5002B21C8 /* RxDataSources.h in Headers */ = {isa = PBXBuildFile; fileRef = C8984C5D1C36AF35001E4272 /* RxDataSources.h */; settings = {ATTRIBUTES = (Public, ); }; };
108+
C8CC634D1CCF8492006AE8CE /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8CC634C1CCF8492006AE8CE /* Array+Extensions.swift */; };
109+
C8CC634E1CCF8492006AE8CE /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8CC634C1CCF8492006AE8CE /* Array+Extensions.swift */; };
110+
C8CC634F1CCF8492006AE8CE /* Array+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C8CC634C1CCF8492006AE8CE /* Array+Extensions.swift */; };
108111
C8E946A01CC6DEC800C5559C /* EditingExampleViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84FC6BED1CA4830A00D3C605 /* EditingExampleViewController.swift */; };
109112
C8EC65C61C4C441B00FC9207 /* RxDataSources.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C8BB76811C4C3E53002B21C8 /* RxDataSources.framework */; };
110113
C8EC65C71C4C441B00FC9207 /* RxDataSources.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = C8BB76811C4C3E53002B21C8 /* RxDataSources.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
@@ -211,6 +214,7 @@
211214
C8BB763E1C4C3D45002B21C8 /* NumberSection.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NumberSection.swift; sourceTree = "<group>"; };
212215
C8BB765B1C4C3DD4002B21C8 /* Example-Carthage.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Example-Carthage.app"; sourceTree = BUILT_PRODUCTS_DIR; };
213216
C8BB76811C4C3E53002B21C8 /* RxDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = RxDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; };
217+
C8CC634C1CCF8492006AE8CE /* Array+Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Array+Extensions.swift"; sourceTree = "<group>"; };
214218
E39CF67E2A16306B47BCE935 /* Pods_RxDataSources.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_RxDataSources.framework; sourceTree = BUILT_PRODUCTS_DIR; };
215219
E4DE8D6B18C8BF4D4C37DB03 /* Pods-Example.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example.release.xcconfig"; path = "Pods/Target Support Files/Pods-Example/Pods-Example.release.xcconfig"; sourceTree = "<group>"; };
216220
E8A1509A86ADB3FDAEE200EF /* Pods-RxDataSources.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RxDataSources.debug.xcconfig"; path = "Pods/Target Support Files/Pods-RxDataSources/Pods-RxDataSources.debug.xcconfig"; sourceTree = "<group>"; };
@@ -350,6 +354,7 @@
350354
C85EE54E1C36F1FC0090614D /* SectionModelType.swift */,
351355
C85EE54F1C36F1FC0090614D /* TableViewSectionedDataSource.swift */,
352356
C8BB762B1C4C3CDF002B21C8 /* UI+SectionedViewType.swift */,
357+
C8CC634C1CCF8492006AE8CE /* Array+Extensions.swift */,
353358
);
354359
path = DataSources;
355360
sourceTree = "<group>";
@@ -758,6 +763,7 @@
758763
9FCDA1761C3AF4A2000F5F94 /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
759764
C8BB76391C4C3CDF002B21C8 /* ItemPath.swift in Sources */,
760765
C8BB76311C4C3CDF002B21C8 /* AnimatableSectionModelType+ItemPath.swift in Sources */,
766+
C8CC634F1CCF8492006AE8CE /* Array+Extensions.swift in Sources */,
761767
9FCDA1751C3AF4A2000F5F94 /* RxCollectionViewSectionedReloadDataSource.swift in Sources */,
762768
C8BB76351C4C3CDF002B21C8 /* IdentifiableType.swift in Sources */,
763769
C8BB76331C4C3CDF002B21C8 /* DataSources.swift in Sources */,
@@ -786,6 +792,7 @@
786792
C85EE5621C36F1FC0090614D /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
787793
C8BB76381C4C3CDF002B21C8 /* ItemPath.swift in Sources */,
788794
C8BB76301C4C3CDF002B21C8 /* AnimatableSectionModelType+ItemPath.swift in Sources */,
795+
C8CC634E1CCF8492006AE8CE /* Array+Extensions.swift in Sources */,
789796
C85EE5611C36F1FC0090614D /* RxCollectionViewSectionedReloadDataSource.swift in Sources */,
790797
C8BB76341C4C3CDF002B21C8 /* IdentifiableType.swift in Sources */,
791798
C8BB76321C4C3CDF002B21C8 /* DataSources.swift in Sources */,
@@ -848,6 +855,7 @@
848855
C8BB76601C4C3E53002B21C8 /* RxTableViewSectionedAnimatedDataSource.swift in Sources */,
849856
C8BB76611C4C3E53002B21C8 /* ItemPath.swift in Sources */,
850857
C8BB76621C4C3E53002B21C8 /* AnimatableSectionModelType+ItemPath.swift in Sources */,
858+
C8CC634D1CCF8492006AE8CE /* Array+Extensions.swift in Sources */,
851859
C8BB76631C4C3E53002B21C8 /* RxCollectionViewSectionedReloadDataSource.swift in Sources */,
852860
C8BB76641C4C3E53002B21C8 /* IdentifiableType.swift in Sources */,
853861
C8BB76651C4C3E53002B21C8 /* DataSources.swift in Sources */,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// Array+Extensions.swift
3+
// RxDataSources
4+
//
5+
// Created by Krunoslav Zaher on 4/26/16.
6+
// Copyright © 2016 kzaher. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
import Foundation
12+
13+
extension Array where Element: SectionModelType {
14+
mutating func moveFromSourceIndexPath(sourceIndexPath: NSIndexPath, destinationIndexPath: NSIndexPath) {
15+
let sourceSection = self[sourceIndexPath.section]
16+
var sourceItems = sourceSection.items
17+
18+
let sourceItem = sourceItems.removeAtIndex(sourceIndexPath.item)
19+
20+
let sourceSectionNew = Element(original: sourceSection, items: sourceItems)
21+
self[sourceIndexPath.section] = sourceSectionNew
22+
23+
let destinationSection = self[destinationIndexPath.section]
24+
var destinationItems = destinationSection.items
25+
destinationItems.insert(sourceItem, atIndex: destinationIndexPath.item)
26+
27+
self[destinationIndexPath.section] = Element(original: destinationSection, items: destinationItems)
28+
}
29+
}

Sources/DataSources/CollectionViewSectionedDataSource.swift

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,43 +24,43 @@ public class _CollectionViewSectionedDataSource
2424
return _numberOfSectionsInCollectionView(collectionView)
2525
}
2626

27-
func _collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
27+
func _rx_collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
2828
return 0
2929
}
3030

3131
public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
32-
return _collectionView(collectionView, numberOfItemsInSection: section)
32+
return _rx_collectionView(collectionView, numberOfItemsInSection: section)
3333
}
3434

35-
func _collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
35+
func _rx_collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
3636
return (nil as UICollectionViewCell?)!
3737
}
3838

3939
public func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
40-
return _collectionView(collectionView, cellForItemAtIndexPath: indexPath)
40+
return _rx_collectionView(collectionView, cellForItemAtIndexPath: indexPath)
4141
}
4242

43-
func _collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
43+
func _rx_collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
4444
return (nil as UICollectionReusableView?)!
4545
}
4646

4747
public func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
48-
return _collectionView(collectionView, viewForSupplementaryElementOfKind: kind, atIndexPath: indexPath)
48+
return _rx_collectionView(collectionView, viewForSupplementaryElementOfKind: kind, atIndexPath: indexPath)
4949
}
5050

51-
func _collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
52-
return false
51+
func _rx_collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
52+
return true
5353
}
5454

5555
public func collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
56-
return _collectionView(collectionView, canMoveItemAtIndexPath: indexPath)
56+
return _rx_collectionView(collectionView, canMoveItemAtIndexPath: indexPath)
5757
}
5858

59-
func _collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
59+
func _rx_collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
6060

6161
}
6262
public func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
63-
_collectionView(collectionView, moveItemAtIndexPath: sourceIndexPath, toIndexPath: destinationIndexPath)
63+
_rx_collectionView(collectionView, moveItemAtIndexPath: sourceIndexPath, toIndexPath: destinationIndexPath)
6464
}
6565

6666
}
@@ -134,33 +134,30 @@ public class CollectionViewSectionedDataSource<S: SectionModelType>
134134
return _sectionModels.count
135135
}
136136

137-
override func _collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
137+
override func _rx_collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
138138
return _sectionModels[section].items.count
139139
}
140140

141-
override func _collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
141+
override func _rx_collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
142142
precondition(indexPath.item < _sectionModels[indexPath.section].items.count)
143143

144144
return cellFactory(self, collectionView, indexPath, itemAtIndexPath(indexPath))
145145
}
146146

147-
override func _collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
147+
override func _rx_collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
148148
return supplementaryViewFactory(self, collectionView, kind, indexPath)
149149
}
150150

151-
override func _collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
151+
override func _rx_collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
152152
guard let canMoveItem = canMoveItemAtIndexPath?(self, indexPath: indexPath) else {
153-
return super._collectionView(collectionView, canMoveItemAtIndexPath: indexPath)
153+
return super._rx_collectionView(collectionView, canMoveItemAtIndexPath: indexPath)
154154
}
155155

156156
return canMoveItem
157157
}
158158

159-
override func _collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
160-
guard let _ = moveItem?(self, sourceIndexPath:sourceIndexPath, destinationIndexPath: destinationIndexPath) else {
161-
super._collectionView(collectionView, moveItemAtIndexPath: sourceIndexPath, toIndexPath: destinationIndexPath)
162-
return
163-
}
159+
override func _rx_collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
160+
self._sectionModels.moveFromSourceIndexPath(sourceIndexPath, destinationIndexPath: destinationIndexPath)
164161
}
165162

166163
}

0 commit comments

Comments
 (0)