project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
branchios-revival
changeset 11257 62d1a66b353d
parent 11254 7f3f96e9fdf1
child 11306 7a9464f9e992
equal deleted inserted replaced
11256:3dc674af7662 11257:62d1a66b353d
    20 #import "SavedGamesViewController.h"
    20 #import "SavedGamesViewController.h"
    21 #import "GameInterfaceBridge.h"
    21 #import "GameInterfaceBridge.h"
    22 
    22 
    23 
    23 
    24 @implementation SavedGamesViewController
    24 @implementation SavedGamesViewController
    25 @synthesize tableView, listOfSavegames, numberOfItems;
    25 @synthesize tableView, listOfSavegames;
    26 
    26 
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    27 -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation {
    28     return rotationManager(interfaceOrientation);
    28     return rotationManager(interfaceOrientation);
    29 }
    29 }
    30 
    30 
    31 -(void) updateTable {
    31 -(void) updateTable {
    32     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL];
    32     NSArray *contentsOfDir = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:SAVES_DIRECTORY() error:NULL];
    33     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    33     NSMutableArray *array = [[NSMutableArray alloc] initWithArray:contentsOfDir copyItems:YES];
    34     self.listOfSavegames = array;
    34     self.listOfSavegames = array;
    35     [array release];
    35     [array release];
    36     self.numberOfItems = [self.listOfSavegames count];
       
    37 
    36 
    38     [self.tableView reloadData];
    37     [self.tableView reloadData];
    39 }
    38 }
    40 
    39 
    41 -(void) viewDidLoad {
    40 -(void) viewDidLoad {
    94         // remove all files and recreate the directory
    93         // remove all files and recreate the directory
    95         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    94         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    96         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    95         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    97 
    96 
    98         // update the table and the cached list
    97         // update the table and the cached list
    99         NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:self.numberOfItems];
    98         NSMutableArray *array = [[NSMutableArray alloc] init];
   100         for (int i = 0; i < self.numberOfItems; i++)
    99         for (int i = 0; i < [self.listOfSavegames count]; i++)
   101             [array addObject:[NSIndexPath indexPathForRow:i inSection:0]];
   100             [array addObject:[NSIndexPath indexPathForRow:i inSection:0]];
   102         self.numberOfItems = 0;
   101         [self.listOfSavegames removeAllObjects];
       
   102         
   103         [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
   103         [self.tableView deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationTop];
   104         [self.listOfSavegames removeAllObjects];
       
   105 
       
   106         [array release];
   104         [array release];
   107     }
   105     }
   108 }
   106 }
   109 
   107 
   110 #pragma mark -
   108 #pragma mark -
   112 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   110 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   113     return 1;
   111     return 1;
   114 }
   112 }
   115 
   113 
   116 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   114 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   117     return self.numberOfItems;
   115     return [self.listOfSavegames count];
   118 }
   116 }
   119 
   117 
   120 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   118 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   121     static NSString *CellIdentifier = @"Cell";
   119     static NSString *CellIdentifier = @"Cell";
   122 
   120 
   123     if (self.listOfSavegames == nil)
       
   124         [self updateTable];
       
   125     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   121     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   126     if (editableCell == nil) {
   122     if (editableCell == nil) {
   127         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   123         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   128         editableCell.delegate = self;
   124         editableCell.delegate = nil;
       
   125         editableCell.textField.userInteractionEnabled = NO;
   129     }
   126     }
   130     editableCell.tag = [indexPath row];
   127     editableCell.tag = [indexPath row];
   131     editableCell.respectEditing = YES;
       
   132     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   128     editableCell.textField.text = [[self.listOfSavegames objectAtIndex:[indexPath row]] stringByDeletingPathExtension];
   133     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   129     editableCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   134 
   130 
   135     return (UITableViewCell *)editableCell;
   131     return (UITableViewCell *)editableCell;
   136 }
   132 }
   156 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   152 -(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
   157     return 60;
   153     return 60;
   158 }
   154 }
   159 
   155 
   160 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   156 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   161     [((EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath]).textField resignFirstResponder];
   157     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
   162     
   158     [self fixTagsForStartTag:[indexPath row]];
   163     self.numberOfItems--;
       
   164     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
   165 
   159 
   166     NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]];
   160     NSString *saveName = [self.listOfSavegames objectAtIndex:[indexPath row]];
   167     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   161     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   168     [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
   162     [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
   169     [self.listOfSavegames removeObject:saveName];
   163     [self.listOfSavegames removeObject:saveName];
       
   164     
       
   165     [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
       
   166 }
       
   167 
       
   168 #pragma mark - Fix cells' tags
       
   169 
       
   170 - (void)fixTagsForStartTag:(NSInteger)tag
       
   171 {
       
   172     for (UITableViewCell *cell in self.tableView.visibleCells)
       
   173     {
       
   174         NSInteger oldTag = cell.tag;
       
   175         
       
   176         if (oldTag > tag)
       
   177         {
       
   178             cell.tag--;
       
   179         }
       
   180     }
   170 }
   181 }
   171 
   182 
   172 #pragma mark -
   183 #pragma mark -
   173 #pragma mark Table view delegate
   184 #pragma mark Table view delegate
   174 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   185 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   187     [self.listOfSavegames addObject:newSaveName];
   198     [self.listOfSavegames addObject:newSaveName];
   188     [newSaveName release];
   199     [newSaveName release];
   189     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
   200     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
   190     [newFilePath release];
   201     [newFilePath release];
   191 
   202 
   192     self.numberOfItems++;
       
   193     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
   203     [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
   194 
   204 
   195     [GameInterfaceBridge registerCallingController:self];
   205     [GameInterfaceBridge registerCallingController:self];
   196     [GameInterfaceBridge startSaveGame:currentFilePath];
   206     [GameInterfaceBridge startSaveGame:currentFilePath];
   197     [currentFilePath release];
   207     [currentFilePath release];