Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
45 changes: 40 additions & 5 deletions Sources/Composed/Core/Section.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public extension Section {

/// A delegate that will respond to update events from a `Section`
public protocol SectionUpdateDelegate: class {
/// A closure that will be called synchronously on the main thread. It must perform the updates
/// associated with the mapping change before returning.
typealias UpdatePerformer = () -> Void

/// Notifies the delegate before a section will process updates
/// - Parameter section: The section that will be updated
Expand All @@ -32,32 +35,50 @@ public protocol SectionUpdateDelegate: class {

/// Notifies the delegate that all sections should be invalidated, ignoring individual updates
/// - Parameter section: The section that requested the invalidation
func invalidateAll(_ section: Section)
func invalidateAll(_ section: Section, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was inserted
/// - Parameters:
/// - section: The section where the insert occurred
/// - index: The index of the element that was inserted
func section(_ section: Section, didInsertElementAt index: Int)
func section(_ section: Section, didInsertElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was inserted
/// - Parameters:
/// - section: The section where the insert occurred
/// - index: The index of the element that was inserted
func section(_ section: Section, didInsertElementsAt indexSet: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was removed
/// - Parameters:
/// - section: The section where the remove occurred
/// - index: The index of the element that was removed
func section(_ section: Section, didRemoveElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was removed
/// - Parameters:
/// - section: The section where the remove occurred
/// - index: The index of the element that was removed
func section(_ section: Section, didRemoveElementAt index: Int)
func section(_ section: Section, didRemoveElementsAt indexSet: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was updated
/// - Parameters:
/// - section: The section where the update occurred
/// - index: The index of the element that was updated
func section(_ section: Section, didUpdateElementAt index: Int)
func section(_ section: Section, didUpdateElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was updated
/// - Parameters:
/// - section: The section where the update occurred
/// - index: The index of the element that was updated
func section(_ section: Section, didUpdateElementsAt indexSet: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that an element was moved
/// - Parameters:
/// - section: The section where the move occurred
/// - index: The source index of the element that was moved
/// - newIndex: The target index of the element that was moved
func section(_ section: Section, didMoveElementAt index: Int, to newIndex: Int)
func section(_ section: Section, didMoveElementAt index: Int, to newIndex: Int, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Returns the currently selected indexes in the specified section
/// - Parameter section: The section to query
Expand All @@ -83,3 +104,17 @@ public protocol SectionUpdateDelegate: class {
func section(_ section: Section, move sourceIndex: Int, to destinationIndex: Int)

}

extension SectionUpdateDelegate {
public func section(_ section: Section, didInsertElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer) {
self.section(section, didInsertElementsAt: IndexSet(arrayLiteral: index), performUpdate: updatePerformer)
}

public func section(_ section: Section, didRemoveElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer) {
self.section(section, didRemoveElementsAt: IndexSet(arrayLiteral: index), performUpdate: updatePerformer)
}

public func section(_ section: Section, didUpdateElementAt index: Int, performUpdate updatePerformer: @escaping UpdatePerformer) {
self.section(section, didUpdateElementsAt: IndexSet(arrayLiteral: index), performUpdate: updatePerformer)
}
}
25 changes: 14 additions & 11 deletions Sources/Composed/Core/SectionProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,18 @@ public protocol AggregateSectionProvider: SectionProvider {
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
- returns: The section offset of the provided section provider, or `nil`if
the section provider is not in the hierachy
*/
func sectionOffset(for provider: SectionProvider) -> Int
func sectionOffset(for provider: SectionProvider) -> Int?

}

/// A delegate that will respond to update events from a `SectionProvider`
public protocol SectionProviderUpdateDelegate: class {
/// A closure that will be called synchronously on the main thread. It must perform the updates
/// associated with the mapping change before returning.
typealias UpdatePerformer = () -> Void

/// /// Notifies the delegate before a provider will process updates
/// - Parameter provider: The provider that will be updated
Expand All @@ -57,21 +60,21 @@ public protocol SectionProviderUpdateDelegate: class {

/// Notifies the delegate that all sections should be invalidated, ignoring individual updates
/// - Parameter provider: The provider that requested the invalidation
func invalidateAll(_ provider: SectionProvider)
func invalidateAll(_ provider: SectionProvider, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that sections were inserted
/// - Parameters:
/// - provider: The provider where the inserts occurred
/// - sections: The sections that were inserted
/// - indexes: The indexes of the sections that were inserted
func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet)
func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer)

/// Notifies the delegate that sections were removed
/// - Parameters:
/// - provider: The provider where the removes occurred
/// - sections: The sections that were removed
/// - indexes: The indexes of the sections that were removed
func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet)
func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer)
}

// Default implementations to minimise `SectionProvider` implementation requirements
Expand All @@ -85,16 +88,16 @@ public extension SectionProviderUpdateDelegate where Self: SectionProvider {
updateDelegate?.didEndUpdating(self)
}

func invalidateAll(_ provider: SectionProvider) {
updateDelegate?.invalidateAll(provider)
func invalidateAll(_ provider: SectionProvider, performUpdate updatePerformer: @escaping UpdatePerformer) {
updateDelegate?.invalidateAll(provider, performUpdate: updatePerformer)
}

func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet) {
updateDelegate?.provider(provider, didInsertSections: sections, at: indexes)
func provider(_ provider: SectionProvider, didInsertSections sections: [Section], at indexes: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer) {
updateDelegate?.provider(provider, didInsertSections: sections, at: indexes, performUpdate: updatePerformer)
}

func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet) {
updateDelegate?.provider(provider, didRemoveSections: sections, at: indexes)
func provider(_ provider: SectionProvider, didRemoveSections sections: [Section], at indexes: IndexSet, performUpdate updatePerformer: @escaping UpdatePerformer) {
updateDelegate?.provider(provider, didRemoveSections: sections, at: indexes, performUpdate: updatePerformer)
}

}
Loading