// // FlipsideViewController.m // Geopher Lite // // Created by Jeremy on 7/5/08. // Copyright Stone Software 2008. All rights reserved. // #import "FlipsideViewController.h" #import "FlipsideView.h" #import "LocatorModel.h" #import "RootViewController.h" #import "Reachability.h" BOOL NetworkIsReachable(void) { BOOL retVal = NO; [[Reachability sharedReachability] setHostName:@"www.geocaching.com"]; if ([[Reachability sharedReachability] internetConnectionStatus] != NotReachable) retVal = YES; return retVal; } @implementation FlipsideViewController @synthesize infoButton; @synthesize backButton; @synthesize helpButton; @synthesize stopButton; @synthesize saveButton; @synthesize updateWaypointString; @synthesize cacheIDTextView; @synthesize waypointLabel; @synthesize logButton; @synthesize mapController; - (void)viewDidLoad { self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor]; if (NO == [self LoadArchivedWebpage]) { if ([[NSUserDefaults standardUserDefaults] boolForKey:kAutoLoadSearchKey] == YES) { [(FlipsideView*)self.view loadInternalURL:self.view]; } else { NSString* path = [[NSBundle mainBundle] pathForResource:@"Search" ofType:@"html"]; if (path != nil) [(FlipsideView*)self.view loadURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; } } // disable back and stop buttons [backButton setEnabled:NO]; [stopButton setEnabled:NO]; if ([LocatorModel sharedInstance].hasValidTarget == YES) [self updateTargetFields]; if (updateWaypointString != nil) { cacheIDTextView.text = updateWaypointString; [updateWaypointString release]; updateWaypointString = nil; } // disable log button as needed. Needs to happen after we re-set the log text. [self waypointEditingDidEnd]; lastURL = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview // Release anything that's not essential, such as cached data } - (void)dealloc { if (updateWaypointString != nil) [updateWaypointString release]; [super dealloc]; } #pragma mark - - (IBAction)editWaypointText { cacheIDTextView.text = [NSString stringWithString:@"GC"]; } - (IBAction)waypointEditingDidEnd { if ([[cacheIDTextView text] length] > 5) [logButton setEnabled:YES]; else [logButton setEnabled:NO]; [g_AppDelegate savePersistentData]; } - (void)webViewDidStartLoad:(UIWebView *)webView { [chasingArrows startAnimating]; [stopButton setEnabled:YES]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [stopButton setEnabled:NO]; if ([webView canGoBack] == YES) [backButton setEnabled:YES]; else [backButton setEnabled:NO]; [chasingArrows stopAnimating]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { // trying to download the GPX or LOC files gives this error: WebKitErrorFrameLoadInterruptedByPolicyChange Is there anything that can be done about it? NSLog(@"%@", error); [chasingArrows stopAnimating]; } - (void) updateTargetFields { [(FlipsideView*)self.view updateTargetFields]; } - (IBAction)doGoogleMaps { if (NetworkIsReachable() == YES) { BOOL useInternalMaps = [[NSUserDefaults standardUserDefaults] boolForKey:kUseInternalMapsKey]; if (NO == useInternalMaps) { // example URL that works: NSMutableString* url = [NSMutableString stringWithFormat: @"http://maps.google.com/maps?q=%@+%@", [[LocatorModel sharedInstance] targetLatitudeText:NO], [[LocatorModel sharedInstance] targetLongitudeText:NO]]; // http://maps.google.com/maps?saddr=N+40+5.457+W+111+37.008&daddr=N+40+5.332+W+111+35.918 // change the spaces to be + signs. [url replaceOccurrencesOfString:@" " withString:@"+" options:NSBackwardsSearch range:NSMakeRange(0, [url length])]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; } else { // turn back on the GPS if it's off in the prefs, so the map view works properly. if ([[NSUserDefaults standardUserDefaults] stringForKey:kAlwaysOnGPSKey] == NO) { [[LocatorModel sharedInstance].locationManager startUpdatingLocation]; [[LocatorModel sharedInstance].locationManager startUpdatingHeading]; } // hide the flipside toolbar RootViewController* root = [g_AppDelegate rootViewController]; [root.flipsideNavigationBar setHidden:YES]; // load our own map view mapController = [[MapViewController alloc] initWithNibName:@"MapView" bundle:nil]; mapController.rootController = self; // set up the curl animation [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.6]; [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES]; // add the map view [mapController viewWillAppear:YES]; [self viewWillDisappear:YES]; [self.view insertSubview:mapController.view belowSubview:self.view ]; [self viewDidDisappear:YES]; [mapController viewDidAppear:YES]; // animate [UIView commitAnimations]; } } else { NSString* path = [[NSBundle mainBundle] pathForResource:@"MapInternetError" ofType:@"html"]; if (path != nil) [(FlipsideView*)self.view loadURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; } } // intercept link clicks and other events in the web view. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { NSURL *url = [request URL]; if (navigationType == UIWebViewNavigationTypeLinkClicked) { // save the last link clicked. lastURL = url; [lastURL retain]; if ([[url scheme] isEqualToString:@"http"]) { // if it's the log button we are pushing if (NSNotFound != [[url resourceSpecifier] rangeOfString:kLogString].location) { [[UIApplication sharedApplication] openURL: url]; return NO; } // // if it's a google maps link send it ot the maps app. // if (NSNotFound != [[url resourceSpecifier] rangeOfString:@"maps.google.com"].location) // { // NSURL* tempURL = [[NSURL alloc] initWithScheme:@"maps" host:[url host] path:[url path]]; // [[UIApplication sharedApplication] openURL: url]; // [tempURL release]; // } } } else { NSLog(@"%@", [url absoluteString]); } return YES; } - (void) hideControlsExcept:(int) stage { static BOOL initialized = NO; // this really should be done elsewhere, but I don't have time now to track down exactly where and I know this will work. if (NO == initialized) { [logButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [stopButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; [backButton setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled]; initialized = YES; } if (stage == latLonStage) { [(FlipsideView*)self.view hideLatLon:NO]; } else { [(FlipsideView*)self.view hideLatLon:YES]; } if (stage == waypointStage) { int one = 320 - 9 - logButton.frame.size.width - 33; int two = one - 15 - cacheIDTextView.frame.size.width; int three = two - 10 - waypointLabel.frame.size.width; logButton.frame = CGRectMake(one, 49, logButton.frame.size.width, logButton.frame.size.height); waypointLabel.frame = CGRectMake(three, 54, waypointLabel.frame.size.width, waypointLabel.frame.size.height); cacheIDTextView.frame = CGRectMake(two, 49, cacheIDTextView.frame.size.width, cacheIDTextView.frame.size.height); } else { logButton.frame = CGRectMake(-100, -100, logButton.frame.size.width, logButton.frame.size.height); waypointLabel.frame = CGRectMake(-100, -100, waypointLabel.frame.size.width, waypointLabel.frame.size.height); cacheIDTextView.frame = CGRectMake(-100, -100, cacheIDTextView.frame.size.width, cacheIDTextView.frame.size.height); } if (stage == browseButtonStage) { stopButton.frame = CGRectMake(152, 49, stopButton.frame.size.width, stopButton.frame.size.height); backButton.frame = CGRectMake(237, 49, backButton.frame.size.width, backButton.frame.size.height); helpButton.frame = CGRectMake(7, 49, helpButton.frame.size.width, helpButton.frame.size.height); } else { stopButton.frame = CGRectMake(-100, -100, stopButton.frame.size.width, stopButton.frame.size.height); backButton.frame = CGRectMake(-100, -100, backButton.frame.size.width, backButton.frame.size.height); helpButton.frame = CGRectMake(-100, -100, helpButton.frame.size.width, helpButton.frame.size.height); } // additional stages can go here as needed. } - (IBAction)doFAQButton { NSString* path = [[NSBundle mainBundle] pathForResource:@"FAQ" ofType:@"html"]; if (path != nil) [(FlipsideView*)self.view loadURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; // [[UIApplication sharedApplication] openURL:[NSURL URLWithString:kGeopherLiteFAQString]]; } - (IBAction) logCache:(id)sender { if ([[cacheIDTextView text] length] > 2) { NSString *webLink = [NSString stringWithFormat:@"http://www.geocaching.com/seek/%@wp=%@", kLogString, [NSString stringWithString:[cacheIDTextView text]]]; // [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webLink]]]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:webLink]]; } } - (BOOL) ArchiveWebView { // static BOOL initialized = NO; BOOL retVal = NO; // CURL *curl; // CURLcode res; // // if (!initialized) // { // curl = curl_easy_init(); // initialized = YES; // } // return retVal; } - (BOOL) LoadArchivedWebpage { BOOL retVal = NO; // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documentsDirectory = [paths objectAtIndex:0]; // NSString* webArchive = [documentsDirectory stringByAppendingPathComponent:kAppWebArchiveFilename]; // // // web archive, if it exists // UIWebView* webView = nil; // webView = [NSKeyedUnarchiver unarchiveObjectWithFile:webArchive]; // // if (webView != nil) // { //// webView.delegate = ((FlipsideView*)self.view).webView.delegate; // ((FlipsideView*)self.view).webView = webView; // [((FlipsideView*)self.view).webView setDelegate:self]; // retVal = YES; // } return retVal; } @end