From c112a84c0a73a7d2f8910a7ef8108c6349ecd40c Mon Sep 17 00:00:00 2001 From: John Estropia Date: Mon, 28 Oct 2019 19:31:02 +0900 Subject: [PATCH] Add debugDescription implementation for new Publisher and Snapshot types --- ...reStore+CustomDebugStringConvertible.swift | 127 +++++++++++++++++- .../DiffableDataSource.TableView-UIKit.swift | 38 ++---- ...edDiffableDataSourceSnapshotDelegate.swift | 26 ---- Sources/ListPublisher.swift | 9 -- Sources/ListSnapshot.swift | 18 +-- Sources/ObjectPublisher.swift | 11 +- Sources/ObjectSnapshot.swift | 19 ++- 7 files changed, 166 insertions(+), 82 deletions(-) diff --git a/Sources/CoreStore+CustomDebugStringConvertible.swift b/Sources/CoreStore+CustomDebugStringConvertible.swift index d1e1564..4eb4144 100644 --- a/Sources/CoreStore+CustomDebugStringConvertible.swift +++ b/Sources/CoreStore+CustomDebugStringConvertible.swift @@ -373,15 +373,34 @@ fileprivate struct CoreStoreFetchedSectionInfoWrapper: CoreStoreDebugStringConve var coreStoreDumpString: String { return createFormattedString( - "\"\(self.sectionInfo.name)\" (", ")", - ("numberOfObjects", self.sectionInfo.numberOfObjects), - ("indexTitle", self.sectionInfo.indexTitle as Any) + "\"\(self.sectionName)\" (", ")", + ("numberOfObjects", self.numberOfObjects), + ("indexTitle", self.sectionIndexTitle as Any) ) } // MARK: FilePrivate - let sectionInfo: NSFetchedResultsSectionInfo + fileprivate init(_ sectionInfo: NSFetchedResultsSectionInfo) { + + self.sectionName = sectionInfo.name + self.numberOfObjects = sectionInfo.numberOfObjects + self.sectionIndexTitle = sectionInfo.indexTitle + } + + fileprivate init(_ section: Internals.DiffableDataSourceSnapshot.Section) { + + self.sectionName = section.differenceIdentifier + self.numberOfObjects = section.elements.count + self.sectionIndexTitle = nil + } + + + // MARK: Private + + private let sectionName: String + private let sectionIndexTitle: String? + private let numberOfObjects: Int } @available(macOS 10.12, *) @@ -409,6 +428,56 @@ extension ListMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConvert } +// MARK: - ListPublisher + +@available(macOS 10.12, *) +extension ListPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return formattedDebugDescription(self) + } + + + // MARK: CoreStoreDebugStringConvertible + + public var coreStoreDumpString: String { + + return createFormattedString( + "(", ")", + ("snapshot", self.snapshot) + ) + } +} + + +// MARK: - ListSnapshot + +extension ListSnapshot: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return formattedDebugDescription(self) + } + + + // MARK: CoreStoreDebugStringConvertible + + public var coreStoreDumpString: String { + + return createFormattedString( + "(", ")", + ("numberOfObjects", self.numberOfItems), + ("sections", self.diffableSnapshot.sections.map(CoreStoreFetchedSectionInfoWrapper.init)) + ) + } +} + + // MARK: - LocalStorageOptions extension LocalStorageOptions: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { @@ -568,6 +637,56 @@ extension ObjectMonitor: CustomDebugStringConvertible, CoreStoreDebugStringConve } +// MARK: - ObjectPublisher + +extension ObjectPublisher: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return formattedDebugDescription(self) + } + + + // MARK: CoreStoreDebugStringConvertible + + public var coreStoreDumpString: String { + + return createFormattedString( + "(", ")", + ("objectID", self.objectID()), + ("object", self.object as Any) + ) + } +} + + +// MARK: - ObjectSnapshot + +extension ObjectSnapshot: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { + + // MARK: CustomDebugStringConvertible + + public var debugDescription: String { + + return formattedDebugDescription(self) + } + + + // MARK: CoreStoreDebugStringConvertible + + public var coreStoreDumpString: String { + + return createFormattedString( + "(", ")", + ("objectID", self.objectID()), + ("dictionaryForValues", self.dictionaryForValues()) + ) + } +} + + // MARK: - OrderBy extension OrderBy: CustomDebugStringConvertible, CoreStoreDebugStringConvertible { diff --git a/Sources/DiffableDataSource.TableView-UIKit.swift b/Sources/DiffableDataSource.TableView-UIKit.swift index a57cb89..35928b2 100644 --- a/Sources/DiffableDataSource.TableView-UIKit.swift +++ b/Sources/DiffableDataSource.TableView-UIKit.swift @@ -127,31 +127,19 @@ extension DiffableDataSource { @nonobjc public func apply(_ snapshot: ListSnapshot, animatingDifferences: Bool = true) { - let diffableSnapshot = snapshot.diffableSnapshot -// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) { -// -// self.modernDataSource.apply( -// diffableSnapshot as! NSDiffableDataSourceSnapshot, -// animatingDifferences: animatingDifferences, -// completion: nil -// ) -// } -// else { - - self.dispatcher.apply( - diffableSnapshot as! Internals.DiffableDataSourceSnapshot, - view: self.tableView, - animatingDifferences: animatingDifferences, - performUpdates: { tableView, changeset, setSections in - - tableView.reload( - using: changeset, - with: self.defaultRowAnimation, - setData: setSections - ) - } - ) -// } + self.dispatcher.apply( + snapshot.diffableSnapshot, + view: self.tableView, + animatingDifferences: animatingDifferences, + performUpdates: { tableView, changeset, setSections in + + tableView.reload( + using: changeset, + with: self.defaultRowAnimation, + setData: setSections + ) + } + ) } /** diff --git a/Sources/Internals.FetchedDiffableDataSourceSnapshotDelegate.swift b/Sources/Internals.FetchedDiffableDataSourceSnapshotDelegate.swift index 4348422..60e3b2d 100644 --- a/Sources/Internals.FetchedDiffableDataSourceSnapshotDelegate.swift +++ b/Sources/Internals.FetchedDiffableDataSourceSnapshotDelegate.swift @@ -41,9 +41,6 @@ import AppKit internal protocol FetchedDiffableDataSourceSnapshotHandler: AnyObject { -// @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) -// func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: NSDiffableDataSourceSnapshot) - func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: Internals.DiffableDataSourceSnapshot) func controller(_ controller: NSFetchedResultsController, sectionIndexTitleForSectionName sectionName: String?) -> String? @@ -80,15 +77,6 @@ extension Internals { internal func initialFetch() { -// #if canImport(UIKit) || canImport(AppKit) -// -// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) { -// -// return -// } -// -// #endif - guard let fetchedResultsController = self.fetchedResultsController else { return @@ -98,20 +86,6 @@ extension Internals { // MARK: NSFetchedResultsControllerDelegate - -// #if canImport(UIKit) || canImport(AppKit) -// -// @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) -// @objc -// dynamic func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) { -// -// self.handler?.controller( -// controller, -// didChangeContentWith: snapshot as NSDiffableDataSourceSnapshot -// ) -// } -// -// #endif @objc dynamic func controllerDidChangeContent(_ controller: NSFetchedResultsController) { diff --git a/Sources/ListPublisher.swift b/Sources/ListPublisher.swift index d09b7b2..71881e1 100644 --- a/Sources/ListPublisher.swift +++ b/Sources/ListPublisher.swift @@ -397,15 +397,6 @@ public final class ListPublisher: Hashable { extension ListPublisher: FetchedDiffableDataSourceSnapshotHandler { // MARK: FetchedDiffableDataSourceSnapshotHandler - -// @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) -// internal func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: NSDiffableDataSourceSnapshot) { -// -// self.snapshot = .init( -// diffableSnapshot: snapshot, -// context: controller.managedObjectContext -// ) -// } internal func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: Internals.DiffableDataSourceSnapshot) { diff --git a/Sources/ListSnapshot.swift b/Sources/ListSnapshot.swift index 2ec94b7..ca13a77 100644 --- a/Sources/ListSnapshot.swift +++ b/Sources/ListSnapshot.swift @@ -629,28 +629,14 @@ public struct ListSnapshot: RandomAccessCollection, Hashable { // MARK: Internal - internal private(set) var diffableSnapshot: DiffableDataSourceSnapshotProtocol + internal private(set) var diffableSnapshot: Internals.DiffableDataSourceSnapshot internal init() { -// if #available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) { -// -// self.diffableSnapshot = NSDiffableDataSourceSnapshot() -// } -// else { - - self.diffableSnapshot = Internals.DiffableDataSourceSnapshot() -// } + self.diffableSnapshot = Internals.DiffableDataSourceSnapshot() self.context = nil } -// @available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *) -// internal init(diffableSnapshot: NSDiffableDataSourceSnapshot, context: NSManagedObjectContext) { -// -// self.diffableSnapshot = diffableSnapshot -// self.context = context -// } - internal init(diffableSnapshot: Internals.DiffableDataSourceSnapshot, context: NSManagedObjectContext) { self.diffableSnapshot = diffableSnapshot diff --git a/Sources/ObjectPublisher.swift b/Sources/ObjectPublisher.swift index 98718f5..ce2cfde 100644 --- a/Sources/ObjectPublisher.swift +++ b/Sources/ObjectPublisher.swift @@ -336,7 +336,6 @@ extension ObjectPublisher { // MARK: - ObjectPublisher where O: NSManagedObject -@available(*, unavailable, message: "KeyPaths accessed from @dynamicMemberLookup types can't generate KVC keys yet (https://bugs.swift.org/browse/SR-11351)") extension ObjectPublisher where O: NSManagedObject { // MARK: Public @@ -344,11 +343,21 @@ extension ObjectPublisher where O: NSManagedObject { /** Returns the value for the property identified by a given key. */ + @available(*, unavailable, message: "KeyPaths accessed from @dynamicMemberLookup types can't generate KVC keys yet (https://bugs.swift.org/browse/SR-11351)") public subscript(dynamicMember member: KeyPath) -> V { fatalError() // return self.snapshot[dynamicMember: member] } + + /** + Returns the value for the property identified by a given key. + */ + public func value(forKeyPath keyPath: KeyPath) -> V! { + + let key = String(keyPath: keyPath) + return self.snapshot?.dictionaryForValues()[key] as! V? + } } diff --git a/Sources/ObjectSnapshot.swift b/Sources/ObjectSnapshot.swift index 92dd900..34f7255 100644 --- a/Sources/ObjectSnapshot.swift +++ b/Sources/ObjectSnapshot.swift @@ -41,6 +41,14 @@ import AppKit */ @dynamicMemberLookup public struct ObjectSnapshot: ObjectRepresentation, Hashable { + + // MARK: Public + + public func dictionaryForValues() -> [String: Any] { + + return self.values + } + // MARK: ObjectRepresentation @@ -127,17 +135,26 @@ public struct ObjectSnapshot: ObjectRepresentation, Hashable { // MARK: - ObjectSnapshot where O: NSManagedObject -@available(*, unavailable, message: "KeyPaths accessed from @dynamicMemberLookup types can't generate KVC keys yet (https://bugs.swift.org/browse/SR-11351)") extension ObjectSnapshot where O: NSManagedObject { /** Returns the value for the property identified by a given key. */ + @available(*, unavailable, message: "KeyPaths accessed from @dynamicMemberLookup types can't generate KVC keys yet (https://bugs.swift.org/browse/SR-11351)") public subscript(dynamicMember member: KeyPath) -> V { let key = String(keyPath: member) return self.values[key] as! V } + + /** + Returns the value for the property identified by a given key. + */ + public func value(forKeyPath keyPath: KeyPath) -> V! { + + let key = String(keyPath: keyPath) + return self.values[key] as! V? + } }