Prototyping SwiftUI utilities

This commit is contained in:
John Estropia
2021-02-16 09:12:36 +09:00
parent edd8ba55d8
commit f7471f56a4
27 changed files with 853 additions and 227 deletions

View File

@@ -33,7 +33,7 @@ import CoreData
Objective-C Foundation types that are natively supported by Core Data managed attributes all conform to `CoreDataNativeType`.
*/
@objc
public protocol CoreDataNativeType: AnyObject, NSObjectProtocol {}
public protocol CoreDataNativeType: NSObjectProtocol {}
// MARK: - NSNumber

View File

@@ -0,0 +1,56 @@
//
// ForEach+SwiftUI.swift
// CoreStore
//
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - ForEach
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
extension ForEach where Content: View {
// MARK: Public
public init<O: DynamicObject>(
_ listSnapshot: Data,
@ViewBuilder content: @escaping (ObjectPublisher<O>) -> Content
) where Data == LiveList<O>.Items, ID == O.ObjectID {
self.init(listSnapshot, id: \.cs_objectID, content: content)
}
public init<O: DynamicObject>(
_ objectPublishers: Data,
@ViewBuilder content: @escaping (ObjectPublisher<O>) -> Content
) where Data.Element == ObjectPublisher<O>, ID == O.ObjectID {
self.init(objectPublishers, id: \.cs_objectID, content: content)
}
}
#endif

View File

@@ -0,0 +1,59 @@
//
// ForEachSection.swift
// CoreStore
//
// Created by John Rommel Estropia on 2021/02/13.
// Copyright © 2021 John Rommel Estropia. All rights reserved.
//
#if canImport(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - ForEachSection
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct ForEachSection<Object: DynamicObject, Sections: View>: View {
// MARK: Internal
public init(
in listSnapshot: LiveList<Object>.Items,
@ViewBuilder sections: @escaping (
_ sectionID: ListSnapshot<Object>.SectionID,
_ objects: [ObjectPublisher<Object>]
) -> Sections
) {
self.listSnapshot = listSnapshot
self.sections = sections
}
// MARK: View
public var body: some View {
ForEach(self.listSnapshot.sectionIDs, id: \.self) { sectionID in
self.sections(
sectionID,
self.listSnapshot.items(inSectionWithID: sectionID)
)
}
}
// MARK: Private
private let listSnapshot: LiveList<Object>.Items
private let sections: (
_ sectionID: ListSnapshot<Object>.SectionID,
_ objects: [ObjectPublisher<Object>]
) -> Sections
}
#endif

View File

@@ -201,6 +201,14 @@ public final class ListPublisher<O: DynamicObject>: Hashable {
}
)
}
/**
Used internally by CoreStore. Do not call directly.
*/
public func cs_dataStack() -> DataStack? {
return self.context.parentStack
}
// MARK: Public (3rd Party Utilities)

78
Sources/ListReader.swift Normal file
View File

@@ -0,0 +1,78 @@
//
// ListReader.swift
// CoreStore
//
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - ListReader
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct ListReader<Object: DynamicObject, Content: View, Value>: View {
// MARK: Internal
public init(
_ listPublisher: ListPublisher<Object>,
@ViewBuilder content: @escaping (Value) -> Content
) where Value == LiveList<Object>.Items {
self._list = .init(listPublisher)
self.content = content
self.keyPath = \.self
}
public init(
_ listPublisher: ListPublisher<Object>,
keyPath: KeyPath<LiveList<Object>.Items, Value>,
@ViewBuilder content: @escaping (Value) -> Content
) {
self._list = .init(listPublisher)
self.content = content
self.keyPath = keyPath
}
// MARK: View
public var body: some View {
self.content(self.list[keyPath: self.keyPath])
}
// MARK: Private
@LiveList
private var list: LiveList<Object>.Items
private let content: (Value) -> Content
private let keyPath: KeyPath<LiveList<Object>.Items, Value>
}
#endif

162
Sources/LiveList.swift Normal file
View File

