- Fix missions regression: refactoring, WIP ios-develop
authorantonc27 <antonc27@mail.ru>
Wed, 14 Mar 2018 00:25:31 +0100
branchios-develop
changeset 13191 0c1f3a8ba0bb
parent 13167 c129707eb1e6
child 13192 d6aea85111c7
- Fix missions regression: refactoring, WIP
project_files/HedgewarsMobile/Classes/MissionTrainingViewController.h
project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m
--- a/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.h	Sun Mar 11 23:24:05 2018 +0100
+++ b/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.h	Wed Mar 14 00:25:31 2018 +0100
@@ -22,10 +22,14 @@
 
 @interface MissionTrainingViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
 
-@property (nonatomic, strong) NSArray *listOfMissionIDs;
-@property (nonatomic, strong) NSDictionary *dictOfMissions;
+@property (nonatomic, strong) NSArray *missionsTypes;
+@property (nonatomic, strong) NSDictionary *dictOfAllMissions;
+
 @property (nonatomic, strong) NSString *missionName;
 
+@property (nonatomic, strong) NSArray *listOfTrainingIDs;
+@property (nonatomic, strong) NSDictionary *dictOfTraining;
+
 @property (nonatomic, strong) IBOutlet UIImageView *previewImage;
 @property (nonatomic, strong) IBOutlet UITableView *tableView;
 @property (nonatomic, strong) IBOutlet UILabel *descriptionLabel;
--- a/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m	Sun Mar 11 23:24:05 2018 +0100
+++ b/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m	Wed Mar 14 00:25:31 2018 +0100
@@ -21,6 +21,7 @@
 #import <QuartzCore/QuartzCore.h>
 #import "GameInterfaceBridge.h"
 
+#define TRAINING_MISSION_TYPE @"Training"
 
 @implementation MissionTrainingViewController
 
@@ -55,7 +56,12 @@
 }
 
 - (void)viewWillAppear:(BOOL)animated {
-    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:arc4random_uniform((int)[self.listOfMissionIDs count]) inSection:0];
+    NSInteger randomSection = arc4random_uniform((int)[self.missionsTypes count]);
+    NSString *type = self.missionsTypes[randomSection];
+    NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
+    NSInteger randomRow = arc4random_uniform((int)[listOfIDs count]);
+    
+    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:randomRow inSection:randomSection];
     [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
     [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
     [super viewWillAppear:animated];
@@ -75,7 +81,7 @@
 
 #pragma mark - Missions dictionaries methods
 
-- (NSDictionary *)newLocalizedMissionsDictionary
+- (NSDictionary *)newLocalizedMissionsDictionaryForType: (NSString *)type
 {
     NSString *languageID = [HWUtils languageID];
     
@@ -84,8 +90,8 @@
     
     if (![languageID isEqualToString:@"en"] && [[NSFileManager defaultManager] fileExistsAtPath:localizedMissionsDescLocation])
     {
-        NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
-        NSDictionary *localizedMissionsDict = [self newMissionsDictionaryFromMissionsFile:localizedMissionsDescLocation];
+        NSDictionary *missionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:missionsDescLocation];
+        NSDictionary *localizedMissionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:localizedMissionsDescLocation];
         
         
         NSMutableDictionary *tempMissionsDict = [[NSMutableDictionary alloc] init];
@@ -107,20 +113,21 @@
     }
     else
     {
-        NSDictionary *missionsDict = [self newMissionsDictionaryFromMissionsFile:missionsDescLocation];
+        NSDictionary *missionsDict = [self newMissionsDictionaryForType:type fromMissionsFile:missionsDescLocation];
         
         
         return missionsDict;
     }
 }
 
