Skip to content

Commit

Permalink
Added support for constant values to allow reusability in the stylesheet
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslt89 committed Jun 10, 2019
1 parent b5dfdca commit 439b510
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
21 changes: 18 additions & 3 deletions Stylish/JSONStylesheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,32 @@ public class JSONStylesheet: Stylesheet {
/// - Throws: An NSError with a description of what went wrong
public init(data: Data, usingPropertyStylerTypes propertyStylerTypes: [AnyPropertyStylerType.Type] = Stylish.builtInPropertyStylerTypes, ignoringUnrecognizedStyleProperties: Bool = false) throws {
let propertyStylerTypes = Dictionary(propertyStylerTypes.map{(($0.wrapped as! AnyPropertyStylerTypeWrapper).propertyKey, $0.wrapped as! AnyPropertyStylerTypeWrapper)}, uniquingKeysWith: { $1 })
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any], let styles = json["styles"] as? [[String: Any]] else {
guard let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
let styles = json["styles"] as? [[String: Any]] else {
throw NSError(domain: "Stylish", code: 100, userInfo: [NSLocalizedDescriptionKey: "Stylesheet JSON was not in the correct format. Please ensure the stylesheet JSON is a top-level dictionary that includes a \"styles\" key with a corresponding value that is an array of style dictionary objects. Each style dictionary object should minimally have a \"styleName\" key with a string value, and \"styleProperties\" key with dictionary value containing property name keys and corresponding values to set"])
}
let keyValues: [(String, Style)] = try styles.compactMap {
jsonStyle in
guard let styleName = jsonStyle["styleName"] as? String, let properties = jsonStyle["styleProperties"] as? [String: Any] else {
guard let styleName = jsonStyle["styleName"] as? String,
let properties = jsonStyle["styleProperties"] as? [String: Any] else {
throw NSError(domain: "Stylish", code: 101, userInfo: [NSLocalizedDescriptionKey: "Stylesheet JSON was not in the correct format. Please ensure the stylesheet JSON is a top-level dictionary that includes a \"styles\" key with a corresponding value that is an array of style dictionary objects. Each style dictionary object should minimally have a \"styleName\" key with a string value, and \"styleProperties\" key with dictionary value containing property name keys and corresponding values to set"])
}
let propertyStylers: [AnyPropertyStyler] = try properties.compactMap {
key, value in
guard let styler = propertyStylerTypes[key]?.propertyStyler(jsonPropertyName: key, jsonPropertyValue: value) else {

var processedValue = value
if let stringValue = value as? String, stringValue.first == "$" {
guard let constants = json["constants"] as? [String: Any],
let constantValue = constants[String(stringValue.dropFirst())] else {
if ignoringUnrecognizedStyleProperties {
return nil
}
throw NSError(domain: "Stylish", code: 104, userInfo: [NSLocalizedDescriptionKey: "None of the provided PropertyStylers could parse the json style property \"\(key)\" with the constant value: \(stringValue.dropFirst())"])
}
processedValue = constantValue
}

guard let styler = propertyStylerTypes[key]?.propertyStyler(jsonPropertyName: key, jsonPropertyValue: processedValue) else {
if ignoringUnrecognizedStyleProperties {
return nil
}
Expand Down
9 changes: 6 additions & 3 deletions StylishExample/StylishExample/stylesheet.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"constants": {
"DefaultButtonColor": "#FDE3A7"
},
"styles": [
{
"styleName": "PrimaryBackgroundColor",
Expand Down Expand Up @@ -38,7 +41,7 @@
"styleName": "DefaultProgressBar",
"styleProperties": {
"progressColor": "#F27935",
"progressTrackColor": "#FDE3A7",
"progressTrackColor": "$DefaultButtonColor",
"cornerRadiusRatio": 0.35,
"progressCornerRadiusRatio": 0.35,
"borderColor": "#F9690E",
Expand Down Expand Up @@ -67,8 +70,8 @@
"styleName": "DefaultButton",
"styleProperties": {
"titleColorForNormalState": "#D35400",
"backgroundColor": "#FDE3A7",
"borderColor": "#FDE3A7",
"backgroundColor": "$DefaultButtonColor",
"borderColor": "$DefaultButtonColor",
"cornerRadius": 6.0
}
}
Expand Down

0 comments on commit 439b510

Please sign in to comment.