project_files/HedgewarsMobile/Classes/CampaignsViewController.m
author antonc27 <antonc27@mail.ru>
Sun, 21 Feb 2016 23:12:12 +0100
changeset 11565 664d3592a66b
parent 11564 2b9e189df3d6
child 11567 32bbf1f6bb2b
permissions -rw-r--r--
- Campaign for iOS: Simply retrieve list of campaigns
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     1
/*
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     2
 * Hedgewars-iOS, a Hedgewars port for iOS devices
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     3
 * Copyright (c) 2015-2016 Anton Malmygin <antonc27@mail.ru>
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     4
 *
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     8
 *
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    12
 * GNU General Public License for more details.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    13
 *
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    17
 */
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    18
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    19
#import "CampaignsViewController.h"
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    20
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    21
@interface CampaignsViewController ()
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    22
@property (nonatomic, retain) NSArray *campaigns;
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    23
@end
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    24
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    25
@implementation CampaignsViewController
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    26
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    27
#pragma mark - Lazy instantiation
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    28
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    29
- (NSArray *)campaigns {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    30
    if (!_campaigns) {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    31
        _campaigns = [self listOfCampaigns];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    32
    }
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    33
    return _campaigns;
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    34
}
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    35
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    36
- (NSArray *)listOfCampaigns {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    37
    NSFileManager *fileManager = [NSFileManager defaultManager];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    38
    NSArray *contents = [fileManager contentsOfDirectoryAtPath:CAMPAIGNS_DIRECTORY() error:nil];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    39
    
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    40
    NSMutableArray *tempCampaigns = [[NSMutableArray alloc] init];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    41
    for (NSString *item in contents) {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    42
        NSString *fullItemPath = [CAMPAIGNS_DIRECTORY() stringByAppendingString:item];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    43
        BOOL isDirectory;
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    44
        if ([fileManager fileExistsAtPath:fullItemPath isDirectory:&isDirectory] && isDirectory) {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    45
            [tempCampaigns addObject:item];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    46
        }
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    47
    }
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    48
    
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    49
    NSArray *campaigns = [tempCampaigns copy];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    50
    [tempCampaigns release];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    51
    return campaigns;
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    52
}
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    53
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    54
#pragma mark - View lifecycle
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    55
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    56
- (void)viewDidLoad {
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    57
    [super viewDidLoad];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    58
    
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    59
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    60
    self.navigationItem.leftBarButtonItem = doneButton;
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    61
    [doneButton release];
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    62
    
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    63
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"campaignCell"];
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    64
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    65
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    66
- (void)dismiss {
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    67
    [self.navigationController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    68
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    69
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    70
- (void)didReceiveMemoryWarning {
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    71
    [super didReceiveMemoryWarning];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    72
    // Dispose of any resources that can be recreated.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    73
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    74
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    75
#pragma mark - Table view data source
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    76
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    77
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    78
    return 1;
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    79
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    80
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    81
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    82
    return [self.campaigns count];
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    83
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    84
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    85
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    86
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"campaignCell" forIndexPath:indexPath];
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    87
    
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
    88
    cell.textLabel.text = self.campaigns[indexPath.row];
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    89
    
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    90
    return cell;
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    91
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    92
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    93
/*
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    94
#pragma mark - Table view delegate
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    95
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    96
// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    97
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    98
    // Navigation logic may go here, for example:
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
    99
    // Create the next view controller.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   100
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   101
    
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   102
    // Pass the selected object to the new view controller.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   103
    
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   104
    // Push the view controller.
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   105
    [self.navigationController pushViewController:detailViewController animated:YES];
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   106
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   107
*/
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   108
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
   109
#pragma mark - Dealloc
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   110
11565
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
   111
- (void)dealloc {
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
   112
    [_campaigns release];
664d3592a66b - Campaign for iOS: Simply retrieve list of campaigns
antonc27 <antonc27@mail.ru>
parents: 11564
diff changeset
   113
    [super dealloc];
11564
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   114
}
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   115
2b9e189df3d6 - Campaign for iOS: Stub for list of campaigns
antonc27 <antonc27@mail.ru>
parents:
diff changeset
   116
@end