Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Add support for JSON config files #4

Open
wants to merge 1 commit into
base: master
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
4 changes: 4 additions & 0 deletions Demo/DB5Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
6AABDD3617802886003DFA59 /* DB5-alt.json in Resources */ = {isa = PBXBuildFile; fileRef = 6AABDD3517802886003DFA59 /* DB5-alt.json */; };
840375F5177BD9A600188A0B /* VSTheme.m in Sources */ = {isa = PBXBuildFile; fileRef = 840375F2177BD9A600188A0B /* VSTheme.m */; };
840375F6177BD9A600188A0B /* VSThemeLoader.m in Sources */ = {isa = PBXBuildFile; fileRef = 840375F4177BD9A600188A0B /* VSThemeLoader.m */; };
840375F8177BDA9E00188A0B /* DB5.plist in Resources */ = {isa = PBXBuildFile; fileRef = 840375F7177BDA9E00188A0B /* DB5.plist */; };
Expand All @@ -24,6 +25,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
6AABDD3517802886003DFA59 /* DB5-alt.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "DB5-alt.json"; sourceTree = "<group>"; };
840375F1177BD9A600188A0B /* VSTheme.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VSTheme.h; path = ../../Source/VSTheme.h; sourceTree = "<group>"; };
840375F2177BD9A600188A0B /* VSTheme.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = VSTheme.m; path = ../../Source/VSTheme.m; sourceTree = "<group>"; };
840375F3177BD9A600188A0B /* VSThemeLoader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = VSThemeLoader.h; path = ../../Source/VSThemeLoader.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -101,6 +103,7 @@
840375F1177BD9A600188A0B /* VSTheme.h */,
840375F2177BD9A600188A0B /* VSTheme.m */,
840375F7177BDA9E00188A0B /* DB5.plist */,
6AABDD3517802886003DFA59 /* DB5-alt.json */,
844CCE17177B6B5900B8BDF0 /* Supporting Files */,
);
path = DB5Demo;
Expand Down Expand Up @@ -173,6 +176,7 @@
buildActionMask = 2147483647;
files = (
844CCE1B177B6B5900B8BDF0 /* InfoPlist.strings in Resources */,
6AABDD3617802886003DFA59 /* DB5-alt.json in Resources */,
844CCE23177B6B5900B8BDF0 /* Default.png in Resources */,
844CCE25177B6B5900B8BDF0 /* [email protected] in Resources */,
844CCE27177B6B5900B8BDF0 /* [email protected] in Resources */,
Expand Down
16 changes: 16 additions & 0 deletions Demo/DB5Demo/DB5-alt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"Default": {
"labelX": 44,
"labelY": 300,
"labelAnimationCurve": "easeout",
"labelFontSize": 18,
"labelFont": "Avenir-Medium",
"labelTextColor": "333333",
"labelAnimationDuration": 0.25,
"backgroundColor": "708090",
"labelAnimationDelay": 2
},
"Example Theme": {
"test": "foo"
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ There is nothing magical about the code or the system: it’s some simple code p

### How it works

See the demo app. You include two classes — VSThemeLoader and VSTheme — and DB5.plist. The plist is where you set values.
See the demo app. You include two classes — VSThemeLoader and VSTheme — and DB5.plist. The plist is where you set values. You can use a JSON file too, if you prefer.

At startup you load the file via VSThemeLoader, then access values via methods in VSTheme.

Expand Down
15 changes: 12 additions & 3 deletions Source/VSThemeLoader.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#import "VSThemeLoader.h"
#import "VSTheme.h"


@interface VSThemeLoader ()

@property (nonatomic, strong, readwrite) VSTheme *defaultTheme;
Expand All @@ -26,8 +25,18 @@ - (id)init {
if (self == nil)
return nil;

NSString *themesFilePath = [[NSBundle mainBundle] pathForResource:@"DB5" ofType:@"plist"];
NSDictionary *themesDictionary = [NSDictionary dictionaryWithContentsOfFile:themesFilePath];
NSString *filename = @"DB5";
NSDictionary *themesDictionary;
NSString *themesFilePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"plist"];
if (themesFilePath != nil) {
themesDictionary = [NSDictionary dictionaryWithContentsOfFile:themesFilePath];
} else {
themesFilePath = [[NSBundle mainBundle] pathForResource:filename ofType:@"json"];
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:themesFilePath];
[stream open];
themesDictionary = [NSJSONSerialization JSONObjectWithStream:stream options:0 error:nil];
[stream close];
}

NSMutableArray *themes = [NSMutableArray array];
for (NSString *oneKey in themesDictionary) {
Expand Down