#import "AnimalViewController.h"
#import "AnimalHouse.h"
#import "AnimalDetail.h"
#import "AnimalDataController.h"
static sqlite3_stmt *selectAnimal_statement = nil;
@implementation AnimalViewController
@synthesize aAnimalHouse, listOfAnimals;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
//If you need to do additional setup after loading the view, override viewDidLoad.
- (void)viewDidLoad {
}
- (void)viewWillAppear:(BOOL)animated {
self.title = aAnimalHouse.animalHouseName;
[tableView reloadData];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
-(NSInteger)tableView:(UITableView *)tv numberOfRowsInSection:(NSInteger)section {
return self.listOfAnimals.count;
}
-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
// Create a new cell. CGRectZero allows the cell to determine the appropriate size.
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIndentifier"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
//Get the object and write out the animal name.
AnimalDetail *cd = [self.listOfAnimals objectAtIndex:indexPath.row];
cell.textLabel.text = cd.animalName; cell.textLabel.numberOfLines = 3;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if(cdController == nil)
cdController = [[DataController alloc] initWithNibName:@"AnimalData" bundle:nil];
cdController.animalDetail = [self.listOfAnimals objectAtIndex:indexPath.row];
[self.navigationController pushViewController:cdController animated:YES];
}
-(void)setAAnimalHouse:(AnimalHouse *)ch {
if(aAnimalHouse == nil)
aAnimalHouse = [[NSMutableArray alloc] init];
aAnimalHouse = ch;
[self getListOfAllAnimalsForThisAnimalHouse:aAnimalHouse.animalHouseID];
}
- (void)getListOfAllAnimalsForThisAnimalHouse:(NSInteger)animalHouseID {
//get the data from the database and fill the array.
self.listOfAnimals = [[NSMutableArray alloc] init];
sqlite3 *database;
//Get the database and connect to it.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//#1 Change
//NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"SQLNavigation.sql"];
NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:@"AnimalInfo.sqlite"];
//open the database
if(sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK)
{
const char *sql = "select id, animal, continent, type, typesoffered, reqs, scores, sizein, sizeout, totalin, totalout, website, comments from animals where continentid=?";
int returnValue;
if(selectAnimal_statement == nil)
returnValue = sqlite3_prepare_v2(database, sql, -1, &selectAnimal_statement, NULL);
sqlite3_bind_int(selectAnimal_statement, 1, animalHouseID);
while(sqlite3_step(selectAnimal_statement) == SQLITE_ROW)
{
//populate the object with the data.
AnimalDetail *cd = [[AnimalDetail alloc] init];
cd.animalDetailID = sqlite3_column_int(selectAnimal_statement, 0);
cd.animalName = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 1)] copy];
//Added objects to facilitate additional data...
cd.continent = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 2)] copy];
cd.type = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 3)] copy];
cd.typesoffered = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 4)] copy];
cd.reqs = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 5)] copy];
cd.scores = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 6)] copy];
cd.sizein = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 7)] copy];
cd.sizeout = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 8)] copy];
cd.totalin = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 9)] copy];
cd.totalout = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 10)] copy];
cd.website = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 11)] copy];
cd.comments = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectAnimal_statement, 12)] copy];
//Add the object to the array.
[self.listOfAnimals addObject:cd];
}
//Reset the statement, so it can be used again.
sqlite3_reset(selectAnimal_statement);
}
//Close the connection.
else
sqlite3_close(database);
}
- (void)dealloc {
}
@end