Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Mostly working round/item management.

  • Loading branch information...
commit 4bb0fde16066828df48564ce0add9f7046112a3e 1 parent 42410a5
David Brooks authored
View
9 TotallyTopSecret/RoundItemsController.h
@@ -9,19 +9,22 @@
#import <UIKit/UIKit.h>
#import "Round.h"
-
@interface RoundItemsController : UITableViewController {
NSManagedObjectContext *managedObjectContext;
+ @private int itemCounter; // temporary; will be replaced once items are static
+ NSManagedObjectID *roundID;
Round *round;
NSArray *roundItems;
}
+
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
+@property (nonatomic, retain, setter = setHiddenRoundID) NSManagedObjectID *roundID;
@property (nonatomic, retain) Round *round;
@property (nonatomic, retain) NSArray *roundItems;
-- (BOOL)setRoundFromID:(NSManagedObjectID *)roundID;
-
+- (int)nextCounter;
+- (void)setRoundID:(NSManagedObjectID *)roundID;
- (IBAction)addRoundItem:(id)sender;
@end
View
116 TotallyTopSecret/RoundItemsController.m
@@ -11,30 +11,36 @@
#import "Round.h"
#import "RoundItem.h"
-
@implementation RoundItemsController
@synthesize managedObjectContext;
+@synthesize roundID;
@synthesize round;
@synthesize roundItems;
-- (BOOL)setRoundFromID:(NSManagedObjectID *)roundID {
- NSLog(@"Attempting to load from %@", roundID);
+- (int)nextCounter {
+ return itemCounter++;
+}
+
+- (void)updateRoundItems {
+ self.roundItems = [[self.round items] sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"item.name" ascending:YES]]];
+}
+
+- (void)setRoundID:(NSManagedObjectID *)roundObjID {
NSError *error = nil;
- self.round = (Round *)[self.managedObjectContext existingObjectWithID:roundID error:&error];
- if(self.round) {
- NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"count" ascending:YES];
- self.roundItems = [[self.round items] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByName]];
-
- [self.tableView reloadData];
- self.title = self.round.name;
- return YES;
- } else {
- NSLog(@"couldn't find round with ID %@", roundID);
- return NO;
- }
+ self.roundID = roundObjID;
+ self.round = (Round *)[self.managedObjectContext existingObjectWithID:self.roundID error:&error];
+ assert(self.round);
+
+ /* load the items and title for the specified round */
+ [self updateRoundItems];
+ [self.tableView reloadData];
+ self.title = self.round.name;
}
+- (void)refreshRoundAndItems {
+ [self setRoundID:self.roundID];
+}
/**
* Helper function to get Entity metadata from entity name, for the shared context.
@@ -43,49 +49,29 @@ - (NSEntityDescription *)getEntityDescription:(NSString *)entityName {
return [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
}
-/**
- * Helper function to create a new Item with a given name.
- */
-- (Item *)makeItem:(NSString *)name {
+- (IBAction)addRoundItem:(id)sender {
+ // not released, because it's a Core Data entity
Item *newItem =[[Item alloc] initWithEntity:[self getEntityDescription:@"Item"] insertIntoManagedObjectContext:self.managedObjectContext];
- newItem.name = name;
- return newItem;
-}
-
-/**
- * Helper function to create a new RoundItem from a round.
- */
-- (RoundItem *)makeRoundItem:(Item *)item {
+ newItem.name = [NSString stringWithFormat:@"New item %d", [self nextCounter]];
+
+ // not released, because it's a Core Data entity
RoundItem *newRoundItem =[[RoundItem alloc] initWithEntity:[self getEntityDescription:@"RoundItem"] insertIntoManagedObjectContext:self.managedObjectContext];
newRoundItem.round = self.round;
- newRoundItem.item = item;
- return newRoundItem;
-}
-
-
-- (IBAction)addRoundItem:(id)sender {
- Item *newItem = [self makeItem:@"New item"];
- RoundItem *newRoundItem = [self makeRoundItem:newItem];
+ newRoundItem.item = newItem;
NSError *error = nil;
if([self.managedObjectContext save:&error]) {
[self.managedObjectContext refreshObject:self.round mergeChanges:YES];
- NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"count" ascending:YES];
- self.roundItems = [[self.round items] sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortByName]];
+ [self updateRoundItems];
- NSUInteger index = [self.roundItems indexOfObject:newRoundItem];
// force tableView update
+ NSUInteger index = [self.roundItems indexOfObject:newRoundItem];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
-
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
- [indexPath release];
} else {
NSLog(@"error creating round item");
}
-
- [newItem release];
- [newRoundItem release];
}
@@ -123,12 +109,6 @@ - (void)viewDidLoad
[addItemButton release];
[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)viewDidUnload
@@ -168,14 +148,6 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
#pragma mark - Table view data source
-/*
-- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
-{
-#warning Potentially incomplete method implementation.
- // Return the number of sections.
- return 1;
-}
-*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [roundItems count];
@@ -190,10 +162,9 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
- // Configure the cell...
+ // Configure the cell label
RoundItem *roundItem = [roundItems objectAtIndex:indexPath.row];
- cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@ %@", roundItem.count, roundItem.item.name];
- [roundItem release];
+ cell.textLabel.text = [NSString stringWithFormat:@"%d %@", [roundItem.count intValue], roundItem.item.name];
return cell;
}
@@ -249,6 +220,31 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
+
+ NSManagedObjectID *roundItemID = [[roundItems objectAtIndex:indexPath.row] objectID];
+ NSLog(@"roundItemIDpointer = %p", roundItemID);
+ NSLog(@"roundItemID = %@", roundItemID);
+ RoundItem *tappedRoundItem = (RoundItem *)[self.managedObjectContext objectWithID:roundItemID];
+ NSLog(@"tappedRoundItemPointer = %p", tappedRoundItem);
+ NSLog(@"tappedRoundItem = %@", tappedRoundItem);
+ NSNumber *lastCountNum = tappedRoundItem.count;
+ NSLog(@"lastCountNum = %@", lastCountNum);
+ int lastCount = [lastCountNum intValue];
+ NSLog(@"lastCount = %d", lastCount);
+ int incremented = lastCount + 1;
+ NSLog(@"incremented = %d", incremented);
+ NSNumber *newCountNum = [NSNumber numberWithInt:incremented];
+ NSLog(@"newCountNum = %@", newCountNum);
+ [tappedRoundItem setCount:newCountNum];
+ NSLog(@"new count = %@", tappedRoundItem.count);
+ NSError *error = nil;
+ if([self.managedObjectContext save:&error]) {
+ NSLog(@"Saved successfully after increment");
+ [self refreshRoundAndItems];
+ [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
+ } else {
+ NSLog(@"Saved failed after increment");
+ }
}
@end
View
5 TotallyTopSecret/RoundsController.h
@@ -10,13 +10,18 @@
#import "Round.h"
@interface RoundsController : UITableViewController {
+ @private int roundCounter;
+ NSObject *delegate;
NSManagedObjectContext *managedObjectContext;
NSMutableArray *rounds;
}
+@property (nonatomic, retain) NSObject *delegate;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *rounds;
+- (int)nextCounter;
- (void)addRound:(Round *)newRound;
+- (void)selectRound:(Round *)newRound;
@end
View
87 TotallyTopSecret/RoundsController.m
@@ -6,15 +6,21 @@
// Copyright 2011 Untyped. All rights reserved.
//
+#import "TotallyTopSecretAppDelegate.h"
#import "RoundsController.h"
#import "Round.h"
@implementation RoundsController
+@synthesize delegate;
@synthesize managedObjectContext;
@synthesize rounds;
+- (int)nextCounter {
+ return roundCounter++;
+}
+
/**
* Prepends a new round to the list of visible rounds.
*/
@@ -25,9 +31,15 @@ - (void)addRound:(Round *)newRound {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
- [indexPath release];
}
+- (void)selectRound:(Round *)round {
+ TotallyTopSecretAppDelegate *ttsad = (TotallyTopSecretAppDelegate *)self.delegate;
+ NSLog(@"ttsad %@", ttsad);
+ [ttsad viewRound:[round objectID]];
+}
+
+
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
@@ -55,8 +67,7 @@ - (void)didReceiveMemoryWarning
- (void)viewDidLoad
{
[super viewDidLoad];
- NSLog(@"viewDidLoad");
-
+
/*
* If the rounds list is unset, initialise as empty
* TotallyTopSercretAppDelegate should already have set this from Core Data.
@@ -68,7 +79,7 @@ - (void)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;
}
@@ -125,50 +136,48 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
// Configure the cell...
Round *rowRound = [rounds objectAtIndex:indexPath.row];
- cell.textLabel.text = rowRound.name;
- [rowRound release];
-
+ cell.textLabel.text = rowRound.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.
- return YES;
-}
-*/
+ // 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:[NSArray arrayWithObject: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 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:[NSArray arrayWithObject: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 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;
-}
-*/
+ // 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
@@ -182,6 +191,8 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
+
+ [self selectRound:[self.rounds objectAtIndex:indexPath.row]];
}
@end
View
BIN  TotallyTopSecret/TotallyTopSecret.xcdatamodeld/TotallyTopSecret.xcdatamodel/elements
Binary file not shown
View
31 TotallyTopSecret/TotallyTopSecretAppDelegate.m
@@ -6,6 +6,7 @@
// Copyright 2011 Untyped. All rights reserved.
//
+//#import <stdlib.h>
#import "TotallyTopSecretAppDelegate.h"
#import "RoundsController.h"
#import "Round.h"
@@ -31,15 +32,6 @@ - (NSEntityDescription *)getEntityDescription:(NSString *)entityName {
return [NSEntityDescription entityForName:entityName inManagedObjectContext:self.managedObjectContext];
}
-/**
- * Helper function to create a new Round.
- */
-- (Round *)makeRound:(NSString *)name {
- Round *newRound =[[Round alloc] initWithEntity:[self getEntityDescription:@"Round"] insertIntoManagedObjectContext:self.managedObjectContext];
- newRound.name = name;
- return newRound;
-}
-
- (void)viewRound:(NSManagedObjectID *)roundID {
[NSException raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)];
}
@@ -53,29 +45,30 @@ - (NSMutableArray *)fetchAllRounds {
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
- if (mutableFetchResults == nil) {
- return [[NSMutableArray alloc] init];
+ [request release];
+ if (mutableFetchResults != nil) {
+ return [mutableFetchResults autorelease];
+ } else {
+ return [NSMutableArray array];
}
-
- return mutableFetchResults;
}
/**
* Add a new round to the list of rounds.
*/
- (IBAction)addRound:(id)sender {
- Round *newRound = [self makeRound:@"New round"];
+ // not released, because it's a Core Data entity
+ Round *newRound =[[Round alloc] initWithEntity:[self getEntityDescription:@"Round"] insertIntoManagedObjectContext:self.managedObjectContext];
+ newRound.name = [NSString stringWithFormat:@"New round %d", [roundsController nextCounter]];
+
[self saveContext];
[roundsController addRound:newRound];
[self viewRound:[newRound objectID]];
- [newRound release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
- // Override point for customization after application launch.
- NSLog(@"Inserting rounds from storage.");
[self.roundsController setRounds:[self fetchAllRounds]];
[self.window makeKeyAndVisible];
return YES;
@@ -142,8 +135,10 @@ - (void)awakeFromNib
if(!self.roundItemsController) {
self.roundItemsController = [[RoundItemsController alloc] init];
}
-
+
+ self.roundsController.delegate = self;
self.roundsController.managedObjectContext = self.managedObjectContext;
+
self.roundItemsController.managedObjectContext = self.managedObjectContext;
NSLog(@"awaking from NIB.");
}
View
7 TotallyTopSecret/iPhone/TotallyTopSecretAppDelegate_iPhone.m
@@ -17,11 +17,8 @@ @implementation TotallyTopSecretAppDelegate_iPhone
* Switches the main navController's view to one for the round and its items
*/
- (void)viewRound:(NSManagedObjectID *)roundID {
- NSLog(@"viewRound %@", roundID);
- if([self.roundItemsController setRoundFromID:roundID]) {
- [self.navController pushViewController:self.roundItemsController animated:YES];
- } else {
- }
+ [self.roundItemsController setRoundID:roundID];
+ [self.navController pushViewController:self.roundItemsController animated:YES];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Please sign in to comment.
Something went wrong with that request. Please try again.