Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate stations and seeds (#320) #324

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/Controllers/StationController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Raab70 marked this conversation as resolved.
Show resolved Hide resolved
if ([currSeedId isEqualToString:seedId]) {
return;
}
}

[seedsOfKind addObject:seed];
seeds[seedKind] = seedsOfKind;
[seedsCurrent reloadData];
Expand Down
19 changes: 18 additions & 1 deletion Sources/Pandora/Pandora.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#import "URLConnection.h"
#import "Notifications.h"
#import "PandoraDevice.h"
#import "StationsController.h"
#import "PlaybackController.h"

#pragma mark Error Codes

Expand Down Expand Up @@ -338,6 +340,7 @@ - (BOOL) isAuthenticated {
#pragma mark - Station Manipulation

- (BOOL) createStation: (NSString*)musicId {

Raab70 marked this conversation as resolved.
Show resolved Hide resolved
NSMutableDictionary *d = [self defaultRequestDictionary];
d[@"musicToken"] = musicId;

Expand All @@ -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;
}

Raab70 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nriley I've cleaned this up a bit, the functionality is the same. Basically the object of this is to skip the addObject and addStation if the station already exists so that we don't get a duplicate in the station list.

dict[@"station"] = s;
[stations addObject:s];
[Station addStation:s];
Expand Down Expand Up @@ -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];
Raab70 marked this conversation as resolved.
Show resolved Hide resolved
}];
return [self sendAuthenticatedRequest:req];
}
Expand Down