Repository navigation
Expand file tree
/
Copy pathHelpSectionController.m
More file actions
executable file
·218 lines (178 loc) · 8.19 KB
/
Copy pathHelpSectionController.m
File metadata and controls
executable file
·218 lines (178 loc) · 8.19 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
//
// HelpContentsController.m
// Documents
//
// Created by Jesse Grosjean on 1/21/10.
//
#import "HelpSectionController.h"
#import "ApplicationViewController.h"
#import "ApplicationController.h"
#import "NSString_Additions.h"
@interface MyWebView : UIWebView { } @end
@implementation HelpSectionController
- (id)init {
NSString *markdownText = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Help" ofType:@"markdown"] encoding:NSUTF8StringEncoding error:NULL];
NSString *htmlText = [NSString stringWithFormat:@"<html>%@</html>", [markdownText markdownToHTML]];
htmlText = [htmlText substringFromIndex:6];
htmlText = [htmlText substringToIndex:[htmlText length] - 7];
return [self initWithTitle:NSLocalizedString(@"Help", nil) subheaderPrefix:@"<h1>" htmlTextContent:htmlText];
}
- (id)initWithTitle:(NSString *)aTitle subheaderPrefix:(NSString *)aPrefix htmlTextContent:(NSString *)htmlTextContent {
self = [super initWithStyle:UITableViewStylePlain];
if (self) {
self.title = aTitle;
NSMutableString *sectionText = [NSMutableString string];
subheaders = [[NSMutableArray array] retain];
subheaderPrefix = [aPrefix retain];
for (NSString *each in [htmlTextContent componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]) {
if ([each hasPrefix:subheaderPrefix]) {
each = [each substringFromIndex:4];
each = [each substringToIndex:[each length] - 5];
each = [each stringByReplacingOccurrencesOfString:@"&" withString:@"&"]; // HACK!!!
[subheaders addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:each, @"title", [NSMutableString string], @"htmlText", nil]];
} else {
if ([subheaders count] > 0) {
[[[subheaders lastObject] objectForKey:@"htmlText"] appendString:[NSString stringWithFormat:@"\n%@", each]];
} else {
if ([each length] > 0) {
[sectionText appendFormat:@"\n%@", each];
}
}
}
}
sectionText = [[[sectionText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy] autorelease];
if ([sectionText length] > 0) {
NSString *helpTemplatePath = [[NSBundle mainBundle] pathForResource:@"HelpPageTemplate" ofType:@"html"];
NSMutableString *helpTemplate = [NSMutableString stringWithContentsOfFile:helpTemplatePath encoding:NSUTF8StringEncoding error:nil];
[helpTemplate replaceOccurrencesOfString:@"PAGE_TITLE" withString:aTitle options:0 range:NSMakeRange(0, [helpTemplate length])];
[helpTemplate replaceOccurrencesOfString:@"PAGE_BODY" withString:sectionText options:0 range:NSMakeRange(0, [helpTemplate length])];
NSString *basePath = [helpTemplatePath stringByDeletingLastPathComponent];
basePath = [basePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
basePath = [basePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
sectionWebViewTableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
sectionWebView = [[MyWebView alloc] initWithFrame:sectionWebViewTableViewCell.frame];
sectionWebView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
sectionWebView.delegate = self;
sectionWebView.backgroundColor = [UIColor clearColor];
sectionWebView.opaque = NO;
[sectionWebView loadHTMLString:helpTemplate baseURL:[NSURL fileURLWithPath:basePath]];
[sectionWebViewTableViewCell.contentView addSubview:sectionWebView];
sectionWebViewTableViewCell.clipsToBounds = NO;
sectionWebViewTableViewCell.contentView.clipsToBounds = NO;
UIScrollView *innerScroller = [[sectionWebView subviews] lastObject];
if ([innerScroller respondsToSelector:@selector(setScrollEnabled:)]) {
innerScroller.scrollEnabled = NO;
}
}
}
return self;
}
- (void)dealloc {
self.tableView.delegate = nil;
self.tableView.dataSource = nil;
sectionWebView.delegate = nil;
[sectionWebView release];
[subheaderPrefix release];
[subheaders release];
[sectionWebViewTableViewCell release];
[super dealloc];
}
- (void)loadView {
[super loadView];
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)viewWillAppear:(BOOL)animated {
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Close", nil) style:UIBarButtonItemStyleDone target:self action:@selector(dismissModalViewControllerAction:)] autorelease];
[super viewWillAppear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
if ([APP_VIEW_CONTROLLER lockOrientation]) {
return interfaceOrientation == [APP_VIEW_CONTROLLER lockedOrientation];
} else {
return YES;
}
}
- (IBAction)dismissModalViewControllerAction:(id)sender {
[self.navigationController dismissModalViewControllerAnimated:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (sectionWebView) {
if (sectionWebViewLoaded) {
return [subheaders count] + 1;
} else {
return 0;
}
}
return [subheaders count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (sectionWebView && indexPath.row == 0) {
sectionWebViewTableViewCell.frame = tableView.frame;
NSString *offsetHeight = [sectionWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];
return [[[[[NSNumberFormatter alloc] init] autorelease] numberFromString:offsetHeight] integerValue] - 36;
}
return tableView.rowHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *HeaderCellIdentifier = @"HeaderCellIdentifier";
if (sectionWebViewTableViewCell && indexPath.row == 0) {
return sectionWebViewTableViewCell;
} else {
UITableViewCell *cell = (id) [tableView dequeueReusableCellWithIdentifier:HeaderCellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:HeaderCellIdentifier] autorelease];
}
if (sectionWebViewTableViewCell) {
cell.textLabel.text = [[subheaders objectAtIndex:[indexPath row] - 1] objectForKey:@"title"];
} else {
cell.textLabel.text = [[subheaders objectAtIndex:[indexPath row]] objectForKey:@"title"];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0 && sectionWebViewTableViewCell != nil) {
return nil;
}
return indexPath;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *selection;
if (sectionWebViewTableViewCell) {
selection = [subheaders objectAtIndex:[indexPath row] - 1];
} else {
selection = [subheaders objectAtIndex:[indexPath row]];
}
if ([subheaderPrefix isEqualToString:@"<h1>"]) {
[self.navigationController pushViewController:[[[HelpSectionController alloc] initWithTitle:[selection objectForKey:@"title"] subheaderPrefix:@"<h2>" htmlTextContent:[selection objectForKey:@"htmlText"]] autorelease] animated:YES];
} else {
[self.navigationController pushViewController:[[[HelpSectionController alloc] initWithTitle:[selection objectForKey:@"title"] subheaderPrefix:@"none" htmlTextContent:[selection objectForKey:@"htmlText"]] autorelease] animated:YES];
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
sectionWebViewLoaded = YES;
[self.tableView reloadData];
[self.tableView performSelector:@selector(reloadData) withObject:nil afterDelay:0];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeOther)
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
@end
@implementation MyWebView
/*
- (void)setFrame:(CGRect)frame {
if (!CGRectEqualToRect([self frame], frame)) {
[super setFrame:frame];
UITableView *tableView = (id) self;
while (tableView != nil && ![tableView isKindOfClass:[UITableView class]]) {
tableView = (id) [tableView superview];
}
}
}*/
@end