-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRZDebugMenuSettingsDataSource.m
More file actions
372 lines (295 loc) · 18.3 KB
/
RZDebugMenuSettingsDataSource.m
File metadata and controls
372 lines (295 loc) · 18.3 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// RZDebugMenuSettingsDataSource.m
// RZDebugMenu
//
// Created by Clayton Rieck on 7/15/14.
// Copyright (c) 2014 Raizlabs. All rights reserved.
//
#import "RZDebugMenuSettingsDataSource.h"
#import "RZDebugMenu.h"
#import "RZDebugMenuSettingsItem.h"
#import "RZDebugMenuMultiValueItem.h"
#import "RZMultiValueSelectionItem.h"
#import "RZDebugMenuToggleItem.h"
#import "RZDebugMenuVersionItem.h"
#import "RZDebugMenuTextFieldItem.h"
#import "RZDebugMenuSliderItem.h"
#import "RZDebugMenuGroupItem.h"
#import "RZDisclosureTableViewCell.h"
#import "RZToggleTableViewCell.h"
#import "RZVersionInfoTableViewCell.h"
#import "RZTextFieldTableViewCell.h"
#import "RZSliderTableViewCell.h"
#import "RZDebugMenuSettingsObserverManager.h"
#import "RZDebugMenuSettingsInterface.h"
static NSString * const kRZPreferenceSpecifiersKey = @"PreferenceSpecifiers";
static NSString * const kRZMultiValueSpecifier = @"PSMultiValueSpecifier";
static NSString * const kRZToggleSwitchSpecifier = @"PSToggleSwitchSpecifier";
static NSString * const kRZTextFieldSpecifier = @"PSTextFieldSpecifier";
static NSString * const kRZSliderSpecifier = @"PSSliderSpecifier";
static NSString * const kRZGroupSpecifer = @"PSGroupSpecifier";
static NSString * const kRZKeyBundleVersionString = @"CFBundleShortVersionString";
static NSString * const kRZKeyItemIdentifier = @"Key";
static NSString * const kRZKeyTitle = @"Title";
static NSString * const kRZKeyType = @"Type";
static NSString * const kRZKeyDefaultValue = @"DefaultValue";
static NSString * const kRZKeyEnvironmentsTitles = @"Titles";
static NSString * const kRZKeyEnvironmentsValues = @"Values";
static NSString * const kRZKeyMaximumValue = @"MaximumValue";
static NSString * const kRZKeyMinimumValue = @"MinimumValue";
static NSString * const kRZVersionCellTitle = @"Version";
static NSString * const kRZDisclosureReuseIdentifier = @"RZSettingsDisclosureCell";
static NSString * const kRZToggleReuseIdentifier = @"RZSettingsToggleSwitchCell";
static NSString * const kRZTextFieldReuseIdentifier = @"RZSettingsTextFieldCell";
static NSString * const kRZSliderReuseIdentifier = @"RZSettingsSliderCell";
static NSString * const kRZVersionInfoReuseIdentifier = @"RZSettingsVersionCell";
static NSString * const kRZVersionGroupTitle = @"Version Info";
static NSString * const kRZEmptyString = @"";
@interface RZDebugMenuSettingsDataSource ()
@property (strong, nonatomic) NSArray *preferenceSpecifiers;
@property (strong, nonatomic, readwrite) NSMutableDictionary *groupedSections;
@property (strong, nonatomic, readwrite) NSMutableArray *sectionGroupTitles;
@end
@implementation RZDebugMenuSettingsDataSource
- (id)initWithDictionary:(NSDictionary *)plistData
{
self = [super init];
if ( self ) {
_preferenceSpecifiers = [plistData objectForKey:kRZPreferenceSpecifiersKey];
_settingsKeys = [[NSMutableArray alloc] init];
NSMutableDictionary *userSettings = [self createMetaDataObjectsAndGenerateUserDefaults:_preferenceSpecifiers];
[self setUpVersionCellMetaData];
[[NSUserDefaults standardUserDefaults] registerDefaults:userSettings];
}
return self;
}
- (void)setSettingsOptionsTableView:(UITableView *)settingsOptionsTableView
{
_settingsOptionsTableView = settingsOptionsTableView;
[_settingsOptionsTableView registerClass:[RZDisclosureTableViewCell class] forCellReuseIdentifier:kRZDisclosureReuseIdentifier];
[_settingsOptionsTableView registerClass:[RZToggleTableViewCell class] forCellReuseIdentifier:kRZToggleReuseIdentifier];
[_settingsOptionsTableView registerClass:[RZVersionInfoTableViewCell class] forCellReuseIdentifier:kRZVersionInfoReuseIdentifier];
[_settingsOptionsTableView registerClass:[RZTextFieldTableViewCell class] forCellReuseIdentifier:kRZTextFieldReuseIdentifier];
[_settingsOptionsTableView registerClass:[RZSliderTableViewCell class] forCellReuseIdentifier:kRZSliderReuseIdentifier];
}
#pragma mark - Model generation methods
- (NSMutableDictionary *)createMetaDataObjectsAndGenerateUserDefaults:(NSArray *)preferences
{
NSMutableDictionary *userSettings = [[NSMutableDictionary alloc] init];
NSString *currentSection = [[NSString alloc] init];
self.groupedSections = [[NSMutableDictionary alloc] init];
self.sectionGroupTitles = [[NSMutableArray alloc] init];
NSDictionary *item = [preferences firstObject];
if ( ![[item objectForKey:kRZKeyType] isEqualToString:kRZGroupSpecifer] ) {
NSMutableArray *rows = [[NSMutableArray alloc] init];
[self.groupedSections setObject:rows forKey:kRZEmptyString];
currentSection = kRZEmptyString;
[self.sectionGroupTitles addObject:kRZEmptyString];
}
for (id settingsItem in preferences) {
NSString *cellTitle = [settingsItem objectForKey:kRZKeyTitle];
NSString *currentSettingsItemType = [settingsItem objectForKey:kRZKeyType];
NSString *plistItemIdentifier = [settingsItem objectForKey:kRZKeyItemIdentifier];
if ( plistItemIdentifier != nil ) {
[self.settingsKeys addObject:plistItemIdentifier];
}
if ( [currentSettingsItemType isEqualToString:kRZMultiValueSpecifier] ) {
NSNumber *cellDefaultValue = [settingsItem objectForKey:kRZKeyDefaultValue];
NSArray *optionTitles = [settingsItem objectForKey:kRZKeyEnvironmentsTitles];
NSArray *optionValues = [settingsItem objectForKey:kRZKeyEnvironmentsValues];
NSAssert((optionTitles.count == optionValues.count && (optionTitles.count > 0 && optionValues.count > 0)), @"The disclosure selection arrays must be of non-zero length and equal length. Check to see in the Plist under your MultiValue item that your Titles and Values items are equal in length and are not 0");
NSMutableArray *selectionItems = [self generateMultiValueOptionsArray:optionTitles withValues:optionValues];
RZDebugMenuMultiValueItem *disclosureTableViewCellMetaData = [[RZDebugMenuMultiValueItem alloc] initWithValue:cellDefaultValue
forKey:plistItemIdentifier
withTitle:cellTitle
andSelectionItems:selectionItems];
NSMutableArray *objectsInSection = [self.groupedSections objectForKey:currentSection];
[objectsInSection addObject:disclosureTableViewCellMetaData];
NSString *multiValueSettingsKey = [RZDebugMenuSettingsInterface generateSettingsKey:plistItemIdentifier];
[userSettings setObject:cellDefaultValue forKey:multiValueSettingsKey];
}
else if ( [currentSettingsItemType isEqualToString:kRZToggleSwitchSpecifier] ) {
NSNumber *defaultValue = [settingsItem objectForKey:kRZKeyDefaultValue];
RZDebugMenuToggleItem *toggleTableViewCellMetaData = [[RZDebugMenuToggleItem alloc] initWithValue:defaultValue
forKey:plistItemIdentifier
withTitle:cellTitle];
NSMutableArray *objectsInSection = [self.groupedSections objectForKey:currentSection];
[objectsInSection addObject:toggleTableViewCellMetaData];
NSString *toggleKey = [RZDebugMenuSettingsInterface generateSettingsKey:plistItemIdentifier];
[userSettings setObject:[settingsItem objectForKey:kRZKeyDefaultValue] forKey:toggleKey];
}
else if ( [currentSettingsItemType isEqualToString:kRZTextFieldSpecifier] ) {
NSString *defaultValue = [settingsItem objectForKey:kRZKeyDefaultValue];
RZDebugMenuTextFieldItem *textFieldTableViewCellMetaData = [[RZDebugMenuTextFieldItem alloc] initWithValue:defaultValue
forKey:plistItemIdentifier
withTitle:cellTitle];
NSMutableArray *objectsInSection = [self.groupedSections objectForKey:currentSection];
[objectsInSection addObject:textFieldTableViewCellMetaData];
NSString *textFieldSettingsKey = [RZDebugMenuSettingsInterface generateSettingsKey:plistItemIdentifier];
[userSettings setObject:defaultValue forKey:textFieldSettingsKey];
}
else if ( [currentSettingsItemType isEqualToString:kRZSliderSpecifier] ) {
NSNumber *defaultValue = [settingsItem objectForKey:kRZKeyDefaultValue];
NSNumber *maximum = [settingsItem objectForKey:kRZKeyMaximumValue];
NSNumber *minimum = [settingsItem objectForKey:kRZKeyMinimumValue];
RZDebugMenuSliderItem *sliderTableViewCellMetaData = [[RZDebugMenuSliderItem alloc] initWithValue:defaultValue
forKey:plistItemIdentifier
withTitle:cellTitle
maxValue:maximum
minValue:minimum];
NSMutableArray *objectsInSection = [self.groupedSections objectForKey:currentSection];
[objectsInSection addObject:sliderTableViewCellMetaData];
NSString *sliderSettingsKey = [RZDebugMenuSettingsInterface generateSettingsKey:plistItemIdentifier];
[userSettings setObject:defaultValue forKey:sliderSettingsKey];
}
else if ( [currentSettingsItemType isEqualToString:kRZGroupSpecifer] ) {
currentSection = cellTitle;
NSMutableArray *rows = [self.groupedSections objectForKey:cellTitle];
if ( !rows ) {
rows = [[NSMutableArray alloc] init];
[self.groupedSections setObject:rows forKey:cellTitle];
}
[self.sectionGroupTitles addObject:cellTitle];
}
}
return userSettings;
}
- (NSMutableArray *)generateMultiValueOptionsArray:(NSArray *)optionTitles withValues:(NSArray *)optionValues
{
NSMutableArray *selectionItems = [[NSMutableArray alloc] init];
for (int i = 0; i < optionTitles.count; i++) {
NSString *title = [optionTitles objectAtIndex:i];
NSNumber *value = [optionValues objectAtIndex:i];
RZMultiValueSelectionItem *selectionItemMetaData = [[RZMultiValueSelectionItem alloc] initWithTitle:title defaultValue:value];
[selectionItems addObject:selectionItemMetaData];
}
return selectionItems;
}
- (void)setUpVersionCellMetaData
{
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:kRZKeyBundleVersionString];
RZDebugMenuVersionItem *versionItem = [[RZDebugMenuVersionItem alloc] initWithTitle:kRZVersionCellTitle andVersionNumber:version];
NSArray *versionItemArray = @[versionItem];
[self.groupedSections setObject:versionItemArray forKey:kRZVersionGroupTitle];
[self.sectionGroupTitles addObject:kRZVersionGroupTitle];
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSUInteger sectionCount = self.sectionGroupTitles.count;
if ( sectionCount == 0 ) {
return 1;
}
else {
return sectionCount;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.sectionGroupTitles objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *cellItems = [self.groupedSections objectForKey:[self.sectionGroupTitles objectAtIndex:section]];
return cellItems.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *currentSection = [self.sectionGroupTitles objectAtIndex:indexPath.section];
NSArray *currentSectionsCells = [self.groupedSections objectForKey:currentSection];
UITableViewCell *cell = nil;
RZDebugMenuSettingsItem *currentMetaDataObject = [currentSectionsCells objectAtIndex:indexPath.row];
if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuMultiValueItem class]] ) {
cell = [self.settingsOptionsTableView dequeueReusableCellWithIdentifier:kRZDisclosureReuseIdentifier forIndexPath:indexPath];
RZDisclosureTableViewCell *disclosureCell = (RZDisclosureTableViewCell *)cell;
NSString *settingsDefaultKey = [self getKeyIdentifierForIndexPath:indexPath];
NSNumber *selectionDefaultValue = [RZDebugMenuSettingsInterface valueForDebugSettingsKey:settingsDefaultKey];
NSInteger defaultValue = [selectionDefaultValue integerValue];
RZDebugMenuMultiValueItem *currentMultiValueItem = (RZDebugMenuMultiValueItem *)currentMetaDataObject;
RZMultiValueSelectionItem *currentSelection = [currentMultiValueItem.selectionItems objectAtIndex:defaultValue];
disclosureCell.textLabel.text = currentMetaDataObject.tableViewCellTitle;
disclosureCell.detailTextLabel.text = currentSelection.selectionTitle;
cell = disclosureCell;
}
else if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuToggleItem class]] ) {
cell = [self.settingsOptionsTableView dequeueReusableCellWithIdentifier:kRZToggleReuseIdentifier forIndexPath:indexPath];
RZToggleTableViewCell *toggleCell = (RZToggleTableViewCell *)cell;
NSString *settingsDefaultKey = [self getKeyIdentifierForIndexPath:indexPath];
NSNumber *toggleSwitchDefaultValue = [RZDebugMenuSettingsInterface valueForDebugSettingsKey:settingsDefaultKey];
cell.textLabel.text = currentMetaDataObject.tableViewCellTitle;
toggleCell.applySettingsSwitch.on = [toggleSwitchDefaultValue boolValue];
cell = toggleCell;
}
else if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuTextFieldItem class]] ) {
cell = [self.settingsOptionsTableView dequeueReusableCellWithIdentifier:kRZTextFieldReuseIdentifier forIndexPath:indexPath];
RZTextFieldTableViewCell *textFieldCell = (RZTextFieldTableViewCell *)cell;
NSString *settingsDefaultKey = [self getKeyIdentifierForIndexPath:indexPath];
NSString *textFieldDefaultValue = [RZDebugMenuSettingsInterface valueForDebugSettingsKey:settingsDefaultKey];
textFieldCell.textLabel.text = currentMetaDataObject.tableViewCellTitle;
textFieldCell.stringTextField.text = textFieldDefaultValue;
cell = textFieldCell;
}
else if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuSliderItem class]] ) {
RZDebugMenuSliderItem *sliderMetaDataObject = (RZDebugMenuSliderItem *)currentMetaDataObject;
cell = [self.settingsOptionsTableView dequeueReusableCellWithIdentifier:kRZSliderReuseIdentifier forIndexPath:indexPath];
RZSliderTableViewCell *sliderCell = (RZSliderTableViewCell *)cell;
NSString *settingsDefaultKey = [self getKeyIdentifierForIndexPath:indexPath];
NSNumber *sliderDefaultValue = [RZDebugMenuSettingsInterface valueForDebugSettingsKey:settingsDefaultKey];
sliderCell.cellSlider.maximumValue = [sliderMetaDataObject.max floatValue];
sliderCell.cellSlider.minimumValue = [sliderMetaDataObject.min floatValue];
[sliderCell.cellSlider setValue:[sliderDefaultValue floatValue]];
cell = sliderCell;
}
else if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuVersionItem class]] ){
cell = [self.settingsOptionsTableView dequeueReusableCellWithIdentifier:kRZVersionInfoReuseIdentifier forIndexPath:indexPath];
cell.textLabel.text = currentMetaDataObject.tableViewCellTitle;
cell.detailTextLabel.text = ((RZDebugMenuVersionItem *)currentMetaDataObject).versionNumber;
}
return cell;
}
- (NSString *)getKeyIdentifierForIndexPath:(NSIndexPath *)indexPath
{
id setting;
NSInteger firstGroupExists = 1;
if ( [[self.sectionGroupTitles firstObject] isEqualToString:kRZEmptyString] ) {
firstGroupExists = 0;
}
if ( indexPath.section == 0 ) {
// plus 1 to the index path if the 'group' item takes up the first spot on the array
setting = [self.preferenceSpecifiers objectAtIndex:indexPath.row+firstGroupExists];
}
else {
unsigned long numberOfPreviousCells = 0;
for (unsigned long i = 0; i < indexPath.section; i++) {
numberOfPreviousCells = numberOfPreviousCells + [self.settingsOptionsTableView numberOfRowsInSection:i];
}
// gets the correct index in the array of settings by taking into account the number of group items and cells before the one that is changing
setting = [self.preferenceSpecifiers objectAtIndex:(indexPath.row+numberOfPreviousCells+indexPath.section+firstGroupExists)];
}
if ( [setting isKindOfClass:[NSDictionary class]] ) {
return [setting objectForKey:kRZKeyItemIdentifier];
}
return nil;
}
- (RZDebugMenuSettingsItem *)settingsItemAtIndexPath:(NSIndexPath *)indexPath
{
if ( indexPath.section >= self.sectionGroupTitles.count ) {
return nil;
}
NSString *currentSection = [self.sectionGroupTitles objectAtIndex:indexPath.section];
NSMutableArray *cellItemsMetaData = [self.groupedSections objectForKey:currentSection];
if ( cellItemsMetaData == nil ) {
return nil;
}
NSUInteger numberOfItems = cellItemsMetaData.count;
if ( indexPath.section >= [self.groupedSections allKeys].count || indexPath.row >= numberOfItems ) {
return nil;
}
return [cellItemsMetaData objectAtIndex:indexPath.row];
}
#pragma mark - reset settings method
- (void)resetTableDefaults
{
[RZDebugMenuSettingsInterface resetDefaultsForSettings:self.groupedSections];
}
@end