Skip to content

Commit

Permalink
Archive logEntry objects with NSCoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nielubowicz committed Feb 19, 2011
1 parent f14be3b commit 68f3beb
Show file tree
Hide file tree
Showing 22 changed files with 1,516 additions and 137 deletions.
19 changes: 17 additions & 2 deletions Classes/iAbetesViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@

#import <UIKit/UIKit.h>

@interface iAbetesViewController : UIViewController {

@interface iAbetesViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UILabel *currentTime;
IBOutlet UITextField *bloodSugar;
IBOutlet UITextField *insulin;
IBOutlet UITextField *exercizeDuration;
IBOutlet UISlider *exercizeIntensity;

NSMutableSet *logEntries;
}

@property (nonatomic, retain) IBOutlet UILabel *currentTime;
@property (nonatomic, retain) IBOutlet UITextField *bloodSugar;
@property (nonatomic, retain) IBOutlet UITextField *insulin;
@property (nonatomic, retain) IBOutlet UITextField *exercizeDuration;
@property (nonatomic, retain) IBOutlet UISlider *exercizeIntensity;

-(IBAction)saveLogEntry;

@end

109 changes: 86 additions & 23 deletions Classes/iAbetesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,102 @@
//

#import "iAbetesViewController.h"
#import "NSDate+TimeString.h"
#import "LogEntry.h"

@interface iAbetesViewController()

-(NSString *)documentsDirectory;
-(void)loadDataFromDisk;
-(void)saveDataToDisk;

@end

@implementation iAbetesViewController

@synthesize currentTime, bloodSugar, exercizeDuration, exercizeIntensity, insulin;

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
logEntries = [[NSMutableSet alloc] init];
}
return self;
}

-(void)awakeFromNib
{
[self loadDataFromDisk];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"current: %@", [[NSDate date] timeString]);
[currentTime setText:[[NSDate date] timeString]];
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {

-(IBAction)saveLogEntry
{
LogEntry *entry = [[[LogEntry alloc] init] autorelease];
if ([[bloodSugar text] doubleValue])
[entry setBloodSugar:[[bloodSugar text] doubleValue]];

if ([[insulin text] doubleValue])
[entry setInsulinBolus:[[insulin text] doubleValue]];

if ([[exercizeDuration text] intValue])
[entry setExercize:[exercizeIntensity value] duration:[[exercizeDuration text] intValue]];

[entry setTime:[NSDate date]];
NSLog(@"saving %@", [entry description]);
[logEntries addObject:entry];
[self saveDataToDisk];
}
*/

-(void)saveDataToDisk
{
NSString * path = [self documentsDirectory];
if (!path) return;

NSLog(@"saving %@ to path: %@", [logEntries description], path);
NSMutableDictionary *rootObject = [NSMutableDictionary dictionary];
[rootObject setValue:logEntries forKey:@"logEntries"];
[NSKeyedArchiver archiveRootObject:rootObject toFile:path];

[logEntries removeAllObjects];
}

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
-(void)loadDataFromDisk
{
NSString *path = [self documentsDirectory];
if (!path) return;

NSDictionary *rootObject = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (logEntries)
[logEntries release];

logEntries = [[rootObject valueForKey:@"logEntries"] retain];
if (!logEntries)
logEntries = [[NSMutableSet alloc] init];

NSLog(@"logs loaded: %@", [logEntries description]);
}
*/

- (NSString *)documentsDirectory {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
Expand All @@ -57,6 +116,10 @@ - (void)viewDidUnload {
// e.g. self.myOutlet = nil;
}

- (void) applicationWillTerminate: (NSNotification *)note
{
[self saveDataToDisk];
}

- (void)dealloc {
[super dealloc];
Expand Down
4 changes: 2 additions & 2 deletions Classes/model/LogEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#import <Foundation/Foundation.h>

@interface LogEntry : NSObject
@interface LogEntry : NSObject <NSCoding>
{
NSMutableDictionary *information;
}
Expand All @@ -19,7 +19,7 @@
-(void)setInsulinBolus:(double)units;
-(void)setInsulinBolus:(double)units overTimeInterval:(NSTimeInterval)bolusDuration;

-(void)addFoodItem:(Food *)item;
//-(void)addFoodItem:(Food *)item;
-(void)setExercize:(double)intensity duration:(NSTimeInterval)duration;


Expand Down
28 changes: 21 additions & 7 deletions Classes/model/LogEntry.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ -(id)init
}
return self;
}
-(id)initWithCoder:(NSCoder *)coder
{
if (self = [super init])
{
information = [[coder decodeObjectForKey:@"information"] retain];
if (!information)
information = [[NSMutableDictionary alloc] init];
}
return self;
}

-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:information forKey:@"information"];
}

-(void)setBloodSugar:(NSUInteger)bloodSugar
{
Expand All @@ -35,7 +50,7 @@ -(void)setBloodSugar:(NSUInteger)bloodSugar

-(void)setTime:(NSDate *)time
{
[information setObject:timeOfEntry forKey:kTime];
[information setObject:time forKey:kTime];
}

-(void)setInsulinBolus:(double)units
Expand All @@ -49,11 +64,6 @@ -(void)setInsulinBolus:(double)units overTimeInterval:(NSTimeInterval)bolusDurat
[information setObject:[NSNumber numberWithDouble:bolusDuration] forKey:kBolusDuration];
}

-(void)addFoodItem:(Food *)item
{
// no food support yet
}

-(void)setExercize:(double)intensity duration:(NSTimeInterval)duration
{
// improve this - add type?
Expand All @@ -66,9 +76,13 @@ -(void)setWaterConsumption:(NSUInteger)fluidOunces
[information setObject:[NSNumber numberWithUnsignedInt:fluidOunces] forKey:kWaterConsumed];
}

-(NSString *)description
{
return [information description];
}

-(void)dealloc
{
[timeOfEntry release];
[information release];
[super dealloc];
}
Expand Down
16 changes: 16 additions & 0 deletions Classes/model/NSDate+TimeString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// NSDate+CurrentTimeString.h
// iAbetes
//
// Created by chris nielubowicz on 2/19/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>


@interface NSDate(TimeString)

-(NSString *)timeString;

@end
22 changes: 22 additions & 0 deletions Classes/model/NSDate+TimeString.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// NSDate+CurrentTimeString.m
// iAbetes
//
// Created by chris nielubowicz on 2/19/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "NSDate+TimeString.h"


@implementation NSDate(TimeString)

-(NSString *)timeString
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
// display in 12HR/24HR (i.e. 11:25PM or 23:25) format according to User Settings
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
return [dateFormatter stringFromDate:self];
}

@end
Binary file modified build/iAbetes.build/iAbetes.pbxindex/categories.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/cdecls.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/decls.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/files.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/imports.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/pbxindex.header
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/protocols.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/refs.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/strings.pbxstrings/control
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/strings.pbxstrings/strings
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/subclasses.pbxbtree
Binary file not shown.
Binary file modified build/iAbetes.build/iAbetes.pbxindex/symbols0.pbxsymbols
Binary file not shown.
Loading

0 comments on commit 68f3beb

Please sign in to comment.