From 0a4f78102a90f44c555320205cf54be826cb0795 Mon Sep 17 00:00:00 2001 From: Eddie Ehlin Date: Thu, 10 Jan 2013 22:55:05 +0100 Subject: Add bean functionality is now in order (for state add new) and left to do is being able to edit a bean thats been added (in state new). --- iBean/iBean.xcodeproj/project.pbxproj | 58 ++++-- .../UserInterfaceState.xcuserstate | Bin 30580 -> 32116 bytes iBean/iBean/AddBeanViewController.h | 34 ++++ iBean/iBean/AddBeanViewController.m | 112 ++++++++++++ iBean/iBean/AppDelegate+Storage.h | 5 +- iBean/iBean/AppDelegate+Storage.m | 19 +- iBean/iBean/Bean+Interface.h | 15 ++ iBean/iBean/Bean+Interface.m | 13 ++ iBean/iBean/Bean.h | 2 +- iBean/iBean/Bean.m | 2 +- iBean/iBean/BeanCollection+Interface.h | 16 ++ iBean/iBean/BeanCollection+Interface.m | 55 ++++++ iBean/iBean/BeanCollection.h | 15 +- iBean/iBean/BeanCollection.m | 2 +- iBean/iBean/BeanCollectionBeanListViewController.h | 4 + iBean/iBean/BeanCollectionBeanListViewController.m | 73 +++++++- .../iBean.xcdatamodeld/iBean.xcdatamodel/contents | 2 +- iBean/iBean/iPhoneStoryboard.storyboard | 196 ++++++++++++++++++--- 18 files changed, 567 insertions(+), 56 deletions(-) create mode 100644 iBean/iBean/AddBeanViewController.h create mode 100644 iBean/iBean/AddBeanViewController.m create mode 100644 iBean/iBean/Bean+Interface.h create mode 100644 iBean/iBean/Bean+Interface.m create mode 100644 iBean/iBean/BeanCollection+Interface.h create mode 100644 iBean/iBean/BeanCollection+Interface.m diff --git a/iBean/iBean.xcodeproj/project.pbxproj b/iBean/iBean.xcodeproj/project.pbxproj index d67b623..c17a052 100644 --- a/iBean/iBean.xcodeproj/project.pbxproj +++ b/iBean/iBean.xcodeproj/project.pbxproj @@ -23,10 +23,13 @@ 020C55C716910EAD00D8224C /* InstaBeanConfiguration.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55C616910EAD00D8224C /* InstaBeanConfiguration.m */; }; 020C55CA1691100000D8224C /* AppDelegate+Storage.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55C91691100000D8224C /* AppDelegate+Storage.m */; }; 020C55FA16983E9900D8224C /* BeanCollectionListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55F916983E9900D8224C /* BeanCollectionListViewController.m */; }; - 020C55FD16983F7400D8224C /* Bean.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55FC16983F7400D8224C /* Bean.m */; }; - 020C560016983F7400D8224C /* BeanCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C55FF16983F7400D8224C /* BeanCollection.m */; }; 020C560316989D4500D8224C /* BeanCollectionInfoViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C560216989D4500D8224C /* BeanCollectionInfoViewController.m */; }; 020C56061698A85000D8224C /* BeanCollectionBeanListViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C56051698A85000D8224C /* BeanCollectionBeanListViewController.m */; }; + 020C5672169E1B0100D8224C /* AddBeanViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C5671169E1B0100D8224C /* AddBeanViewController.m */; }; + 020C567A169F597300D8224C /* Bean.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C5679169F597300D8224C /* Bean.m */; }; + 020C567D169F597300D8224C /* BeanCollection.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C567C169F597300D8224C /* BeanCollection.m */; }; + 020C5680169F5E9A00D8224C /* Bean+Interface.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C567F169F5E9900D8224C /* Bean+Interface.m */; }; + 020C5683169F5FBB00D8224C /* BeanCollection+Interface.m in Sources */ = {isa = PBXBuildFile; fileRef = 020C5682169F5FBB00D8224C /* BeanCollection+Interface.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -54,14 +57,20 @@ 020C55C91691100000D8224C /* AppDelegate+Storage.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "AppDelegate+Storage.m"; sourceTree = ""; }; 020C55F816983E9900D8224C /* BeanCollectionListViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollectionListViewController.h; sourceTree = ""; }; 020C55F916983E9900D8224C /* BeanCollectionListViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollectionListViewController.m; sourceTree = ""; }; - 020C55FB16983F7400D8224C /* Bean.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bean.h; sourceTree = ""; }; - 020C55FC16983F7400D8224C /* Bean.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bean.m; sourceTree = ""; }; - 020C55FE16983F7400D8224C /* BeanCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollection.h; sourceTree = ""; }; - 020C55FF16983F7400D8224C /* BeanCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollection.m; sourceTree = ""; }; 020C560116989D4500D8224C /* BeanCollectionInfoViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollectionInfoViewController.h; sourceTree = ""; }; 020C560216989D4500D8224C /* BeanCollectionInfoViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollectionInfoViewController.m; sourceTree = ""; }; 020C56041698A85000D8224C /* BeanCollectionBeanListViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollectionBeanListViewController.h; sourceTree = ""; }; 020C56051698A85000D8224C /* BeanCollectionBeanListViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollectionBeanListViewController.m; sourceTree = ""; }; + 020C5670169E1B0100D8224C /* AddBeanViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AddBeanViewController.h; sourceTree = ""; }; + 020C5671169E1B0100D8224C /* AddBeanViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AddBeanViewController.m; sourceTree = ""; }; + 020C5678169F597300D8224C /* Bean.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Bean.h; sourceTree = ""; }; + 020C5679169F597300D8224C /* Bean.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Bean.m; sourceTree = ""; }; + 020C567B169F597300D8224C /* BeanCollection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BeanCollection.h; sourceTree = ""; }; + 020C567C169F597300D8224C /* BeanCollection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BeanCollection.m; sourceTree = ""; }; + 020C567E169F5E9900D8224C /* Bean+Interface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "Bean+Interface.h"; sourceTree = ""; }; + 020C567F169F5E9900D8224C /* Bean+Interface.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "Bean+Interface.m"; sourceTree = ""; }; + 020C5681169F5FBA00D8224C /* BeanCollection+Interface.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "BeanCollection+Interface.h"; sourceTree = ""; }; + 020C5682169F5FBB00D8224C /* BeanCollection+Interface.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "BeanCollection+Interface.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -162,6 +171,7 @@ 020C56081698F58900D8224C /* BeanCollectionList */, 020C56091698F59F00D8224C /* BeanCollectionInfo */, 020C56071698F4DC00D8224C /* BeanCollectionBeanList */, + 020C566E169E1ACB00D8224C /* Bean */, 020C55DF169792F800D8224C /* Data */, ); name = iBean; @@ -170,10 +180,11 @@ 020C55DF169792F800D8224C /* Data */ = { isa = PBXGroup; children = ( - 020C55FE16983F7400D8224C /* BeanCollection.h */, - 020C55FF16983F7400D8224C /* BeanCollection.m */, - 020C55FB16983F7400D8224C /* Bean.h */, - 020C55FC16983F7400D8224C /* Bean.m */, + 020C5684169F603700D8224C /* Interface */, + 020C567B169F597300D8224C /* BeanCollection.h */, + 020C567C169F597300D8224C /* BeanCollection.m */, + 020C5678169F597300D8224C /* Bean.h */, + 020C5679169F597300D8224C /* Bean.m */, ); name = Data; sourceTree = ""; @@ -205,6 +216,26 @@ name = BeanCollectionInfo; sourceTree = ""; }; + 020C566E169E1ACB00D8224C /* Bean */ = { + isa = PBXGroup; + children = ( + 020C5670169E1B0100D8224C /* AddBeanViewController.h */, + 020C5671169E1B0100D8224C /* AddBeanViewController.m */, + ); + name = Bean; + sourceTree = ""; + }; + 020C5684169F603700D8224C /* Interface */ = { + isa = PBXGroup; + children = ( + 020C567E169F5E9900D8224C /* Bean+Interface.h */, + 020C567F169F5E9900D8224C /* Bean+Interface.m */, + 020C5681169F5FBA00D8224C /* BeanCollection+Interface.h */, + 020C5682169F5FBB00D8224C /* BeanCollection+Interface.m */, + ); + name = Interface; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -278,10 +309,13 @@ 020C55C716910EAD00D8224C /* InstaBeanConfiguration.m in Sources */, 020C55CA1691100000D8224C /* AppDelegate+Storage.m in Sources */, 020C55FA16983E9900D8224C /* BeanCollectionListViewController.m in Sources */, - 020C55FD16983F7400D8224C /* Bean.m in Sources */, - 020C560016983F7400D8224C /* BeanCollection.m in Sources */, 020C560316989D4500D8224C /* BeanCollectionInfoViewController.m in Sources */, 020C56061698A85000D8224C /* BeanCollectionBeanListViewController.m in Sources */, + 020C5672169E1B0100D8224C /* AddBeanViewController.m in Sources */, + 020C567A169F597300D8224C /* Bean.m in Sources */, + 020C567D169F597300D8224C /* BeanCollection.m in Sources */, + 020C5680169F5E9A00D8224C /* Bean+Interface.m in Sources */, + 020C5683169F5FBB00D8224C /* BeanCollection+Interface.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; 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 b147436..c78c824 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/AddBeanViewController.h b/iBean/iBean/AddBeanViewController.h new file mode 100644 index 0000000..155e18b --- /dev/null +++ b/iBean/iBean/AddBeanViewController.h @@ -0,0 +1,34 @@ +// +// AddBeanViewController.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-09. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import +@class BeanCollection; + +@interface AddBeanViewController : UIViewController + +@property (nonatomic, strong) BeanCollection *beanCollection; +@property (nonatomic, assign) BOOL editMode; + +/* Utility methods */ +- (void) initViewController; +- (void) initWithModeAndBeanCollection:(BOOL)editMode: (BeanCollection*) bc; + +/* UI Outlets */ +@property (weak, nonatomic) IBOutlet UITextField *beanNameTextField; +@property (weak, nonatomic) IBOutlet UILabel *amountLabel; +@property (weak, nonatomic) IBOutlet UIStepper *amountStepper; +@property (weak, nonatomic) IBOutlet UILabel *grinderSettingLabel; +@property (weak, nonatomic) IBOutlet UIStepper *grinderSettingStepper; + +/* UI Actions */ +- (IBAction) amountStepperChanged:(id)sender; +- (IBAction) grinderSettingStepperChanged:(id)sender; +- (IBAction) cancelBean:(id)sender; +- (IBAction) commitBean:(id)sender; + +@end diff --git a/iBean/iBean/AddBeanViewController.m b/iBean/iBean/AddBeanViewController.m new file mode 100644 index 0000000..bebea6f --- /dev/null +++ b/iBean/iBean/AddBeanViewController.m @@ -0,0 +1,112 @@ +// +// AddBeanViewController.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-09. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "AppDelegate+Storage.h" +#import "AddBeanViewController.h" +#import "Bean+Interface.h" +#import "BeanCollection+Interface.h" + +@interface AddBeanViewController () + +@end + +@implementation AddBeanViewController + +- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil +{ + self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; + if (self) { + // Custom initialization + } + return self; +} + +- (void)viewDidLoad +{ + [super viewDidLoad]; + // Do any additional setup after loading the view. +} + +- (void) viewWillAppear:(BOOL)animated +{ + [super viewWillAppear:animated]; + [self initViewController]; +} + +- (void)didReceiveMemoryWarning +{ + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +/***************************************************** + Utility methods + *****************************************************/ +- (void) initViewController +{ + NSLog(@"AddBeanViewController - initViewController"); + + if (self.editMode == YES) + { + //TODO: Update UI to refelect edit mode. + + //+ set UI controls with values from beanCollection. + NSLog(@"AddBeanViewController - in edit state"); + } + else + { + NSLog(@"AddBeanViewController - in add new state"); + } +} + +- (void) initWithModeAndBeanCollection:(BOOL)editMode: (BeanCollection*) bc +{ + NSLog(@"AddBeanViewController - initWithModeAndBeanCollection"); + self.editMode = editMode; + self.beanCollection = bc; +} + +/***************************************************** + UI Actions + *****************************************************/ +#pragma mark - IBActions +- (void) amountStepperChanged:(id)sender +{ + self.amountLabel.text = [NSString stringWithFormat:@"%1.1f", self.amountStepper.value]; +} + +- (void) grinderSettingStepperChanged:(id)sender +{ + self.grinderSettingLabel.text = [NSString stringWithFormat:@"%1.1f", self.grinderSettingStepper.value]; +} + +- (void) cancelBean:(id)sender +{ + [self dismissViewControllerAnimated:YES completion:nil]; +} + +- (void) commitBean:(id)sender +{ + Bean *b = [(AppDelegate*) [[UIApplication sharedApplication] delegate] createBean]; + if (b != nil && self.beanCollection != nil) + { + b.amount = [NSNumber numberWithDouble:self.amountStepper.value]; + b.name = self.beanNameTextField.text; + b.grindSetting = [NSNumber numberWithDouble:self.grinderSettingStepper.value]; + [self.beanCollection addBeansObject:b]; +#warning TODO - If we are in edit mode (for the bean collection) should we commit the managed context? + + //Go back to the bean list. + [self dismissViewControllerAnimated:YES completion:nil]; + } + else + { +#warning TODO - Messagebox telling user that error occured. + } +} +@end diff --git a/iBean/iBean/AppDelegate+Storage.h b/iBean/iBean/AppDelegate+Storage.h index effcd92..e56b8bf 100644 --- a/iBean/iBean/AppDelegate+Storage.h +++ b/iBean/iBean/AppDelegate+Storage.h @@ -9,6 +9,7 @@ #import "AppDelegate.h" @class InstaBeanConfiguration; @class BeanCollection; +@class Bean; /* Main purpose of this category is to have all storage (core data) related @@ -30,8 +31,6 @@ #pragma mark - iBean related storage methods - (NSArray*) getBeanCollections; - (BeanCollection*) createBeanCollection; +- (Bean*) createBean; - - -//warning TODO: Create methods for fetching managedContext (if needed?) as well methods for saving/deleting/fetching core data objects. @end diff --git a/iBean/iBean/AppDelegate+Storage.m b/iBean/iBean/AppDelegate+Storage.m index 9acaeb2..4f8fc0d 100644 --- a/iBean/iBean/AppDelegate+Storage.m +++ b/iBean/iBean/AppDelegate+Storage.m @@ -80,7 +80,7 @@ { NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"BeanCollection"]; //For now we sort them on name. - fetchRequest.sortDescriptors = [NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO]]; + fetchRequest.sortDescriptors = [NSArray arrayWithObject: [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]]; NSError *fetchRequestError = nil; fetchRequestResult = [self.managedObjectContext executeFetchRequest:fetchRequest error:&fetchRequestError]; @@ -113,6 +113,23 @@ return bc; } +- (Bean*) createBean +{ + Bean *b = nil; + + if (self.managedObjectContext != nil) + { + b = [NSEntityDescription insertNewObjectForEntityForName:@"Bean" inManagedObjectContext:self.managedObjectContext]; + + //Set default values + if (b != nil) + { + //TODO: Set default values here... + } + } + return b; +} + @end diff --git a/iBean/iBean/Bean+Interface.h b/iBean/iBean/Bean+Interface.h new file mode 100644 index 0000000..3b4874c --- /dev/null +++ b/iBean/iBean/Bean+Interface.h @@ -0,0 +1,15 @@ +// +// Bean+Interface.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-10. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "Bean.h" + +@interface Bean (Interface) + + + +@end diff --git a/iBean/iBean/Bean+Interface.m b/iBean/iBean/Bean+Interface.m new file mode 100644 index 0000000..baf7430 --- /dev/null +++ b/iBean/iBean/Bean+Interface.m @@ -0,0 +1,13 @@ +// +// Bean+Interface.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-10. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "Bean+Interface.h" + +@implementation Bean (Interface) + +@end diff --git a/iBean/iBean/Bean.h b/iBean/iBean/Bean.h index 80094b3..eb74a10 100644 --- a/iBean/iBean/Bean.h +++ b/iBean/iBean/Bean.h @@ -2,7 +2,7 @@ // Bean.h // iBean // -// Created by Eddie Ehlin on 2013-01-05. +// Created by Eddie Ehlin on 2013-01-10. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // diff --git a/iBean/iBean/Bean.m b/iBean/iBean/Bean.m index fbb21b3..6c6a6b4 100644 --- a/iBean/iBean/Bean.m +++ b/iBean/iBean/Bean.m @@ -2,7 +2,7 @@ // Bean.m // iBean // -// Created by Eddie Ehlin on 2013-01-05. +// Created by Eddie Ehlin on 2013-01-10. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // diff --git a/iBean/iBean/BeanCollection+Interface.h b/iBean/iBean/BeanCollection+Interface.h new file mode 100644 index 0000000..6df1040 --- /dev/null +++ b/iBean/iBean/BeanCollection+Interface.h @@ -0,0 +1,16 @@ +// +// BeanCollection+Interface.h +// iBean +// +// Created by Eddie Ehlin on 2013-01-10. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "BeanCollection.h" + +@interface BeanCollection (Interface) + +- (void) addBeansObject: (Bean *)b; +- (void) removeObjectFromBeansAtIndex:(NSUInteger)beanIndex; +- (void)replaceBeansAtIndexes:(NSIndexSet *)beanIndexes withBeans:(NSArray *)beans; +@end diff --git a/iBean/iBean/BeanCollection+Interface.m b/iBean/iBean/BeanCollection+Interface.m new file mode 100644 index 0000000..e01e25e --- /dev/null +++ b/iBean/iBean/BeanCollection+Interface.m @@ -0,0 +1,55 @@ +// +// BeanCollection+Interface.m +// iBean +// +// Created by Eddie Ehlin on 2013-01-10. +// Copyright (c) 2013 Eddie Ehlin. All rights reserved. +// + +#import "BeanCollection+Interface.h" + +@implementation BeanCollection (Interface) + +- (void) addBeansObject:(Bean *)b +{ + NSMutableOrderedSet *tmpSet = [self mutableOrderedSetValueForKey:@"beans"]; + [tmpSet addObject:b]; +} + +- (void) removeObjectFromBeansAtIndex:(NSUInteger)beanIndex +{ + /* + NSIndexSet* indexes = [NSIndexSet indexSetWithIndex:idx]; + [self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"beans"]; + NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"beans"]]; + [tmpOrderedSet removeObjectAtIndex:idx]; + [self setPrimitiveValue:tmpOrderedSet forKey:@"beans"]; + [self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexes forKey:@"beans"]; + */ + + NSMutableOrderedSet *tmpSet = [self mutableOrderedSetValueForKey:@"beans"]; + if (beanIndex >= 0 && beanIndex < tmpSet.count) + [tmpSet removeObjectAtIndex:beanIndex]; + else + NSLog(@"BeanCollection+Interface - removeBeansObjectAtIndex: Index out of range!"); +} + +- (void)replaceBeansAtIndexes:(NSIndexSet *)beanIndexes withBeans:(NSArray *)beans { + /* + [self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"beans"]; + NSMutableOrderedSet *tmpOrderedSet = [NSMutableOrderedSet orderedSetWithOrderedSet:[self mutableOrderedSetValueForKey:@"beans"]]; + [tmpOrderedSet replaceObjectsAtIndexes:indexes withObjects:values]; + [self setPrimitiveValue:tmpOrderedSet forKey:@"beans"]; + [self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexes forKey:@"beans"]; + */ + + if (beans.count > 0) + { + NSMutableOrderedSet *tmpSet = [self mutableOrderedSetValueForKey:@"beans"]; + [tmpSet replaceObjectsAtIndexes:beanIndexes withObjects:beans]; + } + +} + + +@end diff --git a/iBean/iBean/BeanCollection.h b/iBean/iBean/BeanCollection.h index 801fa4c..08f03da 100644 --- a/iBean/iBean/BeanCollection.h +++ b/iBean/iBean/BeanCollection.h @@ -2,7 +2,7 @@ // BeanCollection.h // iBean // -// Created by Eddie Ehlin on 2013-01-05. +// Created by Eddie Ehlin on 2013-01-10. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // @@ -19,14 +19,19 @@ @property (nonatomic, retain) NSDate * modified; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSString * note; -@property (nonatomic, retain) NSSet *beans; +@property (nonatomic, retain) NSOrderedSet *beans; @end @interface BeanCollection (CoreDataGeneratedAccessors) +- (void)insertObject:(Bean *)value inBeansAtIndex:(NSUInteger)idx; +- (void)removeObjectFromBeansAtIndex:(NSUInteger)idx; +- (void)insertBeans:(NSArray *)value atIndexes:(NSIndexSet *)indexes; +- (void)removeBeansAtIndexes:(NSIndexSet *)indexes; +- (void)replaceObjectInBeansAtIndex:(NSUInteger)idx withObject:(Bean *)value; +- (void)replaceBeansAtIndexes:(NSIndexSet *)indexes withBeans:(NSArray *)values; - (void)addBeansObject:(Bean *)value; - (void)removeBeansObject:(Bean *)value; -- (void)addBeans:(NSSet *)values; -- (void)removeBeans:(NSSet *)values; - +- (void)addBeans:(NSOrderedSet *)values; +- (void)removeBeans:(NSOrderedSet *)values; @end diff --git a/iBean/iBean/BeanCollection.m b/iBean/iBean/BeanCollection.m index 39107ba..21ac2ab 100644 --- a/iBean/iBean/BeanCollection.m +++ b/iBean/iBean/BeanCollection.m @@ -2,7 +2,7 @@ // BeanCollection.m // iBean // -// Created by Eddie Ehlin on 2013-01-05. +// Created by Eddie Ehlin on 2013-01-10. // Copyright (c) 2013 Eddie Ehlin. All rights reserved. // diff --git a/iBean/iBean/BeanCollectionBeanListViewController.h b/iBean/iBean/BeanCollectionBeanListViewController.h index 5865904..ec6e47d 100644 --- a/iBean/iBean/BeanCollectionBeanListViewController.h +++ b/iBean/iBean/BeanCollectionBeanListViewController.h @@ -13,6 +13,7 @@ @property (nonatomic, strong) BeanCollection *beanCollection; @property (nonatomic, assign) BOOL editMode; +@property (nonatomic, strong) UIBarButtonItem *beanListEditDoneButton; /* Utility methods */ - (void) initViewController; @@ -20,8 +21,11 @@ /* UI Outlets */ @property (weak, nonatomic) IBOutlet UITableView *beanListTableView; +@property (strong, nonatomic) IBOutlet UIBarButtonItem *beanListEditButton; +@property (weak, nonatomic) IBOutlet UIToolbar *beanListBottomToolbar; /* UI Actions */ +- (IBAction) toggleEditMode:(id)sender; @end diff --git a/iBean/iBean/BeanCollectionBeanListViewController.m b/iBean/iBean/BeanCollectionBeanListViewController.m index 4564776..08b1eed 100644 --- a/iBean/iBean/BeanCollectionBeanListViewController.m +++ b/iBean/iBean/BeanCollectionBeanListViewController.m @@ -7,7 +7,9 @@ // #import "BeanCollectionBeanListViewController.h" +#import "AddBeanViewController.h" #import "BeanCollection.h" +#import "Bean.h" @interface BeanCollectionBeanListViewController () @@ -45,6 +47,23 @@ // Dispose of any resources that can be recreated. } +- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender +{ + //AddBeanSegue + if ([segue.identifier isEqualToString:@"AddBeanSegue"]) + { + //Pass on the edit/add state and bean collection + AddBeanViewController *beanViewController = segue.destinationViewController; + [beanViewController initWithModeAndBeanCollection:self.editMode :self.beanCollection]; + + //TODO: Check what last grinder setting was (last bean) and pre set it + } + else if ([segue.identifier isEqualToString:@"EditBeanSegue"]) + { + //TODO + } +} + /***************************************************** Utility methods *****************************************************/ @@ -52,8 +71,11 @@ { NSLog(@"BeanCollectionBeanListViewController - initViewController"); + //Allocate edit's "Done" button + self.beanListEditDoneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(toggleEditMode:)]; + //Load beans into the table view. - //[self.beanListTableView reloadData]; + [self.beanListTableView reloadData]; if (self.editMode == YES) { @@ -80,6 +102,20 @@ UI Actions *****************************************************/ #pragma mark - IBActions +- (void) toggleEditMode:(id)sender +{ + NSMutableArray *bottomToolbarItems = [NSMutableArray arrayWithArray:self.beanListBottomToolbar.items]; + if (self.beanListTableView.editing) + { + [bottomToolbarItems replaceObjectAtIndex:0 withObject:self.beanListEditButton]; + } + else + { + [bottomToolbarItems replaceObjectAtIndex:0 withObject:self.beanListEditDoneButton]; + } + [self.beanListTableView setEditing:!self.beanListTableView.editing animated:YES]; + [self.beanListBottomToolbar setItems:bottomToolbarItems animated:YES]; +} /***************************************************** UITableView related delegate / datasource methods @@ -102,9 +138,9 @@ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; //Configure the cell and set its title + Bean *currentBean = [self.beanCollection.beans objectAtIndex:indexPath.row]; #warning TODO: This may fuck things up since the beanCollection.beans set is NOT ordered? - BeanCollection *currentCollection = [[self.beanCollection.beans allObjects] objectAtIndex:indexPath.row]; - cell.textLabel.text = currentCollection.name; + cell.textLabel.text = currentBean.name; return cell; } @@ -121,7 +157,12 @@ { if (editingStyle == UITableViewCellEditingStyleDelete) { // Delete the row from the data source -#warning TODO - Implement this (look at CollectionList....) + if (self.beanCollection != nil) + { +#warning TODO - Do we need to remove it via delegate, and then reload the structure or just remove it? + [self.beanCollection removeObjectFromBeansAtIndex: indexPath.row]; + } +#warning TODO - Double check that correct bean is removed! [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else if (editingStyle == UITableViewCellEditingStyleInsert) { @@ -129,5 +170,29 @@ } } + // 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; + } + + // Override to support rearranging the table view. +//http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html + - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath + { + if (self.beanCollection != nil && fromIndexPath.row != toIndexPath.row) + { + NSLog(@"From: %d, To: %d", fromIndexPath.row, toIndexPath.row); + if (toIndexPath.row < (self.beanCollection.beans.count - 1)) + { + NSMutableIndexSet *beanIndexes = [NSMutableIndexSet indexSetWithIndex:fromIndexPath.row]; + [beanIndexes addIndex:toIndexPath.row]; + NSArray *beans = [NSArray arrayWithObjects:[self.beanCollection.beans objectAtIndex:fromIndexPath.row], [self.beanCollection.beans objectAtIndex:toIndexPath.row], nil]; + [self.beanCollection replaceBeansAtIndexes:beanIndexes withBeans:beans]; + } + } +#warning TODO - Make sure to test to "view/edit" bean after moving it, so that it is correct item! :-) + } @end diff --git a/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents b/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents index 4677f93..f7f7901 100644 --- a/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents +++ b/iBean/iBean/iBean.xcdatamodeld/iBean.xcdatamodel/contents @@ -13,7 +13,7 @@ - + diff --git a/iBean/iBean/iPhoneStoryboard.storyboard b/iBean/iBean/iPhoneStoryboard.storyboard index 5fc65b8..9fbf609 100644 --- a/iBean/iBean/iPhoneStoryboard.storyboard +++ b/iBean/iBean/iPhoneStoryboard.storyboard @@ -236,7 +236,7 @@ - + @@ -269,11 +269,11 @@ - + - + + + + @@ -293,11 +296,15 @@ - + + + + + - + @@ -322,12 +329,91 @@ + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -373,14 +459,14 @@ - + - + - + - + + + + @@ -400,8 +489,12 @@ - - + + - - - - - - - + + + + + + - + - - - + + + + + + + + - + @@ -471,9 +596,26 @@ + + + + + + + + + + + + + + + + + @@ -518,6 +660,6 @@ - + \ No newline at end of file -- cgit v1.2.3