swiftUI support done for now

This commit is contained in:
John Estropia
2019-10-06 23:37:04 +09:00
parent 953c9723a8
commit b073b7e795
14 changed files with 710 additions and 424 deletions

View File

@@ -0,0 +1,45 @@
//
// EnvironmentKeys.swift
// CoreStore
//
// Created by John Rommel Estropia on 2019/10/05.
// Copyright © 2019 John Rommel Estropia. All rights reserved.
//
#if canImport(SwiftUI)
import SwiftUI
// MARK: - DataStackEnvironmentKey
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
public struct DataStackEnvironmentKey: EnvironmentKey {
// MARK: Public
public static var defaultValue: DataStack {
return Shared.defaultStack
}
}
// MARK: - EnvironmentValues
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
extension EnvironmentValues {
// MARK: Public
public var dataStack: DataStack {
get {
return self[DataStackEnvironmentKey.self]
}
set {
self[DataStackEnvironmentKey.self] = newValue
}
}
}
#endif

131
Sources/ListSnapshot.swift Normal file
View File

@@ -0,0 +1,131 @@
//
// ListSnapshot.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if canImport(UIKit) || canImport(AppKit)
import CoreData
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
// 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 {
let context = self.context!
let objectIDs = self.snapshotStruct.itemIdentifiers
return indices.map { position in
let objectID = objectIDs[position]
return context.fetchExisting(objectID)!
}
}
// MARK: SnapshotResult
public typealias ObjectType = D
// MARK: RandomAccessCollection
public var startIndex: Index {
return 0
}
public var endIndex: Index {
return self.snapshotStruct.numberOfItems
}
public subscript(position: Index) -> ObjectType {
let context = self.context!
let objectID = self.snapshotStruct.itemIdentifiers[position]
return context.fetchExisting(objectID)!
}
// MARK: Sequence
public typealias Element = ObjectType
public typealias Index = Int
// MARK: Equatable
public static func == (_ lhs: Self, _ rhs: Self) -> Bool {
return lhs.id == rhs.id
}
// MARK: Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(self.id)
}
// MARK: Internal
internal init() {
self.snapshotReference = .init()
self.snapshotStruct = self.snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
self.context = nil
}
internal init(snapshotReference: NSDiffableDataSourceSnapshotReference, context: NSManagedObjectContext) {
self.snapshotReference = snapshotReference
self.snapshotStruct = snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
self.context = context
}
// MARK: Private
private let id: UUID = .init()
private let snapshotReference: NSDiffableDataSourceSnapshotReference
private let snapshotStruct: NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
private let context: NSManagedObjectContext?
}
#endif

View File

