//
// SingleCache.m
// Geopher OpenCaching
//
// Created by Jeremy on 12/11/10.
// Copyright 2010 Stone Software. All rights reserved.
//
#import "SingleCache.h"
#import "JSON.h"
#import "Geopher_OpenCachingAppDelegate.h"
#import "LocatorModel.h"
#import "CompassViewController.h"
@implementation SingleCache
@synthesize spinner, textView, setTargetButton, cacheName, cacheDescription, cacheID,
cacheType, cacheHint, cacheOxcode, cacheOwnerName, cacheActive,
cacheTags, cacheLogs, cacheAwesomeness, cacheSize, cacheLastUpdated,
cacheDifficultyRating, cacheHiddenDate, cacheOwnerID, cacheLatitude, cacheLongitude,
cacheTerrain, tableView;
@synthesize size = _size;
@synthesize rating = _rating;
@synthesize terrain = _terrain;
@synthesize difficulty = _difficulty;
@synthesize description = _description;
@synthesize logText = _logTextView;
@synthesize info = _info;
@synthesize name = _name;
@synthesize logName = _logName;
@synthesize logDate = _logDate;
#pragma mark -
- (IBAction) sendToNavigon:(id)sender
{
NSString* scheme = @"navigon";
// urlAsString = [NSString stringWithFormat:@"%@://%@|%@|%@|%@|%@|%@|%@|%f|%f",
NSString* urlAsString = [NSString stringWithFormat:@"%@://%@|%@||||||%f|%f",
scheme,
@"OpenCaching", // Field1/AppName:Application or Company Name (e.g. AroundMe)
cacheName, // Field2/NameOfPOI: Name of POI (e.g. Navigon AG Würzburg)
// @"FRA", // Field3/Country: ISO 3166-1 alpha-3 code for country (http://unstats.un.org/unsd/methods/m49/m49alpha.htm) (e.g. DEU)
// @"", // Field4/ZipCode: Postalcode, ZIP code of the POIs city (e.g. 97080)
// @"", // Field5/City: Name of POIs city (e.g. Würzburg)
// thePOI.location.streetAddress, // Field6/Street:POIs street name (e.g. Berliner Platz)
// @"", // Field7/HouseNumber: POIs street/house number (e.g. 11)
[cacheLongitude doubleValue], // Field8/Longitude: Longitude in WGS84 (e.g. 9.870)
[cacheLatitude doubleValue]]; // Field9/Latitude: Latitude in WGS84 (e.g. 49.938)
urlAsString = [urlAsString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// NSLog(@"Starting Navigon app with %@", urlAsString);
NSURL*url = [[[NSURL alloc] initWithString:urlAsString] autorelease];
[[UIApplication sharedApplication ]openURL:url];
}
- (IBAction) setTargetLocation:(id)sender
{
Geopher_OpenCachingAppDelegate* del = GetAppDelegate();
[del setNewTargetWithLatitude:[self.cacheLatitude stringValue] longitude:[self.cacheLongitude stringValue]];
// set target from lat/lon here
CLLocation* location = [[[CLLocation alloc] initWithLatitude:[self.cacheLatitude doubleValue] longitude:[self.cacheLongitude doubleValue]] autorelease];
[[LocatorModel sharedInstance] setTargetLocation:location];
CompassViewController* navController = [GetAppDelegate() compassScreen];
// auto-swap to the compass view
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.0];
[UIView transitionFromView:self.navigationController.view
toView:navController.view
duration:0.75
options:UIViewAnimationOptionTransitionCurlDown
completion:^(BOOL finished) { [self.tabBarController setSelectedIndex:[self.tabBarController.viewControllers indexOfObject:navController]]; }];
[UIView commitAnimations];
}
- (void) loadCacheWithCode:(NSString*)cache
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/api/geocache/%@?%@", kOpenCachingURL, cache, kDeveloperSiteAuthenticationKey]]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
[self.spinner startAnimating];
self.setTargetButton.enabled = NO;
self.navigationItem.backBarButtonItem.enabled = NO;
}
- (NSString*) removeHTML:(NSString*)inString
{
#warning html stripping not done!
// OC.com supports these codes: BR, P, B, I, OL, UL, LI, PRE, BLOCKQUOTE, STRONG, EM, H1, H2, H3, H4, H5, H6
//
and
to returns?
// now strip all other HTML out:
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:inString];
while ([theScanner isAtEnd] == NO)
{
NSString* replaceStr = @" ";
// find start of tag
[theScanner scanUpToString:@"<" intoString:nil] ;
// find end of tag
[theScanner scanUpToString:@">" intoString:&text] ;
// replace certain tags with text equivalent
if ([@"
", text]
withString:replaceStr];
} // while //
return inString;
}
//#pragma mark -
//#pragma mark Table View
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//}
#pragma mark -
#pragma mark Table View data source
- (NSInteger)tableView:(UITableView *)inTableView numberOfRowsInSection:(NSInteger)section
{
return kStandardRows + [cacheLogs count];
}
- (UITableViewCell *)tableView:(UITableView *)inTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// get the appropriate cell
NSString* cellIdentifier;
switch (indexPath.row)
{
case kStatsRow:
cellIdentifier = @"StatsRowCell";
break;
case kNameRow:
cellIdentifier = @"NameRow";
break;
case kDescriptionRow:
cellIdentifier = @"DescriptionRow";
break;
case kHintRow:
cellIdentifier = @"HintRow";
break;
case kInfoRow:
cellIdentifier = @"MiscInfoRow";
break;
case kLogRow:
default:
cellIdentifier = @"LogRow";
break;
}
// get the cached cell object if it exists
// otherwise create a cell
UITableViewCell *cell = [inTableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
// this loads the cell from the xib and links it to our outlet, since we are the owner.
[[NSBundle mainBundle] loadNibNamed:@"SingleCacheTableCells" owner:self options:nil];
}
// fill the cell's details.
switch (indexPath.row)
{
case kStatsRow:
cell = statsCell;
// set up the height, size, etc of the row.
self.difficulty.text = [NSString stringWithFormat:@"%.1f Difficulty", [self.cacheDifficultyRating doubleValue]];
self.rating.text = [NSString stringWithFormat:@"%.1f Awesomeness", [self.cacheAwesomeness doubleValue]];
self.terrain.text = [NSString stringWithFormat:@"%.1f Terrain", [self.cacheTerrain doubleValue]];
self.size.text = [NSString stringWithFormat:@"%.1f Cache Size", [self.cacheSize doubleValue]];
break;
case kNameRow:
cell = nameCell;
self.name.text = self.cacheName;
self.name.textColor = kEveningBlueColor;
break;
case kDescriptionRow:
cell = descriptionCell;
self.description.text = [self removeHTML:self.cacheDescription];
break;
case kHintRow:
hintButton.layer.cornerRadius = kButtonCornerRadius;
hintButton.layer.masksToBounds = YES;
hintButton.layer.borderWidth = kButtonBorderWidth;
[hintButton setTitleColor:kButtonTextColor forState:UIControlStateNormal];
hintButton.layer.borderColor = [kDirtBrownColor CGColor];
navigonButton.layer.cornerRadius = kButtonCornerRadius;
navigonButton.layer.masksToBounds = YES;
navigonButton.layer.borderWidth = kButtonBorderWidth;
[navigonButton setTitleColor:kButtonTextColor forState:UIControlStateNormal];
navigonButton.layer.borderColor = [kDirtBrownColor CGColor];
cell = hintCell;
break;
case kInfoRow:
cell = infoCell;
#warning not yet done, what do we want here?
break;
default:
cell = logCell;
// logs row
if (indexPath.row)
{
NSDictionary* dict = [cacheLogs objectAtIndex:indexPath.row - kTotalNumberOfSetRowTypes];
self.logText.text = [dict objectForKey:kLogCommentOXKey];
self.logName.text = [[dict objectForKey:kLogCommentUserOXKey] objectForKey:kLogCommentUserNameOXKey];
self.logName.textColor = kEveningBlueColor;
self.logDate.text = @"