// // SavedCachesController.m // Geopher iPad // // Created by Jeremy on 11/29/10. // Copyright 2010 Stone Software. All rights reserved. // #import "SavedCachesController.h" #import "Geopher_OpenCachingAppDelegate.h" #import "ParseUtils.h" #import "LocatorModel.h" #import "Calculations.h" #import "SingleCache.h" @implementation SavedCachesController @synthesize cacheTable, navController, waypointList; #pragma mark - #pragma mark View lifecycle - (void) viewWillAppear:(BOOL)animated { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray* fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil]; NSMutableArray* tempList = [NSMutableArray array]; for (NSString* filePath in fileList) { if ([[[filePath pathExtension] uppercaseString] isEqualToString:@"LOC"] || [[[filePath pathExtension] uppercaseString] isEqualToString:@"GPX"]) { [tempList addObject:[filePath lastPathComponent]]; } } self.waypointList = [tempList sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; [self.cacheTable reloadData]; // // test our XML stuff! // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documentsDirectory = [paths objectAtIndex:0]; // NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:@"test.loc"]; // // self.cacheData = parseLocFileToDict(finalPath); // // self.waypointList = [self.cacheData objectForKey:kCacheList];//[waypoints sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; // // [self.cacheTable reloadData]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Overriden to allow any orientation. return YES; } - (void)dealloc { [cacheTable release], cacheTable = nil; [waypointList release], waypointList = nil; [navController release], navController = nil; [super dealloc]; } #pragma mark - #pragma mark Table view data source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [waypointList count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } // get the appropriate object // fill in it's details. NSString* str = [self.waypointList objectAtIndex:indexPath.row]; cell.textLabel.text = str; return cell; } // Customize the appearance of table view cells. //- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // // static NSString *CellIdentifier = @"Cell"; // // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // if (cell == nil) { // cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; // } // // // get the appropriate object // // // fill in it's details. // NSDictionary* dict = [self.waypointList objectAtIndex:indexPath.row]; // cell.textLabel.text = [dict objectForKey:kCacheName]; // // // sub-text // NSString* gccode = [dict objectForKey:kCacheGeocode]; // CLLocationDegrees lat = [[dict objectForKey:kLatitudeNodeName] doubleValue]; // CLLocationDegrees lon = [[dict objectForKey:kLongitudeNodeName] doubleValue]; // CLLocationDistance distToCache = [Calculations calcDistanceFromLat:lat // lon:lon // toLat:[LocatorModel sharedInstance].currentLocation.coordinate.latitude // lon:[LocatorModel sharedInstance].currentLocation.coordinate.longitude]; // NSString* latLonString = [NSString stringWithFormat:@"%@, (%@)", gccode, [Calculations distToDisplayInDecimal:distToCache]]; // cell.detailTextLabel.text = latLonString; // // return cell; //} #pragma mark - #pragma mark Table view delegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString* fileName = [self.waypointList objectAtIndex:indexPath.row]; NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:fileName]; NSArray* infoList = nil; if ([[[fileName uppercaseString] pathExtension] isEqualToString:@"LOC"]) { infoList = parseLocFileToDict(finalPath); } else if ([[[fileName uppercaseString] pathExtension] isEqualToString:@"GPX"] || [[[fileName uppercaseString] pathExtension] isEqualToString:@"XML"]) { infoList = parseGPXFileToDict(finalPath); } NSDictionary* info = [infoList lastObject]; if ([infoList count] == 1) { // open the cache details screen here. SingleCache* detailViewController = [[[SingleCache alloc] initWithNibName:@"SingleCache" bundle:nil] autorelease]; [detailViewController loadCacheWithInfo:[infoList lastObject]]; // Pass the selected object to the new view controller. [self.navController pushViewController:detailViewController animated:YES]; } else if ([infoList count] > 1) { // open the file's cache list here } else { if ([infoList count] > 0) { NSString* lat = [info objectForKey:kLOCLatitudeNodeName]; NSString* lon = [info objectForKey:kLOCLongitudeNodeName]; if (lat && lon) { Geopher_OpenCachingAppDelegate* del = GetAppDelegate(); [del setNewTargetWithLatitude:lat longitude:lon]; } } } } @end