Swiftpack.co - p-x9/EditValueView as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by p-x9.
p-x9/EditValueView 0.7.0
Library that makes easy to display property edit screens for SwiftUI.
⭐️ 15
🕓 3 weeks ago
iOS macOS
.package(url: "https://github.com/p-x9/EditValueView.git", from: "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

GitHub

link
Stars: 15
Last commit: 3 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

v0.7.0
3 weeks ago

What's Changed

Full Changelog: https://github.com/p-x9/EditValueView/compare/0.6.0...0.7.0

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