Skip to content
This repository was archived by the owner on May 14, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added missing update methods (untested) and also some light renaming/…
…refactoring
  • Loading branch information
shaps80 committed Jul 4, 2019
commit de5dcf9ea184d2ae2784da8042c71529d80c8071
8 changes: 4 additions & 4 deletions Composed.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
D941140B2297F30D00077F90 /* SectionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D941140A2297F30D00077F90 /* SectionProvider.swift */; };
D941140D2297F40500077F90 /* ComposedSectionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D941140C2297F40500077F90 /* ComposedSectionProvider.swift */; };
D941140F2297F4FA00077F90 /* ArraySection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D941140E2297F4FA00077F90 /* ArraySection.swift */; };
D94114112297F86100077F90 /* SectionProviderMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = D94114102297F86100077F90 /* SectionProviderMapper.swift */; };
D94114112297F86100077F90 /* SectionProviderMapping.swift in Sources */ = {isa = PBXBuildFile; fileRef = D94114102297F86100077F90 /* SectionProviderMapping.swift */; };
D9D7DCBD22981094001DA4D9 /* SectionProviderMapper+Spec.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D7DCBC22981094001DA4D9 /* SectionProviderMapper+Spec.swift */; };
D9D7DCBF22985C44001DA4D9 /* CollectionViewSectionProviderCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D7DCBE22985C44001DA4D9 /* CollectionViewSectionProviderCoordinator.swift */; };
D9D7DCC122985F04001DA4D9 /* SectionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D7DCC022985F04001DA4D9 /* SectionsViewController.swift */; };
Expand Down Expand Up @@ -221,7 +221,7 @@
D941140A2297F30D00077F90 /* SectionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionProvider.swift; sourceTree = "<group>"; };
D941140C2297F40500077F90 /* ComposedSectionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposedSectionProvider.swift; sourceTree = "<group>"; };
D941140E2297F4FA00077F90 /* ArraySection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArraySection.swift; sourceTree = "<group>"; };
D94114102297F86100077F90 /* SectionProviderMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionProviderMapper.swift; sourceTree = "<group>"; };
D94114102297F86100077F90 /* SectionProviderMapping.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionProviderMapping.swift; sourceTree = "<group>"; };
D9D7DCBC22981094001DA4D9 /* SectionProviderMapper+Spec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SectionProviderMapper+Spec.swift"; sourceTree = "<group>"; };
D9D7DCBE22985C44001DA4D9 /* CollectionViewSectionProviderCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewSectionProviderCoordinator.swift; sourceTree = "<group>"; };
D9D7DCC022985F04001DA4D9 /* SectionsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SectionsViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -448,7 +448,7 @@
D941140A2297F30D00077F90 /* SectionProvider.swift */,
D941140C2297F40500077F90 /* ComposedSectionProvider.swift */,
D941140E2297F4FA00077F90 /* ArraySection.swift */,
D94114102297F86100077F90 /* SectionProviderMapper.swift */,
D94114102297F86100077F90 /* SectionProviderMapping.swift */,
D9D7DCBE22985C44001DA4D9 /* CollectionViewSectionProviderCoordinator.swift */,
D9D7DCC222986942001DA4D9 /* UI */,
);
Expand Down Expand Up @@ -638,7 +638,7 @@
54A1733D225CC72900A7D7FD /* DataSourceInvalidationContext.swift in Sources */,
54A1731B225CC72900A7D7FD /* SectionedDataSource.swift in Sources */,
54A1733A225CC72900A7D7FD /* EmbeddedDataSourceCell.swift in Sources */,
D94114112297F86100077F90 /* SectionProviderMapper.swift in Sources */,
D94114112297F86100077F90 /* SectionProviderMapping.swift in Sources */,
54645B612281A3FD00E53245 /* NoSizingStrategy.swift in Sources */,
54E5CA272282FA3400CCA628 /* ComposedChangeDetails.swift in Sources */,
54A1733C225CC72900A7D7FD /* DataSourceCoordinator.swift in Sources */,
Expand Down
16 changes: 8 additions & 8 deletions Composed/Core/Sections/ArraySection.swift
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import Foundation

