project_files/HedgewarsMobile/Classes/FlagsViewController.m
author koda
Wed, 23 Jun 2010 22:03:56 +0200
changeset 3547 02875b1145b7
parent 3546 ccf4854df294
child 3662 a44406f4369b
permissions -rw-r--r--
i <3 mercurial
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3547
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     1
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     2
//  FlagsViewController.m
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     3
//  HedgewarsMobile
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     4
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     5
//  Created by Vittorio on 08/04/10.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     6
//  Copyright 2010 __MyCompanyName__. All rights reserved.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     7
//
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     8
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
     9
#import "FlagsViewController.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    10
#import "CommodityFunctions.h"
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    11
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    12
@implementation FlagsViewController
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    13
@synthesize teamDictionary, flagArray, lastIndexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    14
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    15
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    16
-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    17
    return rotationManager(interfaceOrientation);
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    18
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    19
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    20
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    21
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    22
#pragma mark View lifecycle
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    23
-(void) viewDidLoad {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    24
    [super viewDidLoad];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    25
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    26
    self.flagArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FLAGS_DIRECTORY() error:NULL];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    27
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    28
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    29
-(void) viewWillAppear:(BOOL)animated {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    30
    [super viewWillAppear:animated];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    31
    [self.tableView reloadData];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    32
    [self.tableView setContentOffset:CGPointMake(0,0) animated:NO];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    33
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    34
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    35
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    36
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    37
#pragma mark Table view data source
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    38
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    39
    return 1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    40
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    41
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    42
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    43
    return [flagArray count];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    44
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    45
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    46
// Customize the appearance of table view cells.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    47
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    48
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    49
    static NSString *CellIdentifier = @"Cell";
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    50
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    51
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    52
    if (cell == nil) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    53
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    54
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    55
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    56
    NSString *flag = [flagArray objectAtIndex:[indexPath row]];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    57
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    58
    NSString *flagFile = [[NSString alloc] initWithFormat:@"%@/%@", FLAGS_DIRECTORY(), flag];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    59
    UIImage *flagSprite = [[UIImage alloc] initWithContentsOfFile:flagFile];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    60
    [flagFile release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    61
    cell.imageView.image = flagSprite;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    62
    [flagSprite release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    63
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    64
    cell.textLabel.text = [flag stringByDeletingPathExtension];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    65
    if ([cell.textLabel.text isEqualToString:[self.teamDictionary objectForKey:@"flag"]]) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    66
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    67
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    68
    } else {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    69
        cell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    70
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    71
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    72
    return cell;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    73
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    74
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    75
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    76
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    77
#pragma mark Table view delegate
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    78
-(void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    79
    int newRow = [indexPath row];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    80
    int oldRow = (lastIndexPath != nil) ? [lastIndexPath row] : -1;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    81
    
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    82
    if (newRow != oldRow) {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    83
        // if the two selected rows differ update data on the hog dictionary and reload table content
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    84
        [self.teamDictionary setValue:[[flagArray objectAtIndex:newRow] stringByDeletingPathExtension] forKey:@"flag"];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    85
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    86
        // tell our boss to write this new stuff on disk
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    87
        [[NSNotificationCenter defaultCenter] postNotificationName:@"setWriteNeedTeams" object:nil];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    88
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    89
        UITableViewCell *newCell = [aTableView cellForRowAtIndexPath:indexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    90
        newCell.accessoryType = UITableViewCellAccessoryCheckmark;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    91
        UITableViewCell *oldCell = [aTableView cellForRowAtIndexPath:lastIndexPath];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    92
        oldCell.accessoryType = UITableViewCellAccessoryNone;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    93
        self.lastIndexPath = indexPath;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    94
        [aTableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    95
    }
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    96
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    97
    [self.navigationController popViewControllerAnimated:YES];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    98
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
    99
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   100
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   101
#pragma mark -
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   102
#pragma mark Memory management
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   103
-(void) didReceiveMemoryWarning {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   104
    // Releases the view if it doesn't have a superview.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   105
    [super didReceiveMemoryWarning];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   106
    // Relinquish ownership any cached data, images, etc that aren't in use.
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   107
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   108
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   109
-(void) viewDidUnload {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   110
    self.teamDictionary = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   111
    self.lastIndexPath = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   112
    self.flagArray = nil;
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   113
    [super viewDidUnload];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   114
    MSG_DIDUNLOAD();
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   115
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   116
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   117
-(void) dealloc {
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   118
    [teamDictionary release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   119
    [lastIndexPath release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   120
    [flagArray release];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   121
    [super dealloc];
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   122
}
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   123
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   124
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   125
@end
02875b1145b7 i <3 mercurial
koda
parents: 3546
diff changeset
   126