Skip to content

sookim-1/k-sdui-ios

Repository files navigation

MIT Contributions Welcome

k-sdui-ios is a library that helps you easily implement Server-Driven UI in SwiftUI. Renders SwiftUI views by decoding JSON into view components based on their types.

logo

⚡️ Features

  • CommonComponent : Defines shared SwiftUI view modifiers
  • TextComponent : Renders as SwiftUI Text
  • ButtonComponent : Renders as SwiftUI Button
  • ImageComponent : Renders as SwiftUI Image or AsyncImage
  • SpacerComponent : Renders as SwiftUI Spacer
  • RectangleComponent : Renders as SwiftUI Rectangle
  • RoundedRectangleComponent : Renders as SwiftUI RoundedRectangle
  • ScrollComponent : Renders as SwiftUI ScrollView
  • CustomComponent : Renders as SwiftUI EmptyView
  • Layout : Renders as SwiftUI HStack, VStack, ZStack, LazyHStack, LazyVStack

🌈 Quick

This code demonstrates how to load and inspect the structure of each JSON file located in the Example directory.

SampleGif

👷‍♂️ Basic Flow

  1. Decode SDUIScene
  2. Define layout using SDUISceneSDUIContainerSDUILayout and include an array of SDUIViews
  3. Use the render function to convert into SwiftUI Views

Scene

public struct SDUIScene: Codable, Identifiable {

    public let id = UUID()
    public var hasNavigationBar: Bool
    public var container: SDUIContainer
    
    ...
    
  • hasNavigationBar: Indicates whether to show a navigation bar
  • container: Top-level container view

Layout

public struct SDUILayout: Codable {
    public var type: String
    public var spacing: CGFloat?
    public var alignment: String

    ...
  • type: Renders HStack, VStack, or ZStack based on h, v, or z. For lh or lv, it renders LazyHStack or LazyVStack
  • spacing: Spacing between views
  • alignment: Alignment direction

🧱 Component Types

CommonComponent : Defines shared SwiftUI view modifiers

All components conform to this protocol.

    public protocol CommonComponent: Codable {
        var componentId: String { get }
        var padding: [PaddingComponent]? { get }
        var frame: FrameComponent? { get }
        var extreamFrame: ExtreamFrameComponent? { get }
        var foregroundColor: String? { get }
        var backgroundColor: String? { get }
        var cornerRadius: CGFloat? { get }
        var overlay: SDUIView? { get }
    }
  • componentId: Unique identifier for the component
  • padding: Applies padding with specified edges and spacing
  • frame: Applies width/height-style frame modifiers
  • extreamFrame: Applies minWidth/minHeight-style frame modifiers
  • foregroundColor: Applies foregroundStyle modifier
  • backgroundColor: Applies background modifier
  • cornerRadius: Applies corner radius
  • overlay: Applies overlay modifier

TextComponent : Renders as SwiftUI Text

public struct TextComponent: CommonComponent {

    ... CommonComponent 
    
    public let text: String
    public let font: FontComponent?
    public let lineLimit: Int?
    
    ...
  • text: The string to display
  • font: Applies font modifiers
  • lineLimit: Maximum number of lines

ButtonComponent : Renders as SwiftUI Button

public struct ButtonComponent: CommonComponent {

    ... CommonComponent

    public let text: String
    public let action: ActionComponent?
    public let customViews: SDUIView?
    
    ...

  • text: Used when rendering a simple text-based button
  • action: Action handler if defined
  • customViews: Used for custom button content

ImageComponent : Renders as SwiftUI Image or AsyncImage

public struct ImageComponent: CommonComponent {

    ... CommonComponent

    public let imageURL: String
    
    ...

  • imageURL: If it’s a valid URL, renders with AsyncImage. Otherwise, uses asset name to render Image

SpacerComponent : Renders as SwiftUI Spacer

public struct SpacerComponent: CommonComponent {

    ... CommonComponent
    

RectangleComponent : Renders as SwiftUI Rectangle

public struct RectangleComponent: CommonComponent {

    ... CommonComponent
    

RoundedRectangleComponent : Renders as SwiftUI RoundedRectangle

public struct RoundedRectangleComponent: CommonComponent {

    ... CommonComponent
    
    public let strokeComponent: StrokeComponent?
    
    ...
  • strokeComponent: Applies stroke color and line width

ScrollComponent : Renders as SwiftUI ScrollView

public struct ScrollComponent: CommonComponent {

    ... CommonComponent
    
    public let axis: String
    public let showIndicator: Bool
    public let containerViews: SDUIView
    
    ...

  • axis: Scroll direction (horizontal or vertical)
  • showIndicator: Whether to show scroll indicators
  • containerViews: Views contained within the scroll view

CustomComponent : Renders as SwiftUI EmptyView

public struct CustomComponent: CommonComponent {

    ... CommonComponent
    

SDUIConatiner : A container view for nested layout and views

public struct SDUIContainer: CommonComponent {

    ... CommonComponent
    
    public var layout: SDUILayout
    public var views: [SDUIView]
    
    ...
  • layout: Defines layout structure
  • views: Array of views inside the layout

About

SDUI make it easy to implement Server Driven UI pattern on iOS

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages