project_files/HedgewarsMobile/Classes/SavedGamesViewController.m
changeset 3971 5c82ee165ed5
parent 3923 694e6f6e0e30
child 3978 9660600e43cb
equal deleted inserted replaced
3970:0f6e3219c108 3971:5c82ee165ed5
    62 }
    62 }
    63 
    63 
    64 -(IBAction) toggleEdit:(id) sender {
    64 -(IBAction) toggleEdit:(id) sender {
    65     BOOL isEditing = self.tableView.editing;
    65     BOOL isEditing = self.tableView.editing;
    66     [self.tableView setEditing:!isEditing animated:YES];
    66     [self.tableView setEditing:!isEditing animated:YES];
    67     UIToolbar *toolbar = (UIToolbar *)[self.view viewWithTag:458912];
    67 }
    68     for (UIBarButtonItem *item in toolbar.items)
    68 
    69         if (item.tag == 452198)
    69 -(void) duplicateEntry:(id) sender {
    70             item.enabled = !isEditing;
    70     UIButton *button = (UIButton *)sender;
       
    71     NSUInteger row = button.tag;
       
    72     
       
    73     if (self.listOfSavegames == nil)
       
    74         [self updateTable];
       
    75     
       
    76     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
       
    77     NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row];
       
    78     NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]];
       
    79     
       
    80     NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName];
       
    81     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName];
       
    82     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
       
    83     [self.listOfSavegames addObject:newSaveName];
       
    84     [self.listOfSavegames sortUsingSelector:@selector(compare:)];
       
    85 
       
    86     //[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.listOfSavegames indexOfObject:newSaveName] inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
       
    87     [self.tableView reloadData];
    71 }
    88 }
    72 
    89 
    73 -(IBAction) clearAll:(id) sender {
    90 -(IBAction) clearAll:(id) sender {
    74     [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
    91     NSString *titleStr, *cancelStr, *confirmStr;
    75     [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
    92     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    76     [self updateTable];
    93         titleStr = nil;
       
    94         cancelStr = nil;
       
    95         confirmStr = NSLocalizedString(@"Tap to confirm",@"");
       
    96     } else {
       
    97         titleStr = NSLocalizedString(@"Are you reeeeeally sure?", @"");
       
    98         cancelStr = NSLocalizedString(@"Well, maybe not...", @"");
       
    99         confirmStr = NSLocalizedString(@"Of course!", @"");
       
   100     }
       
   101 
       
   102     UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleStr
       
   103                                                              delegate:self
       
   104                                                     cancelButtonTitle:cancelStr
       
   105                                                destructiveButtonTitle:confirmStr
       
   106                                                     otherButtonTitles:nil];
       
   107     [actionSheet showInView:self.view];
       
   108     [actionSheet release];
       
   109 }
       
   110 
       
   111 -(void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger) buttonIndex {
       
   112     if ([actionSheet cancelButtonIndex] != buttonIndex) {
       
   113         [[NSFileManager defaultManager] removeItemAtPath:SAVES_DIRECTORY() error:NULL];
       
   114         [[NSFileManager defaultManager] createDirectoryAtPath:SAVES_DIRECTORY() withIntermediateDirectories:NO attributes:nil error:NULL];
       
   115         [self updateTable];
       
   116     }
    77 }
   117 }
    78 
   118 
    79 #pragma mark -
   119 #pragma mark -
    80 #pragma mark Table view data source
   120 #pragma mark Table view data source
    81 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
   121 -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
    82     return 1;
   122     return 1;
    83 }
   123 }
    84 
   124 
    85 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   125 -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
       
   126     if (self.listOfSavegames == nil)
       
   127         [self updateTable];
    86     return [self.listOfSavegames count];
   128     return [self.listOfSavegames count];
    87 }
   129 }
    88 
   130 
    89 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   131 -(UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    90     static NSString *CellIdentifier = @"Cell";
   132     static NSString *CellIdentifier = @"Cell";
    91 
   133 
       
   134     if (self.listOfSavegames == nil)
       
   135         [self updateTable];
    92     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
   136     EditableCellView *editableCell = (EditableCellView *)[aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
    93     if (editableCell == nil) {
   137     if (editableCell == nil) {
    94         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
   138         editableCell = [[[EditableCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    95         editableCell.delegate = self;
   139         editableCell.delegate = self;
    96     }
   140     }
   129 
   173 
   130 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   174 -(void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
   131     NSUInteger row = [indexPath row];
   175     NSUInteger row = [indexPath row];
   132     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
   176     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
   133     
   177     
       
   178     if (self.listOfSavegames == nil)
       
   179         [self updateTable];
       
   180     
   134     NSString *saveName = [self.listOfSavegames objectAtIndex:row];
   181     NSString *saveName = [self.listOfSavegames objectAtIndex:row];
   135     NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   182     NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),saveName];
   136     [[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil];
   183     [[NSFileManager defaultManager] removeItemAtPath:currentFilePath error:nil];
   137     [self.listOfSavegames removeObject:saveName];
   184     [self.listOfSavegames removeObject:saveName];
   138     
   185     
   139     [self.tableView reloadData];
   186     [self.tableView reloadData];
   140 }
   187 }
   141 
   188 
   142 -(void) duplicateEntry:(id) sender {
       
   143     UIButton *button = (UIButton *)sender;
       
   144     NSUInteger row = button.tag;
       
   145     
       
   146     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0]] save:nil];
       
   147     NSString *currentSaveName = [self.listOfSavegames objectAtIndex:row];
       
   148     NSString *newSaveName = [[currentSaveName stringByDeletingPathExtension] stringByAppendingFormat:@" %d.hws",[self.listOfSavegames count]];
       
   149     
       
   150     NSString *currentFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),currentSaveName];
       
   151     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),newSaveName];
       
   152     [[NSFileManager defaultManager] copyItemAtPath:currentFilePath toPath:newFilePath error:nil];
       
   153     [self.listOfSavegames addObject:newSaveName];
       
   154     [self.listOfSavegames sortUsingSelector:@selector(compare:)];
       
   155 
       
   156     //[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[self.listOfSavegames indexOfObject:newSaveName] inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
       
   157     [self.tableView reloadData];
       
   158 }
       
   159 
       
   160 #pragma mark -
   189 #pragma mark -
   161 #pragma mark Table view delegate
   190 #pragma mark Table view delegate
   162 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   191 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   163     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   192     [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
   164     
   193     if (self.listOfSavegames == nil)
       
   194         [self updateTable];
       
   195 
   165     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
   196     [(EditableCellView *)[self.tableView cellForRowAtIndexPath:indexPath] save:nil];
   166     
   197     
   167     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
   198     NSString *filePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:[indexPath row]]];
   168     
   199     
   169     NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
   200     NSDictionary *allDataNecessary = [NSDictionary dictionaryWithObjectsAndKeys:
   176 
   207 
   177 #pragma mark -
   208 #pragma mark -
   178 #pragma mark editableCellView delegate
   209 #pragma mark editableCellView delegate
   179 // rename old file if names differ
   210 // rename old file if names differ
   180 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
   211 -(void) saveTextFieldValue:(NSString *)textString withTag:(NSInteger) tagValue {
       
   212     if (self.listOfSavegames == nil)
       
   213         [self updateTable];
   181     NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]];
   214     NSString *oldFilePath = [NSString stringWithFormat:@"%@/%@",SAVES_DIRECTORY(),[self.listOfSavegames objectAtIndex:tagValue]];
   182     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString];
   215     NSString *newFilePath = [NSString stringWithFormat:@"%@/%@.hws",SAVES_DIRECTORY(),textString];
   183     
   216     
   184     if ([oldFilePath isEqualToString:newFilePath] == NO) {
   217     if ([oldFilePath isEqualToString:newFilePath] == NO) {
   185         [[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil];
   218         [[NSFileManager defaultManager] moveItemAtPath:oldFilePath toPath:newFilePath error:nil];
   189 }
   222 }
   190 
   223 
   191 #pragma mark -
   224 #pragma mark -
   192 #pragma mark Memory Management
   225 #pragma mark Memory Management
   193 -(void) didReceiveMemoryWarning {
   226 -(void) didReceiveMemoryWarning {
       
   227     self.listOfSavegames = nil;
       
   228     MSG_MEMCLEAN();
   194     [super didReceiveMemoryWarning];
   229     [super didReceiveMemoryWarning];
   195 }
   230 }
   196 
   231 
   197 -(void) viewDidUnload {
   232 -(void) viewDidUnload {
   198     self.tableView = nil;
   233     self.tableView = nil;
   199     self.listOfSavegames = nil;
   234     self.listOfSavegames = nil;
       
   235     MSG_DIDUNLOAD();
   200     [super viewDidUnload];
   236     [super viewDidUnload];
   201 }
   237 }
   202 
   238 
   203 -(void) dealloc {
   239 -(void) dealloc {
   204     [tableView release];
   240     [tableView release];