This repository has been archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for storing a single item
- Loading branch information
1 parent
24912f8
commit c4dbca4
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
A `DataSource` that contains a single item. It is backed by a `SingleItemDataStore` | ||
*/ | ||
open class SingleItemDataSource<Element>: BasicDataSource<SingleItemDataStore<Element>> { | ||
|
||
/** | ||
Create a new `SingleItemDataSource` with the provided element | ||
- parameter element: The element to populate the backing store with | ||
*/ | ||
public init(element: Element) { | ||
let store = SingleItemDataStore(element: element) | ||
super.init(store: store) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import Foundation | ||
|
||
/** | ||
A `DataStore` that contains a single element of type `Element` | ||
*/ | ||
open class SingleItemDataStore<Element>: DataStore { | ||
|
||
public let element: Element | ||
|
||
public weak var delegate: DataStoreDelegate? | ||
|
||
public weak var dataSource: Composed.DataSource? | ||
|
||
public let numberOfSections: Int = 1 | ||
|
||
/** | ||
Create a new `SingleItemDataStore` with the provided element | ||
- parameter element: The element to populate the store with | ||
*/ | ||
public init(element: Element) { | ||
self.element = element | ||
} | ||
|
||
public func numberOfElements(in section: Int) -> Int { | ||
return 1 | ||
} | ||
|
||
public func element(at indexPath: IndexPath) -> Element { | ||
guard indexPath.item == 0 else { fatalError() } | ||
return element | ||
} | ||
|
||
public func indexPath(where predicate: @escaping (Element) -> Bool) -> IndexPath? { | ||
return predicate(element) ? IndexPath(item: 0, section: 0) : nil | ||
} | ||
|
||
} |