forked from aral/DocSets-for-iOS
/
DocSetViewController.m
436 lines (372 loc) · 15.5 KB
/
DocSetViewController.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
//
// DocSetViewController.m
// DocSets
//
// Created by Ole Zorn on 05.12.10.
// Copyright 2010 omz:software. All rights reserved.
//
#import "DocSetViewController.h"
#import "DocSet.h"
#import "DetailViewController.h"
#define SEARCH_SPINNER_TAG 1
@interface DocSetViewController ()
- (void)reloadSearchResults;
- (void)openNode:(NSManagedObject *)node;
@end
@implementation DocSetViewController
@synthesize docSet, rootNode, detailViewController, searchResults, searchDisplayController;
- (id)initWithDocSet:(DocSet *)set rootNode:(NSManagedObject *)node
{
self = [super initWithStyle:UITableViewStylePlain];
docSet = set;
rootNode = node;
nodeSections = [docSet nodeSectionsForRootNode:rootNode];
self.title = (rootNode != nil) ? [rootNode valueForKey:@"kName"] : docSet.title;
self.contentSizeForViewInPopover = CGSizeMake(400.0, 1024.0);
iconsByTokenType = [[NSDictionary alloc] initWithObjectsAndKeys:
[UIImage imageNamed:@"Const"], @"econst",
[UIImage imageNamed:@"Member.png"], @"intfm",
[UIImage imageNamed:@"Macro.png"], @"macro",
[UIImage imageNamed:@"Type.png"], @"tdef",
[UIImage imageNamed:@"Class.png"], @"cat",
[UIImage imageNamed:@"Property.png"], @"intfp",
[UIImage imageNamed:@"Const.png"], @"clconst",
[UIImage imageNamed:@"Protocol.png"], @"intf",
[UIImage imageNamed:@"Member.png"], @"instm",
[UIImage imageNamed:@"Class.png"], @"cl",
[UIImage imageNamed:@"Struct.png"], @"tag",
[UIImage imageNamed:@"Member.png"], @"clm",
[UIImage imageNamed:@"Property.png"], @"instp",
[UIImage imageNamed:@"Function.png"], @"func",
[UIImage imageNamed:@"Global.png"], @"data",
nil];
self.clearsSelectionOnViewWillAppear = YES;
return self;
}
- (void)loadView
{
[super loadView];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
searchBar.scopeButtonTitles = [NSArray arrayWithObjects:NSLocalizedString(@"API",nil), NSLocalizedString(@"Title",nil), nil];
searchBar.selectedScopeButtonIndex = 0;
searchBar.showsScopeBar = NO;
self.tableView.tableHeaderView = searchBar;
self.tableView.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!self.navigationController.toolbarHidden) {
[self.navigationController setToolbarHidden:YES animated:animated];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.searchDisplayController.searchBar resignFirstResponder];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
return YES;
}
return interfaceOrientation == UIInterfaceOrientationPortrait;
}
#pragma mark - Search
- (void)searchDisplayControllerDidBeginSearch:(UISearchDisplayController *)controller
{
[docSet prepareSearch];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
self.searchResults = nil;
[self reloadSearchResults];
return YES;
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)searchResultsTableView
{
searchResultsTableView.backgroundColor = [UIColor colorWithWhite:0.96 alpha:1.0];
}
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
self.searchResults = nil;
[self.searchDisplayController.searchResultsTableView reloadData];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if (searchString.length == 0) {
self.searchResults = nil;
return YES;
} else {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(reloadSearchResults) object:nil];
[self performSelector:@selector(reloadSearchResults) withObject:nil afterDelay:0.2];
return (self.searchResults == nil);
}
}
- (void)reloadSearchResults
{
NSString *searchTerm = self.searchDisplayController.searchBar.text;
DocSetSearchCompletionHandler completionHandler = ^(NSString *completedSearchTerm, NSArray *results) {
dispatch_async(dispatch_get_main_queue(), ^{
NSString *currentSearchTerm = self.searchDisplayController.searchBar.text;
if ([currentSearchTerm isEqualToString:completedSearchTerm]) {
self.searchResults = results;
[self.searchDisplayController.searchResultsTableView reloadData];
}
});
};
if (self.searchDisplayController.searchBar.selectedScopeButtonIndex == 0) {
[docSet searchForTokensMatching:searchTerm completion:completionHandler];
} else {
[docSet searchForNodesMatching:searchTerm completion:completionHandler];
}
}
#pragma mark -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
if (aTableView == self.tableView) {
return [nodeSections count];
} else if (aTableView == self.searchDisplayController.searchResultsTableView) {
return 1;
}
return 0;
}
- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section
{
if (aTableView == self.tableView) {
NSDictionary *nodeSection = [nodeSections objectAtIndex:section];
return [nodeSection objectForKey:kNodeSectionTitle];
}
return nil;
}
- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section
{
if (aTableView == self.tableView) {
return [[[nodeSections objectAtIndex:section] objectForKey:kNodeSectionNodes] count];
} else if (aTableView == self.searchDisplayController.searchResultsTableView) {
if (!self.searchResults) {
return 1;
} else {
return [searchResults count];
}
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (aTableView == self.tableView) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
}
NSDictionary *nodeSection = [nodeSections objectAtIndex:indexPath.section];
NSManagedObject *node = [[nodeSection objectForKey:kNodeSectionNodes] objectAtIndex:indexPath.row];
BOOL expandable = [docSet nodeIsExpandable:node];
cell.accessoryType = (expandable) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
if ([[node valueForKey:@"installDomain"] intValue] > 1) {
//external link, e.g. man pages
cell.textLabel.textColor = [UIColor grayColor];
} else {
cell.textLabel.textColor = [UIColor blackColor];
}
int documentType = [[node valueForKey:@"kDocumentType"] intValue];
if (documentType == 1) {
cell.imageView.image = [UIImage imageNamed:@"SampleCodeIcon.png"];
} else if (documentType == 2) {
cell.imageView.image = [UIImage imageNamed:@"ReferenceIcon.png"];
} else if (!expandable) {
cell.imageView.image = [UIImage imageNamed:@"BookIcon.png"];
} else {
cell.imageView.image = nil;
}
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.textLabel.text = [node valueForKey:@"kName"];
cell.detailTextLabel.text = nil;
cell.accessoryView = nil;
return cell;
} else if (aTableView == self.searchDisplayController.searchResultsTableView) {
static NSString *searchCellIdentifier = @"SearchResultCell";
SearchResultCell *cell = (SearchResultCell *)[aTableView dequeueReusableCellWithIdentifier:searchCellIdentifier];
if (cell == nil) {
cell = [[SearchResultCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchCellIdentifier];
cell.textLabel.font = [UIFont boldSystemFontOfSize:15.0];
}
if (!self.searchResults) {
cell.searchTerm = nil;
cell.textLabel.text = NSLocalizedString(@"Searching...", nil);
cell.textLabel.textColor = [UIColor grayColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.imageView.image = nil;
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
cell.accessoryView = spinner;
cell.detailTextLabel.text = nil;
} else {
cell.searchTerm = self.searchDisplayController.searchBar.text;
cell.textLabel.textColor = [UIColor blackColor];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
cell.accessoryView = nil;
NSDictionary *result = [searchResults objectAtIndex:indexPath.row];
if ([result objectForKey:@"tokenType"]) {
NSManagedObject *metaInfo = [docSet.managedObjectContext existingObjectWithID:[result objectForKey:@"metainformation"] error:NULL];
NSSet *deprecatedVersions = [metaInfo valueForKey:@"deprecatedInVersions"];
cell.deprecated = ([deprecatedVersions count] > 0);
NSManagedObjectID *tokenTypeID = [result objectForKey:@"tokenType"];
if (tokenTypeID) {
NSManagedObject *tokenType = [[docSet managedObjectContext] existingObjectWithID:tokenTypeID error:NULL];
NSString *tokenTypeName = [tokenType valueForKey:@"typeName"];
UIImage *icon = [iconsByTokenType objectForKey:tokenTypeName];
cell.imageView.image = icon;
} else {
cell.imageView.image = nil;
}
NSManagedObjectID *parentNodeID = [result objectForKey:@"parentNode"];
if (parentNodeID) {
NSManagedObject *parentNode = [[docSet managedObjectContext] existingObjectWithID:parentNodeID error:NULL];
NSString *parentNodeTitle = [parentNode valueForKey:@"kName"];
cell.detailTextLabel.text = parentNodeTitle;
} else {
cell.detailTextLabel.text = nil;
}
cell.textLabel.text = [result objectForKey:@"tokenName"];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.deprecated = NO;
cell.textLabel.text = [result objectForKey:@"kName"];
NSManagedObjectID *objectID = [result objectForKey:@"objectID"];
NSManagedObject *node = [[docSet managedObjectContext] existingObjectWithID:objectID error:NULL];
BOOL expandable = [docSet nodeIsExpandable:node];
cell.accessoryType = (expandable) ? UITableViewCellAccessoryDisclosureIndicator : UITableViewCellAccessoryNone;
int documentType = [[node valueForKey:@"kDocumentType"] intValue];
if (documentType == 1) {
cell.imageView.image = [UIImage imageNamed:@"SampleCodeIcon.png"];
} else if (documentType == 2) {
cell.imageView.image = [UIImage imageNamed:@"ReferenceIcon.png"];
} else if (!expandable) {
cell.imageView.image = [UIImage imageNamed:@"BookIcon.png"];
} else {
cell.imageView.image = nil;
}
cell.detailTextLabel.text = nil;
}
}
return cell;
}
return nil;
}
- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (aTableView == self.tableView) {
NSDictionary *nodeSection = [nodeSections objectAtIndex:indexPath.section];
NSManagedObject *node = [[nodeSection objectForKey:kNodeSectionNodes] objectAtIndex:indexPath.row];
[self openNode:node];
} else if (aTableView == self.searchDisplayController.searchResultsTableView) {
[self.searchDisplayController.searchBar resignFirstResponder];
NSDictionary *result = [searchResults objectAtIndex:indexPath.row];
if ([result objectForKey:@"tokenType"]) {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[self.detailViewController showToken:result inDocSet:docSet];
} else {
self.detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:self.detailViewController animated:YES];
[self.detailViewController showToken:result inDocSet:docSet];
}
} else {
NSManagedObject *node = [[docSet managedObjectContext] existingObjectWithID:[result objectForKey:@"objectID"] error:NULL];
[self openNode:node];
}
}
}
- (void)openNode:(NSManagedObject *)node
{
BOOL expandable = [docSet nodeIsExpandable:node];
if (expandable) {
DocSetViewController *childViewController = [[DocSetViewController alloc] initWithDocSet:docSet rootNode:node];
childViewController.detailViewController = self.detailViewController;
[self.navigationController pushViewController:childViewController animated:YES];
} else {
if ([[node valueForKey:@"installDomain"] intValue] > 1) {
NSURL *webURL = [docSet webURLForNode:node];
[[UIApplication sharedApplication] openURL:webURL];
return;
}
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
[self.detailViewController showNode:node inDocSet:docSet];
} else {
self.detailViewController = [[DetailViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:self.detailViewController animated:YES];
[self.detailViewController showNode:node inDocSet:docSet];
}
}
}
#pragma mark -
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
@implementation SearchResultCell
@synthesize searchTerm, deprecated;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
highlightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
highlightView.backgroundColor = [UIColor colorWithRed:0.922 green:0.910 blue:0.745 alpha:1.0];
UIView *underlineView = [[UIView alloc] initWithFrame:CGRectMake(0, 9, 10, 1)];
underlineView.backgroundColor = [UIColor colorWithRed:0.929 green:0.792 blue:0.149 alpha:1.0];
underlineView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
[highlightView addSubview:underlineView];
highlightView.hidden = YES;
[self.contentView insertSubview:highlightView belowSubview:self.textLabel];
}
return self;
}
- (void)setDeprecated:(BOOL)deprecatedFlag
{
deprecated = deprecatedFlag;
if (deprecated) {
if (!strikeThroughView) {
strikeThroughView = [[UIView alloc] initWithFrame:CGRectZero];
strikeThroughView.backgroundColor = [UIColor redColor];
[self.contentView addSubview:strikeThroughView];
}
} else {
if (strikeThroughView) {
[strikeThroughView removeFromSuperview];
strikeThroughView = nil;
}
}
}
- (void)layoutSubviews
{
[super layoutSubviews];
if (searchTerm.length > 0) {
NSRange searchTermRange = [self.textLabel.text rangeOfString:searchTerm options:NSCaseInsensitiveSearch];
if (searchTermRange.location != NSNotFound) {
UIFont *font = self.textLabel.font;
CGSize searchTermSize = [searchTerm sizeWithFont:font];
NSString *prefix = [self.textLabel.text substringToIndex:searchTermRange.location];
CGSize prefixSize = [prefix sizeWithFont:font];
CGRect highlightRect = CGRectMake(self.textLabel.frame.origin.x + prefixSize.width, self.textLabel.frame.origin.y, searchTermSize.width, self.textLabel.frame.size.height);
self.textLabel.backgroundColor = [UIColor clearColor];
highlightView.frame = highlightRect;
highlightView.hidden = NO;
} else {
highlightView.hidden = YES;
}
} else {
highlightView.hidden = YES;
}
if (deprecated) {
CGRect textLabelFrame = self.textLabel.frame;
strikeThroughView.frame = CGRectMake(textLabelFrame.origin.x, CGRectGetMidY(textLabelFrame), textLabelFrame.size.width, 1);
}
}
@end