// // FlipsideView.m // Geopher Lite // // Created by Jeremy on 7/5/08. // Copyright Stone Software 2008. All rights reserved. // #import "FlipsideView.h" #import "textConverter.h" #import "LocatorModel.h" #import "Calculations.h" #import "FlipsideViewController.h" @implementation FlipsideView @synthesize latitudeTextView; @synthesize longitudeTextView; @synthesize webView; @synthesize longitudeTextLabel; @synthesize latitudeTextLabel; - (void)textFieldDidBeginEditing:(UITextField *)textField { if (textField == latitudeTextView) { NSString* tempStr = [Calculations degreesToHourMinuteDecimal:[LocatorModel sharedInstance].currentLocation.coordinate.latitude isLatitude:YES]; NSCharacterSet* set = [NSCharacterSet whitespaceCharacterSet]; NSRange range = [tempStr rangeOfCharacterFromSet:set options:NSBackwardsSearch]; textField.text = [tempStr substringToIndex: range.location + 1]; } else if (textField == longitudeTextView) { NSString* tempStr = [Calculations degreesToHourMinuteDecimal:[LocatorModel sharedInstance].currentLocation.coordinate.longitude isLatitude:NO]; NSCharacterSet* set = [NSCharacterSet whitespaceCharacterSet]; NSRange range = [tempStr rangeOfCharacterFromSet:set options:NSBackwardsSearch]; textField.text = [tempStr substringToIndex: range.location + 1]; } } - (BOOL)textFieldDidEndEditing:(UITextField *)theTextField { textConverter* converter = [[[textConverter alloc] initWithString:[theTextField text]] autorelease]; double lon = [LocatorModel sharedInstance].targetLocation.coordinate.longitude; double lat = [LocatorModel sharedInstance].targetLocation.coordinate.latitude; double conv = 0.0; BOOL newVal = [converter decimalFromString:&conv]; // make sure conv is initialized to a good value if we are going forward. if (theTextField == latitudeTextView) { if (newVal) lat = conv; // use the new val else conv = lat; // keep the old val newVal = YES; } else if(theTextField == longitudeTextView) { if (newVal) lon = conv; // use new val else conv = lon; // use old val newVal = YES; } else { newVal = NO; } // if we have something to fill in from lat/lon if (newVal) { // bounds check to make sure we are ok. If not, don't change the values and show the user the current value. if (lat > 90 || lat < -90) { conv = [LocatorModel sharedInstance].targetLocation.coordinate.latitude; } else if (lon > 180 || lon < -180) { conv = [LocatorModel sharedInstance].targetLocation.coordinate.longitude; } else // we are good, change the values { // keep the obtained value. CLLocation* loc = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; [[LocatorModel sharedInstance] setTargetLocation: loc]; [[LocatorModel sharedInstance] update]; [loc release]; } // revamp the target fields with the parsed value. [self updateTargetFields]; [g_AppDelegate savePersistentData]; } // Set the target values to valid or invalid. NSString* latText = [NSString stringWithString:latitudeTextView.text]; NSString* lonText = [NSString stringWithString:longitudeTextView.text]; if ([latText length] < 1 && [lonText length] < 1 ) [[LocatorModel sharedInstance] setHasValidTarget:NO]; else if ( [latText length] > 0 && [lonText length] > 0 ) [[LocatorModel sharedInstance] setHasValidTarget:YES]; return YES; } - (void) loadURL:(NSURL*)url { webView.dataDetectorTypes = UIDataDetectorTypeLink; [webView loadRequest:[NSURLRequest requestWithURL:url]]; } - (IBAction) loadInternalURL:(id)sender { webView.scalesPageToFit = YES; if (NetworkIsReachable() == YES) { double lat = [LocatorModel sharedInstance].currentLocation.coordinate.latitude; double lon = [LocatorModel sharedInstance].currentLocation.coordinate.longitude; NSString* str = kEmptyText; if (YES == [[NSUserDefaults standardUserDefaults] boolForKey:kHideFoundCaches]) str = kExcludeFoundCaches; NSString *webLink = [NSString stringWithFormat:@"http://www.geocaching.com/seek/nearest.aspx?%@%f&%@%f%@", @"lat=", lat, @"lon=", lon, str]; // CFLocaleRef userLocaleRef = CFLocaleCopyCurrent(); // CFStringRef stringRef = CFLocaleGetValue(userLocaleRef, kCFLocaleCountryCode); // str = (NSString*)stringRef; // toll-free bridged // looks like geocaching.hu is not an affiliate of geocaching.com and may have different URL schemes and rules. Sent them an email about // using their site with Geopher Lite. Sounds like they may be interested in helping out, but haven't heard any details back from them. [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:webLink]]]; } else { NSString* path = [[NSBundle mainBundle] pathForResource:@"SearchInternetError" ofType:@"html"]; if (path != nil) [self loadURL:[NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]; } } - (BOOL)textFieldShouldReturn:(UITextField *)theTextField { // When the user presses return, take focus away from the text field so that the keyboard is dismissed. [theTextField resignFirstResponder]; return YES; } - (void) updateTargetFields { // revamp the changed text field with the parsed value. latitudeTextView.text = [Calculations degreesToHourMinuteDecimal:[LocatorModel sharedInstance].targetLocation.coordinate.latitude isLatitude:YES]; longitudeTextView.text = [Calculations degreesToHourMinuteDecimal:[LocatorModel sharedInstance].targetLocation.coordinate.longitude isLatitude:NO]; } - (void) hideLatLon:(BOOL)yesOrNo { if (yesOrNo == YES) { latitudeTextView.frame = CGRectMake(-100, -100, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); longitudeTextView.frame = CGRectMake(-100, -100, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); latitudeTextLabel.frame = CGRectMake(-100, -100, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); longitudeTextLabel.frame = CGRectMake(-100, -100, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); } else { latitudeTextView.frame = CGRectMake(47, 49, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); longitudeTextView.frame = CGRectMake(209, 49, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); latitudeTextLabel.frame = CGRectMake(7, 49, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); longitudeTextLabel.frame = CGRectMake(157, 49, latitudeTextView.frame.size.width, latitudeTextView.frame.size.height); } } // //- (id)initWithFrame:(CGRect)frame { // if (self = [super initWithFrame:frame]) { // // Initialization code // } // return self; //} // //- (void)drawRect:(CGRect)rect { // // Drawing code //} - (void)dealloc { [super dealloc]; [latitudeTextView dealloc]; [longitudeTextView dealloc]; [latitudeTextLabel dealloc]; [longitudeTextLabel dealloc]; [webView dealloc]; } @end