cocoaTouch/FortsViewController.m
changeset 3374 0d522416d97f
parent 3357 3836a31879e7
child 3490 016b3172b645
equal deleted inserted replaced
3373:c1ff724a5c34 3374:0d522416d97f
     9 #import "FortsViewController.h"
     9 #import "FortsViewController.h"
    10 #import "CommodityFunctions.h"
    10 #import "CommodityFunctions.h"
    11 #import "UIImageExtra.h"
    11 #import "UIImageExtra.h"
    12 
    12 
    13 @implementation FortsViewController
    13 @implementation FortsViewController
    14 @synthesize teamDictionary, fortArray, fortSprites, lastIndexPath;
    14 @synthesize teamDictionary, fortArray, lastIndexPath;
    15 
    15 
    16 
    16 
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    17 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    18 	return rotationManager(interfaceOrientation);
    18 	return rotationManager(interfaceOrientation);
    19 }
    19 }
    20 
    20 
    21 
    21 
    22 #pragma mark -
    22 #pragma mark -
    23 #pragma mark View lifecycle
    23 #pragma mark View lifecycle
    24 - (void)viewDidLoad {
    24 -(void) viewDidLoad {
    25     [super viewDidLoad];
    25     [super viewDidLoad];
    26 
    26 
    27     NSString *fortsDirectory = FORTS_DIRECTORY();
    27     NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FORTS_DIRECTORY() error:NULL];
    28     NSArray *directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: fortsDirectory error:NULL];
       
    29     NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
    28     NSMutableArray *filteredContents = [[NSMutableArray alloc] initWithCapacity:([directoryContents count] / 2)];
    30     // we need to remove the double entries and the L.png suffix
    29     // we need to remove the double entries and the L.png suffix
    31     for (int i = 0; i < [directoryContents count]; i++) {
    30     for (int i = 0; i < [directoryContents count]; i++) {
    32         if (i % 2) {
    31         if (i % 2) {
    33             NSString *currentName = [directoryContents objectAtIndex:i];
    32             NSString *currentName = [directoryContents objectAtIndex:i];
    34             NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
    33             NSString *correctName = [currentName substringToIndex:([currentName length] - 5)];
    35             [filteredContents addObject:correctName];
    34             [filteredContents addObject:correctName];
    36         } 
    35         } 
    37     }
    36     }
    38     
       
    39     self.fortArray = filteredContents;
    37     self.fortArray = filteredContents;
    40     [filteredContents release];
    38     [filteredContents release];
    41     
    39     
    42     //NSLog(@"%@",fortArray);
    40     /*
    43     
       
    44     // this creates a scaled down version of the image
    41     // this creates a scaled down version of the image
    45     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
    42     NSMutableArray *spriteArray = [[NSMutableArray alloc] initWithCapacity:[fortArray count]];
    46     for (NSString *fortName in fortArray) {
    43     for (NSString *fortName in fortArray) {
    47         NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
    44         NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", fortsDirectory, fortName];
    48         UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
    45         UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
    50         [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
    47         [spriteArray addObject:[fortSprite scaleToSize:CGSizeMake(196,196)]];
    51         [fortSprite release];
    48         [fortSprite release];
    52     }
    49     }
    53     self.fortSprites = spriteArray;
    50     self.fortSprites = spriteArray;
    54     [spriteArray release];
    51     [spriteArray release];
       
    52     */
    55     
    53     
    56     // statically set row height instead of using delegate method for performance reasons
    54     // statically set row height instead of using delegate method for performance reasons
    57     self.tableView.rowHeight = 200;
    55     self.tableView.rowHeight = 200;
    58 }
    56 }
    59 
    57 
    62     [super viewWillAppear:animated];
    60     [super viewWillAppear:animated];
    63     [self.tableView reloadData];
    61     [self.tableView reloadData];
    64     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    62     [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
    65 }
    63 }
    66 
    64 
    67 /*
       
    68 - (void)viewDidAppear:(BOOL)animated {
       
    69     [super viewDidAppear:animated];
       
    70 }
       
    71 */
       
    72 /*
       
    73 - (void)viewWillDisappear:(BOOL)animated {
       
    74     [super viewWillDisappear:animated];
       
    75 }
       
    76 */
       
    77 /*
       
    78 - (void)viewDidDisappear:(BOOL)animated {
       
    79     [super viewDidDisappear:animated];
       
    80 }
       
    81 */
       
    82 
       
    83 
    65 
    84 #pragma mark -
    66 #pragma mark -
    85 #pragma mark Table view data source
    67 #pragma mark Table view data source
    86 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    68 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    87     return 1;
    69     return 1;
    88 }
    70 }
    89 
    71 
    90 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    72 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    91     return [fortArray count];
    73     return [self.fortArray count];
    92 }
    74 }
    93 
    75 
    94 // Customize the appearance of table view cells.
    76 // Customize the appearance of table view cells.
    95 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    77 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    96     
       
    97     static NSString *CellIdentifier = @"Cell";
    78     static NSString *CellIdentifier = @"Cell";
    98     
    79     
    99     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    80     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   100     if (cell == nil) {
    81     if (cell == nil) {
   101         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
    82         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
   102                                        reuseIdentifier:CellIdentifier] autorelease];
    83                                        reuseIdentifier:CellIdentifier] autorelease];
   103     }
    84     }
   104     
    85     
   105     cell.imageView.image = [fortSprites objectAtIndex:[indexPath row]];
    86     NSString *fortName = [fortArray objectAtIndex:[indexPath row]];
   106     cell.textLabel.text = [fortArray objectAtIndex:[indexPath row]];
    87     cell.textLabel.text = fortName;
       
    88     
       
    89     // this creates a scaled down version of the image
       
    90     // TODO: create preview files, scaling is way too slow!
       
    91     NSString *fortFile = [[NSString alloc] initWithFormat:@"%@/%@L.png", FORTS_DIRECTORY(), fortName];
       
    92     UIImage *fortSprite = [[UIImage alloc] initWithContentsOfFile:fortFile];
       
    93     [fortFile release];
       
    94     cell.imageView.image = [fortSprite scaleToSize:CGSizeMake(196,196)];
       
    95     [fortSprite release];
       
    96     
   107     cell.detailTextLabel.text = @"Insert funny description here";
    97     cell.detailTextLabel.text = @"Insert funny description here";
   108     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
    98     if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"fort"]]) {
   109         cell.accessoryType = UITableViewCellAccessoryCheckmark;
    99         cell.accessoryType = UITableViewCellAccessoryCheckmark;
   110         self.lastIndexPath = indexPath;
   100         self.lastIndexPath = indexPath;
   111     } else {
   101     } else {
   114     
   104     
   115     return cell;
   105     return cell;
   116 }
   106 }
   117 
   107 
   118 
   108 
   119 /*
       
   120 // Override to support conditional editing of the table view.
       
   121 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
       
   122     // Return NO if you do not want the specified item to be editable.
       
   123     return YES;
       
   124 }
       
   125 */
       
   126 
       
   127 
       
   128 /*
       
   129 // Override to support editing the table view.
       
   130 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
       
   131     
       
   132     if (editingStyle == UITableViewCellEditingStyleDelete) {
       
   133         // Delete the row from the data source
       
   134         [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
       
   135     }   
       
   136     else if (editingStyle == UITableViewCellEditingStyleInsert) {
       
   137         // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
       
   138     }   
       
   139 }
       
   140 */
       
   141 
       
   142 
       
   143 /*
       
   144 // Override to support rearranging the table view.
       
   145 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
       
   146 }
       
   147 */
       
   148 
       
   149 
       
   150 /*
       
   151 // Override to support conditional rearranging of the table view.
       
   152 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
       
   153     // Return NO if you do not want the item to be re-orderable.
       
   154     return YES;
       
   155 }
       
   156 */
       
   157 
       
   158 
       
   159 #pragma mark -
   109 #pragma mark -
   160 #pragma mark Table view delegate
   110 #pragma mark Table view delegate
   161 -(void) tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   111 -(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   162     int newRow = [indexPath row];
   112     int newRow = [indexPath row];
   163     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   113     int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
   164     
   114     
   165     if (newRow != oldRow) {
   115     if (newRow != oldRow) {
   166         // if the two selected rows differ update data on the hog dictionary and reload table content
   116         // if the two selected rows differ update data on the hog dictionary and reload table content
   167         [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
   117         [self.teamDictionary setValue:[fortArray objectAtIndex:newRow] forKey:@"fort"];
   168 
   118 
   169         // tell our boss to write this new stuff on disk
   119         // tell our boss to write this new stuff on disk
   170         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   120         [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
   171         [self.tableView reloadData];
   121         
   172 
   122         UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
       
   123         newCell.accessoryType = UITableViewCellAccessoryCheckmark;
       
   124         UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
       
   125         oldCell.accessoryType = UITableViewCellAccessoryNone;
   173         self.lastIndexPath = indexPath;
   126         self.lastIndexPath = indexPath;
   174         [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   127         [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
   175     }
   128     }
   176     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   129     [aTableView deselectRowAtIndexPath:indexPath animated:YES];
   177     [self.navigationController popViewControllerAnimated:YES];
   130     [self.navigationController popViewControllerAnimated:YES];
   178 }
   131 }
   179 
   132 
   180 
   133 
   181 #pragma mark -
   134 #pragma mark -
   188 
   141 
   189 -(void) viewDidUnload {
   142 -(void) viewDidUnload {
   190     self.teamDictionary = nil;
   143     self.teamDictionary = nil;
   191     self.lastIndexPath = nil;
   144     self.lastIndexPath = nil;
   192     self.fortArray = nil;
   145     self.fortArray = nil;
   193     self.fortSprites = nil;
   146 //    self.fortSprites = nil;
   194     [super viewDidUnload];
   147     [super viewDidUnload];
   195 }
   148 }
   196 
   149 
   197 
   150 
   198 - (void)dealloc {
   151 - (void)dealloc {
   199     [teamDictionary release];
   152     [teamDictionary release];
   200     [lastIndexPath release];
   153     [lastIndexPath release];
   201     [fortArray release];
   154     [fortArray release];
   202     [fortSprites release];
   155 //    [fortSprites release];
   203     [super dealloc];
   156     [super dealloc];
   204 }
   157 }
   205 
   158 
   206 
   159 
   207 @end
   160 @end