- Campaign for iOS: Simply retrieve list of campaigns
authorantonc27 <antonc27@mail.ru>
Sun, 21 Feb 2016 23:12:12 +0100
changeset 11560 664d3592a66b
parent 11559 2b9e189df3d6
child 11561 254333210cc8
- Campaign for iOS: Simply retrieve list of campaigns
project_files/HedgewarsMobile/Classes/CampaignsViewController.m
project_files/HedgewarsMobile/Classes/DefinesAndMacros.h
--- a/project_files/HedgewarsMobile/Classes/CampaignsViewController.m	Sun Feb 21 22:21:59 2016 +0100
+++ b/project_files/HedgewarsMobile/Classes/CampaignsViewController.m	Sun Feb 21 23:12:12 2016 +0100
@@ -19,17 +19,48 @@
 #import "CampaignsViewController.h"
 
 @interface CampaignsViewController ()
-
+@property (nonatomic, retain) NSArray *campaigns;
 @end
 
 @implementation CampaignsViewController
 
+#pragma mark - Lazy instantiation
+
+- (NSArray *)campaigns {
+    if (!_campaigns) {
+        _campaigns = [self listOfCampaigns];
+    }
+    return _campaigns;
+}
+
+- (NSArray *)listOfCampaigns {
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSArray *contents = [fileManager contentsOfDirectoryAtPath:CAMPAIGNS_DIRECTORY() error:nil];
+    
+    NSMutableArray *tempCampaigns = [[NSMutableArray alloc] init];
+    for (NSString *item in contents) {
+        NSString *fullItemPath = [CAMPAIGNS_DIRECTORY() stringByAppendingString:item];
+        BOOL isDirectory;
+        if ([fileManager fileExistsAtPath:fullItemPath isDirectory:&isDirectory] && isDirectory) {
+            [tempCampaigns addObject:item];
+        }
+    }
+    
+    NSArray *campaigns = [tempCampaigns copy];
+    [tempCampaigns release];
+    return campaigns;
+}
+
+#pragma mark - View lifecycle
+
 - (void)viewDidLoad {
     [super viewDidLoad];
     
     UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismiss)];
     self.navigationItem.leftBarButtonItem = doneButton;
     [doneButton release];
+    
+    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"campaignCell"];
 }
 
 - (void)dismiss {
@@ -44,58 +75,20 @@
 #pragma mark - Table view data source
 
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-#warning Incomplete implementation, return the number of sections
-    return 0;
+    return 1;
 }
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-#warning Incomplete implementation, return the number of rows
-    return 0;
+    return [self.campaigns count];
 }
 
-/*
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
-    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
+    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"campaignCell" forIndexPath:indexPath];
     
-    // Configure the cell...
+    cell.textLabel.text = self.campaigns[indexPath.row];
     
     return cell;
 }
-*/
-
-/*
-// Override to support conditional editing of the table view.
-- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
-    // Return NO if you do not want the specified item to be editable.
-    return YES;
-}
-*/
-
-/*
-// Override to support editing the table view.
-- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
-    if (editingStyle == UITableViewCellEditingStyleDelete) {
-        // Delete the row from the data source
-        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
-    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
-        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
-    }   
-}
-*/
-
-/*
-// Override to support rearranging the table view.
-- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
-}
-*/
-
-/*
-// Override to support conditional rearranging of the table view.
-- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
-    // Return NO if you do not want the item to be re-orderable.
-    return YES;
-}
-*/
 
 /*
 #pragma mark - Table view delegate
@@ -113,14 +106,11 @@
 }
 */
 
-/*
-#pragma mark - Navigation
+#pragma mark - Dealloc
 
-// In a storyboard-based application, you will often want to do a little preparation before navigation
-- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
-    // Get the new view controller using [segue destinationViewController].
-    // Pass the selected object to the new view controller.
+- (void)dealloc {
+    [_campaigns release];
+    [super dealloc];
 }
-*/
 
 @end
--- a/project_files/HedgewarsMobile/Classes/DefinesAndMacros.h	Sun Feb 21 22:21:59 2016 +0100
+++ b/project_files/HedgewarsMobile/Classes/DefinesAndMacros.h	Sun Feb 21 23:12:12 2016 +0100
@@ -66,6 +66,7 @@
 #define MAPS_DIRECTORY()        [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Maps/"]
 #define MISSIONS_DIRECTORY()    [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Missions/Maps/"]
 #define TRAININGS_DIRECTORY()   [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Missions/Training/"]
+#define CAMPAIGNS_DIRECTORY()   [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Missions/Campaign/"]
 #define LOCALE_DIRECTORY()      [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Locale/"]
 #define SCRIPTS_DIRECTORY()     [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Data/Scripts/Multiplayer/"]