Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module name as parameter #51

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Each object inside the JSON file should contain the name of the UIView as a key

This would apply HelveticaNeue-Bold with size 20 to all the UIButtons except the ones contained inside the LoginView class in your app.

Custom classes must be namespaced by the name of the module they are contained in. e.g. `StyleKitDemo.SKTextField`
Custom classes can be namespaced by the name of the module they are contained in. e.g. `StyleKitDemo.SKTextField`

### Aliases

Expand Down
4 changes: 4 additions & 0 deletions StyleKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
059932DC1D59E0DF0085E522 /* SKTryCatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 059932DB1D59E0DF0085E522 /* SKTryCatch.m */; };
059932DD1D59E1950085E522 /* SKTryCatch.h in Headers */ = {isa = PBXBuildFile; fileRef = 059932D81D59E09D0085E522 /* SKTryCatch.h */; settings = {ATTRIBUTES = (Public, ); }; };
3BD119FA1FB1B6EB000980B0 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 3BD119F91FB1B6EB000980B0 /* README.md */; };
992723731D5B36D700B74CDD /* UIAppearance+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; };
992723741D5B36D700B74CDD /* UIAppearance+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */; };
996775991D5A2E8E005DA08A /* StyleParsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 996775981D5A2E8E005DA08A /* StyleParsable.swift */; };
Expand Down Expand Up @@ -56,6 +57,7 @@
/* Begin PBXFileReference section */
059932D81D59E09D0085E522 /* SKTryCatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKTryCatch.h; sourceTree = "<group>"; };
059932DB1D59E0DF0085E522 /* SKTryCatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTryCatch.m; sourceTree = "<group>"; };
3BD119F91FB1B6EB000980B0 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
992723711D5B36D700B74CDD /* UIAppearance+Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAppearance+Swift.h"; sourceTree = "<group>"; };
992723721D5B36D700B74CDD /* UIAppearance+Swift.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAppearance+Swift.m"; sourceTree = "<group>"; };
996775981D5A2E8E005DA08A /* StyleParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyleParsable.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -126,6 +128,7 @@
A10FD6901D53FA2600341EDD = {
isa = PBXGroup;
children = (
3BD119F91FB1B6EB000980B0 /* README.md */,
A10FD69B1D53FA2600341EDD /* Products */,
A10FD69C1D53FA2600341EDD /* StyleKit */,
A10FD6A81D53FA2600341EDD /* StyleKitTests */,
Expand Down Expand Up @@ -344,6 +347,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
3BD119FA1FB1B6EB000980B0 /* README.md in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
4 changes: 2 additions & 2 deletions StyleKit/StyleKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public class StyleKit {

let stylist: Stylist

public init?(fileUrl: URL, styleParser: StyleParsable? = nil, logLevel: SKLogLevel = .error) {
public init?(fileUrl: URL, styleParser: StyleParsable? = nil, moduleName: String? = nil, logLevel: SKLogLevel = .error) {
let log = SKLogger.defaultInstance()
log.setup(logLevel,
showLogIdentifier: false,
Expand All @@ -17,7 +17,7 @@ public class StyleKit {

let fileLoader = FileLoader.init(fileUrl: fileUrl)
if let data = fileLoader.load() {
self.stylist = Stylist.init(data: data, styleParser: styleParser)
self.stylist = Stylist.init(data: data, styleParser: styleParser, moduleName: moduleName)
} else {
return nil
}
Expand Down
35 changes: 23 additions & 12 deletions StyleKit/Stylist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class Stylist {
let data: Style
let aliases: Style
let styleParser: StyleParsable
let moduleName: String?
var currentComponent: AnyClass?
var viewStack = [UIAppearanceContainer.Type]()

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

var tmpAlias = Style()
Expand All @@ -27,6 +28,7 @@ class Stylist {

self.data = tmpData
self.aliases = tmpAlias
self.moduleName = moduleName
}

func apply() {
Expand All @@ -40,8 +42,8 @@ class Stylist {
private func validateAndApply(_ data: Style) {

for (key, value) in data {
if let value = value as? Style , NSClassFromString(key) != nil {
if selectCurrentComponent(key), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type {
if let value = value as? Style, let component = resolveComponent(from: key) {
if selectCurrentComponent(component), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type {
viewStack.append(appearanceContainer)
}
validateAndApply(value)
Expand All @@ -63,16 +65,25 @@ class Stylist {
}
}
}

private func selectCurrentComponent(_ name: String) -> Bool {

SKLogger.debug("Switching to: \(name)")

guard let currentComponent = NSClassFromString(name) else {
SKLogger.debug("Component \(name) cannot be selected")
return false

private typealias Component = (klass: AnyClass, name: String)

private func resolveComponent(from key: String) -> Component? {
let resolved: Component?
if let klass = NSClassFromString(key) {
resolved = (klass, key)
} else if let name = moduleName.flatMap({ "\($0).\(key)" })
, let klass = NSClassFromString(name) {
resolved = (klass, name)
} else {
resolved = nil
}
self.currentComponent = currentComponent
return resolved
}

private func selectCurrentComponent(_ component: Component) -> Bool {
SKLogger.debug("Switching to: \(component.name)")
self.currentComponent = component.klass
return true
}

Expand Down
3 changes: 1 addition & 2 deletions StyleKitDemo/StyleKitDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
//StyleKit(fileUrl: styleFile, styleParser: StyleParser())?.apply()

// Uses default style parser
StyleKit(fileUrl: styleFile, logLevel: .debug)?.apply()
StyleKit(fileUrl: styleFile, moduleName: "StyleKitDemo", logLevel: .debug)?.apply()

}


return true
}

Expand Down
Loading