Merge pull request #9 from 0xLeif/develop

1.3.0
This commit is contained in:
Zach Eriksen 2021-08-06 22:03:03 -05:00 committed by GitHub
commit d410b7f4a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 6 deletions

View File

@ -8,16 +8,16 @@
import SwiftUI
public struct ObjectView<Content>: View where Content: View {
@StateObject private var object: Object = Object()
@ObservedObject private var object: Object
private var content: (Object) -> Content
private let content: (Object) -> Content
public init(
data: Any? = nil,
content: @escaping (Object) -> Content
@ViewBuilder content: @escaping (Object) -> Content
) {
self.object = Object(data)
self.content = content
self.object.consume(Object(data))
}
public var body: some View {
@ -28,8 +28,11 @@ public struct ObjectView<Content>: View where Content: View {
struct ObjectView_Previews: PreviewProvider {
static var previews: some View {
ObjectView(data: "Hello, World 👋") { object in
object.value(as: String.self).map { message in
Text(message)
if let text = object.value(as: String.self) {
Text(text)
} else {
ProgressView()
}
}
}

View File

@ -0,0 +1,41 @@
//
// ObjectView.swift
// ObjectUI
//
// Created by Leif on 5/24/21.
//
import SwiftUI
public struct StateObjectView<Content>: View where Content: View {
@StateObject private var object: Object = Object()
private let content: (Object) -> Content
public init(
@ViewBuilder content: @escaping (Object) -> Content
) {
self.content = content
}
public var body: some View {
content(object)
}
}
struct StateObjectView_Previews: PreviewProvider {
static var previews: some View {
StateObjectView { object in
if let text = object.value(as: String.self) {
Text(text)
} else {
ProgressView()
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
object.set(value: "👋")
}
}
}
}
}
}