Merge pull request #8 from 0xLeif/feature/StateObjectView
Feature/state object view
This commit is contained in:
commit
20ca367e3c
|
@ -8,16 +8,16 @@
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
public struct ObjectView<Content>: View where Content: View {
|
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(
|
public init(
|
||||||
data: Any? = nil,
|
data: Any? = nil,
|
||||||
content: @escaping (Object) -> Content
|
@ViewBuilder content: @escaping (Object) -> Content
|
||||||
) {
|
) {
|
||||||
|
self.object = Object(data)
|
||||||
self.content = content
|
self.content = content
|
||||||
self.object.consume(Object(data))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public var body: some View {
|
public var body: some View {
|
||||||
|
@ -28,8 +28,11 @@ public struct ObjectView<Content>: View where Content: View {
|
||||||
struct ObjectView_Previews: PreviewProvider {
|
struct ObjectView_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
ObjectView(data: "Hello, World 👋") { object in
|
ObjectView(data: "Hello, World 👋") { object in
|
||||||
object.value(as: String.self).map { message in
|
if let text = object.value(as: String.self) {
|
||||||
Text(message)
|
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