Structures

The following structures are available globally.

  • Struct that contains updated storage and changes applied to it

    Example:

    struct 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]>
    
    
    See more

    Declaration

    Swift

    public struct BinUpdateResult<Backstorage> where Backstorage : Collection
  • Represents a Facade for RangeReplaceableCollection that is a Collection itself

    Provides updating method to find the element if it exists and update or remove it based on the rules provided by CollectionFilter<Element>

    If Backstorage doesn’t contain the element and the element complies to the rules, updating method will append the element

    Every call of updating method returns BinUpdateResult<CollectionBin> that contains info about the update operation

    CollectionBin can 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:

         //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 })
    
    See more

    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:

     let changes = CollectionChanges<[Int]>(updatedIndexes: [0,2,3],
                                            emovedIndexes: [1],
                                            addedIndexes: [4])
    
    
    See more

    Declaration

    Swift

    public struct CollectionChanges<C> where C : Collection
  • Diff is 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 Diffwhere C == [CollectionBin<[T]>] that provides logic for updating element and putting it in propper subcollections of type CollectionBin<[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
           let 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)]
    
    See more

    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.Index
    extension Diff: MutableCollection
  • Generic struct that contains configuration for a one Bin/Section

    Note

    Elements of the collection should conform to Hashable It is very important to chose hash fucntion or value correctly since CollectionBin<Backstorage> will use it to identify the elements
    See more

    Declaration

    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 more

    Declaration

    Swift

    public struct DiffCollectionSnapshot<E> where E : Hashable