Swiftpack.co - ServerDriven/ScreenDataUI-ios as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by ServerDriven.
ServerDriven/ScreenDataUI-ios 1.1.2
SwiftUI Views for ScreenData
⭐️ 6
🕓 1 year ago
iOS watchOS linux macOS iOS
.package(url: "https://github.com/ServerDriven/ScreenDataUI-ios.git", from: "1.1.2")

ScreenDataUI

ServerDriven SwiftUI Views using ScreenData

Usage

import ScreenData
import ScreenDataUI

You will need to also import ScreenDataNavigation if you are dealing with ScreenProviding or ScreenStoring.

SDScreen Example

SDScreen(
    screen: SomeScreen(
        title: "Hello, World!",
        backgroundColor: SomeColor(red: 1, green: 1, blue: 1),
        someView: SomeLabel(title: "👋", font: .largeTitle).someView
    )
)

Note: The title will only show up if there is a NavigationView

ScreenProvider Example

ScreenProvider

import ScreenDataNavigation

struct ScreenProvider: ScreenProviding {
    func screen(forID id: String) -> AnyPublisher<SomeScreen, Error> {
        Just(
            SomeScreen(
                title: "Hello, World!",
                backgroundColor: SomeColor(red: 1, green: 1, blue: 1),
                someView: SomeLabel(title: "👋 \(id)", font: .largeTitle).someView
            )
        )
        .mapError { Never -> Error in }
        .eraseToAnyPublisher()
    }
}

ScreenProviderStore

class ScreenProviderStore: ObservableObject {
    private var task: AnyCancellable?
    
    @Published var screen: SomeScreen?
    
    func fetch(screenID id: String) {
        task = ScreenProvider().screen(forID: id)
            .sink(
                receiveCompletion: { _ in },
                receiveValue:{ screen in
                    DispatchQueue.main.async {
                        self.screen = screen
                    }
                }
            )
    }
}

ScreenDataContentView

struct ScreenDataContentView: View {
    @StateObject private var store = ScreenProviderStore()
    
    public var baseID: String
    
    var body: some View {
        if let screen = store.screen {
            NavigationView {
                SDScreen(screen: screen)
            }
        } else {
            ProgressView()
                .onAppear {
                    store.fetch(screenID: baseID)
                }
        }
    }
}

Example SDCustomizedView

import SwiftUI
import ScreenData
import ScreenDataUI

struct DividerView: SDCustomizedView {
    var id: String {
        "dividerView"
    }
    
    func view(forSomeCustomView customView: SomeCustomView) -> AnyView {
        AnyView(Divider().background(with: customView.style))
    }
}

Configuration

Before showing a SDScreen you can override the ScreenDataUI default values. You can also add your own custom views that conform to SDCustomizedView.

// MARK: SDScreenProvider default

SDScreenProvider.default = ScreenProvider()

// MARK: SDScreenStore default
// FileScreenStore is from ScreenDataNavigation

SDScreenStore.default = FileScreenStore(baseKey: "example")

// MARK: SDCustomViews
// Views that conform to SDCustomizedView

SDCustomView.add(customView: DividerView())

// MARK: Default Colors
//

SDImage.defaultForegroundColor = .white

// MARK: SDButton Actions
//

SDButton.add(actionWithID: "some.id") {
    print("Button Tapped!")
}

// MARK: SDFont Values
//

SDFont.largeTitleFont = Font.custom("Thonburi", size: 34, relativeTo: .largeTitle)
SDFont.titleFont = Font.custom("Thonburi", size: 28, relativeTo: .title)
SDFont.headlingFont = Font.custom("Thonburi-Bold", size: 17, relativeTo: .headline)
SDFont.bodyFont = Font.custom("Thonburi", size: 17, relativeTo: .body)
SDFont.footnoteFont = Font.custom("Thonburi", size: 13, relativeTo: .footnote)
SDFont.captionFont = Font.custom("Futura-Bold", size: 12, relativeTo: .caption)

GitHub

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

Release Notes

1.1.2
1 year ago

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