@@ -0,0 +1,162 @@
//
// LiveList.swift
// CoreStore
//
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - LiveList
@propertyWrapper
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct LiveList<Object: DynamicObject>: DynamicProperty {
// MARK: Public
public typealias Items = ListSnapshot<Object>
public init(
_ listPublisher: ListPublisher<Object>
) {
self.observer = .init(listPublisher: listPublisher)
}
public init(
_ from: From<Object>,
_ fetchClauses: FetchClause...,
in dataStack: DataStack
) {
self.init(from, fetchClauses, in: dataStack)
}
public init(
_ from: From<Object>,
_ fetchClauses: [FetchClause],
in dataStack: DataStack
) {
self.init(dataStack.publishList(from, fetchClauses))
}
public init<B: FetchChainableBuilderType>(
_ clauseChain: B,
in dataStack: DataStack
) where B.ObjectType == Object {
self.init(dataStack.publishList(clauseChain))
}
public init(
_ from: From<Object>,
_ sectionBy: SectionBy<Object>,
_ fetchClauses: FetchClause...,
in dataStack: DataStack
) {
self.init(from, sectionBy, fetchClauses, in: dataStack)
}
public init(
_ from: From<Object>,
_ sectionBy: SectionBy<Object>,
_ fetchClauses: [FetchClause],
in dataStack: DataStack
) {
self.init(dataStack.publishList(from, sectionBy, fetchClauses))
}
public init<B: SectionMonitorBuilderType>(
_ clauseChain: B,
in dataStack: DataStack
) where B.ObjectType == Object {
self.init(dataStack.publishList(clauseChain))
}
// MARK: @propertyWrapper
public var wrappedValue: Items {
return self.observer.items
}
public var projectedValue: ListPublisher<Object> {
return self.observer.listPublisher
}
// MARK: DynamicProperty
public mutating func update() {
self._observer.update()
}
// MARK: Private
@ObservedObject
private var observer: Observer
// MARK: - Observer
private final class Observer: ObservableObject {
@Published
var items: Items
let listPublisher: ListPublisher<Object>
init(listPublisher: ListPublisher<Object>) {
self.listPublisher = listPublisher
self.items = listPublisher.snapshot
listPublisher.addObserver(self) { [weak self] (listPublisher) in
guard let self = self else {
return
}
self.items = listPublisher.snapshot
}
}
deinit {
self.listPublisher.removeObserver(self)
}
}
}
#endif

110
Sources/LiveObject.swift Normal file
View File

@@ -0,0 +1,110 @@
//
// LiveObject.swift
// CoreStore
//
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - LiveObject
@propertyWrapper
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct LiveObject<O: DynamicObject>: DynamicProperty {
// MARK: Public
public typealias Item = ObjectSnapshot<O>
public init(_ objectPublisher: ObjectPublisher<O>?) {
self.observer = .init(objectPublisher: objectPublisher)
}
// MARK: @propertyWrapper
public var wrappedValue: Item? {
return self.observer.item
}
// MARK: DynamicProperty
public mutating func update() {
self._observer.update()
}
// MARK: Private
@ObservedObject
private var observer: Observer
// MARK: - Observer
private final class Observer: ObservableObject {
@Published
var item: Item?
let objectPublisher: ObjectPublisher<O>?
init(objectPublisher: ObjectPublisher<O>?) {
guard
let dataStack = objectPublisher?.cs_dataStack(),
let objectPublisher = objectPublisher?.asPublisher(in: dataStack)
else {
self.objectPublisher = objectPublisher
self.item = nil
return
}
self.objectPublisher = objectPublisher
self.item = objectPublisher.snapshot
objectPublisher.addObserver(self) { [weak self] (objectPublisher) in
guard let self = self else {
return
}
self.item = objectPublisher.snapshot
}
}
deinit {
self.objectPublisher?.removeObserver(self)
}
}
}
#endif

View File

@@ -202,6 +202,11 @@ public final class ObjectPublisher<O: DynamicObject>: ObjectRepresentation, Hash
// MARK: Internal
internal var cs_objectID: O.ObjectID {
return self.objectID()
}
internal static func createUncached(objectID: O.ObjectID, context: NSManagedObjectContext) -> ObjectPublisher<O> {

View File

@@ -1,9 +1,26 @@
//
// ListState.swift
// ObjectReader.swift
// CoreStore
//
// Created by John Rommel Estropia on 2020/12/26.
// Copyright © 2020 John Rommel Estropia. All rights reserved.
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
@@ -15,40 +32,29 @@ import SwiftUI
// MARK: - ObjectReader
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct ObjectReader<Object: DynamicObject, Content: View, Placeholder: View>: View {
public struct ObjectReader<Object: DynamicObject, Content: View, Value>: View {
// MARK: Internal
public init(
_ objectPublisher: ObjectPublisher<Object>?,
@ViewBuilder content: @escaping (ObjectSnapshot<Object>) -> Content,
@ViewBuilder placeholder: @escaping () -> Placeholder
) {
@ViewBuilder content: @escaping (Value) -> Content
) where Value == LiveObject<Object>.Item {
self.objectPublisher = .init(
objectPublisher.flatMap {
guard let dataStack = $0.cs_dataStack() else {
return nil
}
return $0.asPublisher(in: dataStack)
}
)
self._object = .init(objectPublisher)
self.content = content
self.placeholder = placeholder
self.keyPath = \.self
}
public init(
_ objectPublisher: ObjectPublisher<Object>?,
@ViewBuilder content: @escaping (ObjectSnapshot<Object>) -> Content
) where Placeholder == EmptyView {
keyPath: KeyPath<LiveObject<Object>.Item, Value>,
@ViewBuilder content: @escaping (Value) -> Content
) {
self.init(
objectPublisher,
content: content,
placeholder: EmptyView.init
)
self._object = .init(objectPublisher)
self.content = content
self.keyPath = keyPath
}
@@ -56,45 +62,20 @@ public struct ObjectReader<Object: DynamicObject, Content: View, Placeholder: Vi
public var body: some View {
if let snapshot = self.objectPublisher.wrappedValue?.snapshot {
if let object = self.object {
self.content(snapshot)
}
else {
self.placeholder()
self.content(object[keyPath: self.keyPath])
}
}
// MARK: Private
@ObservedObject
private var objectPublisher: OptionalObservedObject<ObjectPublisher<Object>>
@LiveObject
private var object: LiveObject<Object>.Item?
private let content: (ObjectSnapshot<Object>) -> Content
private let placeholder: () -> Placeholder
// MARK: - OptionalObservedObject
fileprivate final class OptionalObservedObject<T: ObservableObject>: ObservableObject where ObservableObjectPublisher == T.ObjectWillChangePublisher {
// MARK: Internal
let wrappedValue: T?
init(_ wrappedValue: T?) {
self.wrappedValue = wrappedValue
self.objectWillChange = wrappedValue.map(\.objectWillChange) ?? .init()
}
// MARK: ObservableObject
let objectWillChange: ObservableObjectPublisher
}
private let content: (Value) -> Content
private let keyPath: KeyPath<LiveObject<Object>.Item, Value>
}
#endif

View File

@@ -0,0 +1,65 @@
//
// SectionsReader.swift
// CoreStore
//
// Copyright © 2021 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(Combine) && canImport(SwiftUI)
import Combine
import SwiftUI
// MARK: - SectionsReader
@available(iOS 13.0, tvOS 13.0, watchOS 6.0, macOS 10.15, *)
public struct SectionsReader<Object: DynamicObject, Content: View>: View {
// MARK: Internal
public init(
_ listPublisher: ListPublisher<Object>,
@ViewBuilder content: @escaping (LiveList<Object>.Items) -> Content
) {
self._list = .init(listPublisher)
self.content = content
}
// MARK: View
public var body: some View {
self.content(self.list)
}
// MARK: Private
@LiveList
private var list: LiveList<Object>.Items
private let content: (LiveList<Object>.Items) -> Content
}
#endif