Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
805 | - | 1 | // |
2 | // InAppSetting.m |
||
3 | // InAppSettingsTestApp |
||
4 | // |
||
5 | // Created by David Keegan on 11/21/09. |
||
6 | // Copyright 2009 InScopeApps{+}. All rights reserved. |
||
7 | // |
||
8 | |||
9 | #import "InAppSettings.h" |
||
10 | #import "InAppSettingsSpecifier.h" |
||
11 | #import "InAppSettingsConstants.h" |
||
12 | |||
13 | @implementation InAppSettingsSpecifier |
||
14 | |||
15 | @synthesize stringsTable; |
||
16 | @synthesize delegate; |
||
17 | @synthesize dataSource; |
||
18 | |||
19 | - (NSString *) getKey { |
||
20 | return [self valueForKey:InAppSettingsSpecifierKey]; |
||
21 | } |
||
22 | |||
23 | - (NSString *) getType { |
||
24 | return [self valueForKey:InAppSettingsSpecifierType]; |
||
25 | } |
||
26 | |||
27 | - (BOOL) isType:(NSString *)type { |
||
28 | return [[self getType] isEqualToString:type]; |
||
29 | } |
||
30 | |||
31 | - (id) valueForKey:(NSString *)key { |
||
32 | return [settingDictionary objectForKey:key]; |
||
33 | } |
||
34 | |||
35 | - (NSString *) localizedTitle { |
||
36 | NSString * title = [self valueForKey:InAppSettingsSpecifierTitle]; |
||
37 | |||
38 | if ([self valueForKey:InAppSettingsSpecifierInAppTitle]) { |
||
39 | title = [self valueForKey:InAppSettingsSpecifierInAppTitle]; |
||
40 | } |
||
41 | return InAppSettingsLocalize(title, self.stringsTable); |
||
42 | } |
||
43 | |||
44 | - (NSString *) cellName { |
||
45 | |||
46 | if ([self valueForKey:InAppSettingsSpecifierInAppCellClass]) { |
||
47 | return [self valueForKey:InAppSettingsSpecifierInAppCellClass]; |
||
48 | } |
||
49 | |||
50 | return [NSString stringWithFormat:@"%@%@Cell", InAppSettingsProjectName, [self getType]]; |
||
51 | } |
||
52 | |||
53 | - (id) getValue { |
||
54 | |||
55 | id value; |
||
56 | |||
57 | if ( self.dataSource != nil ) |
||
58 | value = [self.dataSource objectForKey:[self getKey]]; |
||
59 | else |
||
60 | value = [[NSUserDefaults standardUserDefaults] valueForKey:[self getKey]]; |
||
61 | |||
62 | if (value == nil) { |
||
63 | value = [self valueForKey:InAppSettingsSpecifierDefaultValue]; |
||
64 | } |
||
65 | return value; |
||
66 | } |
||
67 | |||
68 | - (void) setValue:(id)newValue { |
||
69 | NSString * key = [self getKey]; |
||
70 | |||
71 | if ( self.dataSource != nil ) { |
||
72 | [self.dataSource setObject:newValue forKey:key]; |
||
73 | } else |
||
74 | [[NSUserDefaults standardUserDefaults] setObject:newValue forKey:key]; |
||
75 | |||
76 | [self.delegate settingsSpecifierUpdated:self]; |
||
77 | } |
||
78 | |||
79 | #pragma mark validation |
||
80 | |||
81 | - (BOOL) hasTitle { |
||
82 | return ([self valueForKey:InAppSettingsSpecifierTitle]) ? YES : NO; |
||
83 | } |
||
84 | |||
85 | - (BOOL) hasKey { |
||
86 | NSString * key = [self getKey]; |
||
87 | |||
88 | return (key && (![key isEqualToString:@""])); |
||
89 | } |
||
90 | |||
91 | - (BOOL) hasDefaultValue { |
||
92 | return ([self valueForKey:InAppSettingsSpecifierDefaultValue]) ? YES : NO; |
||
93 | } |
||
94 | |||
95 | - (BOOL) isValid { |
||
96 | if (![self getType]) { |
||
97 | return NO; |
||
98 | } |
||
99 | |||
100 | if ([self isType:InAppSettingsPSGroupSpecifier]) { |
||
101 | return YES; |
||
102 | } |
||
103 | |||
104 | if ([self isType:InAppSettingsPSMultiValueSpecifier]) { |
||
105 | if (![self hasKey]) { |
||
106 | return NO; |
||
107 | } |
||
108 | |||
109 | if (![self hasDefaultValue]) { |
||
110 | return NO; |
||
111 | } |
||
112 | |||
113 | // check the localized and un-locatlized values |
||
114 | if (![self hasTitle] || [[self valueForKey:InAppSettingsSpecifierTitle] length] == 0) { |
||
115 | return NO; |
||
116 | } |
||
117 | |||
118 | NSArray * titles = [self valueForKey:InAppSettingsSpecifierTitles]; |
||
119 | if ((!titles) || ([titles count] == 0)) { |
||
120 | return NO; |
||
121 | } |
||
122 | |||
123 | NSArray * values = [self valueForKey:InAppSettingsSpecifierValues]; |
||
124 | if ((!values) || ([values count] == 0)) { |
||
125 | return NO; |
||
126 | } |
||
127 | |||
128 | if ([titles count] != [values count]) { |
||
129 | return NO; |
||
130 | } |
||
131 | |||
132 | return YES; |
||
133 | } |
||
134 | |||
135 | if ([self isType:InAppSettingsPSSliderSpecifier]) { |
||
136 | if (![self hasKey]) { |
||
137 | return NO; |
||
138 | } |
||
139 | |||
140 | if (![self hasDefaultValue]) { |
||
141 | return NO; |
||
142 | } |
||
143 | |||
144 | // The settings app allows min>max |
||
145 | if (![self valueForKey:InAppSettingsSpecifierMinimumValue]) { |
||
146 | return NO; |
||
147 | } |
||
148 | |||
149 | if (![self valueForKey:InAppSettingsSpecifierMaximumValue]) { |
||
150 | return NO; |
||
151 | } |
||
152 | |||
153 | return YES; |
||
154 | } |
||
155 | |||
156 | if ([self isType:InAppSettingsPSToggleSwitchSpecifier]) { |
||
157 | if (![self hasKey]) { |
||
158 | return NO; |
||
159 | } |
||
160 | |||
161 | if (![self hasDefaultValue]) { |
||
162 | return NO; |
||
163 | } |
||
164 | |||
165 | if (![self hasTitle]) { |
||
166 | return NO; |
||
167 | } |
||
168 | |||
169 | return YES; |
||
170 | } |
||
171 | |||
172 | if ([self isType:InAppSettingsPSTitleValueSpecifier]) { |
||
173 | if (![self hasKey]) { |
||
174 | return NO; |
||
175 | } |
||
176 | |||
177 | if (![self hasDefaultValue]) { |
||
178 | return NO; |
||
179 | } |
||
180 | |||
181 | return YES; |
||
182 | } |
||
183 | |||
184 | if ([self isType:InAppSettingsPSTextFieldSpecifier]) { |
||
185 | if (![self hasKey]) { |
||
186 | return NO; |
||
187 | } |
||
188 | |||
189 | if (![self hasTitle]) { |
||
190 | return NO; |
||
191 | } |
||
192 | |||
193 | return YES; |
||
194 | } |
||
195 | |||
196 | if ([self isType:InAppSettingsPSChildPaneSpecifier]) { |
||
197 | if (![self hasTitle]) { |
||
198 | return NO; |
||
199 | } |
||
200 | |||
201 | if (![self valueForKey:InAppSettingsSpecifierFile]) { |
||
202 | return NO; |
||
203 | } |
||
204 | |||
205 | return YES; |
||
206 | } |
||
207 | |||
208 | return NO; |
||
209 | } |
||
210 | |||
211 | #pragma mark init/dealloc |
||
212 | |||
213 | - (id) init { |
||
214 | return [self initWithDictionary:nil andStringsTable:nil]; |
||
215 | } |
||
216 | |||
217 | - (id) initWithDictionary:(NSDictionary *)dictionary andStringsTable:(NSString *)table { |
||
218 | self = [super init]; |
||
219 | if (self != nil) { |
||
220 | if (dictionary) { |
||
221 | self.stringsTable = table; |
||
222 | settingDictionary = [dictionary retain]; |
||
223 | } |
||
224 | } |
||
225 | return self; |
||
226 | } |
||
227 | |||
228 | - (void) dealloc { |
||
229 | self.delegate = nil; |
||
230 | [stringsTable release]; |
||
231 | [settingDictionary release]; |
||
232 | [super dealloc]; |
||
233 | } |
||
234 | |||
235 | @end |