Structures
The following structures are available globally.
-
Struct that contains updated storage and changes applied to it
Example:
See morestruct AnyModel { // any implementation } let result: BinUpdateResult<[AnyModel]> = // returned by a function // result.bin is a type of [AnyModel] // result.changes is a type of CollectionChanges<[Int]>Declaration
Swift
public struct BinUpdateResult<Backstorage> where Backstorage : Collection -
Represents a Facade for
RangeReplaceableCollectionthat is aCollectionitselfProvides
updatingmethod tofindthe element if it exists andupdateorremoveit based on the rules provided byCollectionFilter<Element>If
Backstoragedoesn’t contain the element and the element complies to the rules,updatingmethod will append the elementEvery call of
updatingmethod returnsBinUpdateResult<CollectionBin>that contains info about the update operationCollectionBincan be initialized with filter and sorting closures specifying the rules how to treat the elements.Example
//let's have a model like struct CollectionTestObjectMock: Hashable, Equatable { var value: String var status: ObjectStatus var rank: Int func hash(into hasher: inout Hasher) { hasher.combine(value) hasher.combine(rank) } // -Note: since value and rank are a part of unique value of the object the only part that // cab be changed is the status static func == (lhs: CollectionTestObjectMock, rhs: CollectionTestObjectMock) -> Bool { lhs.status == rhs.status } }Then a basic initizlization will be:
See more//Will contain object with status == .cold and sorted by rank descending DiffCollectionFilter<CollectionTestObjectMock>(name: "Status is new or old", filter: { $0.status == .cold}, sort: { $0.rank > $1.rank })Declaration
Swift
public struct CollectionBin<Backstorage> : Collection where Backstorage : RangeReplaceableCollection -
Struct that contains information about added/removed/updated indexes Indexes are represented by any colection:
Usage example:
See morelet changes = CollectionChanges<[Int]>(updatedIndexes: [0,2,3], emovedIndexes: [1], addedIndexes: [4])Declaration
Swift
public struct CollectionChanges<C> where C : Collection -
Diffis a concept of a container for multiple collections and provides methods to access elements of those collections Example:let names = ["Adams", "Bryant", "Channing"] let cars = ["Ford", "Dodge"] let collection = Diff([names,cars]) let subSequence = collection[1] print(subSequence) // ["Ford", "Dodge"] let element = collection[IndexPath(row: 0,section: 1)] print(element) // "Ford"Contains a very powerful extension for
DiffwhereC== [CollectionBin<[T]>] that provides logic for updating element and putting it in propper subcollections of typeCollectionBin<[T]>An update operation returns
DiffCollectionSnapshot<T>containing modifications made to the collection and its subcollections.Usage Example
Define a model
//let's have a model like struct TestObject: Hashable, Equatable { var value: String var status: ObjectStatus var rank: Int func hash(into hasher: inout Hasher) { hasher.combine(value) hasher.combine(rank) } // -Note: since value and rank are a part of unique value of the object the only part that // cab be changed is the status static func == (lhs: Self, rhs: Self) -> Bool { lhs.status == rhs.status } }Define filters for sections:
let startsARankSorted = DiffCollectionFilter<TestObject>(name: "Starts with a", filter: { $0.value.starts(with: "a") }, sort: { $0.rank > $1.rank }) let startedBValueSorted = DiffCollectionFilter<TestObject>(name: "Starts with b", filter: { $0.value.starts(with: "b") }, sort: { $0.value > $1.value })- initialize
Diff
var diffCollection = [startsARankSorted, startedBValueSorted]- Start using by calling upade function
See morelet elementA = TestObject(value: "Arm", status: .new, rank: 100) let snapshot = diffCollection.update(with: elementA) debugPrint(snapshot.changes) // updatedIndexes = [], removedIndexes = [], addedIndexes = [IndexPath(row: 0, section: 0)]Declaration
Swift
public struct Diff<T, C>: Collection where T: Equatable & Hashable, C: RangeReplaceableCollection, C.Element: Collection, C.Element.Element == T, C.Index == Int, C.Element.Index == C.Indexextension Diff: MutableCollection -
Generic struct that contains configuration for a one Bin/Section
See moreNote
Elements of the collection should conform toHashableIt is very important to chose hash fucntion or value correctly sinceCollectionBin<Backstorage>will use it to identify the elementsDeclaration
Swift
public struct DiffCollectionFilter<T> where T : Hashable -
Respresents a snapshot of a collection after updating with an element Contains info about the element, new collection and changes
See moreDeclaration
Swift
public struct DiffCollectionSnapshot<E> where E : Hashable
Structures Reference