This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
NIRadioGroup.m
334 lines (269 loc) · 10.2 KB
/
NIRadioGroup.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
//
// Copyright 2011-2014 NimbusKit
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "NIRadioGroup.h"
#import "NIRadioGroupController.h"
#import "NITableViewModel.h"
#import "NimbusCore.h"
#import <objc/runtime.h>
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "Nimbus requires ARC support."
#endif
static const NSInteger kInvalidSelection = NSIntegerMin;
@interface NIRadioGroup()
@property (nonatomic, readonly, weak) UIViewController* controller;
@property (nonatomic, readonly, strong) NSMutableDictionary* objectMap;
@property (nonatomic, readonly, strong) NSMutableSet* objectSet;
@property (nonatomic, readonly, strong) NSMutableArray* objectOrder;
@property (nonatomic, assign) BOOL hasSelection;
@property (nonatomic, readonly, strong) NSMutableSet* forwardDelegates;
@end
@implementation NIRadioGroup
@synthesize selectedIdentifier = _selectedIdentifier;
- (id)initWithController:(UIViewController *)controller {
if ((self = [super init])) {
_controller = controller;
_objectMap = [[NSMutableDictionary alloc] init];
_objectSet = [[NSMutableSet alloc] init];
_objectOrder = [[NSMutableArray alloc] init];
_forwardDelegates = NICreateNonRetainingMutableSet();
_tableViewCellSelectionStyle = UITableViewCellSelectionStyleBlue;
}
return self;
}
- (id)init {
return [self initWithController:nil];
}
#pragma mark - Private
- (id)keyForIdentifier:(NSInteger)identifier {
return [NSNumber numberWithInteger:identifier];
}
#pragma mark - NICellObject
- (Class)cellClass {
return [NIRadioGroupCell class];
}
- (UITableViewCellStyle)cellStyle {
return UITableViewCellStyleValue1;
}
#pragma mark - Forward Invocations
- (BOOL)shouldForwardSelector:(SEL)selector {
struct objc_method_description description;
description = protocol_getMethodDescription(@protocol(UITableViewDelegate), selector, NO, YES);
return (description.name != NULL && description.types != NULL);
}
- (BOOL)respondsToSelector:(SEL)selector {
if ([super respondsToSelector:selector]) {
return YES;
} else if ([self shouldForwardSelector:selector]) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:selector]) {
return YES;
}
}
}
return NO;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
NSMethodSignature *signature = [super methodSignatureForSelector:selector];
if (signature == nil) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:selector]) {
signature = [delegate methodSignatureForSelector:selector];
}
}
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)invocation {
BOOL didForward = NO;
if ([self shouldForwardSelector:invocation.selector]) {
for (id delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:invocation.selector]) {
[invocation invokeWithTarget:delegate];
didForward = YES;
break;
}
}
}
if (!didForward) {
[super forwardInvocation:invocation];
}
}
- (id<UITableViewDelegate>)forwardingTo:(id<UITableViewDelegate>)forwardDelegate {
[self.forwardDelegates addObject:forwardDelegate];
return self;
}
- (void)removeForwarding:(id<UITableViewDelegate>)forwardDelegate {
[self.forwardDelegates removeObject:forwardDelegate];
}
#pragma mark - Public
- (id)mapObject:(id)object toIdentifier:(NSInteger)identifier {
NIDASSERT(nil != object);
NIDASSERT(identifier != kInvalidSelection);
NIDASSERT(![self isObjectInRadioGroup:object]);
if (nil == object) {
return nil;
}
if (kInvalidSelection == identifier) {
return nil;
}
if ([self isObjectInRadioGroup:object]) {
return nil;
}
[self.objectMap setObject:object forKey:[self keyForIdentifier:identifier]];
[self.objectSet addObject:object];
[self.objectOrder addObject:object];
return object;
}
- (void)setSelectedIdentifier:(NSInteger)selectedIdentifier {
id key = [self keyForIdentifier:selectedIdentifier];
NIDASSERT(nil != [self.objectMap objectForKey:key]);
if (nil != [self.objectMap objectForKey:key]) {
_selectedIdentifier = selectedIdentifier;
self.hasSelection = YES;
} else {
// If we set an invalid identifier then clear the current selection.
self.hasSelection = NO;
}
}
- (NSInteger)selectedIdentifier {
return self.hasSelection ? _selectedIdentifier : kInvalidSelection;
}
- (void)clearSelection {
self.hasSelection = NO;
}
- (BOOL)isObjectInRadioGroup:(id)object {
if (nil == object) {
return NO;
}
return [self.objectSet containsObject:object];
}
- (BOOL)isObjectSelected:(id)object {
if (nil == object) {
return NO;
}
NIDASSERT(nil != object);
NIDASSERT([self isObjectInRadioGroup:object]);
NSArray* keys = [self.objectMap allKeysForObject:object];
NSInteger selectedIdentifier = self.selectedIdentifier;
for (NSNumber* key in keys) {
if ([key intValue] == selectedIdentifier) {
return YES;
}
}
return NO;
}
- (NSInteger)identifierForObject:(id)object {
if (nil == object) {
return NO;
}
NIDASSERT(nil != object);
NIDASSERT([self isObjectInRadioGroup:object]);
NSArray* keys = [self.objectMap allKeysForObject:object];
return keys.count > 0 ? [[keys objectAtIndex:0] intValue] : kInvalidSelection;
}
- (NSArray *)allObjects {
return [self.objectOrder copy];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NIDASSERT([tableView.dataSource isKindOfClass:[NITableViewModel class]]);
if ([tableView.dataSource isKindOfClass:[NITableViewModel class]]) {
NITableViewModel* model = (NITableViewModel *)tableView.dataSource;
id object = [model objectAtIndexPath:indexPath];
if ([self isObjectInRadioGroup:object]) {
cell.accessoryType = ([self isObjectSelected:object]
? UITableViewCellAccessoryCheckmark
: UITableViewCellAccessoryNone);
cell.selectionStyle = self.tableViewCellSelectionStyle;
}
}
// Forward the invocation along.
for (id<UITableViewDelegate> delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:_cmd]) {
[delegate tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NIDASSERT([tableView.dataSource isKindOfClass:[NITableViewModel class]]);
if ([tableView.dataSource isKindOfClass:[NITableViewModel class]]) {
NITableViewModel* model = (NITableViewModel *)tableView.dataSource;
id object = [model objectAtIndexPath:indexPath];
if (object == self) {
// You must provide a controller in the initWithController: initializer.
NIDASSERT(nil != self.controller);
NIRadioGroupController* controller = [[NIRadioGroupController alloc] initWithRadioGroup:self tappedCell:(id<NICell>)[tableView cellForRowAtIndexPath:indexPath]];
controller.title = self.controllerTitle;
BOOL shouldPush = YES;
// Notify the delegate that the controller is about to appear.
if ([self.delegate respondsToSelector:@selector(radioGroup:radioGroupController:willAppear:)]) {
shouldPush = [self.delegate radioGroup:self radioGroupController:controller willAppear:YES];
}
if (shouldPush) {
[self.controller.navigationController pushViewController:controller animated:YES];
}
} else if ([self isObjectInRadioGroup:object]) {
NSInteger newSelection = [self identifierForObject:object];
if (newSelection != self.selectedIdentifier) {
[self setSelectedIdentifier:newSelection];
// It's easiest to simply reload the visible table cells. Reloading only the radio group
// cells would require iterating through the visible cell objects and determining whether
// each was in the radio group. This is more complex behavior that should be relegated to
// the controller.
[tableView reloadRowsAtIndexPaths:tableView.indexPathsForVisibleRows
withRowAnimation:UITableViewRowAnimationNone];
// After we reload the table view the selection will be lost, so set the selection again.
[tableView selectRowAtIndexPath:indexPath
animated:NO
scrollPosition:UITableViewScrollPositionNone];
[self.delegate radioGroup:self didSelectIdentifier:newSelection];
}
// Fade the selection out.
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
// Forward the invocation along.
for (id<UITableViewDelegate> delegate in self.forwardDelegates) {
if ([delegate respondsToSelector:_cmd]) {
[delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
}
}
@end
@implementation NIRadioGroupCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
return self;
}
- (void)prepareForReuse {
[super prepareForReuse];
self.textLabel.text = nil;
self.detailTextLabel.text = nil;
}
- (BOOL)shouldUpdateCellWithObject:(NIRadioGroup *)radioGroup {
self.selectionStyle = radioGroup.tableViewCellSelectionStyle;
// You should provide a cell title for the radio group.
NIDASSERT(NIIsStringWithAnyText(radioGroup.cellTitle));
self.textLabel.text = radioGroup.cellTitle;
if ([radioGroup.delegate respondsToSelector:@selector(radioGroup:textForIdentifier:)]) {
self.detailTextLabel.text = [radioGroup.delegate radioGroup:radioGroup
textForIdentifier:radioGroup.selectedIdentifier];
}
return YES;
}
@end