Skip to content

Instantly share code, notes, and snippets.

@evolve2k
Created January 29, 2011 14:43
Show Gist options
  • Select an option

  • Save evolve2k/801879 to your computer and use it in GitHub Desktop.

Select an option

Save evolve2k/801879 to your computer and use it in GitHub Desktop.
LineupViewController.m
//
// LineupViewController.m
// FootyAssistant
//
//
#import "LineupViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "AppScrollView.h"
#import "DataController.h"
@implementation LineupViewController
@synthesize fetchedResultsController=fetchedResultsController_, managedObjectContext=managedObjectContext_;
@synthesize game, team, player;
@synthesize pickerView;
@synthesize scrollView;
@synthesize objects;
@synthesize pickerArray, aString;
@synthesize positions, positionViews;
@synthesize position1,position2,position3;
@synthesize position4,position5,position6;
@synthesize position7,position8,position9;
@synthesize position10,position11,position12;
@synthesize position13,position14,position15;
@synthesize position16,position17,position18;
@synthesize selectedPosition, selectedPositionView;
- (void)vibrate {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
}
- (void)viewDidLoad {
// Format page
scrollView.contentSize = CGSizeMake(200, 430);
// Initialise PositionsView array
positionViews = [[NSArray alloc] initWithObjects:position1,position2,position3,position4,position5,position6,position7,position8,position9,position10,position11,position12,position13,position14,position15,position16,position17,position18,nil];
// Load managedObject
//Fetch All players from PlayerPosition where game matches this game and store in objects
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"PlayerPosition" inManagedObjectContext:managedObjectContext_]; // Select ALL records from PlayerPosition
[request setEntity:entityDescription];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(game = %@)", self.game]; //WHERE game = self.game
[request setPredicate:pred];
NSError *error;
objects = [managedObjectContext_ executeFetchRequest:request error:&error]; //execute fetch and store in objects
if (objects == nil) {
NSLog(@"Error! Objects came back as empty, seems the fetch didn't work");
}
NSLog(@"Objects: %@", [objects description]);
// Populate View with data from the managedObject
// go through each stored playerPosition (pp)
// find out the fieldName from the pp.position
// set fieldName to pp.player.shortname
NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>> Populate View with data from the managedObject.. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
NSNumber *i = 1;
for (NSManagedObject *object in objects) {
//loop through the objects in PlayerPosition for this game and update UI elements.
Player *objectPlayer = [object valueForKey:@"player"];
NSLog(@"object Player: %@", [objectPlayer description]);
if ([object position] == nil) {
NSLog(@"ITS NULL FOOL: %@", [object position]);
} else {
NSLog(@"Hmm.. NOT NULL: %@", [object position]);
}
NSLog(@"=======================================");
NSLog(@"Is of type: %@", [[object position] class]);
NSLog(@"Is of type NSString?: %@", ([[[object position] class] isMemberOfClass:[NSString class]])? @"Yes" : @"No");
NSLog(@"Is a kind of NSString: %@", ([[[object position] classForCoder] isSubclassOfClass:[NSString class]])? @"Yes" : @"No");
NSNumber *objectPosition = [[object position] integerValue];
i++;
NSLog(@"Object: %@", [object valueForKey:@"position"]);
NSString *fieldName = [NSString stringWithFormat:@"position%d", [objectPosition intValue]];
PlayerPositionView *theField = [self valueForKey:fieldName];
[[theField textLabel] setText:objectPlayer.playerShortName];
NSLog(@"object Position: %@", [objectPosition description]);
}
NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>> ..view Data Loaded <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
[super viewDidLoad];
}
#pragma mark -
#pragma mark User selects a position
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
//NSLog(@"touchesEnded > Objects: %@", [objects description]);
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] isKindOfClass:[PlayerPositionView class]]) { //Checks if player clicked a position box
//Reset color for all positions
//Divide RGB color by 256 as iphone uses scale from 0.0 to 1.0
//http://stackoverflow.com/questions/1628567/uicolor-limitations-or-bugs-on-iphone-3-0
//Use the percentages in bottom left of http://colorschemedesigner.com/
for (int i = 0; i < positionViews.count ; i++) {
PlayerPositionView *playerPositionView = [positionViews objectAtIndex:i];
if (playerPositionView.textLabel.text.length == 0) {
[playerPositionView setBackgroundColor:[UIColor colorWithRed:0.64 green:0.88 blue:0.36 alpha:1.0]]; //default box green
} else {
[selectedPositionView setBackgroundColor:[UIColor colorWithRed:0.41 green:0.75 blue:0.00 alpha:1.0]]; //Oval Green
}
}
//Highlight the selected position
[[touch view] setBackgroundColor:[UIColor colorWithRed:0.94 green:0.93 blue:0.38 alpha:1.0]]; // Orange
//Set selectedPositionView to the position selected
selectedPositionView = [positionViews objectAtIndex:(touch.view.tag - 1)]; //Need to minus one as we are looking at an Array.
selectedPosition = [NSNumber numberWithInt:selectedPositionView.tag]; //Tag matches position no need to minus one.
}
}
#pragma mark -
#pragma mark Picker View
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerArray count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
player = [pickerArray objectAtIndex:row];
return [NSString stringWithFormat:@"%@ (%@)",[player playerName],[player playerShortName]];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//store selectedRow as player
player = [pickerArray objectAtIndex:row];
//Update visual display
[[selectedPositionView textLabel] setText:[player playerShortName]];
[self vibrate]; //make me vibrate "BZZZZZZZ"
[selectedPositionView setBackgroundColor:[UIColor colorWithRed:0.41 green:0.75 blue:0.00 alpha:1.0]]; //Oval Green
// From the Player Positions Array select the selectedPosition item otherwise make a new position
//Redefine object just cause its failing like a whale
// Shouldn't need to rediscover it at this stage..
// Load managedObject
//Fetch All players from PlayerPosition where game matches this game and store in objects
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"PlayerPosition" inManagedObjectContext:managedObjectContext_]; // Select ALL records from PlayerPosition
[request setEntity:entityDescription];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(game = %@)", self.game]; //WHERE game = self.game
[request setPredicate:pred];
NSError *error;
objects = [managedObjectContext_ executeFetchRequest:request error:&error]; //execute fetch and store in objects
if (objects == nil) {
NSLog(@"Error! Objects came back as empty, seems the fetch didn't work");
}
NSLog(@"Objects: %@", [objects description]);
///////////////
NSLog(@">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UPDATING managedObject <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
BOOL createNewRecord = YES;
for (NSManagedObject *object in objects) {
if ([object valueForKey:@"position"] == selectedPosition) {
NSLog(@">>> SelectedPosition: %@", selectedPosition);
NSLog(@"Object: %@", object);
NSManagedObject *selectedObject = object;
//Set values within the record
[selectedObject setValue:player forKey:@"player"];
createNewRecord = NO;
} else {
}
}
if (createNewRecord == YES) {
NSLog(@">>> Creating a new Record, that position seemed empty to me");
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:@"PlayerPosition" inManagedObjectContext:managedObjectContext_];
//Set values within the record
[newManagedObject setValue:self.game forKey:@"game"];
[newManagedObject setValue:selectedPosition forKey:@"position"];
[newManagedObject setValue:player forKey:@"player"];
}
////////////////////////////////////////////////////////////////////////////////////
//Save the record
////////////////////////////////////////////////////////////////////////////////////
//set the context to the main context as initialised in DataController
if (![managedObjectContext_ save:&error]) {
// Update to handle the error appropriately.
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0) {
for(NSError* detailedError in detailedErrors) {
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else {
NSLog(@" %@", [error userInfo]);
}
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
exit(-1); // Fail
}
////////////////////////////////////////////////////////////////////////////////////
}
#pragma mark -
#pragma mark Drag and Drop
- (bool) canCancelContentTouches {return NO;}
#pragma mark -
#pragma mark Memory Management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
}
- (void)dealloc {
[pickerView release];
[scrollView release];
[selectedPosition release];
[selectedPositionView release];
[positions release];
[positionViews release];
[position1 release];
[position2 release];
[position3 release];
[position4 release];
[position5 release];
[position6 release];
[position7 release];
[position8 release];
[position9 release];
[position10 release];
[position11 release];
[position12 release];
[position13 release];
[position14 release];
[position15 release];
[position16 release];
[position17 release];
[position18 release];
[pickerArray release];
[objects release];
[aString release];
[game release];
[team release];
[player release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment