-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathFLEXNetworkSettingsController.m
More file actions
263 lines (213 loc) · 9.58 KB
/
FLEXNetworkSettingsController.m
File metadata and controls
263 lines (213 loc) · 9.58 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
//
// FLEXNetworkSettingsController.m
// FLEXInjected
//
// Created by Ryan Olson on 2/20/15.
//
#import "FLEXNetworkSettingsController.h"
#import "FLEXNetworkObserver.h"
#import "FLEXNetworkRecorder.h"
#import "FLEXUtility.h"
#import "FLEXTableView.h"
#import "FLEXColor.h"
#import "NSUserDefaults+FLEX.h"
@interface FLEXNetworkSettingsController () <UIActionSheetDelegate>
@property (nonatomic) float cacheLimitValue;
@property (nonatomic, readonly) NSString *cacheLimitCellTitle;
@property (nonatomic, readonly) UISwitch *observerSwitch;
@property (nonatomic, readonly) UISwitch *cacheMediaSwitch;
@property (nonatomic, readonly) UISwitch *jsonViewerSwitch;
@property (nonatomic, readonly) UISlider *cacheLimitSlider;
@property (nonatomic) UILabel *cacheLimitLabel;
@property (nonatomic) NSMutableArray<NSString *> *hostDenylist;
@end
@implementation FLEXNetworkSettingsController
- (void)viewDidLoad {
[super viewDidLoad];
// Force LTR layout for network settings screen
self.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.tableView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
self.navigationController.view.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
[self disableToolbar];
self.hostDenylist = FLEXNetworkRecorder.defaultRecorder.hostDenylist.mutableCopy;
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
_observerSwitch = [UISwitch new];
_cacheMediaSwitch = [UISwitch new];
_jsonViewerSwitch = [UISwitch new];
_cacheLimitSlider = [UISlider new];
self.observerSwitch.on = FLEXNetworkObserver.enabled;
[self.observerSwitch addTarget:self
action:@selector(networkDebuggingToggled:)
forControlEvents:UIControlEventValueChanged
];
self.cacheMediaSwitch.on = FLEXNetworkRecorder.defaultRecorder.shouldCacheMediaResponses;
[self.cacheMediaSwitch addTarget:self
action:@selector(cacheMediaResponsesToggled:)
forControlEvents:UIControlEventValueChanged
];
self.jsonViewerSwitch.on = defaults.flex_registerDictionaryJSONViewerOnLaunch;
[self.jsonViewerSwitch addTarget:self
action:@selector(jsonViewerSettingToggled:)
forControlEvents:UIControlEventValueChanged
];
[self.cacheLimitSlider addTarget:self
action:@selector(cacheLimitAdjusted:)
forControlEvents:UIControlEventValueChanged
];
UISlider *slider = self.cacheLimitSlider;
self.cacheLimitValue = FLEXNetworkRecorder.defaultRecorder.responseCacheByteLimit;
const NSUInteger fiftyMega = 50 * 1024 * 1024;
slider.minimumValue = 0;
slider.maximumValue = fiftyMega;
slider.value = self.cacheLimitValue;
}
- (void)setCacheLimitValue:(float)cacheLimitValue {
_cacheLimitValue = cacheLimitValue;
self.cacheLimitLabel.text = self.cacheLimitCellTitle;
[FLEXNetworkRecorder.defaultRecorder setResponseCacheByteLimit:cacheLimitValue];
}
- (NSString *)cacheLimitCellTitle {
NSInteger cacheLimit = self.cacheLimitValue;
NSInteger limitInMB = round(cacheLimit / (1024 * 1024));
return [NSString stringWithFormat:@"Cache Limit (%@ MB)", @(limitInMB)];
}
#pragma mark - Settings Actions
- (void)networkDebuggingToggled:(UISwitch *)sender {
FLEXNetworkObserver.enabled = sender.isOn;
}
- (void)cacheMediaResponsesToggled:(UISwitch *)sender {
FLEXNetworkRecorder.defaultRecorder.shouldCacheMediaResponses = sender.isOn;
}
- (void)jsonViewerSettingToggled:(UISwitch *)sender {
[NSUserDefaults.standardUserDefaults flex_toggleBoolForKey:kFLEXDefaultsRegisterJSONExplorerKey];
}
- (void)cacheLimitAdjusted:(UISlider *)sender {
self.cacheLimitValue = sender.value;
}
#pragma mark - Table View Data Source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0: return 5;
case 1: return self.hostDenylist.count;
default: return 0;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0: return @"General";
case 1: return @"Host Denylist";
default: return nil;
}
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if (section == 0) {
return @"By default, JSON is rendered in a webview. Turn on "
"\"View JSON as a dictionary/array\" to convert JSON payloads "
"to objects and view them in an object explorer. "
"This setting requires a restart of the app.";
}
return nil;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.tableView
dequeueReusableCellWithIdentifier:kFLEXDefaultCell forIndexPath:indexPath
];
cell.accessoryView = nil;
cell.textLabel.textColor = FLEXColor.primaryTextColor;
// Force LTR layout for network settings cells
cell.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
cell.contentView.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
cell.textLabel.semanticContentAttribute = UISemanticContentAttributeForceLeftToRight;
switch (indexPath.section) {
// Settings
case 0: {
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Network Debugging";
cell.accessoryView = self.observerSwitch;
break;
case 1:
cell.textLabel.text = @"Cache Media Responses";
cell.accessoryView = self.cacheMediaSwitch;
break;
case 2:
cell.textLabel.text = @"View JSON as a dictionary/array";
cell.accessoryView = self.jsonViewerSwitch;
break;
case 3:
cell.textLabel.text = @"Reset Host Denylist";
cell.textLabel.textColor = tableView.tintColor;
break;
case 4:
cell.textLabel.text = self.cacheLimitCellTitle;
self.cacheLimitLabel = cell.textLabel;
[self.cacheLimitSlider removeFromSuperview];
[cell.contentView addSubview:self.cacheLimitSlider];
CGRect container = cell.contentView.frame;
UISlider *slider = self.cacheLimitSlider;
[slider sizeToFit];
CGFloat sliderWidth = 150.f;
CGFloat sliderOriginY = FLEXFloor((container.size.height - slider.frame.size.height) / 2.0);
CGFloat sliderOriginX = CGRectGetMaxX(container) - sliderWidth - tableView.separatorInset.left;
self.cacheLimitSlider.frame = CGRectMake(
sliderOriginX, sliderOriginY, sliderWidth, slider.frame.size.height
);
// Make wider, keep in middle of cell, keep to trailing edge of cell
self.cacheLimitSlider.autoresizingMask = ({
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin;
});
break;
}
break;
}
// Denylist entries
case 1: {
cell.textLabel.text = self.hostDenylist[indexPath.row];
break;
}
default:
@throw NSInternalInconsistencyException;
break;
}
return cell;
}
#pragma mark - Table View Delegate
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)ip {
// Can only select the "Reset Host Denylist" row
return ip.section == 0 && ip.row == 2;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[FLEXAlert makeAlert:^(FLEXAlert *make) {
make.title(@"Reset Host Denylist");
make.message(@"You cannot undo this action. Are you sure?");
make.button(@"Reset").destructiveStyle().handler(^(NSArray<NSString *> *strings) {
self.hostDenylist = nil;
[FLEXNetworkRecorder.defaultRecorder.hostDenylist removeAllObjects];
[FLEXNetworkRecorder.defaultRecorder synchronizeDenylist];
[self.tableView deleteSections:
[NSIndexSet indexSetWithIndex:1]
withRowAnimation:UITableViewRowAnimationAutomatic];
});
make.button(@"Cancel").cancelStyle();
} showFrom:self];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return indexPath.section == 1;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)style
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSParameterAssert(style == UITableViewCellEditingStyleDelete);
NSString *host = self.hostDenylist[indexPath.row];
[self.hostDenylist removeObjectAtIndex:indexPath.row];
[FLEXNetworkRecorder.defaultRecorder.hostDenylist removeObject:host];
[FLEXNetworkRecorder.defaultRecorder synchronizeDenylist];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end