From 1cfc9903ef5fefb4ff052858e8c44ed7b7e5bd0a Mon Sep 17 00:00:00 2001 From: Rob Harrigan Date: Wed, 13 Dec 2017 11:48:49 -0600 Subject: [PATCH 1/5] Prevent duplicate stations and seeds --- Sources/Controllers/StationController.m | 8 ++++++++ Sources/Pandora/Pandora.m | 19 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Sources/Controllers/StationController.m b/Sources/Controllers/StationController.m index 2b43b12..beb50b2 100644 --- a/Sources/Controllers/StationController.m +++ b/Sources/Controllers/StationController.m @@ -233,6 +233,14 @@ - (void) seedAdded:(NSNotification*) not { if (seedsOfKind == nil) { seedsOfKind = seeds[seedKind] = [NSMutableArray array]; } + NSString *seedId = seed[@"seedId"]; + for (NSDictionary *currSeed in seedsOfKind) { + NSString *currSeedId = [currSeed objectForKey:@"seedId"]; + if ([currSeedId isEqualToString:seedId]) { + return; + } + } + [seedsOfKind addObject:seed]; seeds[seedKind] = seedsOfKind; [seedsCurrent reloadData]; diff --git a/Sources/Pandora/Pandora.m b/Sources/Pandora/Pandora.m index a0d5c92..aa64295 100644 --- a/Sources/Pandora/Pandora.m +++ b/Sources/Pandora/Pandora.m @@ -13,6 +13,8 @@ #import "URLConnection.h" #import "Notifications.h" #import "PandoraDevice.h" +#import "StationsController.h" +#import "PlaybackController.h" #pragma mark Error Codes @@ -338,6 +340,7 @@ - (BOOL) isAuthenticated { #pragma mark - Station Manipulation - (BOOL) createStation: (NSString*)musicId { + NSMutableDictionary *d = [self defaultRequestDictionary]; d[@"musicToken"] = musicId; @@ -348,6 +351,18 @@ - (BOOL) createStation: (NSString*)musicId { NSDictionary *result = d[@"result"]; Station *s = [self parseStationFromDictionary:result]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; + // Check if station exists: + NSUInteger stationIndex = [stations indexOfObject:s]; + + if (stationIndex <= [stations count]) { + // Station already exists, play it + Station *existingStation = [stations objectAtIndex:stationIndex]; + // This is misleading but it closes the search box + dict[@"station"] = existingStation; + [self postNotification:PandoraDidCreateStationNotification result:dict]; + return; + } + dict[@"station"] = s; [stations addObject:s]; [Station addStation:s]; @@ -614,7 +629,9 @@ - (BOOL) addSeed: (NSString*)token toStation:(Station*)station { [req setRequest:d]; [req setTls:FALSE]; [req setCallback:^(NSDictionary* d) { - [self postNotification:PandoraDidAddSeedNotification result:d[@"result"]]; + NSDictionary *result = d[@"result"]; + + [self postNotification:PandoraDidAddSeedNotification result:result]; }]; return [self sendAuthenticatedRequest:req]; } From 6b0a2001fb7c1b863aa0cb1a2244d1ce6cad9272 Mon Sep 17 00:00:00 2001 From: Rob Harrigan Date: Fri, 15 Dec 2017 08:51:39 -0600 Subject: [PATCH 2/5] cleaning up cosmetics for PR --- Sources/Controllers/StationController.m | 2 +- Sources/Pandora/Pandora.m | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/Sources/Controllers/StationController.m b/Sources/Controllers/StationController.m index beb50b2..2706194 100644 --- a/Sources/Controllers/StationController.m +++ b/Sources/Controllers/StationController.m @@ -235,7 +235,7 @@ - (void) seedAdded:(NSNotification*) not { } NSString *seedId = seed[@"seedId"]; for (NSDictionary *currSeed in seedsOfKind) { - NSString *currSeedId = [currSeed objectForKey:@"seedId"]; + NSString *currSeedId = currSeed[@"seedId"]; if ([currSeedId isEqualToString:seedId]) { return; } diff --git a/Sources/Pandora/Pandora.m b/Sources/Pandora/Pandora.m index aa64295..a992684 100644 --- a/Sources/Pandora/Pandora.m +++ b/Sources/Pandora/Pandora.m @@ -13,8 +13,6 @@ #import "URLConnection.h" #import "Notifications.h" #import "PandoraDevice.h" -#import "StationsController.h" -#import "PlaybackController.h" #pragma mark Error Codes @@ -340,7 +338,6 @@ - (BOOL) isAuthenticated { #pragma mark - Station Manipulation - (BOOL) createStation: (NSString*)musicId { - NSMutableDictionary *d = [self defaultRequestDictionary]; d[@"musicToken"] = musicId; @@ -629,9 +626,7 @@ - (BOOL) addSeed: (NSString*)token toStation:(Station*)station { [req setRequest:d]; [req setTls:FALSE]; [req setCallback:^(NSDictionary* d) { - NSDictionary *result = d[@"result"]; - - [self postNotification:PandoraDidAddSeedNotification result:result]; + [self postNotification:PandoraDidAddSeedNotification result:d[@"result"]]; }]; return [self sendAuthenticatedRequest:req]; } From 1334fe52a2ff7943e914ada18e60a8d7c094f3fd Mon Sep 17 00:00:00 2001 From: Rob Harrigan Date: Fri, 15 Dec 2017 08:52:47 -0600 Subject: [PATCH 3/5] indentation fix --- Sources/Pandora/Pandora.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Pandora/Pandora.m b/Sources/Pandora/Pandora.m index a992684..844201f 100644 --- a/Sources/Pandora/Pandora.m +++ b/Sources/Pandora/Pandora.m @@ -626,7 +626,7 @@ - (BOOL) addSeed: (NSString*)token toStation:(Station*)station { [req setRequest:d]; [req setTls:FALSE]; [req setCallback:^(NSDictionary* d) { - [self postNotification:PandoraDidAddSeedNotification result:d[@"result"]]; + [self postNotification:PandoraDidAddSeedNotification result:d[@"result"]]; }]; return [self sendAuthenticatedRequest:req]; } From 74f8061d4eeb555fa633bde5a5151ac4e18a6424 Mon Sep 17 00:00:00 2001 From: Rob Harrigan Date: Fri, 15 Dec 2017 08:56:50 -0600 Subject: [PATCH 4/5] clarify duplicate station behavior --- Sources/Pandora/Pandora.m | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Sources/Pandora/Pandora.m b/Sources/Pandora/Pandora.m index 844201f..6e9663c 100644 --- a/Sources/Pandora/Pandora.m +++ b/Sources/Pandora/Pandora.m @@ -354,15 +354,12 @@ - (BOOL) createStation: (NSString*)musicId { if (stationIndex <= [stations count]) { // Station already exists, play it Station *existingStation = [stations objectAtIndex:stationIndex]; - // This is misleading but it closes the search box dict[@"station"] = existingStation; - [self postNotification:PandoraDidCreateStationNotification result:dict]; - return; + } else { + dict[@"station"] = s; + [stations addObject:s]; + [Station addStation:s]; } - - dict[@"station"] = s; - [stations addObject:s]; - [Station addStation:s]; [self postNotification:PandoraDidCreateStationNotification result:dict]; }]; return [self sendAuthenticatedRequest:req]; From 3a4437514849a9646edd96a9b5d622ca9b2423e6 Mon Sep 17 00:00:00 2001 From: Rob Harrigan Date: Fri, 15 Dec 2017 09:17:49 -0600 Subject: [PATCH 5/5] reduce code for duplicate stations --- Sources/Pandora/Pandora.m | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/Sources/Pandora/Pandora.m b/Sources/Pandora/Pandora.m index 6e9663c..ab7ca01 100644 --- a/Sources/Pandora/Pandora.m +++ b/Sources/Pandora/Pandora.m @@ -348,18 +348,15 @@ - (BOOL) createStation: (NSString*)musicId { NSDictionary *result = d[@"result"]; Station *s = [self parseStationFromDictionary:result]; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; - // Check if station exists: - NSUInteger stationIndex = [stations indexOfObject:s]; + dict[@"station"] = s; - if (stationIndex <= [stations count]) { - // Station already exists, play it - Station *existingStation = [stations objectAtIndex:stationIndex]; - dict[@"station"] = existingStation; - } else { - dict[@"station"] = s; + /* Add the station internally if it doesn't exist */ + NSUInteger stationIndex = [stations indexOfObject:s]; + if (stationIndex > [stations count]) { [stations addObject:s]; [Station addStation:s]; } + [self postNotification:PandoraDidCreateStationNotification result:dict]; }]; return [self sendAuthenticatedRequest:req];