Swiftpack.co - 0xLeif/ObjectUI as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by 0xLeif.
0xLeif/ObjectUI 1.3.0
Create SwiftUI Views with any data; Prefer using https://github.com/0xOpenBytes/CacheStore
⭐️ 20
🕓 2 years ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/0xLeif/ObjectUI.git", from: "1.3.0")

Prefer CacheStore

ObjectUI

Create SwiftUI Views with any data

Usage

import ObjectUI

Basic Examples

ObjectView

struct ContentView: View {
    var body: some View {
        ObjectView(data: "Hello, World 👋") { object in
            if let text = object.value(as: String.self) {
                Text(text)
            } else {
                ProgressView()
            }
        }
    }
}

StateObjectView

struct ContentView: View {
    var body: 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: "👋")
                        }
                    }
            }
        }
    }
}

JSON Example

struct ContentView: View {
    let json = """
        {
          "userId": 1,
          "id": 2,
          "title": "quis ut nam facilis et officia qui",
          "completed": false
        }
        """
        .data(using: .utf8)
    
    var body: some View {
        ObjectView(data: json) { object in
            VStack {
                object.userId.value(as: Int.self).map { userId in
                    Text("\(userId)")
                }
                
                object.id.value(as: Int.self).map { id in
                    Text("\(id)")
                }
                
                object.title.value(as: String.self).map { title in
                    Text(title)
                }
                
                object.completed.value(as: Bool.self).map { completed in
                    Text(completed.description)
                }
            }
        }
    }
}

GitHub

link
Stars: 20
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

1.3.0
2 years ago

1.3.0

1.2.0

1.1.0

1.0.1

1.0.0

Create SwiftUI Views with any data

Usage

import ObjectUI

Basic Example

struct ContentView: View {
    var body: some View {
        ObjectView { object in
            VStack {
                if let value = object.value(as: String.self) {
                    Text(value)
                        .font(.largeTitle)
                } else {
                    Text("Waiting...")
                }
                
                Button("Wave") {
                    DispatchQueue.main.async {
                        object.set(value: "👋")
                    }
                }
            }
        }
    }
}

JSON Example

struct ContentView: View {
    let json = """
        {
          "userId": 1,
          "id": 2,
          "title": "quis ut nam facilis et officia qui",
          "completed": false
        }
        """
        .data(using: .utf8)
    
    var body: some View {
        ObjectView(data: json) { object in
            VStack {
                object.userId.value(as: Int.self).map { userId in
                    Text("\(userId)")
                }
                
                object.id.value(as: Int.self).map { id in
                    Text("\(id)")
                }
                
                object.title.value(as: String.self).map { title in
                    Text(title)
                }
                
                object.completed.value(as: Bool.self).map { completed in
                    Text(completed.description)
                }
            }
        }
    }
}

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics