Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use property wrappers in templates #748

Open
djbe opened this issue Jul 9, 2020 · 0 comments
Open

Use property wrappers in templates #748

djbe opened this issue Jul 9, 2020 · 0 comments

Comments

@djbe
Copy link
Member

djbe commented Jul 9, 2020

Needed some help from @IanKeen to get it working, we got to this:

protocol AssetLoadable {
  associatedtype Asset
  static func loadAsset(named name: String) -> Asset
}
@propertyWrapper
struct AssetCatalogEntry<T: AssetLoadable> where T == T.Asset {
  let name: String
  var wrappedValue: T.Asset {
    T.loadAsset(named: name)
  }
  var projectedValue: AssetCatalogEntry<T> {
    self
  }
}
extension NSImage: AssetLoadable {
  static func loadAsset(named name: String) -> NSImage {
    guard let result = Bundle.main.image(forResource: name) else {
      preconditionFailure("Unable to load image asset '\(name)'")
    }
    return result
  }
}
extension NSDataAsset: AssetLoadable {
  static func loadAsset(named name: String) -> NSDataAsset {
    guard let result = NSDataAsset(name: name, bundle: .main) else {
      preconditionFailure("Unable to load data asset '\(name)'")
    }
    return result
  }
}

enum Asset {
  @AssetCatalogEntry(name: "Foo") static var foo: NSImage
  @AssetCatalogEntry(name: "Bar") static var bar: NSDataAsset
}

Users can then access an asset directly using Asset.foo, and if they want other properties such as the name, they can still get it using Asset.$foo.name.

First question

Do we add a new swift5.1 template, or somehow add it to the existing swift5 templates based on swift version? It would be a breaking change for all users, so probably best to go with swift5.1?

Second question

To which parsers/templates do we add these?

  • Colors: swift5, not the literals template
  • Fonts: NO as a font still needs a size. We can use a closure as a wrappedValue, but it'd be (CGFloat) -> Font, where the parameter is unclear.
  • IB: maybe for storyboard scenes? Instead of using instantiate().
  • XCAssets: will need some tweaks for color caching and other perf. related things we do now in the template

Once we decide on these topics, I'll open a dedicated issue for each template to discuss the implementation, before creating a PR.

References #603 (inconvenient access to assets) and #605 (PR for that issue).

@djbe djbe added this to the 6.4.0 milestone Jul 9, 2020
@djbe djbe modified the milestones: 6.5.0, Next minor (new features) Oct 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant