-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
TiAppPropertiesProxy.m
232 lines (200 loc) Β· 5.37 KB
/
TiAppPropertiesProxy.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
#ifdef USE_TI_APP
#import "TiAppPropertiesProxy.h"
#import "TiUtils.h"
#import "TiApp.h"
@implementation TiAppPropertiesProxy {
NSData *_defaultsNull;
}
-(void)dealloc
{
TiThreadPerformOnMainThread(^{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}, YES);
RELEASE_TO_NIL(defaultsObject);
RELEASE_TO_NIL(_defaultsNull);
[super dealloc];
}
-(NSString*)apiName
{
return @"Ti.App.Properties";
}
-(void)_listenerAdded:(NSString*)type count:(int)count
{
if (count == 1 && [type isEqual:@"change"])
{
TiThreadPerformOnMainThread(^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NSUserDefaultsDidChange) name:NSUserDefaultsDidChangeNotification object:nil];
}, YES);
}
}
-(void)_listenerRemoved:(NSString*)type count:(int)count
{
if (count == 0 && [type isEqual:@"change"])
{
TiThreadPerformOnMainThread(^{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSUserDefaultsDidChangeNotification object:nil];
}, YES);
}
}
-(void)_configure
{
defaultsObject = [[NSUserDefaults standardUserDefaults] retain];
_defaultsNull = [[NSData alloc] initWithBytes:"NULL" length:4];
[super _configure];
}
-(BOOL)propertyExists: (NSString *) key;
{
if (![key isKindOfClass:[NSString class]]) return NO;
[defaultsObject synchronize];
return ([defaultsObject objectForKey:key] != nil);
}
#define GETPROP \
ENSURE_TYPE(args,NSArray);\
NSString *key = [args objectAtIndex:0];\
NSString *appProp = [[TiApp tiAppProperties] objectForKey:key]; \
if(appProp) { \
return appProp; \
} \
id defaultValue = [args count] > 1 ? [args objectAtIndex:1] : [NSNull null];\
if (![self propertyExists:key]) return defaultValue; \
-(id)getBool:(id)args
{
GETPROP
return [NSNumber numberWithBool:[defaultsObject boolForKey:key]];
}
-(id)getDouble:(id)args
{
GETPROP
return [NSNumber numberWithDouble:[defaultsObject doubleForKey:key]];
}
-(id)getInt:(id)args
{
GETPROP
return [NSNumber numberWithInt:[defaultsObject integerForKey:key]];
}
-(id)getString:(id)args
{
GETPROP
return [defaultsObject stringForKey:key];
}
-(id)getList:(id)args
{
GETPROP
NSArray *value = [defaultsObject arrayForKey:key];
NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:[value count]] autorelease];
[(NSArray *)value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSData class]] && [_defaultsNull isEqualToData:obj]) {
obj = [NSNull null];
}
[array addObject:obj];
}];
return array;
}
-(id)getObject:(id)args
{
GETPROP
id theObject = [defaultsObject objectForKey:key];
if ([theObject isKindOfClass:[NSData class]]) {
return [NSKeyedUnarchiver unarchiveObjectWithData:theObject];
}
else {
return theObject;
}
}
#define SETPROP \
ENSURE_TYPE(args,NSArray);\
NSString *key = [args objectAtIndex:0];\
NSString *appProp = [[TiApp tiAppProperties] objectForKey:key]; \
if(appProp) { \
DebugLog(@"Property \"%@\" already exist and cannot be overwritten", key); \
return; \
} \
id value = [args count] > 1 ? [args objectAtIndex:1] : nil;\
if (value==nil || value==[NSNull null]) {\
[defaultsObject removeObjectForKey:key];\
[defaultsObject synchronize]; \
return;\
}\
if ([self propertyExists:key] && [ [defaultsObject objectForKey:key] isEqual:value]) {\
return;\
}\
-(void)setBool:(id)args
{
SETPROP
[defaultsObject setBool:[TiUtils boolValue:value] forKey:key];
[defaultsObject synchronize];
}
-(void)setDouble:(id)args
{
SETPROP
[defaultsObject setDouble:[TiUtils doubleValue:value] forKey:key];
[defaultsObject synchronize];
}
-(void)setInt:(id)args
{
SETPROP
[defaultsObject setInteger:[TiUtils intValue:value] forKey:key];
[defaultsObject synchronize];
}
-(void)setString:(id)args
{
SETPROP
[defaultsObject setObject:[TiUtils stringValue:value] forKey:key];
[defaultsObject synchronize];
}
-(void)setList:(id)args
{
SETPROP
if ([value isKindOfClass:[NSArray class]]) {
NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:[value count]] autorelease];
[(NSArray *)value enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSNull class]]) {
obj = _defaultsNull;
}
[array addObject:obj];
}];
value = array;
}
[defaultsObject setObject:value forKey:key];
[defaultsObject synchronize];
}
-(void)setObject:(id)args
{
SETPROP
NSData* encoded = [NSKeyedArchiver archivedDataWithRootObject:value];
[defaultsObject setObject:encoded forKey:key];
[defaultsObject synchronize];
}
-(void)removeProperty:(id)args
{
ENSURE_SINGLE_ARG(args,NSString);
[defaultsObject removeObjectForKey:[TiUtils stringValue:args]];
[defaultsObject synchronize];
}
-(void)removeAllProperties {
NSArray *keys = [[defaultsObject dictionaryRepresentation] allKeys];
for(NSString *key in keys) {
[defaultsObject removeObjectForKey:key];
}
}
-(id)hasProperty:(id)args
{
ENSURE_SINGLE_ARG(args,NSString);
return [NSNumber numberWithBool:[self propertyExists:[TiUtils stringValue:args]]];
}
-(id)listProperties:(id)args
{
return [[defaultsObject dictionaryRepresentation] allKeys];
}
-(void) NSUserDefaultsDidChange
{
[self fireEvent:@"change" withObject:nil];
}
@end
#endif