Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
Add support for storing a single item
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephDuffy committed Apr 12, 2019
1 parent 24912f8 commit c4dbca4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Composed.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
54BAB1642214D05C0064CE51 /* GlobalFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 54BAB1632214D05C0064CE51 /* GlobalFooterView.swift */; };
54BAB1662214D0630064CE51 /* GlobalFooterView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54BAB1652214D0630064CE51 /* GlobalFooterView.xib */; };
54D0A8A521F64AF600C65B72 /* HeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 54D0A8A421F64AF600C65B72 /* HeaderView.xib */; };
D937381F2260C554001F29EB /* SingleItemDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D937381E2260C554001F29EB /* SingleItemDataStore.swift */; };
D93738212260C641001F29EB /* SingleItemDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = D93738202260C641001F29EB /* SingleItemDataStore.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -158,6 +160,8 @@
54BAB1632214D05C0064CE51 /* GlobalFooterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalFooterView.swift; sourceTree = "<group>"; };
54BAB1652214D0630064CE51 /* GlobalFooterView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = GlobalFooterView.xib; sourceTree = "<group>"; };
54D0A8A421F64AF600C65B72 /* HeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HeaderView.xib; sourceTree = "<group>"; };
D937381E2260C554001F29EB /* SingleItemDataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleItemDataStore.swift; sourceTree = "<group>"; };
D93738202260C641001F29EB /* SingleItemDataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleItemDataStore.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -289,6 +293,7 @@
54A172FB225CC71A00A7D7FD /* EmbeddingDataSource.swift */,
54A172FC225CC71A00A7D7FD /* BasicDataSource.swift */,
54A172FD225CC71A00A7D7FD /* ComposedDataSource.swift */,
D93738202260C641001F29EB /* SingleItemDataStore.swift */,
);
path = DataSources;
sourceTree = "<group>";
Expand All @@ -300,6 +305,7 @@
54A17300225CC71A00A7D7FD /* ManagedDataStore.swift */,
54A17301225CC71A00A7D7FD /* DataStore.swift */,
54A17302225CC71A00A7D7FD /* PhotosDataStore.swift */,
D937381E2260C554001F29EB /* SingleItemDataStore.swift */,
);
path = DataStores;
sourceTree = "<group>";
Expand Down Expand Up @@ -514,8 +520,10 @@
54A17333225CC72900A7D7FD /* FlowLayout.swift in Sources */,
54A17332225CC72900A7D7FD /* FlowLayoutAttributes.swift in Sources */,
54A1733D225CC72900A7D7FD /* DataSourceInvalidationContext.swift in Sources */,
D937381F2260C554001F29EB /* SingleItemDataStore.swift in Sources */,
54A1731B225CC72900A7D7FD /* SectionedDataSource.swift in Sources */,
54A1733A225CC72900A7D7FD /* EmbeddedDataSourceCell.swift in Sources */,
D93738212260C641001F29EB /* SingleItemDataStore.swift in Sources */,
54A1733C225CC72900A7D7FD /* CollectionViewWrapper.swift in Sources */,
54A17329225CC72900A7D7FD /* DataSource.swift in Sources */,
54A17324225CC72900A7D7FD /* AggregateDataSource.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Composed/Core/DataSources/SingleItemDataStore.swift
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)
}

}
38 changes: 38 additions & 0 deletions Composed/Core/DataStores/SingleItemDataStore.swift
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
}

}

0 comments on commit c4dbca4

Please sign in to comment.