Skip to content

Commit

Permalink
add insertItems:toIndexPaths method, add totalNumberOfItems computed …
Browse files Browse the repository at this point in the history
…property
  • Loading branch information
DenTelezhkin committed Apr 27, 2016
1 parent 491e4bb commit 1a7a2df
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
# Change Log
All notable changes to this project will be documented in this file.

## Master
## [2.5.0](https://github.com/DenHeadless/DTModelStorage/releases/tag/2.5.0)

## Breaking

* Update to Swift 2.2. This release is not backwards compatible with Swift 2.1.

## Added

* `insertItems(_:toIndexPaths:)` method, that mirrors UITableView `insertRowsAtIndexPaths(_:withRowAnimation:)` and UICollectionView `insertItemsAtIndexPaths(_:)` method
* `totalNumberOfItems` computed property in `MemoryStorage`, that allows getting current total number of items across all storage.

## Changed

* Require Only-App-Extension-Safe API is set to YES in framework targets.

## [2.4.4](https://github.com/DenHeadless/DTModelStorage/releases/tag/2.4.4)
Expand Down
2 changes: 1 addition & 1 deletion DTModelStorage.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'DTModelStorage'
s.version = '2.4.4'
s.version = '2.5.0'
s.license = 'MIT'
s.summary = 'Storage classes for datasource based controls.'
s.homepage = 'https://github.com/DenHeadless/DTModelStorage'
Expand Down
41 changes: 38 additions & 3 deletions DTModelStorage/Sources/Core/MemoryStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,26 @@ import Foundation
/// This struct contains error types that can be thrown for various MemoryStorage errors
public struct MemoryStorageErrors
{
/// Errors that can happen when inserting items into memory storage
/// Errors that can happen when inserting items into memory storage - `insertItem(_:toIndexPath:)` method
public enum Insertion: ErrorType
{
case IndexPathTooBig
}

/// Errors that can happen when replacing item in memory storage
/// Errors that can be thrown, when calling `insertItems(_:toIndexPaths:)` method
public enum BatchInsertion: ErrorType
{
/// Is thrown, if length of batch inserted array is different from length of array of index paths.
case ItemsCountMismatch
}

/// Errors that can happen when replacing item in memory storage - `replaceItem(_:replacingItem:)` method
public enum Replacement: ErrorType
{
case ItemNotFound
}

/// Errors that can happen when removing item from memory storage
/// Errors that can happen when removing item from memory storage - `removeItem(:_)` method
public enum Removal : ErrorType
{
case ItemNotFound
Expand All @@ -57,6 +64,13 @@ public class MemoryStorage: BaseStorage, StorageProtocol, SupplementaryStoragePr
/// sections of MemoryStorage
public var sections: [Section] = [SectionModel]()

/// Returns total number of items contained in all `MemoryStorage` sections
public var totalNumberOfItems : Int {
return sections.reduce(0) { sum, section in
return sum + section.numberOfItems
}
}

/// Retrieve item at index path from `MemoryStorage`
/// - Parameter path: NSIndexPath for item
/// - Returns: model at indexPath or nil, if item not found
Expand Down Expand Up @@ -221,6 +235,27 @@ public class MemoryStorage: BaseStorage, StorageProtocol, SupplementaryStoragePr
self.finishUpdate()
}

/// Insert item to indexPaths
/// - Parameter items: items to insert
/// - Parameter toIndexPaths: indexPaths to insert
/// - Throws: if items.count is different from indexPaths.count, will throw MemoryStorageErrors.BatchInsertion.ItemsCountMismatch
public func insertItems<T>(items: [T], toIndexPaths indexPaths: [NSIndexPath]) throws
{
if items.count != indexPaths.count {
throw MemoryStorageErrors.BatchInsertion.ItemsCountMismatch
}
performUpdates {
indexPaths.enumerate().forEach { itemIndex, indexPath in
let section = getValidSection(indexPath.section)
guard section.items.count >= indexPath.item else {
return
}
section.items.insert(items[itemIndex], atIndex: indexPath.item)
currentUpdate?.insertedRowIndexPaths.insert(indexPath)
}
}
}

/// Reload item
/// - Parameter item: item to reload.
public func reloadItem<T:Equatable>(item: T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,31 @@ class SectionSupplementariesTestCase : XCTestCase

expect(self.storage.sectionAtIndex(1)?.itemsOfType(Int)) == [7,8,9]
}

func testInsertItemsAtIndexPathsSuccessfullyInsertsItems() {
try! storage.insertItems([1,2,3], toIndexPaths: [indexPath(0, 0), indexPath(1, 0), indexPath(2, 0)])

expect(self.storage.itemsInSection(0)?.count) == 3
expect(self.storage.sectionAtIndex(0)?.itemsOfType(Int)) == [1,2,3]
}

func testWrongCountsRaisesException() {
do {
try storage.insertItems([1,2], toIndexPaths: [indexPath(0, 0)])
}
catch MemoryStorageErrors.BatchInsertion.ItemsCountMismatch {
return
}
catch {
XCTFail()
}
XCTFail()
}

func testInsertItemsAtIndexPathsDoesNotTryToInsertItemsPastItemsCount() {
try! storage.insertItems([1,2,3], toIndexPaths: [indexPath(0, 0), indexPath(1, 0),indexPath(3, 0)])

expect(self.storage.itemsInSection(0)?.count) == 2
expect(self.storage.sectionAtIndex(0)?.itemsOfType(Int)) == [1,2]
}
}

0 comments on commit 1a7a2df

Please sign in to comment.