-
Notifications
You must be signed in to change notification settings - Fork 0
/
CQFormItem.m
523 lines (420 loc) · 11.5 KB
/
CQFormItem.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
Copyright (C) 2012 Quentin Mathe
Author: Quentin Mathe <quentin.mathe@gmail.com>
Date: June 2012
License: MIT
*/
#import "CQFormItem.h"
#import "CQFormCell.h"
#import "CQMacros.h"
NSString * const CQFormItemUndefinedValue = @"CQFormItemUndefinedValue";
@interface CQFormItem ()
- (void)setViewLabel: (NSString *)aLabel;
@end
@implementation CQFormItem
{
BOOL _isChangingEditorValue;
}
#pragma mark - Initialization
- (instancetype)initWithView: (UIView *)aView
{
NILARG_EXCEPTION_TEST(aView);
SUPERINIT;
_view = aView;
_sectionName = [[self class] defaultSectionName];
_canRefresh = YES;
[self disableRefresh];
self.refreshBlock = ^(CQFormItem *item)
{
UIView *editor = [item editorForView: item.view];
[item setViewLabel: item.label];
[item refreshObjectValueForEditor: editor];
[item refreshDetailTextLabel];
};
[self enableRefresh];
[self updateControlEvents];
return self;
}
- (instancetype)init
{
CQFormCell *formCell = (CQFormCell *)[[NSBundle mainBundle]
loadNibNamed: @"CQFormCell" owner: self options: nil];
formCell.selectionStyle = UITableViewCellSelectionStyleNone;
return [self initWithView: formCell];
}
- (void)dealloc
{
[self stopControlEventObservation];
self.observedKeyPaths = [NSSet set];
}
#pragma mark - Debugging
- (NSString *)description
{
return [[super description] stringByAppendingFormat: @" - %@ in %@",
self.keyPath, self.representedObject];
}
#pragma mark - Key-Value Observation
- (void)observeValueForKeyPath: (NSString *)keyPath
ofObject: (id)object
change: (NSDictionary *)change
context: (void *)context
{
NSParameterAssert([_observedKeyPaths containsObject: keyPath]);
/*NSLog(@"Did change value of %@ from %@ to %@", object,
[change objectForKey: NSKeyValueChangeOldKey],
[change objectForKey: NSKeyValueChangeNewKey]);*/
[self refreshViewFromRepresentedObject];
}
- (void)setObservedKeyPaths: (NSSet *)keyPaths
{
for (NSString *keyPath in _observedKeyPaths)
{
[self removeObserver: self forKeyPath: keyPath];
}
_observedKeyPaths = [keyPaths copy];
for (NSString *keyPath in _observedKeyPaths)
{
[self addObserver: self
forKeyPath: keyPath
options: NSKeyValueObservingOptionNew
context: NULL];
}
}
- (void)updateObservedKeyPaths
{
NSMutableSet *keyPaths = [NSMutableSet new];
if (self.keyPath != nil)
{
[keyPaths addObject: [@"representedObject." stringByAppendingString: self.keyPath]];
}
self.observedKeyPaths = keyPaths;
}
#pragma mark - Form Configuration
- (void)setView: (UIView *)aView
{
NILARG_EXCEPTION_TEST(aView);
[self stopControlEventObservation];
_view = aView;
[self refreshViewFromRepresentedObject];
/* Will call -resetControlEventObservation */
[self updateControlEvents];
}
- (void)setRepresentedObject: (id)anObject
{
_representedObject = anObject;
[self refreshViewFromRepresentedObject];
}
+ (NSString *)defaultSectionName
{
return @"";
}
- (void)setSectionName: (NSString *)aName
{
NILARG_EXCEPTION_TEST(aName);
_sectionName = [aName copy];
}
- (void)setKeyPath: (NSSet *)aKeyPath
{
NILARG_EXCEPTION_TEST(aKeyPath);
_keyPath = [aKeyPath copy];
[self updateObservedKeyPaths];
[self refreshViewFromRepresentedObject];
}
- (id)value
{
if (self.representedObject == nil)
return CQFormItemUndefinedValue;
id value = nil;
@try
{
value = [self.representedObject valueForKeyPath: self.keyPath];
}
@catch (NSException *exception)
{
value = CQFormItemUndefinedValue;
}
return value;
}
- (void)setValue:(id)aValue
{
//NSLog(@"== Value changing from %@ to %@ for %@ ==", self.value, aValue, self.representedObject);
// TODO: Move the next line into a updateBlock and call instead -updateRepresentedObjectFromView
[self.representedObject setValue: aValue
forKeyPath: self.keyPath];
/* This ensures that all the views are refreshed, whether -setValue: was
triggered by user interaction or called directly.
When a user interaction triggered it, then the value editor is not
refreshed but other views might be e.g. detail text label. */
[self refreshViewFromRepresentedObject];
}
- (void)setViewLabel: (NSString *)aLabel
{
if ([self.view conformsToProtocol: @protocol(CQValueEditor)])
{
((id <CQValueEditor>)self.view).valueLabel.text = aLabel;
}
else if ([self.view isKindOfClass: [UITableViewCell class]])
{
((UITableViewCell *)self.view).textLabel.text = aLabel;
}
}
- (void)setLabel: (NSString *)aLabel
{
_label = [aLabel copy];
[self setViewLabel: aLabel];
}
#pragma mark - Refreshing View
- (void)setRefreshBlock: (CQFormItemActionBlock)aBlock
{
_refreshBlock = [aBlock copy];
[self refreshViewFromRepresentedObject];
}
- (void)refreshViewFromRepresentedObject
{
if (!self.canRefresh || _refreshBlock == NULL)
return;
_refreshBlock(self);
}
/**
* For a nil represented object, the detail text label is not set to
* 'CQFormItemUndefinedValue' to avoid setting this value on the first refresh
* by the initializer, otherwise this requires any custom refresh code to always
* set the detail text label.
*/
- (void)refreshDetailTextLabel
{
if ([self.view isKindOfClass: [UITableViewCell class]] && self.representedObject != nil)
{
((UITableViewCell *)self.view).detailTextLabel.text = [self stringFromObjectValue: self.value];
}
}
- (void)refreshObjectValueForEditor: (UIView *)aValueEditor
{
if (_isChangingEditorValue)
return;
BOOL isSlider = [aValueEditor respondsToSelector: @selector(setValue:)];
BOOL isSwitch = [aValueEditor respondsToSelector: @selector(setOn:)];
BOOL isText = [aValueEditor respondsToSelector: @selector(setText:)];
if (isSlider)
{
[aValueEditor setValue: self.value forKey: @"value"];
}
else if (isSwitch)
{
id boolValue = [self.value isEqual: CQFormItemUndefinedValue] ? @(NO) : self.value;
[aValueEditor setValue: boolValue forKey: @"on"];
}
else if (isText)
{
[aValueEditor setValue: [self stringFromObjectValue: self.value] forKey: @"text"];
}
}
- (void)enableRefresh
{
NSAssert(!_canRefresh, @"Refresh is already enabled.");
_canRefresh = YES;
[self refreshViewFromRepresentedObject];
}
- (void)disableRefresh
{
NSAssert(_canRefresh, @"Refresh is already disabled.");
_canRefresh = NO;
}
/*- (void)enableRefresh
{
[super enableRefresh];
[self resetControlEventObservation];
}
- (void)disableRefresh
{
self.changeEvents = 0;
}*/
- (UIView *)editorForView: (UIView *)aView
{
if ([aView conformsToProtocol: @protocol(CQValueEditor)])
{
return [(id <CQValueEditor>)aView valueEditor];
}
return nil;
}
#pragma mark - Updating Representing Object
- (void)setUpdateBlock: (CQFormItemActionBlock)aBlock
{
_refreshBlock = [aBlock copy];
[self updateRepresentedObjectFromView];
}
- (void)updateRepresentedObjectFromView
{
if (_updateBlock == NULL)
return;
_updateBlock(self);
}
- (NSString *)stringFromObjectValue: (id)aValue
{
if (self.formatter == nil)
{
return ([aValue isKindOfClass: [NSString class]] ? aValue : nil);
}
if (aValue == nil)
return nil;
id value = [self.formatter stringForObjectValue: aValue];
NSAssert(value != nil, @"The formatter must not return nil.");
return value;
}
#pragma mark - Converting Value between View and Model
- (id)objectValueFromString: (NSString *)aValue
{
if (self.formatter == nil)
{
return aValue;
}
id value = nil;
NSString *errorDesc = nil;
[self.formatter getObjectValue: &value
forString: aValue
errorDescription: &errorDesc];
NSAssert(errorDesc == nil, @"The editor value must be validated by the "
"formatter while the user is editing.");
return value;
}
- (id)objectValueFromEditor: (UIView *)aValueEditor
{
id value = nil;
BOOL isSlider = [aValueEditor respondsToSelector: @selector(value)];
BOOL isSwitch = [aValueEditor respondsToSelector: @selector(isOn)];
BOOL isText = [aValueEditor respondsToSelector: @selector(text)];
if (isSlider)
{
value = [aValueEditor valueForKey: @"value"];
}
else if (isSwitch)
{
value = [aValueEditor valueForKey: @"on"];
}
else if (isText)
{
value = [self objectValueFromString: [aValueEditor valueForKey: @"text"]];
}
return value;
}
#pragma mark - Control Event Observation
- (void)updateControlEvents
{
/* Will update the view target/actions */
self.changeEvents = UIControlEventValueChanged | UIControlEventEditingChanged;
if ([[self editorForView: self.view] conformsToProtocol: @protocol(UITextInput)])
{
self.beginEditingEvents = UIControlEventEditingDidBegin;
// NOTE: Don't include .EditingDidEndOnExit, to prevent sending the action twice
self.endEditingEvents = UIControlEventEditingDidEnd;
}
else
{
self.beginEditingEvents = UIControlEventTouchDown;
self.endEditingEvents = UIControlEventTouchUpOutside | UIControlEventTouchUpInside;
}
[self resetControlEventObservation];
}
/**
* Prevents view to send actions to on incorrect or deallocated item, when the
* view is reused by -[UITableView dequeueReusableCellWithIdentifier:].
*
* See also -dealloc.
*/
- (void)stopControlEventObservation
{
id editor = [self editorForView: self.view];
if ([editor isKindOfClass: [UIControl class]])
{
[editor removeTarget: self
action: NULL
forControlEvents: UIControlEventAllEvents];
}
}
- (void)resetControlEventObservation
{
[self stopControlEventObservation];
id editor = [self editorForView: self.view];
if ([editor isKindOfClass: [UIControl class]])
{
if (self.beginEditingEvents != 0)
{
//NSLog(@"Will call -editorDidBeginEditing: for %@ on %lu", self, (unsigned long)self.beginEditingEvents);
[editor addTarget: self
action: @selector(editorDidBeginEditing:)
forControlEvents: self.beginEditingEvents];
}
if (self.changeEvents != 0)
{
//NSLog(@"Will call -editorDidChangeValue: for %@ on %lu", self, (unsigned long)self.changeEvents);
[editor addTarget: self
action: @selector(editorDidChangeValue:)
forControlEvents: self.changeEvents];
}
if (self.endEditingEvents != 0)
{
//NSLog(@"Will call -editorDidEndEditing for %@ on %lu", self, (unsigned long)self.endEditingEvents);
[editor addTarget: self
action: @selector(editorDidEndEditing:)
forControlEvents: self.endEditingEvents];
}
}
}
#pragma mark - Editing
- (void)setBeginEditingEvents: (UIControlEvents)events
{
_beginEditingEvents = events;
[self resetControlEventObservation];
}
- (void)editorDidBeginEditing: (UIControl *)aValueEditor
{
}
- (void)setChangeEvents: (UIControlEvents)events
{
_changeEvents = events;
[self resetControlEventObservation];
}
- (void)editorDidChangeValue: (UIView *)aValueEditor
{
_isChangingEditorValue = YES;
self.value = [self objectValueFromEditor: aValueEditor];
_isChangingEditorValue = NO;
}
- (void)setEndEditingEvents: (UIControlEvents)events
{
_endEditingEvents = events;
[self resetControlEventObservation];
}
- (void)endEditing
{
// NOTE: Probably not needed, because this is -changeEvents responsability.
// At least, must come before -endEditing: to prevent damaging the editing
// context after any commit triggered by -endEditing:.
// [self editorDidChangeValue: [self editorForView: self.view]];
[self.view endEditing: YES];
}
- (void)editorDidEndEditing: (UIControl *)aValueEditor
{
if (self.formatter == nil)
return;
[self refreshObjectValueForEditor: [self editorForView: self.view]];
}
#pragma mark - Selection and Highlighting
- (BOOL)isHighlightable
{
return (self.contentViewController != nil || self.selectBlock != NULL);
}
- (BOOL)isChecked
{
return NO;
}
- (void)didSelect
{
if (self.selectBlock == NULL)
return;
self.selectBlock(self);
}
- (void)didDeselect
{
}
@end