commit
d410b7f4a3
|
@ -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()
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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: "👋")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue