Fix a crash when the rating index is outside the bounds of the ratings codes. This happens when a users changes the ratings settings in the preferences.

This commit is contained in:
Damiano Galassi committed 2015-03-29 21:40:44 +02:00
1 parent acbc6a3f93
commit 0fdad0eebf
2 files changed
+49 -28

No files matched your search

+17 -6
View File
@@ -22,9 +22,9 @@
return sharedRatingsManager;
}
- (id) init {
- (instancetype) init {
if (self = [super init]) {
NSString* ratingsJSON = [[NSBundle bundleForClass:[MP42Ratings class]] pathForResource:@"Ratings" ofType:@"json"];
NSString *ratingsJSON = [[NSBundle bundleForClass:[MP42Ratings class]] pathForResource:@"Ratings" ofType:@"json"];
if (!ratingsJSON) {
[self release];
return nil;
@@ -32,28 +32,36 @@
JSONDecoder *jsonDecoder = [JSONDecoder decoder];
ratingsDictionary = [[jsonDecoder objectWithData:[NSData dataWithContentsOfFile:ratingsJSON]] retain];
// construct movie ratings
ratings = [[NSMutableArray alloc] init];
iTunesCodes = [[NSMutableArray alloc] init];
// if a specific country is picked, include the USA ratings at the end
NSString *selectedCountry = [[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"];
NSDictionary *usaRatings = nil;
for (NSDictionary *countryRatings in ratingsDictionary) {
NSString *countryName = [countryRatings valueForKey:@"country"];
if ([countryName isEqualToString:@"USA"]) {
usaRatings = countryRatings;
}
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"All countries"]) {
if (![countryName isEqualToString:@"Unknown"] && ![countryName isEqualToString:[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"]]) {
if (![selectedCountry isEqualToString:@"All countries"]) {
if (![countryName isEqualToString:@"Unknown"] && ![countryName isEqualToString:selectedCountry]) {
continue;
}
}
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", countryName, [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
[iTunesCodes addObject:[NSString stringWithFormat:@"%@|%@|%@|", [rating valueForKey:@"prefix"], [rating valueForKey:@"itunes-code"], [rating valueForKey:@"itunes-value"]]];
}
}
if (![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"All countries"] && ![[[NSUserDefaults standardUserDefaults] valueForKey:@"SBRatingsCountry"] isEqualToString:@"USA"]) {
if (![selectedCountry isEqualToString:@"All countries"] && ![selectedCountry isEqualToString:@"USA"]) {
if (usaRatings) {
for (NSDictionary *rating in [usaRatings valueForKey:@"ratings"]) {
[ratings addObject:[NSString stringWithFormat:@"%@ %@: %@", @"USA", [rating valueForKey:@"media"], [rating valueForKey:@"description"]]];
@@ -113,9 +121,11 @@
return i;
}
}
if (aRatingString != nil) {
NSLog(@"Unknown rating information: %@", target1);
}
}
for (NSDictionary *countryRatings in ratingsDictionary) {
if ([[countryRatings valueForKey:@"country"] isEqualToString:aCountry]) {
for (NSDictionary *rating in [countryRatings valueForKey:@"ratings"]) {
@@ -125,6 +135,7 @@
}
}
}
return [self unknownIndex];
}