Skip to content

Commit

Permalink
- Added handling of aliases in style.json
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenriolo committed Oct 19, 2016
1 parent aa9fedf commit 943845a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
40 changes: 36 additions & 4 deletions StyleKit/Stylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ class Stylist {
typealias setValueForControlState = @convention(c) (AnyObject, Selector, AnyObject, UInt) -> Void

let data: Style
let aliases: Style
let styleParser: StyleParsable
var currentComponent: AnyClass?
var viewStack = [UIAppearanceContainer.Type]()

init(data: Style, styleParser: StyleParsable?) {
self.styleParser = styleParser ?? StyleParser()
self.data = data

var tmpAlias: Style = [:]

This comment has been minimized.

Copy link
@BernardGatt

BernardGatt Oct 19, 2016

We can use the following

        var tmpAlias = Style()
        var tmpData = Style()
var tmpData: Style = [:]

for (key, value) in data {
if key.hasPrefix("@") {
tmpAlias[key] = value
} else {
tmpData[key] = value
}
}

self.data = tmpData
self.aliases = tmpAlias
}

func apply() {
Expand Down Expand Up @@ -78,14 +92,33 @@ class Stylist {
if let styles = object as? Stylist.Style {
var stylesToApply = Stylist.Style()
for (style, value) in styles {
stylesToApply[style] = styleParser.getStyle(forName: name, value: value)
stylesToApply[style] = styleParser.getStyle(forName: name, value: self.getValue(value))
}
callAppearanceSelector(selectorName, valueOne: stylesToApply as AnyObject?, valueTwo: state)
} else {
let value = styleParser.getStyle(forName: name, value: object)
let value = styleParser.getStyle(forName: name, value: self.getValue(object))
callAppearanceSelector(selectorName, valueOne: value, valueTwo: state)
}
}

/**
Checks if the value is an alias, and if it is resolves to the correct value.
Otherwise, it will return the passed value.

- Parameter value: The alias name or actual value
*/

This comment has been minimized.

Copy link
@BernardGatt

BernardGatt Oct 19, 2016

Minor refactor

    private func getValue(_ value: AnyObject) -> AnyObject {

        guard let stringValue = value as? String, stringValue.hasPrefix("@") else {
            return value
        }

        if let aliasValue = aliases[stringValue] {
            return aliasValue
        } else {
            SKLogger.error("Value declared is using Alias \(value) which has not been defined")

            return value
        }
    }
private func getValue(_ value: AnyObject) -> AnyObject {
guard value is String, value.hasPrefix("@") else {
return value
}

if let aliasValue = aliases[value as! String] {
return aliasValue
} else {
SKLogger.error("Value declared is using Alias \(value) which has not been defined")

return value
}
}

private func callAppearanceSelector(_ selector: String, valueOne: AnyObject?, valueTwo: AnyObject?) {
Expand Down Expand Up @@ -136,5 +169,4 @@ class Stylist {
}
}
}

}
14 changes: 9 additions & 5 deletions StyleKitDemo/StyleKitDemo/Style/style.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
{
"@mainFont": "HelveticaNeue-Bold:20.0",
"@primaryColor": "#000000",
"@secondaryColor": "#000FFF",
"@transperancyValue": 90,
"StyleKitDemo.SKNavigationBar": {
"tintColor": "#000",
"tintColor": "@secondaryColor",
"titleTextAttributes": {
"NSColor": "#99000000",
"NSFont": "HelveticaNeue-Light:18.0"
"NSFont": "@mainFont"
}
},
"UITableView": {
Expand All @@ -21,12 +25,12 @@
}
},
"StyleKitDemo.SKButton": {
"backgroundColor:normal": "#cd0073",
"backgroundColor:normal": "@primaryColor",
"backgroundColor:highlighted": "#8000c6",
"titleColor:normal": "#fff",
"titleColor:highlighted": "#fff",
"font": "HelveticaNeue-Light:13.0",
"transparency":70
"transparency":"@transperancyValue"
},
"UIButton": {
"font": "HelveticaNeue-Medium:13.0",
Expand Down Expand Up @@ -83,4 +87,4 @@
"UIActivityIndicatorView": {
"color": "#cd0073"
}
}
}

2 comments on commit 943845a

@BernardGatt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BernardGatt
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good man, we might need to do some refactoring to reuse the same mechanism for mixins since they are stored into aliases as well.

See:
146BC#34

Please sign in to comment.