Skip to content

Commit

Permalink
Initial features.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Pirri committed Aug 22, 2012
1 parent 194eb7e commit 843867b
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 3 deletions.
7 changes: 4 additions & 3 deletions LocationFrobber/LFAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import "LFAppDelegate.h"

#import "LFMasterViewController.h"
#import "LFTraceTableViewController.h"

@implementation LFAppDelegate

Expand All @@ -17,10 +17,11 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.

LFMasterViewController *masterViewController = [[LFMasterViewController alloc] initWithNibName:@"LFMasterViewController" bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:masterViewController];
LFTraceTableViewController *traceTableViewController = [[LFTraceTableViewController alloc] initWithNibName:nil bundle:nil];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:traceTableViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

return YES;
}

Expand Down
15 changes: 15 additions & 0 deletions LocationFrobber/LFTraceTableViewController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// LFTraceTableViewController.h
// LocationFrobber
//
// Created by Mark Pirri on 8/21/12.
// Copyright (c) 2012 Mark Pirri. All rights reserved.
//

#import <UIKit/UIKit.h>

#import <MessageUI/MessageUI.h>

@interface LFTraceTableViewController : UITableViewController <MFMailComposeViewControllerDelegate>

@end
201 changes: 201 additions & 0 deletions LocationFrobber/LFTraceTableViewController.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
//
// LFTraceTableViewController.m
// LocationFrobber
//
// Created by Mark Pirri on 8/21/12.
// Copyright (c) 2012 Mark Pirri. All rights reserved.
//

#import "LFTraceTableViewController.h"

#import "LFLocationTraceManager.h"

@interface LFTraceTableViewController ()

@end

@implementation LFTraceTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self updateBarButtonItems];
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[[LFLocationTraceManager sharedInstance] locationTrace] timeRecords] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

NSDate *time = [[[[LFLocationTraceManager sharedInstance] locationTrace] timeRecords] objectAtIndex:indexPath.row];
NSArray *locationTraces = [[[LFLocationTraceManager sharedInstance] locationTrace] tracesForTime:time];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterNoStyle;
NSString *textStr = [dateFormatter stringFromDate:time];

if ([locationTraces count] > 1) {
CLLocation *firstLocation = [locationTraces objectAtIndex:0];
CLLocation *lastLocation = [locationTraces objectAtIndex:[locationTraces count] - 1];
CLLocationDistance distanceTraveledMetric = [lastLocation distanceFromLocation:firstLocation];
CLLocationDistance distanceTraveledAmerican = distanceTraveledMetric * 3.28084f;
textStr = [textStr stringByAppendingFormat:@" (%.1f ft)",floor(distanceTraveledAmerican)];
}

cell.textLabel.text = textStr;
cell.detailTextLabel.text = [NSString stringWithFormat:@"%d updates",[locationTraces count]];

return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


#pragma mark - change notifications

- (void)locationTraceAddedNotification:(NSNotification *)inNotification
{
[self.tableView reloadData];
}


#pragma mark - bar button items

- (void)startLogging
{
[[LFLocationTraceManager sharedInstance] startLoggingLocation];
[self updateBarButtonItems];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(locationTraceAddedNotification:)
name:[[[LFLocationTraceManager sharedInstance] locationTrace] locationAddedNotificationName]
object:nil];
}

- (void)stopLogging
{
[[LFLocationTraceManager sharedInstance] stopLoggingLocation];
[self updateBarButtonItems];
}

- (void)exportLog
{
MFMailComposeViewController *composeViewController = [[MFMailComposeViewController alloc] init];
composeViewController.mailComposeDelegate = self;
[composeViewController setMessageBody:[[[LFLocationTraceManager sharedInstance] locationTrace] prettyTextWhatForMailingToMyBoss]
isHTML:NO];
[self.navigationController presentModalViewController:composeViewController animated:YES];
}

- (void)updateBarButtonItems
{
if ([[LFLocationTraceManager sharedInstance] isLoggingLocation]) {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Stop" style:UIBarButtonItemStylePlain target:self action:@selector(stopLogging)];
}
else {
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStylePlain target:self action:@selector(startLogging)];
}
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(exportLog)];
}


#pragma mark - MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[controller dismissModalViewControllerAnimated:YES];
}

@end
25 changes: 25 additions & 0 deletions LocationFrobber/Managers/LFLocationTraceManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// LFLocationTraceManager.h
// LocationFrobber
//
// Created by Mark Pirri on 8/21/12.
// Copyright (c) 2012 Mark Pirri. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <CoreLocation/CoreLocation.h>

#import "LFLocationTrace.h"

@interface LFLocationTraceManager : NSObject <CLLocationManagerDelegate>

+ (LFLocationTraceManager *)sharedInstance;

- (LFLocationTrace *)locationTrace;

- (BOOL)isLoggingLocation;
- (void)startLoggingLocation;
- (void)stopLoggingLocation;

@end
66 changes: 66 additions & 0 deletions LocationFrobber/Managers/LFLocationTraceManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// LFLocationTraceManager.m
// LocationFrobber
//
// Created by Mark Pirri on 8/21/12.
// Copyright (c) 2012 Mark Pirri. All rights reserved.
//

#import "LFLocationTraceManager.h"

@interface LFLocationTraceManager ()

@property(nonatomic,assign) BOOL isLoggingLocation;
@property(nonatomic,strong) CLLocationManager *locationManager;
@property(nonatomic,strong) LFLocationTrace *locationTrace;

@end

@implementation LFLocationTraceManager

+ (LFLocationTraceManager *)sharedInstance
{
static LFLocationTraceManager *s_instance = nil;

if (nil == s_instance) {
s_instance = [[[self class] alloc] init];
}

return s_instance;
}

- (id)init
{
self = [super init];
if (nil != self) {
_locationTrace = [[LFLocationTrace alloc] init];
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
}
return self;
}

- (void)startLoggingLocation
{
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
self.isLoggingLocation = YES;
}

- (void)stopLoggingLocation
{
[self.locationManager stopUpdatingLocation];
self.isLoggingLocation = NO;
}


#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[self.locationTrace saveTrace:newLocation forDate:[NSDate date]];
}

@end
23 changes: 23 additions & 0 deletions LocationFrobber/Models/LFLocationTrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// LFLocationTrace.h
// LocationFrobber
//
// Created by Mark Pirri on 8/21/12.
// Copyright (c) 2012 Mark Pirri. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface LFLocationTrace : NSObject

- (NSString *)locationAddedNotificationName;

- (NSArray *)timeRecords;
- (NSArray *)tracesForTime:(NSDate *)inDate;

- (void)saveTrace:(CLLocation *)inLocation forDate:(NSDate *)inDate;

- (NSString *)prettyTextWhatForMailingToMyBoss;

@end
Loading

0 comments on commit 843867b

Please sign in to comment.