AAAHHH! HOLY CRAP! I finally got intelligence sliding to work perfectly on the iphone. Now need to combine it seamlessly with paging, and it'll be DONE.

This commit is contained in:
Samuel Clay 2011-07-29 21:27:37 -07:00
parent 5dd493e8f3
commit d9b3f5910a
4 changed files with 60 additions and 78 deletions

View file

@ -137,41 +137,22 @@
NSDictionary *results = [[NSDictionary alloc] NSDictionary *results = [[NSDictionary alloc]
initWithDictionary:[jsonS JSONValue]]; initWithDictionary:[jsonS JSONValue]];
NSArray *newStories = [results objectForKey:@"stories"]; NSArray *newStories = [results objectForKey:@"stories"];
NSInteger newStoriesCount = [newStories count]; NSInteger existingStoriesCount = [[appDelegate activeFeedStoryLocations] count];
NSInteger existingStoriesCount = appDelegate.storyCount;
int storyCount = 0;
if (self.feedPage == 1) { if (self.feedPage == 1) {
[appDelegate setStories:newStories]; [appDelegate setStories:newStories];
} else if (newStoriesCount > 0) { } else if ([newStories count] > 0) {
for (int i=0; i < [appDelegate storyCount]; i++) {
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:i];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
int intelligenceLevel = [appDelegate selectedIntelligence];
if (score >= intelligenceLevel) {
storyCount += 1;
}
}
[appDelegate addStories:newStories]; [appDelegate addStories:newStories];
} }
NSInteger newStoriesCount = [[appDelegate activeFeedStoryLocations] count] - existingStoriesCount;
// NSLog(@"Stories: %d stories, page %d. %d new stories.", existingStoriesCount, self.feedPage, newStoriesCount); // NSLog(@"Stories: %d stories, page %d. %d new stories.", existingStoriesCount, self.feedPage, newStoriesCount);
if (existingStoriesCount > 0 && newStoriesCount > 0) { if (existingStoriesCount > 0 && newStoriesCount > 0) {
NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; NSMutableArray *indexPaths = [[NSMutableArray alloc] init];
int visibleRows = 0;
int intelligenceLevel = [appDelegate selectedIntelligence];
for (int i=0; i < newStoriesCount; i++) { for (int i=0; i < newStoriesCount; i++) {
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:existingStoriesCount+i]; [indexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
if (score >= intelligenceLevel) {
visibleRows += 1;
int row = storyCount+visibleRows;
[indexPaths addObject:[NSIndexPath indexPathForRow:row inSection:0]];
}
} }
[self.storyTitlesTable insertRowsAtIndexPaths:indexPaths [self.storyTitlesTable insertRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationNone]; withRowAnimation:UITableViewRowAnimationNone];
@ -253,17 +234,7 @@
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// The + 1 is for the finished/loading bar. // The + 1 is for the finished/loading bar.
NSLog(@"Current intelligence: %d", [appDelegate selectedIntelligence]); NSLog(@"Current intelligence: %d", [appDelegate selectedIntelligence]);
int storyCount = 0; int storyCount = [[appDelegate activeFeedStoryLocations] count];
for (int i=0; i < [appDelegate storyCount]; i++) {
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:i];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
int intelligenceLevel = [appDelegate selectedIntelligence];
if (score >= intelligenceLevel) {
storyCount += 1;
} else {
NSLog(@"Skipping %@", [story objectForKey:@"story_title"]);
}
}
return storyCount + 1; return storyCount + 1;
} }
@ -283,11 +254,10 @@
} }
} }
if (indexPath.row >= appDelegate.storyCount) { if (indexPath.row >= [[appDelegate activeFeedStoryLocations] count]) {
return [self makeLoadingCell]; return [self makeLoadingCell];
} }
NSDictionary *story = [self getStoryAtRow:indexPath.row]; NSDictionary *story = [self getStoryAtRow:indexPath.row];
if ([[story objectForKey:@"story_authors"] class] != [NSNull class]) { if ([[story objectForKey:@"story_authors"] class] != [NSNull class]) {
cell.storyAuthor.text = [[story objectForKey:@"story_authors"] uppercaseString]; cell.storyAuthor.text = [[story objectForKey:@"story_authors"] uppercaseString];
@ -338,20 +308,11 @@
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// NSLog(@"Height for row: %d of %d stories. (Finished: %d)", indexPath.row, appDelegate.storyCount, self.pageFinished); // NSLog(@"Height for row: %d of %d stories. (Finished: %d)", indexPath.row, appDelegate.storyCount, self.pageFinished);
if (indexPath.row >= appDelegate.storyCount) { if (indexPath.row >= [[appDelegate activeFeedStoryLocations] count]) {
if (self.pageFinished) return 16; if (self.pageFinished) return 16;
else return kTableViewRowHeight; else return kTableViewRowHeight;
} else { } else {
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:indexPath.row]; return kTableViewRowHeight;
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
int intelligenceLevel = [appDelegate selectedIntelligence];
if (score >= intelligenceLevel) {
// NSLog(@"Score: %d on %d %@ - %@", score, intelligenceLevel-1, score >= (intelligenceLevel-1) ? @"SHOW" : @"HIDE",
// [story objectForKey:@"story_title"]);
return kTableViewRowHeight;
} else {
return 0.0f;
}
} }
} }
@ -384,11 +345,15 @@
NSMutableArray *insertIndexPaths = [NSMutableArray array]; NSMutableArray *insertIndexPaths = [NSMutableArray array];
NSMutableArray *deleteIndexPaths = [NSMutableArray array]; NSMutableArray *deleteIndexPaths = [NSMutableArray array];
[appDelegate setSelectedIntelligence:newLevel]; if (newLevel < previousLevel) {
[appDelegate setSelectedIntelligence:newLevel];
[appDelegate calculateStoryLocations];
}
for (int i=0; i < [appDelegate storyCount]; i++) { for (int i=0; i < [[appDelegate activeFeedStoryLocations] count]; i++) {
int location = [[[appDelegate activeFeedStoryLocations] objectAtIndex:i] intValue];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:i]; NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:location];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]]; int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
if (previousLevel == -1) { if (previousLevel == -1) {
@ -401,6 +366,7 @@
if (newLevel == -1 && score == -1) { if (newLevel == -1 && score == -1) {
[insertIndexPaths addObject:indexPath]; [insertIndexPaths addObject:indexPath];
} else if (newLevel == 1 && score == 0) { } else if (newLevel == 1 && score == 0) {
NSLog(@"Deleting: %d", i);
[deleteIndexPaths addObject:indexPath]; [deleteIndexPaths addObject:indexPath];
} }
} else if (previousLevel == 1) { } else if (previousLevel == 1) {
@ -412,31 +378,27 @@
} }
} }
NSLog(@"Select: %d deleted, %d inserted. Pre: %d, post: %d", [deleteIndexPaths count], [insertIndexPaths count], previousLevel, newLevel); NSLog(@"Select: %d deleted, %d inserted. Pre: %d, post: %d", [deleteIndexPaths count], [insertIndexPaths count], previousLevel, newLevel);
[self.storyTitlesTable beginUpdates];
[self.storyTitlesTable deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
[self.storyTitlesTable insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
[self.storyTitlesTable endUpdates];
if (newLevel > previousLevel) {
[appDelegate setSelectedIntelligence:newLevel];
[appDelegate calculateStoryLocations];
}
[self.storyTitlesTable beginUpdates];
if ([deleteIndexPaths count] > 0) {
[self.storyTitlesTable deleteRowsAtIndexPaths:deleteIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
}
if ([insertIndexPaths count] > 0) {
[self.storyTitlesTable insertRowsAtIndexPaths:insertIndexPaths
withRowAnimation:UITableViewRowAnimationNone];
}
[self.storyTitlesTable endUpdates];
} }
- (NSDictionary *)getStoryAtRow:(NSInteger)indexPathRow { - (NSDictionary *)getStoryAtRow:(NSInteger)indexPathRow {
int row = 0;
int intelligenceLevel = [appDelegate selectedIntelligence]; int row = [[[appDelegate activeFeedStoryLocations] objectAtIndex:indexPathRow] intValue];
for (int i=0; i < [appDelegate storyCount]; i++) {
NSDictionary *story = [appDelegate.activeFeedStories objectAtIndex:i];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
if (score >= intelligenceLevel) {
if (row == indexPathRow) {
row = i;
break;
} else {
row++;
}
}
}
NSLog(@"i: %d, %d, %d", row, indexPathRow, intelligenceLevel);
return [appDelegate.activeFeedStories objectAtIndex:row]; return [appDelegate.activeFeedStories objectAtIndex:row];
} }

View file

@ -28,6 +28,7 @@
NSString * activeUsername; NSString * activeUsername;
NSDictionary * activeFeed; NSDictionary * activeFeed;
NSArray * activeFeedStories; NSArray * activeFeedStories;
NSMutableArray * activeFeedStoryLocations;
NSDictionary * activeStory; NSDictionary * activeStory;
NSURL * activeOriginalStoryURL; NSURL * activeOriginalStoryURL;
int storyCount; int storyCount;
@ -49,6 +50,7 @@
@property (readwrite, retain) NSString * activeUsername; @property (readwrite, retain) NSString * activeUsername;
@property (readwrite, retain) NSDictionary * activeFeed; @property (readwrite, retain) NSDictionary * activeFeed;
@property (readwrite, retain) NSArray * activeFeedStories; @property (readwrite, retain) NSArray * activeFeedStories;
@property (readwrite, retain) NSMutableArray * activeFeedStoryLocations;
@property (readwrite, retain) NSDictionary * activeStory; @property (readwrite, retain) NSDictionary * activeStory;
@property (readwrite, retain) NSURL * activeOriginalStoryURL; @property (readwrite, retain) NSURL * activeOriginalStoryURL;
@property (readwrite) int storyCount; @property (readwrite) int storyCount;
@ -73,6 +75,7 @@
- (int)unreadCount; - (int)unreadCount;
- (void)markActiveStoryRead; - (void)markActiveStoryRead;
- (void)markActiveFeedAllRead; - (void)markActiveFeedAllRead;
- (void)calculateStoryLocations;
+ (int)computeStoryScore:(NSDictionary *)intelligence; + (int)computeStoryScore:(NSDictionary *)intelligence;
@end @end

View file

@ -27,6 +27,7 @@
@synthesize activeUsername; @synthesize activeUsername;
@synthesize activeFeed; @synthesize activeFeed;
@synthesize activeFeedStories; @synthesize activeFeedStories;
@synthesize activeFeedStoryLocations;
@synthesize activeStory; @synthesize activeStory;
@synthesize storyCount; @synthesize storyCount;
@synthesize selectedIntelligence; @synthesize selectedIntelligence;
@ -61,6 +62,7 @@
[activeUsername release]; [activeUsername release];
[activeFeed release]; [activeFeed release];
[activeFeedStories release]; [activeFeedStories release];
[activeFeedStoryLocations release];
[activeStory release]; [activeStory release];
[activeOriginalStoryURL release]; [activeOriginalStoryURL release];
[recentlyReadStories release]; [recentlyReadStories release];
@ -182,12 +184,14 @@
// NSLog(@"Adding: %d to %@", [stories count], stories); // NSLog(@"Adding: %d to %@", [stories count], stories);
self.activeFeedStories = [self.activeFeedStories arrayByAddingObjectsFromArray:stories]; self.activeFeedStories = [self.activeFeedStories arrayByAddingObjectsFromArray:stories];
self.storyCount = [self.activeFeedStories count]; self.storyCount = [self.activeFeedStories count];
[self calculateStoryLocations];
} }
- (void)setStories:(NSArray *)activeFeedStoriesValue { - (void)setStories:(NSArray *)activeFeedStoriesValue {
self.activeFeedStories = activeFeedStoriesValue; self.activeFeedStories = activeFeedStoriesValue;
self.storyCount = [self.activeFeedStories count]; self.storyCount = [self.activeFeedStories count];
self.recentlyReadStories = [[NSMutableArray alloc] init]; self.recentlyReadStories = [[NSMutableArray alloc] init];
[self calculateStoryLocations];
} }
- (void)markActiveStoryRead { - (void)markActiveStoryRead {
@ -216,6 +220,19 @@
[self.activeFeed setValue:[NSNumber numberWithInt:0] forKey:@"ng"]; [self.activeFeed setValue:[NSNumber numberWithInt:0] forKey:@"ng"];
} }
- (void)calculateStoryLocations {
self.activeFeedStoryLocations = [[NSMutableArray alloc] init];
for (int i=0; i < self.storyCount; i++) {
NSDictionary *story = [self.activeFeedStories objectAtIndex:i];
int score = [NewsBlurAppDelegate computeStoryScore:[story objectForKey:@"intelligence"]];
int intelligenceLevel = self.selectedIntelligence;
if (score >= intelligenceLevel) {
NSNumber *location = [NSNumber numberWithInt:i];
[self.activeFeedStoryLocations addObject:location];
}
}
}
+ (int)computeStoryScore:(NSDictionary *)intelligence { + (int)computeStoryScore:(NSDictionary *)intelligence {
int score = 0; int score = 0;
int title = [[intelligence objectForKey:@"title"] intValue]; int title = [[intelligence objectForKey:@"title"] intValue];

View file

@ -142,12 +142,12 @@
self.dictFolders = [results objectForKey:@"flat_folders"]; self.dictFolders = [results objectForKey:@"flat_folders"];
self.dictFeeds = [results objectForKey:@"feeds"]; self.dictFeeds = [results objectForKey:@"feeds"];
//NSLog(@"Received Feeds: %@", dictFolders); //NSLog(@"Received Feeds: %@", dictFolders);
NSSortDescriptor *sortDescriptor; // NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"feed_title" // sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"feed_title"
ascending:YES] autorelease]; // ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor]; // NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSMutableDictionary *sortedFolders = [[NSMutableDictionary alloc] init]; NSMutableDictionary *sortedFolders = [[NSMutableDictionary alloc] init];
NSArray *sortedArray; // NSArray *sortedArray;
for (id f in self.dictFolders) { for (id f in self.dictFolders) {
[self.dictFoldersArray addObject:f]; [self.dictFoldersArray addObject:f];