open class ArraySection<Element>: MutableSection {
open class ArraySection<Element>: Section {

public weak var updateDelegate: SectionUpdateDelegate?

public var elements: [Element]

public init(elements: [Element] = []) {
self.elements = elements
}

public func element(at index: Int) -> Element {
return elements[index]
}

public var numberOfElements: Int {
return elements.count
}

public func append(element: Element) {
let index = elements.count
elements.append(element)
updateDelegate?.section(self, didInsertElementAt: index)
}

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,51 @@
import UIKit

public final class CollectionViewSectionProviderCoordinator: NSObject, UICollectionViewDataSource, SectionProviderMapperDelegate {
public final class CollectionViewSectionProviderCoordinator: NSObject, UICollectionViewDataSource, SectionProviderMappingDelegate {

private let mapper: SectionProviderMapper
private let mapper: SectionProviderMapping
private let collectionView: UICollectionView

public init(collectionView: UICollectionView, sectionProvider: SectionProvider) {
self.collectionView = collectionView
mapper = SectionProviderMapper(globalProvider: sectionProvider)
mapper = SectionProviderMapping(provider: sectionProvider)

super.init()

collectionView.dataSource = self
}

// MARK: - SectionProviderMapperDelegate
public func sectionProviderMapper(_ sectionProviderMapper: SectionProviderMapper, didInsertSections sections: IndexSet) {
// MARK: - SectionProviderMappingDelegate

public func mapping(_ mapping: SectionProviderMapping, didInsertSections sections: IndexSet) {
collectionView.insertSections(sections)
}
public func sectionProviderMapper(_ sectionProviderMapper: SectionProviderMapper, didInsertElementsAt indexPaths: [IndexPath]) {

public func mapping(_ mapping: SectionProviderMapping, didInsertElementsAt indexPaths: [IndexPath]) {
collectionView.insertItems(at: indexPaths)
}


public func mapping(_ mapping: SectionProviderMapping, didRemoveSections sections: IndexSet) {
collectionView.deleteSections(sections)
}

public func mapping(_ mapping: SectionProviderMapping, didRemoveElementsAt indexPaths: [IndexPath]) {
collectionView.deleteItems(at: indexPaths)
}

public func mapping(_ mapping: SectionProviderMapping, didUpdateSections sections: IndexSet) {
collectionView.reloadSections(sections)
}

public func mapping(_ mapping: SectionProviderMapping, didUpdateElementsAt indexPaths: [IndexPath]) {
collectionView.reloadItems(at: indexPaths)
}

public func mapping(_ mapping: SectionProviderMapping, didMoveElementsAt moves: [(IndexPath, IndexPath)]) {
moves.forEach {
collectionView.moveItem(at: $0.0, to: $0.1)
}
}

// MARK: - UICollectionViewDataSource

public func numberOfSections(in collectionView: UICollectionView) -> Int {
Expand Down Expand Up @@ -54,7 +76,7 @@ public final class CollectionViewSectionProviderCoordinator: NSObject, UICollect
}

private func collectionUIConfigurationProvider(for section: Int) -> CollectionUIConfiguration? {
return (mapper.globalProvider.sections[section] as? CollectionUIConfigurationProvider)?.collectionUIConfiguration
return (mapper.provider.sections[section] as? CollectionUIConfigurationProvider)?.collectionUIConfiguration
}

}
47 changes: 27 additions & 20 deletions Composed/Core/Sections/ComposedSectionProvider.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import Foundation

public final class ComposedSectionProvider: AggregateSectionProvider, SectionProviderUpdateDelegate {

private enum Child {
case provider(SectionProvider)
case section(Section)
}

public var updateDelegate: SectionProviderUpdateDelegate?

private var children: [Child] = []

public var sections: [Section] {
return children.flatMap { kind -> [Section] in
switch kind {
Expand All @@ -21,7 +21,7 @@ public final class ComposedSectionProvider: AggregateSectionProvider, SectionPro
}
}
}

public var providers: [SectionProvider] {
return children.compactMap { kind in
switch kind {
Expand All @@ -31,7 +31,7 @@ public final class ComposedSectionProvider: AggregateSectionProvider, SectionPro
}
}
}

public var numberOfSections: Int {
return children.reduce(into: 0, { result, kind in
switch kind {
Expand All @@ -40,18 +40,18 @@ public final class ComposedSectionProvider: AggregateSectionProvider, SectionPro
}
})
}

public init() { }

public func numberOfElements(in section: Int) -> Int {
return sections[section].numberOfElements
}

public func sectionOffset(for provider: SectionProvider) -> Int {
guard provider !== self else { return 0 }

var offset: Int = 0

for child in children {
switch child {
case .section:
Expand All @@ -65,34 +65,41 @@ public final class ComposedSectionProvider: AggregateSectionProvider, SectionPro
return offset + sectionOffset
}
}

offset += childProvider.numberOfSections
}
}

// Provider is not in the hierachy
return -1
}

public func append(_ child: SectionProvider) {
child.updateDelegate = self

let firstIndex = sections.count
let endIndex = firstIndex + child.sections.count

children.append(.provider(child))
updateDelegate?.provider(self, didInsertSections: child.sections, at: IndexSet(integersIn: firstIndex..<endIndex))
}

public func append(_ child: Section) {
let index = children.count
children.append(.section(child))
updateDelegate?.provider(self, didInsertSections: [child], at: IndexSet(integer: index))
}

public func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet) {
updateDelegate?.provider(provider, didInsertSections: sections, at: indexes)
}



public func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet) {
updateDelegate?.provider(provider, didRemoveSections: sections, at: indexes)
}

public func provider(_ provider: SectionProvider, didUpdateSections sections: [Section], at indexes: IndexSet) {
updateDelegate?.provider(provider, didUpdateSections: sections, at: indexes)
}

}
9 changes: 1 addition & 8 deletions Composed/Core/Sections/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,9 @@ public extension Section {
var isEmpty: Bool { return numberOfElements == 0 }
}

public protocol MutableSection: Section { }

public protocol ElementProvider {
associatedtype Element

var elements: [Element] { get }
}

public protocol SectionUpdateDelegate: class {
func section(_ section: Section, didInsertElementAt index: Int)
func section(_ section: Section, didRemoveElementAt index: Int)
func section(_ section: Section, didUpdateElementAt index: Int)
func section(_ section: Section, didMoveElementAt index: Int, to newIndex: Int)
}
38 changes: 20 additions & 18 deletions Composed/Core/Sections/SectionProvider.swift
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
import Foundation

public protocol SectionProvider: class {

var updateDelegate: SectionProviderUpdateDelegate? { get set }

var sections: [Section] { get }

var isEmpty: Bool { get }

var numberOfSections: Int { get }

func numberOfElements(in section: Int) -> Int

}

extension SectionProvider {

public var isEmpty: Bool {
return sections.isEmpty || sections.allSatisfy { $0.isEmpty }
}

public var numberOfSections: Int {
return sections.count
}

public func numberOfElements(in section: Int) -> Int {
return sections[section].numberOfElements
}

}

public protocol AggregateSectionProvider: SectionProvider {

var providers: [SectionProvider] { get }

/**
Calculates the section offset for the provided section provider in the
context of the callee

- parameter provider: The provider to calculate the section offset of
- returns: The section offset of the provided section provider, or -1 if
the section provider is not in the hierachy
the section provider is not in the hierachy
*/
func sectionOffset(for provider: SectionProvider) -> Int

}

public struct HashableProvider: Hashable {

public static func == (lhs: HashableProvider, rhs: HashableProvider) -> Bool {
return lhs.provider === rhs.provider
}

private let provider: SectionProvider

public init(_ provider: SectionProvider) {
self.provider = provider
}

public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(provider))
}
Expand All @@ -67,4 +67,6 @@ public struct HashableProvider: Hashable {

public protocol SectionProviderUpdateDelegate: class {
func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet)
func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet)
func provider(_ provider: SectionProvider, didUpdateSections sections: [Section], at indexes: IndexSet)
}
Loading