mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-26 03:11:30 +01:00
WIP
This commit is contained in:
@@ -23,8 +23,6 @@
|
||||
// SOFTWARE.
|
||||
//
|
||||
|
||||
#if canImport(UIKit) || canImport(AppKit)
|
||||
|
||||
import CoreData
|
||||
|
||||
#if canImport(UIKit)
|
||||
@@ -38,21 +36,170 @@ import AppKit
|
||||
|
||||
// MARK: - LiveList
|
||||
|
||||
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
|
||||
public struct ListSnapshot<D: DynamicObject>: SnapshotResult, RandomAccessCollection, Hashable {
|
||||
|
||||
// MARK: Public
|
||||
|
||||
public subscript<S: Sequence>(indices: S) -> [ObjectType] where S.Element == Index {
|
||||
|
||||
|
||||
public typealias SectionID = String
|
||||
public typealias ItemID = D.ObjectID
|
||||
|
||||
public subscript<S: Sequence>(indices indices: S) -> [ObjectType] where S.Element == Index {
|
||||
|
||||
let context = self.context!
|
||||
let objectIDs = self.snapshotStruct.itemIdentifiers
|
||||
let objectIDs = self.diffableSnapshot.itemIdentifiers
|
||||
return indices.map { position in
|
||||
|
||||
|
||||
let objectID = objectIDs[position]
|
||||
return context.fetchExisting(objectID)!
|
||||
}
|
||||
}
|
||||
|
||||
public subscript(section sectionID: SectionID) -> [ObjectType] {
|
||||
|
||||
let context = self.context!
|
||||
let objectIDs = self.itemIdentifiers(inSection: sectionID)
|
||||
return objectIDs.map {
|
||||
|
||||
return context.fetchExisting($0)!
|
||||
}
|
||||
}
|
||||
|
||||
public subscript<S: Sequence>(section sectionID: SectionID, itemIndices itemIndices: S) -> [ObjectType] where S.Element == Int {
|
||||
|
||||
let context = self.context!
|
||||
let objectIDs = self.itemIdentifiers(inSection: sectionID)
|
||||
return itemIndices.map { position in
|
||||
|
||||
let objectID = objectIDs[position]
|
||||
return context.fetchExisting(objectID)!
|
||||
}
|
||||
}
|
||||
|
||||
public var numberOfItems: Int {
|
||||
|
||||
return self.diffableSnapshot.numberOfItems
|
||||
}
|
||||
|
||||
public var numberOfSections: Int {
|
||||
|
||||
return self.diffableSnapshot.numberOfSections
|
||||
}
|
||||
|
||||
public var sectionIdentifiers: [String] {
|
||||
|
||||
return self.diffableSnapshot.sectionIdentifiers as [String]
|
||||
}
|
||||
|
||||
public var itemIdentifiers: [ItemID] {
|
||||
|
||||
return self.diffableSnapshot.itemIdentifiers as [ItemID]
|
||||
}
|
||||
|
||||
public func numberOfItems(inSection identifier: SectionID) -> Int {
|
||||
|
||||
return self.diffableSnapshot.numberOfItems(inSection: identifier as NSString)
|
||||
}
|
||||
|
||||
public func itemIdentifiers(inSection identifier: SectionID) -> [ItemID] {
|
||||
|
||||
return self.diffableSnapshot.itemIdentifiers(inSection: identifier as NSString)
|
||||
}
|
||||
|
||||
public func itemIdentifiers(inSection identifier: SectionID, atIndices indices: IndexSet) -> [ItemID] {
|
||||
|
||||
let itemIDs = self.itemIdentifiers(inSection: identifier)
|
||||
return indices.map({ itemIDs[$0] })
|
||||
}
|
||||
|
||||
public func sectionIdentifier(containingItem identifier: ItemID) -> SectionID? {
|
||||
|
||||
return self.diffableSnapshot.sectionIdentifier(containingItem: identifier) as SectionID?
|
||||
}
|
||||
|
||||
public func indexOfItem(_ identifier: ItemID) -> Index? {
|
||||
|
||||
return self.diffableSnapshot.indexOfItem(identifier)
|
||||
}
|
||||
|
||||
public func indexOfSection(_ identifier: SectionID) -> Int? {
|
||||
|
||||
return self.diffableSnapshot.indexOfSection(identifier as NSString)
|
||||
}
|
||||
|
||||
public mutating func appendItems(_ identifiers: [ItemID], toSection sectionIdentifier: SectionID? = nil) {
|
||||
|
||||
self.diffableSnapshot.appendItems(identifiers, toSection: sectionIdentifier as NSString?)
|
||||
}
|
||||
|
||||
public mutating func insertItems(_ identifiers: [ItemID], beforeItem beforeIdentifier: ItemID) {
|
||||
|
||||
self.diffableSnapshot.insertItems(identifiers, beforeItem: beforeIdentifier)
|
||||
}
|
||||
|
||||
public mutating func insertItems(_ identifiers: [ItemID], afterItem afterIdentifier: ItemID) {
|
||||
|
||||
self.diffableSnapshot.insertItems(identifiers, afterItem: afterIdentifier)
|
||||
}
|
||||
|
||||
public mutating func deleteItems(_ identifiers: [ItemID]) {
|
||||
|
||||
self.diffableSnapshot.deleteItems(identifiers)
|
||||
}
|
||||
|
||||
public mutating func deleteAllItems() {
|
||||
|
||||
self.diffableSnapshot.deleteAllItems()
|
||||
}
|
||||
|
||||
public mutating func moveItem(_ identifier: ItemID, beforeItem toIdentifier: ItemID) {
|
||||
|
||||
self.diffableSnapshot.moveItem(identifier, beforeItem: toIdentifier)
|
||||
}
|
||||
|
||||
public mutating func moveItem(_ identifier: ItemID, afterItem toIdentifier: ItemID) {
|
||||
|
||||
self.diffableSnapshot.moveItem(identifier, afterItem: toIdentifier)
|
||||
}
|
||||
|
||||
public mutating func reloadItems(_ identifiers: [ItemID]) {
|
||||
|
||||
self.diffableSnapshot.reloadItems(identifiers)
|
||||
}
|
||||
|
||||
public mutating func appendSections(_ identifiers: [SectionID]) {
|
||||
|
||||
self.diffableSnapshot.appendSections(identifiers as [NSString])
|
||||
}
|
||||
|
||||
public mutating func insertSections(_ identifiers: [SectionID], beforeSection toIdentifier: SectionID) {
|
||||
|
||||
self.diffableSnapshot.insertSections(identifiers as [NSString], beforeSection: toIdentifier as NSString)
|
||||
}
|
||||
|
||||
public mutating func insertSections(_ identifiers: [SectionID], afterSection toIdentifier: SectionID) {
|
||||
|
||||
self.diffableSnapshot.insertSections(identifiers as [NSString], afterSection: toIdentifier as NSString)
|
||||
}
|
||||
|
||||
public mutating func deleteSections(_ identifiers: [SectionID]) {
|
||||
|
||||
self.diffableSnapshot.deleteSections(identifiers as [NSString])
|
||||
}
|
||||
|
||||
public mutating func moveSection(_ identifier: SectionID, beforeSection toIdentifier: SectionID) {
|
||||
|
||||
self.diffableSnapshot.moveSection(identifier as NSString, beforeSection: toIdentifier as NSString)
|
||||
}
|
||||
|
||||
public mutating func moveSection(_ identifier: SectionID, afterSection toIdentifier: SectionID) {
|
||||
|
||||
self.diffableSnapshot.moveSection(identifier as NSString, afterSection: toIdentifier as NSString)
|
||||
}
|
||||
|
||||
public mutating func reloadSections(_ identifiers: [SectionID]) {
|
||||
|
||||
self.diffableSnapshot.reloadSections(identifiers as [NSString])
|
||||
}
|
||||
|
||||
|
||||
// MARK: SnapshotResult
|
||||
@@ -64,18 +211,18 @@ public struct ListSnapshot<D: DynamicObject>: SnapshotResult, RandomAccessCollec
|
||||
|
||||
public var startIndex: Index {
|
||||
|
||||
return 0
|
||||
return self.diffableSnapshot.itemIdentifiers.startIndex
|
||||
}
|
||||
|
||||
public var endIndex: Index {
|
||||
|
||||
return self.snapshotStruct.numberOfItems
|
||||
return self.diffableSnapshot.itemIdentifiers.endIndex
|
||||
}
|
||||
|
||||
public subscript(position: Index) -> ObjectType {
|
||||
|
||||
let context = self.context!
|
||||
let objectID = self.snapshotStruct.itemIdentifiers[position]
|
||||
let objectID = self.diffableSnapshot.itemIdentifiers[position]
|
||||
return context.fetchExisting(objectID)!
|
||||
}
|
||||
|
||||
@@ -106,16 +253,14 @@ public struct ListSnapshot<D: DynamicObject>: SnapshotResult, RandomAccessCollec
|
||||
// MARK: Internal
|
||||
|
||||
internal init() {
|
||||
|
||||
self.snapshotReference = .init()
|
||||
self.snapshotStruct = self.snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
|
||||
|
||||
self.diffableSnapshot = Internals.FallbackDiffableDataSourceSnapshot()
|
||||
self.context = nil
|
||||
}
|
||||
|
||||
internal init(snapshotReference: NSDiffableDataSourceSnapshotReference, context: NSManagedObjectContext) {
|
||||
|
||||
self.snapshotReference = snapshotReference
|
||||
self.snapshotStruct = snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
|
||||
internal init(diffableSnapshot: Internals.DiffableDataSourceSnapshot, context: NSManagedObjectContext) {
|
||||
|
||||
self.diffableSnapshot = diffableSnapshot
|
||||
self.context = context
|
||||
}
|
||||
|
||||
@@ -123,9 +268,6 @@ public struct ListSnapshot<D: DynamicObject>: SnapshotResult, RandomAccessCollec
|
||||
// MARK: Private
|
||||
|
||||
private let id: UUID = .init()
|
||||
private let snapshotReference: NSDiffableDataSourceSnapshotReference
|
||||
private let snapshotStruct: NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
|
||||
private let context: NSManagedObjectContext?
|
||||
private var diffableSnapshot: Internals.DiffableDataSourceSnapshot
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user