This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
iFixitAppDelegate.m
424 lines (339 loc) · 15.6 KB
/
iFixitAppDelegate.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
//
// iFixitAppDelegate.m
// iFixit
//
// Created by David Patierno on 8/6/10.
// Copyright iFixit 2010. All rights reserved.
//
#import "iFixitAppDelegate.h"
#import "Config.h"
#import "ListViewController.h"
#import "AreasViewController.h"
#import "DetailViewController.h"
#import "FeaturedViewController.h"
#import "GuideViewController.h"
#import "DozukiSplashViewController.h"
#import "DozukiInfoViewController.h"
#import "SVWebViewController.h"
#import "GuideBookmarks.h"
#import "Guide.h"
#import "LoginViewController.h"
#import "LoginBackgroundViewController.h"
#import "UIColor+Hex.h"
#import "GANTracker.h"
static const NSInteger kGANDispatchPeriodSec = 10;
@implementation UISplitViewController (SplitViewRotate)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
@end
@implementation iFixitAppDelegate
@synthesize window, splitViewController, areasViewController, detailViewController;
@synthesize api, firstLoad, showsTabBar;
#pragma mark -
#pragma mark Application lifecycle
- (void)setupAnalytics {
if ([Config currentConfig].dozuki)
return;
[[GANTracker sharedTracker] startTrackerWithAccountID:@"UA-30506-9"
dispatchPeriod:kGANDispatchPeriodSec
delegate:nil];
[[GANTracker sharedTracker] setCustomVariableAtIndex:1
name:@"model"
value:[UIDevice currentDevice].model
withError:NULL];
[[GANTracker sharedTracker] setCustomVariableAtIndex:2
name:@"systemVersion"
value:[UIDevice currentDevice].systemVersion
withError:NULL];
[[GANTracker sharedTracker] trackPageview:[NSString stringWithFormat:@"/launch/%@", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]] withError:NULL];
}
// Override point for customization after app launch.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* Configure. */
[Config currentConfig].dozuki = NO;
/* Track. */
[TestFlight takeOff:@"6b356258f037dc15f6d69d0e5d27fdf7_MzAyOTUyMDEyLTAyLTEyIDE1OjQ2OjUwLjA0NTg5OQ"];
[self setupAnalytics];
/* iOS 5 appearance */
if ([UITabBar respondsToSelector:@selector(appearance)])
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"customTabBarBackground.png"]];
/* Setup and launch. */
self.window.rootViewController = nil;
firstLoad = YES;
/* iFixit is easy. */
if (![Config currentConfig].dozuki) {
[self showSiteSplash];
}
/* Dozuki gets a little more complicated. */
else {
NSDictionary *site = [[NSUserDefaults standardUserDefaults] objectForKey:@"site"];
if (site) {
[self loadSite:site];
[self showSiteSplash];
}
else {
[self showDozukiSplash];
}
firstLoad = NO;
}
return YES;
}
- (void)showDozukiSplash {
[detailViewController.popoverController dismissPopoverAnimated:NO];
// Make sure we're not pointing at a site requiring setup.
[[Config currentConfig] setSite:ConfigIFixit];
// Reset the saved choice.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:nil forKey:@"site"];
[defaults synchronize];
// Dozuki splash
DozukiSplashViewController *dsvc = [[DozukiSplashViewController alloc] init];
dsvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.window.rootViewController = dsvc;
[dsvc release];
[window makeKeyAndVisible];
}
- (void)showSiteSplash {
[GuideBookmarks reset];
if (self.window.rootViewController) {
for (UIView *subview in self.window.subviews)
[subview removeFromSuperview];
}
UIViewController *root = nil;
UINavigationController *nvc = nil;
if ([Config currentConfig].private) {
// Private sites require immediate login.
LoginViewController *vc = [[LoginViewController alloc] init];
vc.message = @"Private site. Authentication required.";
vc.delegate = self;
nvc = [[[UINavigationController alloc] initWithRootViewController:vc] autorelease];
nvc.modalPresentationStyle = UIModalPresentationFormSheet;
nvc.navigationBar.tintColor = [Config currentConfig].toolbarColor;
[vc release];
UIImage *icon = [UIImage imageNamed:@"backtosites.png"];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleBordered
target:self
action:@selector(showDozukiSplash)];
vc.navigationItem.leftBarButtonItem = button;
[button release];
// iPad: display in form sheet
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
root = [[[LoginBackgroundViewController alloc] init] autorelease];
self.window.rootViewController = root;
[window makeKeyAndVisible];
[root presentModalViewController:nvc animated:NO];
return;
}
else {
// iPhone: set as root
root = nvc;
}
}
else {
root = [UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad ?
[self iPadRoot] : [self iPhoneRoot];
}
self.window.rootViewController = root;
[window makeKeyAndVisible];
}
- (void)refresh {
if ([iFixitAPI sharedInstance].user) {
[self showSiteSplash];
}
}
- (void)presentModalViewController:(UIViewController *)viewController animated:(BOOL)animated {
[self.window.rootViewController presentModalViewController:viewController animated:animated];
}
- (UIViewController *)iPadRoot {
self.showsTabBar = [Config currentConfig].collectionsEnabled || [Config currentConfig].store;
// Create the split controller children.
AreasViewController *rvc = [[AreasViewController alloc] init];
self.areasViewController = rvc;
[rvc release];
DetailViewController *dvc = [[DetailViewController alloc] init];
self.detailViewController = dvc;
[dvc release];
// Create the split view controller.
UISplitViewController *svc = [[UISplitViewController alloc] init];
svc.delegate = detailViewController;
self.splitViewController = svc;
[svc release];
areasViewController.detailViewController = detailViewController;
ListViewController *lvc = [[ListViewController alloc] initWithRootViewController:areasViewController];
splitViewController.viewControllers = [NSArray arrayWithObjects:lvc, detailViewController, nil];
[lvc release];
areasViewController.delegate = self;
// Stop here, or put a fancy tab bar at the bottom.
if (!self.showsTabBar)
return splitViewController;
// Initialize the tab bar items.
NSString *guideTitle = @"Guides";
if ([Config currentConfig].site == ConfigMake)
guideTitle = @"Projects";
else if ([Config currentConfig].site == ConfigIFixit)
guideTitle = @"Repair Manuals";
if ([Config currentConfig].site == ConfigIFixit) {
splitViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:guideTitle image:[UIImage imageNamed:@"tabBarItemWrench.png"] tag:0] autorelease];
}
else {
splitViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:guideTitle image:[UIImage imageNamed:@"tabBarItemBook.png"] tag:0] autorelease];
}
// Optionally add the store button.
SVWebViewController *storeViewController = nil;
NSString *storeTitle = @"Store";
UIImage *storeImage = [UIImage imageNamed:@"tabBarItemPricetag.png"];
if ([Config currentConfig].store) {
if ([Config currentConfig].site == ConfigIFixit) {
storeTitle = @"Parts & Tools";
storeImage = [UIImage imageNamed:@"tabBarItemGears.png"];
}
storeViewController = [[SVWebViewController alloc] initWithAddress:[Config currentConfig].store];
storeViewController.tintColor = [Config currentConfig].toolbarColor;
storeViewController.showsDoneButton = NO;
storeViewController.tabBarItem = [[[UITabBarItem alloc] initWithTitle:storeTitle image:storeImage tag:0] autorelease];
}
// Create the tab bar.
UITabBarController *tbc = [[UITabBarController alloc] init];
if ([Config currentConfig].collectionsEnabled) {
FeaturedViewController *featuredViewController = [[FeaturedViewController alloc] init];
featuredViewController.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFeatured tag:0] autorelease];
tbc.viewControllers = [NSArray arrayWithObjects:featuredViewController, splitViewController, storeViewController, nil];
[featuredViewController release];
}
else {
tbc.viewControllers = [NSArray arrayWithObjects:splitViewController, storeViewController, nil];
}
[storeViewController release];
return [tbc autorelease];
}
- (UIViewController *)iPhoneRoot {
AreasViewController *avc = [[AreasViewController alloc] init];
self.areasViewController = avc;
[avc release];
ListViewController *lvc = [[ListViewController alloc] initWithRootViewController:areasViewController];
lvc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
return [lvc autorelease];
}
- (void)loadSiteWithDomain:(NSString *)domain {
NSDictionary *site = [NSDictionary dictionaryWithObject:domain forKey:@"domain"];
[self loadSite:site];
}
- (void)loadSite:(NSDictionary *)site {
NSString *domain = [site valueForKey:@"domain"];
//NSString *colorHex = [site valueForKey:@"color"];
//UIColor *color = [UIColor colorFromHexString:colorHex];
// Load the right site
if ([domain isEqual:@"www.ifixit.com"]) {
[[Config currentConfig] setSite:ConfigIFixit];
}
else if ([domain isEqual:@"www.cominor.com"]) {
[[Config currentConfig] setSite:ConfigIFixitDev];
}
else if ([domain isEqual:@"makeprojects.com"]) {
[[Config currentConfig] setSite:ConfigMake];
}
else {
[[Config currentConfig] setSite:ConfigDozuki];
[Config currentConfig].host = domain;
[Config currentConfig].custom_domain = [site valueForKey:@"custom_domain"];
[Config currentConfig].baseURL = [NSString stringWithFormat:@"http://%@/Guide", domain];
//if (color)
// [Config currentConfig].toolbarColor = color;
}
// Enable/disable Answers and/or Collections
if ([Config currentConfig].site == ConfigIFixit) {
[Config currentConfig].answersEnabled = YES;
[Config currentConfig].collectionsEnabled = YES;
}
else {
[Config currentConfig].answersEnabled = [[site valueForKey:@"answers"] boolValue];
[Config currentConfig].collectionsEnabled = [[site valueForKey:@"collections"] boolValue];
}
[Config currentConfig].private = [[site valueForKey:@"private"] boolValue];
[Config currentConfig].sso = [[site valueForKey:@"authentication"] valueForKey:@"sso"];
[Config currentConfig].store = [site valueForKey:@"store"];
// Save this choice for future launches, first removing any null values.
NSMutableDictionary *simpleSite = [NSMutableDictionary dictionary];
for (NSString *key in [site allKeys]) {
NSObject *value = [site objectForKey:key];
if (![value isEqual:[NSNull null]])
[simpleSite setValue:value forKey:key];
}
[[NSUserDefaults standardUserDefaults] setValue:simpleSite forKey:@"site"];
[[NSUserDefaults standardUserDefaults] synchronize];
[Config currentConfig].siteData = simpleSite;
// Show the main app!
[[iFixitAPI sharedInstance] loadSession];
[self showSiteSplash];
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
NSString *urlString = [url absoluteString];
// Pull out the site name with a regex.
if ([Config currentConfig].dozuki) {
if (urlString) {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^dozuki://(.*?)$"
options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
if (match) {
NSRange keyRange = [match rangeAtIndex:1];
NSString *domain = [urlString substringWithRange:keyRange];
NSDictionary *site = [NSDictionary dictionaryWithObject:domain forKey:@"domain"];
[[NSUserDefaults standardUserDefaults] setValue:site forKey:@"site"];
[[NSUserDefaults standardUserDefaults] synchronize];
[self loadSite:site];
return YES;
}
}
}
else {
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^ifixit://guide/(.*?)$"
options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:urlString options:0 range:NSMakeRange(0, [urlString length])];
if (match) {
NSRange keyRange = [match rangeAtIndex:1];
NSString *guideidString = [urlString substringWithRange:keyRange];
NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber *guideid = [f numberFromString:guideidString];
[f release];
GuideViewController *vc = [[GuideViewController alloc] initWithGuideid:[guideid intValue]];
[self.window.rootViewController presentModalViewController:vc animated:NO];
[vc release];
return YES;
}
}
return NO;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive.
*/
[areasViewController viewWillAppear:NO];
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[splitViewController release];
[window release];
[super dealloc];
}
@end