From 455a156007f76dc0b8db2c20bf98124ad7139e96 Mon Sep 17 00:00:00 2001 From: Vladislav Kondrashkov Date: Wed, 2 Sep 2020 10:36:14 +0300 Subject: [PATCH 1/2] Extended Array2D to be conformed to Collection --- Sources/Bond/Data Structures/Array2D.swift | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Sources/Bond/Data Structures/Array2D.swift b/Sources/Bond/Data Structures/Array2D.swift index 0d591ff0..b4e0809a 100644 --- a/Sources/Bond/Data Structures/Array2D.swift +++ b/Sources/Bond/Data Structures/Array2D.swift @@ -219,3 +219,37 @@ extension Array2D { } } } + +// MARK: - Sequence conformance + +extension Array2D: Swift.Sequence { + public typealias Iterator = IndexingIterator<[Array2D.Section]> + + public func makeIterator() -> Iterator { + return sections.makeIterator() + } +} + +// MARK: - Collection conformance + +extension Array2D: Collection { + public typealias Index = Int + + public var startIndex: Index { + return sections.startIndex + } + + public var endIndex: Index { + return sections.endIndex + } + + public subscript(position: Index) -> Iterator.Element { + precondition(indices.contains(position), "out of bounds") + let element = sections[position] + return element + } + + public func index(after i: Index) -> Index { + return sections.index(after: i) + } +} From aad8d2f89ecb60f16b93a945675e5ed4d3acbb2c Mon Sep 17 00:00:00 2001 From: Vladislav Kondrashkov Date: Wed, 2 Sep 2020 10:36:35 +0300 Subject: [PATCH 2/2] Extended Array2D with SectionedDataSourceChangesetConvertible --- .../SectionedDataSourceChangesetConvertible.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Sources/Bond/Data Sources/SectionedDataSourceChangesetConvertible.swift b/Sources/Bond/Data Sources/SectionedDataSourceChangesetConvertible.swift index 7cd7b158..874c5ea2 100644 --- a/Sources/Bond/Data Sources/SectionedDataSourceChangesetConvertible.swift +++ b/Sources/Bond/Data Sources/SectionedDataSourceChangesetConvertible.swift @@ -75,6 +75,12 @@ extension Array: SectionedDataSourceChangesetConvertible { } } +extension Array2D: SectionedDataSourceChangesetConvertible { + public var asSectionedDataSourceChangeset: OrderedCollectionChangeset { + return OrderedCollectionChangeset(collection: self, patch: []) + } +} + extension OrderedCollectionChangeset: SectionedDataSourceChangeset where Diff.Index: SectionedDataIndexPathConvertable, Collection: SectionedDataSourceProtocol {} extension OrderedCollectionChangeset: SectionedDataSourceChangesetConvertible where Diff.Index: SectionedDataIndexPathConvertable, Collection: SectionedDataSourceProtocol {