-
Notifications
You must be signed in to change notification settings - Fork 5
/
PWWidgetPlistParser.m
285 lines (214 loc) · 8.47 KB
/
PWWidgetPlistParser.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// ProWidgets
//
// 1.0.0
//
// Created by Alan Yip on 18 Jan 2014
// Copyright 2014 Alan Yip. All rights reserved.
//
#import "PWWidgetPlistParser.h"
#import "PWController.h"
#import "PWWidget.h"
#import "PWWidgetJS.h"
#import "PWWidgetItem.h"
#import "PWTheme.h"
#import "PWContentViewController.h"
#import "PWContentItemViewController.h"
@implementation PWWidgetPlistParser
+ (void)parse:(NSDictionary *)dict forWidget:(PWWidget *)widget {
LOG(@"PWWidgetPlistParser: Parsing plist for widget (%@)", widget);
NSBundle *bundle = widget.bundle;
// requiresProtectedDataAccess
NSNumber *requiresProtectedDataAccess = dict[@"requiresProtectedDataAccess"];
if (requiresProtectedDataAccess != nil) {
[widget setRequiresProtectedDataAccess:[requiresProtectedDataAccess boolValue]];
}
// supportResizing
NSNumber *supportResizing = dict[@"supportResizing"];
if (supportResizing != nil) {
[widget setSupportResizing:[supportResizing boolValue]];
}
// title
NSString *title = dict[@"title"];
if (title != nil) widget.title = T(title, bundle);
// layout
NSString *layoutName = [dict[@"layout"] lowercaseString];
// JS widgets must use default layout
PWWidgetLayout layout = PWWidgetLayoutDefault;
if (![widget isKindOfClass:[PWWidgetJS class]]) {
if ([layoutName hasSuffix:@"custom"]) {
layout = PWWidgetLayoutCustom;
}
}
widget.layout = layout;
// preferred tint color
NSString *preferredTintColor = dict[@"preferredTintColor"];
if (preferredTintColor != nil) {
UIColor *color = [PWTheme parseColorString:preferredTintColor];
widget.preferredTintColor = color;
}
// preferred bar text color
NSString *preferredBarTextColor = dict[@"preferredBarTextColor"];
if (preferredBarTextColor != nil) {
UIColor *color = [PWTheme parseColorString:preferredBarTextColor];
widget.preferredBarTextColor = color;
}
// widget theme
NSString *themeName = dict[@"theme"];
if (themeName != nil) {
// try plist
BOOL success = [widget loadThemePlist:themeName];
// then load theme from other bundles OR defined in the same bundle
if (!success)
[widget loadThemeNamed:themeName];
}
// defaultItemViewControllerPlist
if (layout == PWWidgetLayoutDefault) {
NSString *defaultItemViewControllerPlist = dict[@"defaultItemViewControllerPlist"];
if (defaultItemViewControllerPlist != nil) {
widget.defaultItemViewControllerPlist = defaultItemViewControllerPlist;
}
}
}
+ (void)parse:(NSDictionary *)dict forContentViewController:(PWContentViewController *)viewController {
NSBundle *bundle = viewController.widget.bundle;
// title
NSString *title = dict[@"title"];
if (title != nil) viewController.title = title;
else title = T(title, bundle);
// closeButtonText
NSString *closeButtonText = dict[@"closeButtonText"];
if (closeButtonText != nil) [viewController setCloseButtonText:T(closeButtonText, bundle)];
// actionButtonText
NSString *actionButtonText = dict[@"actionButtonText"];
if (actionButtonText != nil) [viewController setActionButtonText:T(actionButtonText, bundle)];
// shouldAutoConfigureStandardButtons
NSNumber *shouldAutoConfigureStandardButtons = dict[@"shouldAutoConfigureStandardButtons"];
if (shouldAutoConfigureStandardButtons != nil) {
[viewController setShouldAutoConfigureStandardButtons:[shouldAutoConfigureStandardButtons boolValue]];
}
// wantsFullscreen
NSNumber *wantsFullscreen = dict[@"wantsFullscreen"];
if (wantsFullscreen != nil) {
[viewController setWantsFullscreen:[wantsFullscreen boolValue]];
}
// shouldMaximizeContentHeight
NSNumber *shouldMaximizeContentHeight = dict[@"shouldMaximizeContentHeight"];
if (shouldMaximizeContentHeight != nil) {
[viewController setShouldMaximizeContentHeight:[shouldMaximizeContentHeight boolValue]];
}
// requiresKeyboard
NSNumber *requiresKeyboard = dict[@"requiresKeyboard"];
if (requiresKeyboard != nil) {
[viewController setRequiresKeyboard:[requiresKeyboard boolValue]];
}
}
+ (void)parse:(NSDictionary *)dict forContentItemViewController:(PWContentItemViewController *)itemViewController {
[self parse:dict forContentViewController:itemViewController];
NSBundle *bundle = itemViewController.widget.bundle;
// override content height
id overrideContentHeight = dict[@"overrideContentHeight"];
if (overrideContentHeight != nil) {
NSString *expressionForPortrait = nil;
NSString *expressionForLandscape = nil;
#define EXPRESSION_FROM_STRING(x) NSString *expression = (NSString *)x;
#define EXPRESSION_FROM_NUMBER(x) NSNumber *expressionNumber = (NSNumber *)x;\
NSString *expression = [NSString stringWithFormat:@"%f", [expressionNumber doubleValue]];
if ([overrideContentHeight isKindOfClass:[NSString class]]) {
EXPRESSION_FROM_STRING(overrideContentHeight)
expressionForPortrait = expression;
expressionForLandscape = expression;
} else if ([overrideContentHeight isKindOfClass:[NSNumber class]]) {
EXPRESSION_FROM_NUMBER(overrideContentHeight)
expressionForPortrait = expression;
expressionForLandscape = expression;
} else if ([overrideContentHeight isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)overrideContentHeight;
id portrait = dict[@"portrait"];
id landscape = dict[@"landscape"];
if (portrait != nil) {
if ([portrait isKindOfClass:[NSString class]]) {
EXPRESSION_FROM_STRING(portrait)
expressionForPortrait = expression;
} else if ([portrait isKindOfClass:[NSNumber class]]) {
EXPRESSION_FROM_NUMBER(portrait)
expressionForPortrait = expression;
}
}
if (landscape != nil) {
if ([landscape isKindOfClass:[NSString class]]) {
EXPRESSION_FROM_STRING(landscape)
expressionForLandscape = expression;
} else if ([landscape isKindOfClass:[NSNumber class]]) {
EXPRESSION_FROM_NUMBER(landscape)
expressionForLandscape = expression;
}
}
}
if (expressionForPortrait != nil)
[itemViewController setOverrideContentHeightExpression:expressionForPortrait forOrientation:PWWidgetOrientationPortrait];
if (expressionForLandscape != nil)
[itemViewController setOverrideContentHeightExpression:expressionForLandscape forOrientation:PWWidgetOrientationLandscape];
}
// items
NSArray *items = dict[@"items"];
NSMutableArray *parsedItems = nil;
if (items != nil && [items count] > 0) {
// initialized the array
parsedItems = [NSMutableArray new];
for (NSDictionary *item in items) {
// required 'key'
NSString *key = item[@"key"];
// title
NSString *title = item[@"title"];
if (title != nil) title = T(title, bundle);
// type
NSString *type = item[@"type"];
// create the widget item
PWWidgetItem *widgetItem = [PWWidgetItem createItemNamed:type forItemViewController:itemViewController]; // auto release
if (widgetItem == nil) {
LOG(@"PWWidgetPlistParser: Error occurs when creating an item. Reason: item class name defined in 'type' does not exist.");
continue;
}
// default value
id defaultValue = item[@"default"];
// actionEventName
NSString *actionEventName = item[@"actionEventName"];
// hideChevron (default NO)
NSNumber *_hideChevron = item[@"hideChevron"];
BOOL hideChevron = _hideChevron != nil ? [_hideChevron boolValue] : NO;
// icon
NSString *icon = item[@"icon"];
UIImage *iconImage = nil;
if (icon != nil) {
iconImage = [UIImage imageNamed:icon inBundle:bundle];
}
// should fill height
BOOL shouldFillHeight = [item[@"shouldFillHeight"] boolValue];
// minimum fill height
CGFloat minimumFillHeight = (CGFloat)[item[@"minimumFillHeight"] doubleValue];
// override height
CGFloat overrideHeight = (CGFloat)[item[@"overrideHeight"] doubleValue];
// configure the widget item
widgetItem.key = key;
widgetItem.title = title;
widgetItem.actionEventName = actionEventName;
widgetItem.hideChevron = hideChevron;
widgetItem.icon = iconImage;
widgetItem.shouldFillHeight = shouldFillHeight;
widgetItem.minimumFillHeight = minimumFillHeight;
widgetItem.overrideHeight = overrideHeight;
// set extra attributes (like list item titles and values)
[widgetItem setExtraAttributes:item];
// set default value
widgetItem.value = defaultValue;
LOG(@"PWWidgetPlistParser: Created a widget item (%@)", widgetItem);
[parsedItems addObject:widgetItem];
}
} else {
LOG(@"PWWidgetPlistParser: No items specified in plist.");
}
[itemViewController setItems:parsedItems];
[parsedItems release];
}
@end