From 50004ee7e4c682d1a7bf38004ddb911ddd130f08 Mon Sep 17 00:00:00 2001 From: Eddie Ehlin Date: Sat, 5 Jan 2013 01:45:27 +0100 Subject: Added Bean collection list view controller and managed object classes (bean collection and bean) generated from core data --- iBean/iBean.xcodeproj/project.pbxproj | 35 +++++ .../UserInterfaceState.xcuserstate | Bin 21878 -> 25626 bytes iBean/iBean/AppDelegate+Storage.h | 3 +- iBean/iBean/AppDelegate+Storage.m | 39 ++++- iBean/iBean/Bean.h | 21 +++ iBean/iBean/Bean.m | 20 +++ iBean/iBean/BeanCollection.h | 32 ++++ iBean/iBean/BeanCollection.m | 23 +++ iBean/iBean/InstaBeanViewController.m | 3 +- .../iBean.xcdatamodeld/iBean.xcdatamodel/contents | 17 ++ iBean/iBean/iBeanListViewController.h | 23 +++ iBean/iBean/iBeanListViewController.m | 175 +++++++++++++++++++++ iBean/iBean/iPhoneStoryboard.storyboard | 35 +++-- 13 files changed, 413 insertions(+), 13 deletions(-) create mode 100644 iBean/iBean/Bean.h create mode 100644 iBean/iBean/Bean.m create mode 100644 iBean/iBean/BeanCollection.h create mode 100644 iBean/iBean/BeanCollection.m create mode 100644 iBean/iBean/iBeanListViewController.h create mode 100644 iBean/iBean/iBeanListViewController.m (limited to 'iBean') diff --git a/iBean/iBean.xcodeproj/project.pbxproj b/iBean/iBean.xcodeproj/project.pbxproj index b66ea7d..264aee4 100644 --- a/iBean/iBean.xcodeproj/project.pbxproj +++ b/iBean/iBean.xcodeproj/project.pbxproj @@ -22,6 +22,9 @@ 020C55C41691059800D8224C /* InstaBeanViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55C31691059800D8224C /* InstaBeanViewController.m */; }; 020C55C716910EAD00D8224C /* InstaBeanConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55C616910EAD00D8224C /* InstaBeanConfiguration.m */; }; 020C55CA1691100000D8224C /* AppDelegate+Storage.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55C91691100000D8224C /* AppDelegate+Storage.m */; }; + 020C55DA1695CAD200D8224C /* iBeanListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55D91695CAD200D8224C /* iBeanListViewController.m */; }; + 020C55EF1697970200D8224C /* Bean.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55EE1697970200D8224C /* Bean.m */; }; + 020C55F21697970200D8224C /* BeanCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55F11697970200D8224C /* BeanCollection.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -47,6 +50,12 @@ 020C55C616910EAD00D8224C /* InstaBeanConfiguration.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = InstaBeanConfiguration.m; sourceTree = ""; }; 020C55C81691100000D8224C /* AppDelegate+Storage.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "AppDelegate+Storage.h"; sourceTree = ""; }; 020C55C91691100000D8224C /* AppDelegate+Storage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AppDelegate+Storage.m"; sourceTree = ""; }; + 020C55D81695CAD200D8224C /* iBeanListViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = iBeanListViewController.h; sourceTree = ""; }; + 020C55D91695CAD200D8224C /* iBeanListViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = iBeanListViewController.m; sourceTree = ""; }; + 020C55ED1697970200D8224C /* Bean.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bean.h; sourceTree = ""; }; + 020C55EE1697970200D8224C /* Bean.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bean.m; sourceTree = ""; }; + 020C55F01697970200D8224C /* BeanCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollection.h; sourceTree = ""; }; + 020C55F11697970200D8224C /* BeanCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollection.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -95,6 +104,7 @@ 020C559F1691014E00D8224C /* iBean */ = { isa = PBXGroup; children = ( + 020C55D31695C2C800D8224C /* iBean */, 020C55BB169101CE00D8224C /* Storyboards */, 020C55B9169101A300D8224C /* InstaBean */, 020C55A81691014E00D8224C /* AppDelegate.h */, @@ -140,6 +150,27 @@ name = Storyboards; sourceTree = ""; }; + 020C55D31695C2C800D8224C /* iBean */ = { + isa = PBXGroup; + children = ( + 020C55DF169792F800D8224C /* Data */, + 020C55D81695CAD200D8224C /* iBeanListViewController.h */, + 020C55D91695CAD200D8224C /* iBeanListViewController.m */, + ); + name = iBean; + sourceTree = ""; + }; + 020C55DF169792F800D8224C /* Data */ = { + isa = PBXGroup; + children = ( + 020C55F01697970200D8224C /* BeanCollection.h */, + 020C55F11697970200D8224C /* BeanCollection.m */, + 020C55ED1697970200D8224C /* Bean.h */, + 020C55EE1697970200D8224C /* Bean.m */, + ); + name = Data; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -212,6 +243,9 @@ 020C55C41691059800D8224C /* InstaBeanViewController.m in Sources */, 020C55C716910EAD00D8224C /* InstaBeanConfiguration.m in Sources */, 020C55CA1691100000D8224C /* AppDelegate+Storage.m in Sources */, + 020C55DA1695CAD200D8224C /* iBeanListViewController.m in Sources */, + 020C55EF1697970200D8224C /* Bean.m in Sources */, + 020C55F21697970200D8224C /* BeanCollection.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -322,6 +356,7 @@ 020C55B81691014E00D8224C /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ diff --git a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate index 1c1f737..170f786 100644 Binary files a/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate and b/iBean/iBean.xcodeproj/project.xcworkspace/xcuserdata/eddiex.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/iBean/iBean/AppDelegate+Storage.h b/iBean/iBean/AppDelegate+Storage.h index 3fa5acd..09ef412 100644 --- a/iBean/iBean/AppDelegate+Storage.h +++ b/iBean/iBean/AppDelegate+Storage.h @@ -18,13 +18,14 @@ #pragma mark - Common storage related methods - (NSError*) save; +- (NSError*) deleteObject: (NSManagedObject*) managedObject; #pragma mark - InstaBean related storage methods - (InstaBeanConfiguration*) getInstaBeanConfiguration; #pragma mark - iBean related storage methods - +- (NSArray*) getBeanCollections; diff --git a/iBean/iBean/AppDelegate+Storage.m b/iBean/iBean/AppDelegate+Storage.m index 0fb5fac..6d4288a 100644 --- a/iBean/iBean/AppDelegate+Storage.m +++ b/iBean/iBean/AppDelegate+Storage.m @@ -20,6 +20,17 @@ return error; } +- (NSError*) deleteObject: (NSManagedObject*) managedObject +{ + NSError *error = nil; + if (managedObject != nil && self.managedObjectContext != nil) + { + [self.managedObjectContext deleteObject:managedObject]; + error = [self save]; + } + return error; +} + #pragma mark - InstaBean related storage methods - (InstaBeanConfiguration*) getInstaBeanConfiguration { @@ -33,7 +44,7 @@ NSError *fetchRequestError = nil; NSArray *fetchRequestResult = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchRequestError]; - if (fetchRequestResult == nil || fetchRequestError) + if (fetchRequestResult == nil || fetchRequestError != nil) { NSLog(@"getInstaBeanConfiguration - Fetch request resulted in an error!"); #warning TODO: Implement proper error handling (perhaps a message box?) @@ -51,6 +62,32 @@ return config; } +#pragma mark - iBean (Bean Collections) related storage methods +- (NSArray*) getBeanCollections +{ + NSArray *fetchRequestResult = nil; + + if (self.managedObjectContext != nil) + { + NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"BeanCollection"]; + //For now we sort them on name. + fetchRequest.sortDescriptors = [NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]]; + + NSError *fetchRequestError = nil; + fetchRequestResult = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchRequestError]; + + if (fetchRequestResult == nil || fetchRequestError != nil) + { + NSLog(@"getBeanCollections - Fetch request resulted in an error!"); +#warning TODO: Implement proper error handling (perhaps a message box?) + } + } + + return fetchRequestResult; +} + + + @end diff --git a/iBean/iBean/Bean.h b/iBean/iBean/Bean.h new file mode 100644 index 0000000..d42a98f --- /dev/null +++ b/iBean/iBean/Bean.h @@ -0,0 +1,21 @@ +// +// Bean.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-05. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import +#import + +@class BeanCollection; + +@interface Bean : NSManagedObject + +@property (nonatomic, retain) NSString * name; +@property (nonatomic, retain) NSNumber * amount; +@property (nonatomic, retain) NSNumber * grindSetting; +@property (nonatomic, retain) BeanCollection *beanCollection; + +@end diff --git a/iBean/iBean/Bean.m b/iBean/iBean/Bean.m new file mode 100644 index 0000000..e9b85fa --- /dev/null +++ b/iBean/iBean/Bean.m @@ -0,0 +1,20 @@ +// +// Bean.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-05. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "Bean.h" +#import "BeanCollection.h" + + +@implementation Bean + +@dynamic name; +@dynamic amount; +@dynamic grindSetting; +@dynamic beanCollection; + +@end diff --git a/iBean/iBean/BeanCollection.h b/iBean/iBean/BeanCollection.h new file mode 100644 index 0000000..73f935d --- /dev/null +++ b/iBean/iBean/BeanCollection.h @@ -0,0 +1,32 @@ +// +// BeanCollection.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-05. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import +#import + +@class Bean; + +@interface BeanCollection : NSManagedObject + +@property (nonatomic, retain) NSString * name; +@property (nonatomic, retain) NSNumber * extractionTime; +@property (nonatomic, retain) NSDate * created; +@property (nonatomic, retain) NSNumber * extractionCounter; +@property (nonatomic, retain) NSString * note; +@property (nonatomic, retain) NSDate * modified; +@property (nonatomic, retain) NSSet *beans; +@end + +@interface BeanCollection (CoreDataGeneratedAccessors) + +- (void)addBeansObject:(Bean *)value; +- (void)removeBeansObject:(Bean *)value; +- (void)addBeans:(NSSet *)values; +- (void)removeBeans:(NSSet *)values; + +@end diff --git a/iBean/iBean/BeanCollection.m b/iBean/iBean/BeanCollection.m new file mode 100644 index 0000000..cfeceda --- /dev/null +++ b/iBean/iBean/BeanCollection.m @@ -0,0 +1,23 @@ +// +// BeanCollection.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-05. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "BeanCollection.h" +#import "Bean.h" + + +@implementation BeanCollection + +@dynamic name; +@dynamic extractionTime; +@dynamic created; +@dynamic extractionCounter; +@dynamic note; +@dynamic modified; +@dynamic beans; + +@end diff --git a/iBean/iBean/InstaBeanViewController.m b/iBean/iBean/InstaBeanViewController.m index 15b8330..4b28005 100644 --- a/iBean/iBean/InstaBeanViewController.m +++ b/iBean/iBean/InstaBeanViewController.m @@ -15,7 +15,7 @@ @implementation InstaBeanViewController -/* + - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; @@ -24,7 +24,6 @@ } return self; } -*/ - (void)viewDidLoad { diff --git a/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents b/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents index 3098915..cbceae9 100644 --- a/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents +++ b/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents @@ -1,9 +1,26 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/iBean/iBean/iBeanListViewController.h b/iBean/iBean/iBeanListViewController.h new file mode 100644 index 0000000..f0a76b9 --- /dev/null +++ b/iBean/iBean/iBeanListViewController.h @@ -0,0 +1,23 @@ +// +// iBeanListViewController.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-03. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import + +@interface iBeanListViewController : UITableViewController + +@property (nonatomic, strong) NSArray *beanCollections; + +/* Utility methods */ +- (void) initViewController; + +/* UI Outlets */ + + +/* UI Actions */ + +@end diff --git a/iBean/iBean/iBeanListViewController.m b/iBean/iBean/iBeanListViewController.m new file mode 100644 index 0000000..ac97a10 --- /dev/null +++ b/iBean/iBean/iBeanListViewController.m @@ -0,0 +1,175 @@ +// +// iBeanListViewController.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-03. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "iBeanListViewController.h" +#import "BeanCollection.h" +#import "AppDelegate+Storage.h" + +@interface iBeanListViewController () + +@end + +@implementation iBeanListViewController + +- (id)initWithStyle:(UITableViewStyle)style +{ + self = [super initWithStyle:style]; + if (self) { + // Custom initialization + } + return self; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + NSLog(@"iBeanListViewController view's loaded!"); + + + // 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 initViewController]; + + //TODO: Rollback if view is "backed" to from add-state, so that nothing gets saved...that shouldn't be saved. +} + +- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ + if ([segue.identifier isEqualToString:@"AddNewStep1Segue"]) + { + NSLog(@"Going to Add new - Step 1"); + //TODO: Create new bean collection object (managed context object) and push it through the segue + //to the next view controller! + //segue.destinationViewController Points to next view controller! Fetch it and add/set the bean collection object to use. + } +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +#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 (we only have one section). + return self.beanCollections.count; +} + +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath +{ + static NSString *CellIdentifier = @"BeanCollectionCell"; + UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; + + //Configure the cell and set its title + BeanCollection *currentCollection = [self.beanCollections objectAtIndex:indexPath.row]; + cell.textLabel.text = currentCollection.name; + + 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 (we want to be able to REMOVE though). + 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 + if (self.beanCollections != nil) + { + if ([(AppDelegate*) [[UIApplication sharedApplication] delegate] deleteObject:[self.beanCollections objectAtIndex:indexPath.row]] != nil) + { + NSLog(@"deleteObject returned an error when deleting bean collection!"); +#warning TODO: Handle deletion error!? + } + else + { + //Reload our bean collections array from storage (since it is no longer consistent) + self.beanCollections = [(AppDelegate*) [[UIApplication sharedApplication] delegate] getBeanCollections]; + } + } + [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 +{ + // Navigation logic may go here. Create and push another view controller. + /* + <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; + // ... + // Pass the selected object to the new view controller. + [self.navigationController pushViewController:detailViewController animated:YES]; + */ +} + + +/***************************************************** + Utility methods + *****************************************************/ +- (void) initViewController +{ + NSLog(@"iBeanListViewController - initViewController"); + + //Load bean collections from core data storage + self.beanCollections = [(AppDelegate*) [[UIApplication sharedApplication] delegate] getBeanCollections]; + if (self.beanCollections != nil) + { + [self.tableView reloadData]; + } +} + +/***************************************************** + UI Actions + *****************************************************/ +#pragma mark - IBActions + +@end diff --git a/iBean/iBean/iPhoneStoryboard.storyboard b/iBean/iBean/iPhoneStoryboard.storyboard index 00a86fa..aca958d 100644 --- a/iBean/iBean/iPhoneStoryboard.storyboard +++ b/iBean/iBean/iPhoneStoryboard.storyboard @@ -4,21 +4,28 @@ - + - + - + + + + @@ -32,14 +39,14 @@ - + - + @@ -177,7 +184,7 @@ - +