-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRZDebugMenuModalViewController.m
More file actions
218 lines (172 loc) · 9.32 KB
/
RZDebugMenuModalViewController.m
File metadata and controls
218 lines (172 loc) · 9.32 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
//
// RZDebugMenuModalViewController.m
// RZDebugMenu
//
// Created by Clayton Rieck on 6/2/14.
// Copyright (c) 2014 Raizlabs. All rights reserved.
//
#import "RZDebugMenuModalViewController.h"
#import "RZDebugMenuSettingsInterface.h"
#import "RZDebugMenuSettingsItem.h"
#import "RZDebugMenuMultiItemListViewController.h"
#import "RZDebugMenuMultiValueItem.h"
#import "RZDebugMenuToggleItem.h"
#import "RZMultiValueSelectionItem.h"
#import "RZDebugMenuTextFieldItem.h"
#import "RZDebugMenuSliderItem.h"
#import "RZDisclosureTableViewCell.h"
#import "RZToggleTableViewCell.h"
#import "RZTextFieldTableViewCell.h"
#import "RZSliderTableViewCell.h"
static NSString * const kRZNavigationBarTitle = @"Settings";
static NSString * const kRZNavigationBarDoneButtonTitle = @"Done";
static NSString * const kRZDisclosureReuseIdentifier = @"environments";
static NSString * const kRZToggleReuseIdentifier = @"toggle";
static NSString * const kRZVersionInfoReuseIdentifier = @"version";
static NSString * const kRZResetButtonTitle = @"Reset";
@interface RZDebugMenuModalViewController ()
<RZDebugMenuMultiItemListViewControllerDelegate,
RZTextFieldTableViewCellDelegate,
RZToggleTableViewCellDelegate,
RZSliderTableViewCellDelegate>
@property (strong, nonatomic) RZDebugMenuSettingsDataSource *debugSettingsDataSource;
@property (strong, nonatomic) UITableView *optionsTableView;
@end
@implementation RZDebugMenuModalViewController
- (id)initWithDataSource:(RZDebugMenuSettingsDataSource *)dataSource
{
self = [super init];
if ( self ) {
self.title = kRZNavigationBarTitle;
_debugSettingsDataSource = dataSource;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
CGFloat width = CGRectGetWidth(self.view.bounds);
CGFloat height = CGRectGetHeight(self.view.bounds);
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:kRZResetButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:@selector(resetSettings)];
self.optionsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, width, height) style:UITableViewStyleGrouped];
self.optionsTableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.optionsTableView];
self.debugSettingsDataSource.settingsOptionsTableView = self.optionsTableView;
self.optionsTableView.delegate = self;
self.optionsTableView.dataSource = self.debugSettingsDataSource;
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:kRZNavigationBarDoneButtonTitle
style:UIBarButtonItemStylePlain
target:self
action:@selector(closeView)];
self.navigationItem.rightBarButtonItem = doneButton;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidAppearOrHide:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidAppearOrHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillAppear:(BOOL)animated
{
NSIndexPath *selectedIndexPath = [self.optionsTableView indexPathForSelectedRow];
if ( selectedIndexPath ) {
[self.optionsTableView deselectRowAtIndexPath:selectedIndexPath animated:YES];
}
}
- (void)keyboardDidAppearOrHide:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
UIEdgeInsets newTableViewEdgeInsets = self.optionsTableView.contentInset;
CGFloat keyboardHeight = keyboardFrame.size.height;
newTableViewEdgeInsets.bottom = keyboardHeight - newTableViewEdgeInsets.bottom;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionBeginFromCurrentState | animationCurve
animations:^{
self.optionsTableView.contentInset = newTableViewEdgeInsets;
}
completion:nil];
}
#pragma mark - nav bar buttons methods
- (void)closeView
{
[[NSUserDefaults standardUserDefaults] synchronize];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)resetSettings
{
[self.debugSettingsDataSource resetTableDefaults];
[self.optionsTableView reloadData];
}
#pragma mark - table view delegate methods
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ( [cell isKindOfClass:[RZToggleTableViewCell class]] ) {
RZToggleTableViewCell *toggleCell = (RZToggleTableViewCell *)cell;
toggleCell.delegate = self;
}
else if ( [cell isKindOfClass:[RZTextFieldTableViewCell class]] ) {
RZTextFieldTableViewCell *textFieldCell = (RZTextFieldTableViewCell *)cell;
textFieldCell.delegate = self;
}
else if ( [cell isKindOfClass:[RZSliderTableViewCell class]] ) {
RZSliderTableViewCell *sliderCell = (RZSliderTableViewCell *)cell;
sliderCell.delegate = self;
}
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RZDebugMenuSettingsItem *currentMetaDataObject = [self.debugSettingsDataSource settingsItemAtIndexPath:indexPath];
if ( [currentMetaDataObject isKindOfClass:[RZDebugMenuMultiValueItem class]] ) {
RZDebugMenuMultiValueItem *disclosureCellOptions = (RZDebugMenuMultiValueItem *)currentMetaDataObject;
NSString *multiValueKey = disclosureCellOptions.settingsKey;
id currentSelection = [RZDebugMenuSettingsInterface valueForDebugSettingsKey:multiValueKey];
NSArray *disclosureCellSelectableItems = disclosureCellOptions.selectionItems;
NSInteger indexOfCurrentValue;
for (int i = 0; i < disclosureCellSelectableItems.count; i++) {
RZMultiValueSelectionItem *item = [disclosureCellSelectableItems objectAtIndex:i];
if ( [currentSelection isEqual:item.selectionValue] ) {
indexOfCurrentValue = i;
break;
}
}
RZDebugMenuMultiItemListViewController *environmentsViewController = [[RZDebugMenuMultiItemListViewController alloc] initWithSelectionItems:disclosureCellSelectableItems delegate:self andSelectedRow:indexOfCurrentValue];
[self.navigationController pushViewController:environmentsViewController animated:YES];
}
}
#pragma mark - table view cell delegate methods
- (void)multiItemListDidMakeNewSelectionAtIndexPath:(RZMultiValueSelectionItem *)selectedItem
{
NSIndexPath *selectedIndexPath = [self.optionsTableView indexPathForSelectedRow];
RZDebugMenuMultiValueItem *disclosureMultiValueItem = (RZDebugMenuMultiValueItem *)[self.debugSettingsDataSource settingsItemAtIndexPath:selectedIndexPath];
[RZDebugMenuSettingsInterface setValue:selectedItem.selectionValue forDebugSettingsKey:disclosureMultiValueItem.settingsKey];
RZDisclosureTableViewCell *selectedCell = (RZDisclosureTableViewCell *)[self.optionsTableView cellForRowAtIndexPath:selectedIndexPath];
selectedCell.detailTextLabel.text = selectedItem.selectionTitle;
}
- (void)didChangeToggleStateOfCell:(RZToggleTableViewCell *)cell
{
NSIndexPath *toggleCellIndexPath = [self.optionsTableView indexPathForCell:cell];
RZDebugMenuToggleItem *toggleItem = (RZDebugMenuToggleItem *)[self.debugSettingsDataSource settingsItemAtIndexPath:toggleCellIndexPath];
[RZDebugMenuSettingsInterface setValue:[NSNumber numberWithBool:cell.applySettingsSwitch.on] forDebugSettingsKey:toggleItem.settingsKey];
}
- (void)didEditTextLabelOfCell:(RZTextFieldTableViewCell *)cell
{
NSIndexPath *textFieldIndexPath = [self.optionsTableView indexPathForCell:cell];
RZDebugMenuTextFieldItem *textFieldItem = (RZDebugMenuTextFieldItem *)[self.debugSettingsDataSource settingsItemAtIndexPath:textFieldIndexPath];
[RZDebugMenuSettingsInterface setValue:cell.stringTextField.text forDebugSettingsKey:textFieldItem.settingsKey];
}
- (void)didChangeSliderPosition:(RZSliderTableViewCell *)cell
{
NSIndexPath *sliderIndexPath = [self.optionsTableView indexPathForCell:cell];
RZDebugMenuSliderItem *sliderItem = (RZDebugMenuSliderItem *)[self.debugSettingsDataSource settingsItemAtIndexPath:sliderIndexPath];
[RZDebugMenuSettingsInterface setValue:[NSNumber numberWithFloat:cell.cellSlider.value] forDebugSettingsKey:sliderItem.settingsKey];
}
@end