forked from 146BC/StyleKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added handling of aliases in style.json
- Loading branch information
1 parent
aa9fedf
commit 943845a
Showing
2 changed files
with
45 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.
Sorry, something went wrong. |
||
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() { | ||
|
@@ -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.
Sorry, something went wrong.
BernardGatt
|
||
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?) { | ||
|
@@ -136,5 +169,4 @@ class Stylist { | |
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 comments
on commit 943845a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the style.json, see below:
https://gist.github.com/BernardGatt/eb9070ee52899ebf2e3a4742be349716
There was a problem hiding this comment.
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
We can use the following