Skip to content

Commit

Permalink
Improved ObjC support. (#57)
Browse files Browse the repository at this point in the history
feat(ios): allow ObjC consumers to access component properties
feat(ios): added `DEZ` prefix to exposed ObjC classes and `dez_` prefix to extension functions
  • Loading branch information
n8chur authored and roperzh committed Nov 14, 2019
1 parent 64a7321 commit 3618fbd
Show file tree
Hide file tree
Showing 18 changed files with 139 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
Diez(MyStateBag(), layout).attach(fun(component) {
runOnUiThread {
text.text = component.copy
text.text = component.text
text.textStyle = component.textStyle
view.backgroundImage = component.image
this.haikuView.load(component.haiku)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ViewController: UIViewController {
diez.attach { [weak self] component in
guard let self = self else { return }

self.label.text = "\(component.copy). \(component.numbers[10])"
self.label.text = "\(component.text). \(component.numbers[10])"
self.label.apply(component.textStyle)
if let image = component.image.image {
self.view.backgroundColor = UIColor(patternImage: image)
Expand Down
2 changes: 1 addition & 1 deletion examples/playground/src/MyStateBag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class MyStateBag extends Component {

private name = 'Diez';

@property copy = `Hello ${this.name}`;
@property text = `Hello ${this.name}`;

@property numbers: Integer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

Expand Down
2 changes: 1 addition & 1 deletion examples/poodle-surf/ios-objc/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ EXTERNAL SOURCES:
:path: "../ios/Diez"

SPEC CHECKSUMS:
Diez: 7a63235cd5da6d13c47bdd9a13774b2e9269428c
Diez: 1aeb11d509e979133051f7abb00520477dd8d533
lottie-ios: 3fef45d3fabe63e3c7c2eb603dd64ddfffc73062

PODFILE CHECKSUM: 1026d05550ab2f00b347bae8bf5d7c9c35968981
Expand Down
6 changes: 3 additions & 3 deletions examples/poodle-surf/ios-objc/PoodleSurfObjC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@interface ViewController ()

@property (nonatomic, nullable) DiezDesignSystem *diez;
@property (nonatomic, nullable) DEZDiezDesignSystem *diez;

@end

Expand All @@ -23,9 +23,9 @@ @implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];

self.diez = [[DiezDesignSystem alloc] initWithView:self.view];
self.diez = [[DEZDiezDesignSystem alloc] initWithView:self.view];

[self.diez attach:^(DesignSystem *component) {
[self.diez attach:^(DEZDesignSystem *component) {
NSLog(@"Received component update:\n%@", component);
}];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/targets/sources/ios/bindings/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extension Color {
/**
A `UIColor` representation of the color.
*/
public var color: UIColor {
@objc public var color: UIColor {
let brightness = l + s * min(l, 1 - l)
let saturation = (brightness == 0) ? 0 : 2 - 2 * l / brightness
return UIColor(hue: h, saturation: saturation, brightness: brightness, alpha: a)
Expand Down
2 changes: 1 addition & 1 deletion packages/targets/sources/ios/bindings/Haiku.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ extension Haiku {
- See: [File.url](x-source-tag://File.url)
*/
public var url: URL? {
@objc public var url: URL? {
return file.url
}

Expand Down
2 changes: 2 additions & 0 deletions packages/targets/sources/ios/bindings/HaikuView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
A view responsible for rendering a Haiku animation.
*/
@objc(DEZHaikuView)
public final class HaikuView: UIView {
public override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -17,6 +18,7 @@ public final class HaikuView: UIView {
/**
Loads the provided `Haiku`.
*/
@objc(loadHaiku:)
public func load(_ haiku: Haiku) {
// TODO: Add a parameter that allows a fade in animated and add a description of the parameter to doc comment.
guard let request = haiku.file.request else {
Expand Down
12 changes: 7 additions & 5 deletions packages/targets/sources/ios/bindings/Image.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extension Image {
- See: [url(forScale:)](x-source-tag://Image.urlForScale)
*/
public var url: URL? {
@objc public var url: URL? {
return url(forScale: UIScreen.main.scale)
}

Expand All @@ -15,7 +15,7 @@ extension Image {
- The @1x image asset does not exist
- The `URL` failed to resolve
*/
public var urlAt1x: URL? {
@objc public var urlAt1x: URL? {
return file1x.url
}

Expand All @@ -26,7 +26,7 @@ extension Image {
- The @2x image asset does not exist
- The `URL` failed to resolve
*/
public var urlAt2x: URL? {
@objc public var urlAt2x: URL? {
return file2x.url
}

Expand All @@ -37,7 +37,7 @@ extension Image {
- The @3x image asset does not exist
- The `URL` failed to resolve
*/
public var urlAt3x: URL? {
@objc public var urlAt3x: URL? {
return file3x.url
}

Expand All @@ -46,7 +46,7 @@ extension Image {
- See: [image(withScale:)](x-source-tag://Image.imageWithScale)
*/
public var image: UIImage? {
@objc public var image: UIImage? {
return image(withScale: UIScreen.main.scale)
}

Expand All @@ -65,6 +65,7 @@ extension Image {
- Returns: The `URL` of the image at the provided scale, or nil.
*/
@objc(urlForScale:)
public func url(forScale scale: CGFloat) -> URL? {
switch round(scale) {
case 1: return file1x.url
Expand All @@ -86,6 +87,7 @@ extension Image {
- See: [url(forScale:)](x-source-tag://Image.urlForScale)
*/
@objc(imageForScale:)
public func image(withScale scale: CGFloat) -> UIImage? {
guard let url = url(forScale: scale) else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ extension LOTAnimationView {
public typealias LoadCompletion = (Result<Void, LottieError>) -> Void

/**
- Tag: LOTAnimationView.loadLottieSessionCompletion
Loads the provided `Lottie` animation.
- Parameters:
Expand Down Expand Up @@ -98,4 +100,23 @@ extension LOTAnimationView {
DispatchQueue.main.async { completion?(.failure(.deserializationError(data, error)))}
}
}

/**
The Objective-C equivalent of load(:session:completion:).
- See: [load(:session:completion:)](x-source-tag://LOTAnimationView.loadLottieSessionCompletion)
*/
@available(swift, obsoleted: 0.0.1)
@discardableResult
@objc(dez_loadLottie:withSession:completion:)
public func load(_ lottie: Lottie, session: URLSession = .shared, completion: ((_ success: Bool, _ error: NSError?) -> Void)? = nil) -> URLSessionDataTask? {
return load(lottie, session: session) { result in
switch result {
case .success:
completion?(true, nil)
case .failure(let error):
completion?(false, error as NSError)
}
}
}
}
2 changes: 1 addition & 1 deletion packages/targets/sources/ios/bindings/Lottie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extension Lottie {
- See: [File.url](x-source-tag://File.url)
*/
public var url: URL? {
@objc public var url: URL? {
return file.url
}
}
2 changes: 1 addition & 1 deletion packages/targets/sources/ios/bindings/SVG.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extension SVG {
- See: [File.url](x-source-tag://File.url)
*/
public var url: URL? {
@objc public var url: URL? {
return file.url
}

Expand Down
2 changes: 2 additions & 0 deletions packages/targets/sources/ios/bindings/SVGView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
A view responsible for rendering an SVG.
*/
@objc(DEZSVGView)
public final class SVGView: UIView {
public override init(frame: CGRect) {
super.init(frame: frame)
Expand All @@ -18,6 +19,7 @@ public final class SVGView: UIView {
/**
Loads the provided `SVG`.
*/
@objc(loadSVG:)
public func load(_ svg: SVG) {
// TODO: Add a parameter that allows a fade in animated and add a description of the parameter to doc comment.
guard let request = svg.file.request else {
Expand Down
5 changes: 4 additions & 1 deletion packages/targets/sources/ios/bindings/TextStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extension TextStyle {
- Note: If the font fails to load this will fallback to the `UIFont.systemFont(ofSize:)`.
*/
public var font: UIFont {
@objc public var font: UIFont {
guard let font = UIFont(name: fontName, size: fontSize) else {
// TODO: Should this instead return nil? Update doc comment if this changes.
return UIFont.systemFont(ofSize: fontSize)
Expand All @@ -15,20 +15,23 @@ extension TextStyle {
}

public extension UILabel {
@objc(dez_applyTextStyle:)
func apply(_ textStyle: TextStyle) {
font = textStyle.font
textColor = textStyle.color.color
}
}

public extension UITextView {
@objc(dez_applyTextStyle:)
func apply(_ textStyle: TextStyle) {
font = textStyle.font
textColor = textStyle.color.color
}
}

public extension UITextField {
@objc(dez_applyTextStyle:)
func apply(_ textStyle: TextStyle) {
font = textStyle.font
textColor = textStyle.color.color
Expand Down
2 changes: 0 additions & 2 deletions packages/targets/sources/ios/core/Diez.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
public typealias Method = (String, Any?) -> Void

/**
- Tag: StateBag
Expand Down
8 changes: 5 additions & 3 deletions packages/targets/sources/ios/ios.component.handlebars
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
@objc(DEZ{{{componentName}}})
{{#if public}}
@objc public final class {{{componentName}}}: NSObject, StateBag {
public final class {{{componentName}}}: NSObject, StateBag {
{{else}}
public final class {{{componentName}}}: NSObject, Decodable {
{{/if}}
{{#each properties}}
public var {{@key}}: {{this.type}}
@objc public var {{@key}}: {{this.type}}
{{/each}}
{{#if hasProperties}}

Expand Down Expand Up @@ -62,7 +63,8 @@ extension {{{componentName}}}: ReflectedCustomStringConvertible {

/// This is only intended to be used by Objective-C consumers.
/// In Swift use Diez<{{{componentName}}}>.
@objc(Diez{{{componentName}}})
@available(swift, obsoleted: 0.0.1)
@objc(DEZDiez{{{componentName}}})
public final class DiezBridged{{{componentName}}}: NSObject {
@objc public init(view: UIView) {
diez = Diez(view: view)
Expand Down
Loading

0 comments on commit 3618fbd

Please sign in to comment.