Skip to content

Commit 3498029

Browse files
authored
Merge pull request #3 from jakubpetrik/feature/module_name
Module name
2 parents 243201e + 7a8cd16 commit 3498029

File tree

6 files changed

+99
-88
lines changed

6 files changed

+99
-88
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Each object inside the JSON file should contain the name of the UIView as a key
7878

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

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

8383
### Aliases
8484

StyleKit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
/* Begin PBXBuildFile section */
1010
059932DC1D59E0DF0085E522 /* SKTryCatch.m in Sources */ = {isa = PBXBuildFile; fileRef = 059932DB1D59E0DF0085E522 /* SKTryCatch.m */; };
1111
059932DD1D59E1950085E522 /* SKTryCatch.h in Headers */ = {isa = PBXBuildFile; fileRef = 059932D81D59E09D0085E522 /* SKTryCatch.h */; settings = {ATTRIBUTES = (Public, ); }; };
12+
3BD119FA1FB1B6EB000980B0 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 3BD119F91FB1B6EB000980B0 /* README.md */; };
1213
992723731D5B36D700B74CDD /* UIAppearance+Swift.h in Headers */ = {isa = PBXBuildFile; fileRef = 992723711D5B36D700B74CDD /* UIAppearance+Swift.h */; settings = {ATTRIBUTES = (Public, ); }; };
1314
992723741D5B36D700B74CDD /* UIAppearance+Swift.m in Sources */ = {isa = PBXBuildFile; fileRef = 992723721D5B36D700B74CDD /* UIAppearance+Swift.m */; };
1415
996775991D5A2E8E005DA08A /* StyleParsable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 996775981D5A2E8E005DA08A /* StyleParsable.swift */; };
@@ -56,6 +57,7 @@
5657
/* Begin PBXFileReference section */
5758
059932D81D59E09D0085E522 /* SKTryCatch.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SKTryCatch.h; sourceTree = "<group>"; };
5859
059932DB1D59E0DF0085E522 /* SKTryCatch.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SKTryCatch.m; sourceTree = "<group>"; };
60+
3BD119F91FB1B6EB000980B0 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
5961
992723711D5B36D700B74CDD /* UIAppearance+Swift.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIAppearance+Swift.h"; sourceTree = "<group>"; };
6062
992723721D5B36D700B74CDD /* UIAppearance+Swift.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIAppearance+Swift.m"; sourceTree = "<group>"; };
6163
996775981D5A2E8E005DA08A /* StyleParsable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StyleParsable.swift; sourceTree = "<group>"; };
@@ -126,6 +128,7 @@
126128
A10FD6901D53FA2600341EDD = {
127129
isa = PBXGroup;
128130
children = (
131+
3BD119F91FB1B6EB000980B0 /* README.md */,
129132
A10FD69B1D53FA2600341EDD /* Products */,
130133
A10FD69C1D53FA2600341EDD /* StyleKit */,
131134
A10FD6A81D53FA2600341EDD /* StyleKitTests */,
@@ -344,6 +347,7 @@
344347
isa = PBXResourcesBuildPhase;
345348
buildActionMask = 2147483647;
346349
files = (
350+
3BD119FA1FB1B6EB000980B0 /* README.md in Resources */,
347351
);
348352
runOnlyForDeploymentPostprocessing = 0;
349353
};

StyleKit/StyleKit.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class StyleKit {
44

55
let stylist: Stylist
66

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

1818
let fileLoader = FileLoader.init(fileUrl: fileUrl)
1919
if let data = fileLoader.load() {
20-
self.stylist = Stylist.init(data: data, styleParser: styleParser)
20+
self.stylist = Stylist.init(data: data, styleParser: styleParser, moduleName: moduleName)
2121
} else {
2222
return nil
2323
}

StyleKit/Stylist.swift

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class Stylist {
88
let data: Style
99
let aliases: Style
1010
let styleParser: StyleParsable
11+
let moduleName: String?
1112
var currentComponent: AnyClass?
1213
var viewStack = [UIAppearanceContainer.Type]()
1314

14-
init(data: Style, styleParser: StyleParsable?) {
15+
init(data: Style, styleParser: StyleParsable?, moduleName: String?) {
1516
self.styleParser = styleParser ?? StyleParser()
1617

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

2829
self.data = tmpData
2930
self.aliases = tmpAlias
31+
self.moduleName = moduleName
3032
}
3133

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

4244
for (key, value) in data {
43-
if let value = value as? Style , NSClassFromString(key) != nil {
44-
if selectCurrentComponent(key), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type {
45+
if let value = value as? Style, let component = resolveComponent(from: key) {
46+
if selectCurrentComponent(component), let appearanceContainer = self.currentComponent! as? UIAppearanceContainer.Type {
4547
viewStack.append(appearanceContainer)
4648
}
4749
validateAndApply(value)
@@ -63,16 +65,25 @@ class Stylist {
6365
}
6466
}
6567
}
66-
67-
private func selectCurrentComponent(_ name: String) -> Bool {
68-
69-
SKLogger.debug("Switching to: \(name)")
70-
71-
guard let currentComponent = NSClassFromString(name) else {
72-
SKLogger.debug("Component \(name) cannot be selected")
73-
return false
68+
69+
private typealias Component = (klass: AnyClass, name: String)
70+
71+
private func resolveComponent(from key: String) -> Component? {
72+
let resolved: Component?
73+
if let klass = NSClassFromString(key) {
74+
resolved = (klass, key)
75+
} else if let name = moduleName.flatMap({ "\($0).\(key)" })
76+
, let klass = NSClassFromString(name) {
77+
resolved = (klass, name)
78+
} else {
79+
resolved = nil
7480
}
75-
self.currentComponent = currentComponent
81+
return resolved
82+
}
83+
84+
private func selectCurrentComponent(_ component: Component) -> Bool {
85+
SKLogger.debug("Switching to: \(component.name)")
86+
self.currentComponent = component.klass
7687
return true
7788
}
7889

StyleKitDemo/StyleKitDemo/AppDelegate.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1515
//StyleKit(fileUrl: styleFile, styleParser: StyleParser())?.apply()
1616

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

2020
}
2121

22-
2322
return true
2423
}
2524

0 commit comments

Comments
 (0)