Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWDG committed Jun 6, 2024
1 parent ef6a729 commit 0b43dd3
Show file tree
Hide file tree
Showing 30 changed files with 533 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ fastlane/test_output

### Code Injection
iOSInjectionProject/

### Generated docs
docs/
20 changes: 16 additions & 4 deletions Sources/DynamicUI/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,18 @@ import Foundation
/// let someOptionalCodable: AnyCodable?
/// }
/// ```
enum AnyCodable {
public enum AnyCodable {
/// String value
case string(String)
/// Integer value
case int(Int)
/// Data value
case data(Data)
/// Double value
case double(Double)
/// Boolean value
case bool(Bool)
/// No value
case none

enum AnyCodableError: Error {
Expand Down Expand Up @@ -87,7 +93,7 @@ extension AnyCodable {
/// Check if value is nil
/// - Returns: nil if value is none/empty
public func isNil() -> Bool {
if case let .none = self {
if case .none = self {
return true
}

Expand All @@ -100,7 +106,10 @@ extension AnyCodable: Codable, Hashable {
case string, int, data, double, bool
}

init(from decoder: Decoder) throws {
/// Decode the values
///
/// - Parameter decoder:
public init(from decoder: Decoder) throws {
if let int = try? decoder.singleValueContainer().decode(Int.self) {
self = .int(int)
return
Expand Down Expand Up @@ -131,7 +140,10 @@ extension AnyCodable: Codable, Hashable {
self = .none
}

func encode(to encoder: Encoder) throws {
/// Encode the values
///
/// - Parameter encoder: Encoder
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .string(let value):
Expand Down
25 changes: 17 additions & 8 deletions Sources/DynamicUI/DynamicUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,22 @@ import SwiftUI
/// DynamicUI
///
/// DynamicUI is a SwiftUI View that can be used to display an interface based on JSON.
/// - Parameter json: JSON Data
/// - Returns: A SwiftUI View
public func DynamicUI(json: Data?) -> some View {
// swiftlint:disable:previous identifier_name
return AnyView(
InternalDynamicUI(json: json)
)
public struct DynamicUI: View {
/// JSON data
public var json: Data?

/// Initialize DynamicUI
/// - Parameter json: JSON data
public init(json: Data? = nil) {
self.json = json
}

/// Generated body for SwiftUI
public var body: some View {
AnyView(
InternalDynamicUI(json: json)
)
}
}

/// InternalDynamicUI (internal)
Expand All @@ -38,7 +47,7 @@ struct InternalDynamicUI: View {
/// This state is used to store the error message
private var error: String?

/// Init
/// Initialize the InternalDynamicUI
var body: some View {
VStack {
if let layout = layout {
Expand Down
24 changes: 12 additions & 12 deletions Sources/DynamicUI/UIComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@ import SwiftUI
/// This struct constructs a UI Component from JSON.
public struct UIComponent: Codable, Hashable {
/// Type of component
let type: String
public let type: String

/// Text within component
let text: String?
public let text: String?

/// Title of component
let title: String?
public let title: String?

/// Default value of component
let defaultValue: AnyCodable?
public let defaultValue: AnyCodable?

/// Styling of components (not yet used)
let styling: [[String: AnyCodable]]?
public let styling: [[String: AnyCodable]]?

/// Parameters of component (not yet used)
let parameters: [[String: AnyCodable]]?
public let parameters: [[String: AnyCodable]]?

/// Image URL
let imageURL: String?
public let imageURL: String?

/// Children (used in VStack, HStack, ZStack)
let children: [UIComponent]?
public let children: [UIComponent]?

/// Minumum value description
let minumum: String?
public let minumum: String?

/// Minumum value
let minimumValue: Double?
public let minimumValue: Double?

/// Maximum value description
let maximum: String?
public let maximum: String?

/// Maximum value
let maximumValue: Double?
public let maximumValue: Double?
}
17 changes: 15 additions & 2 deletions Sources/DynamicUI/Views/DynamicDisclosureGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@

import SwiftUI

/// DynamicUI: DynamicDisclosureGroup
/// DynamicUI: DisclosureGroup
///
/// DynamicDisclosureGroup is a SwiftUI View that can be used to display an DisclosureGroup.
///
/// JSON Example:
/// ```json
/// {
/// "type": "DisclosureGroup",
/// "children": [ ]
/// }
/// ```
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicDisclosureGroup: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
Expand All @@ -20,11 +32,12 @@ public struct DynamicDisclosureGroup: View {
/// The component to display
private let component: UIComponent

/// Init
/// Initialize the DynamicDisclosureGroup
public init(_ component: UIComponent) {
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
DisclosureGroup("\(component.title ?? "")") {
if let children = component.children {
Expand Down
15 changes: 14 additions & 1 deletion Sources/DynamicUI/Views/DynamicDivider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,19 @@

import SwiftUI

/// DynamicUI: DynamicDivider
/// DynamicUI: Divider
///
/// DynamicDivider is a SwiftUI View that can be used to display an Divider.
///
/// JSON Example:
/// ```json
/// {
/// "type": "Divider"
/// }
/// ```
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicDivider: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
Expand All @@ -20,10 +31,12 @@ public struct DynamicDivider: View {
/// The component to display
private let component: UIComponent

/// Initialize the DynamicDivider
init(_ component: UIComponent) {
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
Divider()
}
Expand Down
16 changes: 15 additions & 1 deletion Sources/DynamicUI/Views/DynamicForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,20 @@

import SwiftUI

/// DynamicUI: DynamicForm
/// DynamicUI: Form
///
/// DynamicForm is a SwiftUI View that can be used to display an Form.
///
/// JSON Example:
/// ```json
/// {
/// "type": "Form",
/// "children": [ ]
/// }
/// ```
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicForm: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
Expand All @@ -20,10 +32,12 @@ public struct DynamicForm: View {
/// The component to display
private let component: UIComponent

/// Initialize the DynamicForm
init(_ component: UIComponent) {
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
Form {
if let children = component.children {
Expand Down
23 changes: 20 additions & 3 deletions Sources/DynamicUI/Views/DynamicGauge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,24 @@

import SwiftUI

/// DynamicUI: DynamicGauge
/// DynamicGauge is a SwiftUI View that can be used to display an Gauge.
struct DynamicGauge: View {
/// DynamicUI: Gauge
///
/// ⚠ DynamicGauge is a SwiftUI View that can be used to display an Gauge.
///
/// JSON Example:
/// ```json
/// {
/// "type": "Gauge",
/// "title": "Title",
/// "defaultValue": 0.5
/// }
/// ```
///
/// - Warning: This component is not finished yet.
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicGauge: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
private var dynamicUIEnvironment
Expand All @@ -24,11 +39,13 @@ struct DynamicGauge: View {
/// The state of the Gauge
private var state: Double

/// Initialize the DynamicGauge
init(_ component: UIComponent) {
self.state = component.defaultValue?.toDouble() ?? 0
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
if #available(macOS 13.0, *) {
Gauge(value: state) {
Expand Down
18 changes: 16 additions & 2 deletions Sources/DynamicUI/Views/DynamicGroupBox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@

import SwiftUI

/// DynamicUI: DynamicGroupBox
/// DynamicUI: GroupBox
///
/// DynamicGroupBox is a SwiftUI View that can be used to display an GroupBox.
struct DynamicGroupBox: View {
///
/// JSON Example:
/// ```json
/// {
/// "type": "GroupBox",
/// "children": [ ]
/// }
/// ```
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicGroupBox: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
private var dynamicUIEnvironment

/// The component to display
private let component: UIComponent

/// Initialize the DynamicGroupBox
init(_ component: UIComponent) {
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
GroupBox {
if let children = component.children {
Expand Down
17 changes: 15 additions & 2 deletions Sources/DynamicUI/Views/DynamicHSplitView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@

import SwiftUI

/// DynamicUI: DynamicHSplitView
/// DynamicUI: HSplitView
///
/// DynamicHSplitView is a SwiftUI View that can be used to display an HSplitView.
struct DynamicHSplitView: View {
/// JSON Example:
/// ```json
/// {
/// "type": "HSplitView",
/// "children": [ ]
/// }
/// ```
///
/// - Note: This is a internal view, you should not use this directly. \
/// Use ``DynamicUI`` instead. this function is public to generate documentation.
public struct DynamicHSplitView: View {
@Environment(\.internalDynamicUIEnvironment)
/// Internal: dynamicUIEnvironment
private var dynamicUIEnvironment

/// The component to display
private let component: UIComponent

/// Initialize the DynamicHSplitView
init(_ component: UIComponent) {
self.component = component
}

/// Generated body for SwiftUI
public var body: some View {
HSplitView {
if let children = component.children {
Expand Down
Loading

0 comments on commit 0b43dd3

Please sign in to comment.