Contents

p-x9/editvalueview

Library that makes easy to display property edit screens for SwiftUI.

Demo

| String | Bool | Int | | ---- | ---- | ---- | | [String-light] | [Bool-light] | [Int-light] |

| Double | Date | Color | | ---- | ---- | ---- | | [Double-light] | [Date-light] | [Color-light] |

| Image | UI/NSImage | | ---- | ---- | | [Image-light] | [UIImage-light] |

| Array | Dictionary | | ---- | ---- | | [Array-light] | [Dictionary-light] |

| Enum(CaseIterable) | Enum(CaseIterable & RawRepresentable) | | ---- | ---- | | [Enum(CaseIterable)-light] | [Enum(CaseIterable RawRepresentable)-light] |

| Codable | | ---- | | [Codable-light] |

Supported types

  • String
  • Bool
  • any Numerics
  • Date
  • Color/UIColor/NSColor/CGColor/CIColor
  • Image/UIImage/CGImage/CIImage (iOS Only)
  • Array(Codable)
  • Dictionary(Codable)
  • CaseIterable
  • CaseIterable & RawRepresentable
  • Codable

Usage

Note If you want to use the camera for editing images, you must add a key named NSCameraUsageDescription to the info.plist file.

SwiftUI

Initialize
  • Initialize with key and initial value

``swift var name = "" EditValueView(key: "name", value: name) .onUpdate { newValue in name = newValue } ``

  • Initialize with keyPath

``swift EditValueView(target, key: "name", keyPath: \Item.name) .onUpdate { newValue in target[keyPath: \.name] = newValue } ``

  • Initialize with binding

``swift @State var name: String = "" EditValueView(key: "name", binding: $name) ``

Update Handler

You can receive an edit callback when you press the save button.

EditValueView(target, key: "name", keyPath: \Item.name)
    .onUpdate { newValue in
        // update
    }
Input Validation

You can validate input values.

EditValueView(target, key: "name", keyPath: \Item.name)
    .validate { newValue -> Bool in
        // input validation
        return !name.isEmpty
    }

UIKit

let vc = EditValueViewController(target, key: "name", keyPath: \Item.name)
vc.onUpdate = { target, newValue in
    // update
}
vc.validate = { target, newValue -> Bool in
    // input validation
}

Protocol

When using optional types, type hints for Codable cannot be displayed when nil is used. To avoid such problems, provide a default value in accordance with the protocol named DefaultRepresentable.

struct Item: Codable {
    var name: String
    var date: Date
}

struct Message: Codable {
    var content: String
    var item: Item?
}
// Confirm to `DefaultRepresentable` protocol
extension Item: DefaultRepresentable {
    static var defaultValue: Self {
        .init(name: "name", date: Date())
     }
}
// give default value
EditValueView(target, key: "item", keyPath: \Message.item, defaultValue: .init(name: "name", date: Date()))

License

EditValueView is released under the MIT License. See LICENSE

Package Metadata

Repository: p-x9/editvalueview

Default branch: main

README: README.md