EditValueView

0.7.0

Library that makes easy to display property edit screens for SwiftUI.
p-x9/EditValueView

What's New

v0.7.0

2024-03-31T16:20:24Z

What's Changed

  • Improve type description view by @p-x9 in #31

Full Changelog: 0.6.0...0.7.0

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
    var name = ""
    EditValueView(key: "name", value: name)
      .onUpdate { newValue in
          name = newValue
      }
  • Initialize with keyPath
    EditValueView(target, key: "name", keyPath: \Item.name)
      .onUpdate { newValue in
          target[keyPath: \.name] = newValue
      }
  • Initialize with binding
    @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

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Sat Nov 02 2024 06:33:29 GMT-0900 (Hawaii-Aleutian Daylight Time)