Skip to content

Commit

Permalink
Merge pull request #24 from whitespectre/master
Browse files Browse the repository at this point in the history
Added support for Constant values that can be reused in the stylesheet
  • Loading branch information
daniel-hall authored Jun 23, 2019
2 parents ab65aed + 439b510 commit e621654
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 24 deletions.
15 changes: 10 additions & 5 deletions Stylish.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
CEC4B3931FB180AE00A97AB5 /* Stylish.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC4B38D1FB180AE00A97AB5 /* Stylish.swift */; };
CEC4B3941FB180AE00A97AB5 /* StylesheetParseableExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC4B38E1FB180AE00A97AB5 /* StylesheetParseableExtensions.swift */; };
D7C473021F3E4F8A00B5880A /* Stylish.h in Headers */ = {isa = PBXBuildFile; fileRef = D7C473001F3E4F8A00B5880A /* Stylish.h */; settings = {ATTRIBUTES = (Public, ); }; };
FD8888BA227CAABD00749F18 /* StyleableComponents.swift in Headers */ = {isa = PBXBuildFile; fileRef = CEC4B3891FB180AE00A97AB5 /* StyleableComponents.swift */; settings = {ATTRIBUTES = (Public, ); }; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -78,6 +79,7 @@
buildActionMask = 2147483647;
files = (
D7C473021F3E4F8A00B5880A /* Stylish.h in Headers */,
FD8888BA227CAABD00749F18 /* StyleableComponents.swift in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -108,21 +110,22 @@
D7C472F41F3E4F8A00B5880A /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1000;
LastUpgradeCheck = 1020;
TargetAttributes = {
D7C472FC1F3E4F8A00B5880A = {
CreatedOnToolsVersion = 8.2.1;
LastSwiftMigration = 1000;
LastSwiftMigration = 1020;
ProvisioningStyle = Manual;
};
};
};
buildConfigurationList = D7C472F71F3E4F8A00B5880A /* Build configuration list for PBXProject "Stylish" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Base,
);
mainGroup = D7C472F31F3E4F8A00B5880A;
productRefGroup = D7C472FE1F3E4F8A00B5880A /* Products */;
Expand Down Expand Up @@ -165,6 +168,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
Expand Down Expand Up @@ -227,6 +231,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ANALYZER_NONNULL = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
Expand Down Expand Up @@ -297,7 +302,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -319,7 +324,7 @@
PRODUCT_BUNDLE_IDENTIFIER = io.danielhall.Stylish;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
80 changes: 80 additions & 0 deletions Stylish.xcodeproj/xcshareddata/xcschemes/Stylish.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7C472FC1F3E4F8A00B5880A"
BuildableName = "Stylish.framework"
BlueprintName = "Stylish"
ReferencedContainer = "container:Stylish.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7C472FC1F3E4F8A00B5880A"
BuildableName = "Stylish.framework"
BlueprintName = "Stylish"
ReferencedContainer = "container:Stylish.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "D7C472FC1F3E4F8A00B5880A"
BuildableName = "Stylish.framework"
BlueprintName = "Stylish"
ReferencedContainer = "container:Stylish.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
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
20 changes: 10 additions & 10 deletions Stylish/PropertyStylers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ public extension Style {


public extension Stylish {
public enum PropertyStylers {}
enum PropertyStylers {}
}

public extension Stylish.PropertyStylers {

public class UIView {
class UIView {

public struct BackgroundColor: PropertyStyler {
public static var propertyKey: String { return "backgroundColor" }
Expand Down Expand Up @@ -366,7 +366,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UIControl: UIView {
class UIControl: UIView {

public struct IsEnabled: PropertyStyler {
public static var propertyKey: String { return "isEnabled" }
Expand All @@ -376,7 +376,7 @@ public extension Stylish.PropertyStylers {
}
}

public class StylishTextControl: UIControl {
class StylishTextControl: UIControl {

public struct AdjustsFontSizeToFitWidth: PropertyStyler {
public static var propertyKey: String { return "adjustsFontSizeToFitWidth" }
Expand Down Expand Up @@ -465,7 +465,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UILabel: StylishTextControl {
class UILabel: StylishTextControl {

public struct BaselineAdjustment: PropertyStyler {
public static var propertyKey: String { return "baselineAdjustment" }
Expand Down Expand Up @@ -517,7 +517,7 @@ public extension Stylish.PropertyStylers {
}
}

public class StylishTextInputControl: StylishTextControl {
class StylishTextInputControl: StylishTextControl {

public struct AllowsEditingTextAttributes: PropertyStyler {
public static var propertyKey: String { return "allowsEditingTextAttributes" }
Expand Down Expand Up @@ -628,7 +628,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UITextField: StylishTextInputControl {
class UITextField: StylishTextInputControl {

public struct Background: PropertyStyler {
public static var propertyKey: String { return "background" }
Expand Down Expand Up @@ -712,7 +712,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UITextView: StylishTextInputControl {
class UITextView: StylishTextInputControl {

public struct IsEditable: PropertyStyler {
public static var propertyKey: String { return "isEditable" }
Expand Down Expand Up @@ -743,7 +743,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UIButton: UIControl {
class UIButton: UIControl {

public struct AdjustsImageWhenDisabled: PropertyStyler {
public static var propertyKey: String { return "adjustsImageWhenDisabled" }
Expand Down Expand Up @@ -926,7 +926,7 @@ public extension Stylish.PropertyStylers {
}
}

public class UIImageView: UIView {
class UIImageView: UIView {

public struct Image: PropertyStyler {
public static var propertyKey: String { return "image" }
Expand Down
6 changes: 3 additions & 3 deletions Stylish/Stylish.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public protocol PropertyStyler: AnyPropertyStylerType {
}

public extension PropertyStyler {
public static func set(value: PropertyType?) -> AnyPropertyStyler {
static func set(value: PropertyType?) -> AnyPropertyStyler {
return AnyPropertyStyler {
if let target = $0 as? TargetType {
Self.apply(value: value, to: target, using: $1)
}
}
}

public static func set(value: @escaping (Bundle, UITraitCollection?) -> PropertyType?) -> AnyPropertyStyler {
static func set(value: @escaping (Bundle, UITraitCollection?) -> PropertyType?) -> AnyPropertyStyler {
return AnyPropertyStyler {
if let target = $0 as? TargetType {
let value = value($1, (target as? UITraitEnvironment)?.traitCollection)
Expand All @@ -90,7 +90,7 @@ public protocol AnyPropertyStylerType {
}

public extension PropertyStyler {
public static var wrapped: Any {
static var wrapped: Any {
return AnyPropertyStylerTypeWrapper(Self.self)
}
}
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 e621654

Please sign in to comment.