Skip to content
This repository has been archived by the owner on Sep 11, 2018. It is now read-only.

Commit

Permalink
Make randomizing actually choose a random story! It was previously ju…
Browse files Browse the repository at this point in the history
…st selecting a story from the last subreddit.
  • Loading branch information
rbruels committed Dec 23, 2010
1 parent ceec579 commit 3c47e84
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Classes/Data/SubredditDataModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ - (void)requestDidFinishLoad:(TTURLRequest*)request
// parse the JSON data that we retrieved from the server
NSDictionary *json = [NSDictionary dictionaryWithJSONString:responseBody error:nil];
[responseBody release];

// drill down into the JSON object to get the part
// that we're actually interested in.

Expand Down
9 changes: 7 additions & 2 deletions Classes/Data/SubredditDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@ - (void)tableViewDidLoadModel:(UITableView*)tableView
lastLoadedSubreddit = [self retain];
}

- (NSArray *)items
{
return _subredditModel.stories;
}

- (Story *)storyWithIndex:(int)anIndex
{
if (anIndex < 0 || anIndex > [self.items count])
if (anIndex < 0 || anIndex > [((SubredditDataModel *)self.model) totalStories])
return nil;
return [self.items objectAtIndex:anIndex];
return [((SubredditDataModel *)self.model).stories objectAtIndex:anIndex];
}

#pragma mark TTDataSource
Expand Down
5 changes: 2 additions & 3 deletions Classes/iRedditAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,15 @@ - (void)showRandomStory
//[[Beacon shared] startSubBeaconWithName:@"serendipityTime" timeSession:YES];
}

SubredditDataSource *activeDataSource = [SubredditDataSource lastLoadedSubreddit];
NSInteger count = [((SubredditDataModel *)activeDataSource.model) totalStories];
NSInteger count = [(SubredditDataModel *)randomDataSource.model totalStories];
NSInteger randomIndex = count > 0 ? arc4random() % count : 0;

Story *story = nil;

int i=0;
while (!story && i++ < count)
{
story = [activeDataSource storyWithIndex:(randomIndex++)%count];
story = [randomDataSource storyWithIndex:(randomIndex++)%count];

if ([story visited] && i < count - 1)
story = nil;
Expand Down

0 comments on commit 3c47e84

Please sign in to comment.