-- (NSDictionary *)newMissionsDictionaryFromMissionsFile:(NSString *)filePath
+- (NSDictionary *)newMissionsDictionaryForType:(NSString *)type fromMissionsFile:(NSString *)filePath
 {
     NSMutableDictionary *missionsDict = [[NSMutableDictionary alloc] init];
     
     NSString *missionsFileContents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
     NSArray *missionsLines = [missionsFileContents componentsSeparatedByString:@"\n"];
     
+    NSString *directory = [self missionsDirectoryForType:type];
     for (NSString *line in missionsLines)
     {
         if ([line length] > 0)
@@ -129,7 +136,7 @@
             
             NSString *missionID = [line substringToIndex:firstDotLocation];
             
-            NSString *missionFullPath = [NSString stringWithFormat:@"%@%@.lua", TRAININGS_DIRECTORY(), missionID];
+            NSString *missionFullPath = [NSString stringWithFormat:@"%@%@.lua", directory, missionID];
             if (![[NSFileManager defaultManager] fileExistsAtPath:missionFullPath])
             {
                 continue;
@@ -169,35 +176,85 @@
 #pragma mark -
 #pragma mark override setters/getters for better memory handling
 
--(NSArray *)listOfMissionIDs
+- (NSArray *)missionsTypes
+{
+    if (!_missionsTypes)
+    {
+        _missionsTypes = @[ TRAINING_MISSION_TYPE ];
+    }
+    
+    return _missionsTypes;
+}
+
+- (NSDictionary *)dictOfAllMissions
 {
-    if (!_listOfMissionIDs)
+    if (!_dictOfAllMissions)
     {
-        NSArray *sortedKeys = [[self.dictOfMissions allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
-        _listOfMissionIDs = [[NSArray alloc] initWithArray:sortedKeys];
+        NSArray *types = [self missionsTypes];
+        _dictOfAllMissions = @{ types[0] : [self dictOfTraining] };
+    }
+    
+    return _dictOfAllMissions;
+}
+
+- (NSArray *)listOfTrainingIDs
+{
+    if (!_listOfTrainingIDs)
+    {
+        NSArray *sortedKeys = [[self.dictOfTraining allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
+        _listOfTrainingIDs = [[NSArray alloc] initWithArray:sortedKeys];
     }
     
-    return _listOfMissionIDs;
+    return _listOfTrainingIDs;
+}
+
+- (NSDictionary *)dictOfTraining
+{
+    if (!_dictOfTraining)
+    {
+        _dictOfTraining = [self newLocalizedMissionsDictionaryForType:TRAINING_MISSION_TYPE];
+    }
+    
+    return _dictOfTraining;
 }
 
-- (NSDictionary *)dictOfMissions
+#pragma mark -
+#pragma mark Missions types
+
+- (NSString *)missionsDirectoryForType:(NSString *)type
+{
+    if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
+        return TRAININGS_DIRECTORY();
+    }
+    return nil;
+}
+
+- (NSArray *)listOfMissionsIDsForType:(NSString *)type
 {
-    if (!_dictOfMissions)
-    {
-        _dictOfMissions = [self newLocalizedMissionsDictionary];
+    if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
+        return self.listOfTrainingIDs;
     }
-    
-    return _dictOfMissions;
+    return nil;
+}
+
+- (NSDictionary *)dictOfMissionsForType:(NSString *)type
+{
+    if ([type isEqualToString:TRAINING_MISSION_TYPE]) {
+        return self.dictOfTraining;
+    }
+    return nil;
 }
 
 #pragma mark -
 #pragma mark Table view data source
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
-    return 1;
+    return [self.missionsTypes count];
 }
 
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
-    return [self.listOfMissionIDs count];
+    NSString *type = self.missionsTypes[section];
+    NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
+    return [listOfIDs count];
 }
 
 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
@@ -213,15 +270,20 @@
         cell = [[UITableViewCell alloc] initWithStyle:(IS_IPAD()) ? UITableViewCellStyleDefault : UITableViewCellStyleSubtitle
                                        reuseIdentifier:CellIdentifier];
     
-    NSString *missionID = [self.listOfMissionIDs objectAtIndex:row];
-    cell.textLabel.text = self.dictOfMissions[missionID][@"name"];
+    NSInteger section = [indexPath section];
+    NSString *type = self.missionsTypes[section];
+    NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
+    NSDictionary *dict = [self dictOfMissionsForType:type];
+    
+    NSString *missionID = [listOfIDs objectAtIndex:row];
+    cell.textLabel.text = dict[missionID][@"name"];
     
     cell.textLabel.textColor = [UIColor lightYellowColor];
     //cell.textLabel.font = [UIFont fontWithName:@"Bradley Hand Bold" size:[UIFont labelFontSize]];
     cell.textLabel.textAlignment = (IS_IPAD()) ? NSTextAlignmentCenter : NSTextAlignmentLeft;
     cell.textLabel.backgroundColor = [UIColor clearColor];
     cell.textLabel.adjustsFontSizeToFitWidth = YES;
-    cell.detailTextLabel.text = (IS_IPAD()) ? nil : self.dictOfMissions[missionID][@"desc"];
+    cell.detailTextLabel.text = (IS_IPAD()) ? nil : dict[missionID][@"desc"];
     cell.detailTextLabel.textColor = [UIColor whiteColor];
     cell.detailTextLabel.backgroundColor = [UIColor clearColor];
     cell.detailTextLabel.adjustsFontSizeToFitWidth = YES;
@@ -240,15 +302,19 @@
 #pragma mark -
 #pragma mark Table view delegate
 - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
+    NSInteger section = [indexPath section];
+    NSString *type = self.missionsTypes[section];
+    NSArray *listOfIDs = [self listOfMissionsIDsForType:type];
+    
     NSInteger row = [indexPath row];
-
-    self.missionName = [self.listOfMissionIDs objectAtIndex:row];
+    self.missionName = [listOfIDs objectAtIndex:row];
     NSString *size = IS_IPAD() ? @"@2x" : @"";
-    NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/Training/%@%@.png",GRAPHICS_DIRECTORY(),self.missionName,size];
+    NSString *filePath = [[NSString alloc] initWithFormat:@"%@/Missions/%@/%@%@.png",GRAPHICS_DIRECTORY(), type, self.missionName, size];
     UIImage *img = [[UIImage alloc] initWithContentsOfFile:filePath];
     [self.previewImage setImage:img];
 
-    self.descriptionLabel.text = self.dictOfMissions[self.missionName][@"desc"];
+    NSDictionary *dict = [self dictOfMissionsForType:type];
+    self.descriptionLabel.text = dict[self.missionName][@"desc"];
 }
 
 #pragma mark -
@@ -256,9 +322,10 @@
 
 - (void)didReceiveMemoryWarning
 {
-    self.missionName = nil;
-    self.listOfMissionIDs = nil;
-    self.dictOfMissions = nil;
+    self.listOfTrainingIDs = nil;
+    self.dictOfTraining = nil;
+    self.dictOfAllMissions = nil;
+    self.missionsTypes = nil;
     // if you nil this one it won't get updated anymore
     //self.previewImage = nil;
     [super didReceiveMemoryWarning];