Skip to content

Commit

Permalink
treat collectionView property as optional instead of using implicitly…
Browse files Browse the repository at this point in the history
… unwrapped optional
  • Loading branch information
DenTelezhkin committed Oct 12, 2015
1 parent 684bb07 commit 5169d63
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 19 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Change Log
All notable changes to this project will be documented in this file.

## [4.2.1](https://github.com/DenHeadless/DTCollectionViewManager/releases/tag/4.2.1)

### Updated

* Improved stability by treating UICollectionView as optional

## [4.2.0](https://github.com/DenHeadless/DTCollectionViewManager/releases/tag/4.2.0)

Dependency changelog -> [DTModelStorage 2.1 and higher](https://github.com/DenHeadless/DTModelStorage/releases)
Expand Down
2 changes: 1 addition & 1 deletion DTCollectionViewManager.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DTCollectionViewManager'
s.version = '4.2.0'
s.version = '4.2.1'
s.license = 'MIT'
s.summary = 'Protocol-oriented UICollectionView management, powered by generics and associated types.'
s.homepage = 'https://github.com/DenHeadless/DTCollectionViewManager'
Expand Down
42 changes: 24 additions & 18 deletions DTCollectionViewManager/DTCollectionViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extension DTCollectionViewManageable
/// - SeeAlso: `startManagingWithDelegate:`
public class DTCollectionViewManager : NSObject {

private var collectionView : UICollectionView! {
private var collectionView : UICollectionView? {
return self.delegate?.collectionView
}

Expand All @@ -71,9 +71,13 @@ public class DTCollectionViewManager : NSObject {
/// Factory for creating cells and reusable views for UICollectionView
private lazy var viewFactory: CollectionViewFactory = {
precondition(self.collectionView != nil, "Please call manager.startManagingWithDelegate(self) before calling any other DTCollectionViewManager methods")
return CollectionViewFactory(collectionView: self.collectionView)
return CollectionViewFactory(collectionView: self.collectionView!)
}()

deinit {
delegate = nil
}

/// Bundle to search your xib's in. This can sometimes be useful for unit-testing. Defaults to NSBundle.mainBundle()
public var viewBundle = NSBundle.mainBundle()
{
Expand Down Expand Up @@ -145,7 +149,7 @@ public class DTCollectionViewManager : NSObject {
{
guard cell != nil else { return nil }

if let indexPath = self.collectionView.indexPathForCell(cell!) {
if let indexPath = collectionView?.indexPathForCell(cell!) {
return storage.itemAtIndexPath(indexPath) as? T.ModelType
}
return nil
Expand Down Expand Up @@ -327,7 +331,7 @@ public extension DTCollectionViewManager
reaction.viewType = _reflect(T)
reaction.reactionBlock = { [weak self, unowned reaction] in
if let indexPath = reaction.reactionData as? NSIndexPath,
let cell = self?.collectionView.cellForItemAtIndexPath(indexPath) as? T,
let cell = self?.collectionView?.cellForItemAtIndexPath(indexPath) as? T,
let model = self?.storage.itemAtIndexPath(indexPath) as? T.ModelType
{
closure(cell, model, indexPath)
Expand All @@ -346,7 +350,7 @@ public extension DTCollectionViewManager
reaction.viewType = _reflect(T)
reaction.reactionBlock = { [weak self, unowned reaction] in
if let indexPath = reaction.reactionData as? NSIndexPath,
let cell = self?.collectionView.cellForItemAtIndexPath(indexPath) as? T,
let cell = self?.collectionView?.cellForItemAtIndexPath(indexPath) as? T,
let model = self?.storage.itemAtIndexPath(indexPath) as? T.ModelType,
let delegate = self?.delegate as? U
{
Expand Down Expand Up @@ -574,31 +578,31 @@ extension DTCollectionViewManager : StorageUpdating
public func storageDidPerformUpdate(update: StorageUpdate) {
self.controllerWillUpdateContent()

collectionView.performBatchUpdates({
if update.insertedRowIndexPaths.count > 0 { self.collectionView.insertItemsAtIndexPaths(Array(update.insertedRowIndexPaths)) }
if update.deletedRowIndexPaths.count > 0 { self.collectionView.deleteItemsAtIndexPaths(Array(update.deletedRowIndexPaths)) }
if update.updatedRowIndexPaths.count > 0 { self.collectionView.reloadItemsAtIndexPaths(Array(update.updatedRowIndexPaths)) }
collectionView?.performBatchUpdates({
if update.insertedRowIndexPaths.count > 0 { self.collectionView?.insertItemsAtIndexPaths(Array(update.insertedRowIndexPaths)) }
if update.deletedRowIndexPaths.count > 0 { self.collectionView?.deleteItemsAtIndexPaths(Array(update.deletedRowIndexPaths)) }
if update.updatedRowIndexPaths.count > 0 { self.collectionView?.reloadItemsAtIndexPaths(Array(update.updatedRowIndexPaths)) }
if update.movedRowIndexPaths.count > 0 {
for moveAction in update.movedRowIndexPaths {
if let from = moveAction.first, let to = moveAction.last {
self.collectionView.moveItemAtIndexPath(from, toIndexPath: to)
self.collectionView?.moveItemAtIndexPath(from, toIndexPath: to)
}
}
}

if update.insertedSectionIndexes.count > 0 { self.collectionView.insertSections(update.insertedSectionIndexes.makeNSIndexSet()) }
if update.deletedSectionIndexes.count > 0 { self.collectionView.deleteSections(update.deletedSectionIndexes.makeNSIndexSet()) }
if update.updatedSectionIndexes.count > 0 { self.collectionView.reloadSections(update.updatedSectionIndexes.makeNSIndexSet())}
if update.insertedSectionIndexes.count > 0 { self.collectionView?.insertSections(update.insertedSectionIndexes.makeNSIndexSet()) }
if update.deletedSectionIndexes.count > 0 { self.collectionView?.deleteSections(update.deletedSectionIndexes.makeNSIndexSet()) }
if update.updatedSectionIndexes.count > 0 { self.collectionView?.reloadSections(update.updatedSectionIndexes.makeNSIndexSet())}
if update.movedSectionIndexes.count > 0 {
for moveAction in update.movedSectionIndexes {
if let from = moveAction.first, let to = moveAction.last {
self.collectionView.moveSection(from, toSection: to)
self.collectionView?.moveSection(from, toSection: to)
}
}
}
}) { (finished) in
if update.insertedSectionIndexes.count + update.deletedSectionIndexes.count + update.updatedSectionIndexes.count > 0 {
self.collectionView.reloadData()
self.collectionView?.reloadData()
}
}
self.controllerDidUpdateContent()
Expand All @@ -607,7 +611,7 @@ extension DTCollectionViewManager : StorageUpdating
/// Call this method, if you want UICollectionView to be reloaded, and beforeContentUpdate: and afterContentUpdate: closures to be called.
public func storageNeedsReloading() {
self.controllerWillUpdateContent()
collectionView.reloadData()
collectionView?.reloadData()
self.controllerDidUpdateContent()
}

Expand All @@ -626,7 +630,9 @@ extension DTCollectionViewManager : StorageUpdating
extension DTCollectionViewManager : CollectionViewStorageUpdating
{
public func performAnimatedUpdate(block: (UICollectionView) -> Void) {
block(collectionView)
guard collectionView != nil else { return }

block(collectionView!)
}
}

Expand All @@ -642,7 +648,7 @@ extension DTCollectionViewManager
{
guard cell != nil else { return nil }

if let indexPath = self.collectionView.indexPathForCell(cell!) {
if let indexPath = collectionView?.indexPathForCell(cell!) {
return storage.itemAtIndexPath(indexPath) as? T.ModelType
}
return nil
Expand Down

0 comments on commit 5169d63

Please sign in to comment.