@@ -35,6 +35,11 @@ import AppKit
#endif
#if canImport(Combine)
import Combine
#endif
#if canImport(SwiftUI)
import SwiftUI
@@ -44,58 +49,41 @@ import SwiftUI
// MARK: - LiveList
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
public final class LiveList<D: DynamicObject>: Hashable {
public final class LiveList<D: DynamicObject> {
// MARK: Public (Accessors)
/**
The type for the objects contained bye the `ListMonitor`
*/
public typealias ObjectType = D
public fileprivate(set) var snapshot: Snapshot = .empty {
didSet {
// MARK: Public
public fileprivate(set) var snapshot: ListSnapshot<ObjectType> = .init() {
willSet {
#if canImport(Combine)
let newValue = self.snapshot
let oldValue = self.snapshot
guard newValue != oldValue else {
return
}
#if canImport(SwiftUI)
withAnimation {
self.objectWillChange.send()
}
#else
self.objectWillChange.send()
#endif
#endif
}
}
// MARK: Equatable
public static func == (_ lhs: LiveList<D>, _ rhs: LiveList<D>) -> Bool {
return lhs === rhs
}
// MARK: Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
// MARK: LiveResult
public typealias ObjectType = D
public typealias SnapshotType = ListSnapshot<D>
// MARK: Internal
@@ -273,7 +261,7 @@ public final class LiveList<D: DynamicObject>: Hashable {
// transactionQueue.async {
//
// try! self.fetchedResultsController.performFetchFromSpecifiedStores()
// try!internal self.fetchedResultsController.performFetchFromSpecifiedStores()
// self.taskGroup.notify(queue: .main) {
//
// createAsynchronously(self)
@@ -285,108 +273,6 @@ public final class LiveList<D: DynamicObject>: Hashable {
try! self.fetchedResultsController.performFetchFromSpecifiedStores()
}
}
// MARK: - Snapshot
public struct Snapshot: RandomAccessCollection, Hashable {
public subscript(indices: IndexSet) -> [ObjectType] {
let context = self.context!
let objectIDs = self.snapshotStruct.itemIdentifiers
return indices.map { position in
let objectID = objectIDs[position]
return context.fetchExisting(objectID)!
}
}
// MARK: RandomAccessCollection
public var startIndex: Index {
return 0
}
public var endIndex: Index {
return self.snapshotStruct.numberOfItems
}
public subscript(position: Index) -> ObjectType {
let context = self.context!
let objectID = self.snapshotStruct.itemIdentifiers[position]
return context.fetchExisting(objectID)!
}
// MARK: Sequence
public typealias Element = ObjectType
public typealias Index = Int
// public typealias SubSequence = Slice<Snapshot<ObjectType>>
//
// /// A type that represents the indices that are valid for subscripting the
// /// collection, in ascending order.
// public typealias Indices = Range<Int>
//
// /// A type that provides the collection's iteration interface and
// /// encapsulates its iteration state.
// ///
// /// By default, a collection conforms to the `Sequence` protocol by
// /// supplying `IndexingIterator` as its associated `Iterator`
// /// type.
// public typealias Iterator = IndexingIterator<FetchedResults<Result>>
// MARK: Equatable
public static func == (_ lhs: Snapshot, _ rhs: Snapshot) -> Bool {
return lhs.snapshotReference == rhs.snapshotReference
}
// MARK: Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(self.snapshotReference)
}
// MARK: Internal
internal static var empty: Snapshot {
return .init()
}
internal init(snapshotReference: NSDiffableDataSourceSnapshotReference, context: NSManagedObjectContext) {
self.snapshotReference = snapshotReference
self.snapshotStruct = snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
self.context = context
}
// MARK: Private
private let snapshotReference: NSDiffableDataSourceSnapshotReference
private let snapshotStruct: NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
private let context: NSManagedObjectContext?
private init() {
self.snapshotReference = .init()
self.snapshotStruct = self.snapshotReference as NSDiffableDataSourceSnapshot<NSString, NSManagedObjectID>
self.context = nil
}
}
}
@@ -407,10 +293,10 @@ extension LiveList: FetchedDiffableDataSourceSnapshotHandler {
#if canImport(Combine)
import Combine
// MARK: - LiveList: ObservableObject
// MARK: - LiveList: LiveResult
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
extension LiveList: ObservableObject {
extension LiveList: LiveResult {
// MARK: ObservableObject

165
Sources/LiveQuery.swift Normal file
View File

@@ -0,0 +1,165 @@
//
// LiveQuery.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if canImport(SwiftUI) && canImport(Combine)
import CoreData
import Combine
import SwiftUI
#warning("TODO: autoupdating doesn't work yet")
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
@propertyWrapper
public struct LiveQuery<Result: LiveResult>: DynamicProperty {
// MARK: Public
@Environment(\.dataStack)
public var dataStack: DataStack
public typealias ObjectType = Result.ObjectType
// MARK: @propertyWrapper
public fileprivate(set) var wrappedValue: Result {
get {
return self.nonMutatingWrappedValue.wrappedValue
}
set {
self.nonMutatingWrappedValue = LazyNonmutating { newValue }
}
}
public var projectedValue: Result {
return self.wrappedValue
}
// MARK: DynamicProperty
public mutating func update() {
SwiftUI.withAnimation {
let dataStack = self.dataStack
if self.set(dataStack: dataStack) {
return
}
self.wrappedValue = self.newWrappedValue(dataStack)
}
}
// MARK: FilePrivate
fileprivate let newWrappedValue: (DataStack) -> Result
fileprivate init(newWrappedValue: @escaping (DataStack) -> Result) {
self.newWrappedValue = newWrappedValue
}
// MARK: Private
private var nonMutatingWrappedValue: LazyNonmutating<Result> = .init { fatalError() }
private var currentDataStack: DataStack?
private mutating func set(dataStack: DataStack) -> Bool {
guard self.currentDataStack != dataStack else {
return false
}
self.currentDataStack = dataStack
let newWrappedValue = self.newWrappedValue
self.nonMutatingWrappedValue = LazyNonmutating<Result> {
newWrappedValue(dataStack)
}
return true
}
// MARK: - LazyNonmutating
fileprivate final class LazyNonmutating<Value> {
// MARK: FilePrivate
lazy var wrappedValue: Value = self.initializer()
init(_ initializer: @escaping () -> Value) {
self.initializer = initializer
}
// MARK: Private
private var initializer: () -> Value
}
}
#if canImport(UIKit) || canImport(AppKit)
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
extension LiveQuery {
public init<D: DynamicObject>(liveList: LiveList<D>) where Result == LiveList<D> {
self.init(
newWrappedValue: { _ in liveList }
)
}
public init<D: DynamicObject>(_ clauseChain: FetchChainBuilder<D>) where Result == LiveList<D> {
self.init(
newWrappedValue: { $0.liveList(clauseChain) }
)
}
public init<D: DynamicObject>(_ clauseChain: SectionMonitorChainBuilder<D>) where Result == LiveList<D> {
self.init(
newWrappedValue: { $0.liveList(clauseChain) }
)
}
}
#endif
#endif

46
Sources/LiveResult.swift Normal file
View File

@@ -0,0 +1,46 @@
//
// File.swift
// CoreStore iOS
//
// Copyright © 2018 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if canImport(UIKit) || canImport(AppKit)
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
// MARK: - LiveResult
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
public protocol LiveResult: ObservableObject {
associatedtype ObjectType
associatedtype SnapshotType: SnapshotResult where SnapshotType.ObjectType == Self.ObjectType
}
#endif

View File

@@ -38,7 +38,6 @@ public enum Shared {
*/
public static var logger: CoreStoreLogger = DefaultLogger()
@available(*, deprecated, message: "Call methods directly from the DataStack instead")
public static var defaultStack: DataStack {
get {

View File

@@ -0,0 +1,45 @@
//
// SnapshotResult.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if canImport(UIKit) || canImport(AppKit)
#if canImport(UIKit)
import UIKit
#elseif canImport(AppKit)
import AppKit
#endif
// MARK: - SnapshotResult
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 15.0, *)
public protocol SnapshotResult {
associatedtype ObjectType: DynamicObject
}
#endif