Skip to content

Commit

Permalink
Add API that allows waiting for a section to load; use this API when …
Browse files Browse the repository at this point in the history
…restoring specifiers by ID
  • Loading branch information
rpetrich committed Feb 19, 2013
1 parent cebd7dc commit e8c8022
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
6 changes: 4 additions & 2 deletions ALApplicationPreferenceViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,14 @@ - (id)appliedValueForKey:(NSString *)key inCellDescriptor:(id)cellDescriptor sec

- (PSSpecifier *)specifierForIndexPath:(NSIndexPath *)indexPath
{
NSInteger section = indexPath.section;
[_dataSource waitUntilDate:nil forContentInSectionAtIndex:section];
id cellDescriptor = [_dataSource cellDescriptorForIndexPath:indexPath];
if (!cellDescriptor) {
NSLog(@"AppList: no cell descriptor for cell!");
return nil;
}
NSDictionary *sectionDescriptor = [_dataSource.sectionDescriptors objectAtIndex:indexPath.section];
NSDictionary *sectionDescriptor = [_dataSource.sectionDescriptors objectAtIndex:section];
NSDictionary *entry = [self appliedValueForKey:@"entry" inCellDescriptor:cellDescriptor sectionDescriptor:sectionDescriptor];
if (!entry) {
NSLog(@"AppList: entry key missing!");
Expand All @@ -416,7 +418,7 @@ - (PSSpecifier *)specifierForIndexPath:(NSIndexPath *)indexPath
}
PSSpecifier *specifier = [specifiers objectAtIndex:0];
if ([specifier respondsToSelector:@selector(setIdentifier:)]) {
[specifier setIdentifier:[NSString stringWithFormat:@"applist:%d,%d", indexPath.section, indexPath.row]];
[specifier setIdentifier:[NSString stringWithFormat:@"applist:%d,%d", section, indexPath.row]];
}
return specifier;
}
Expand Down
2 changes: 2 additions & 0 deletions ALApplicationTableDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
- (void)removeSectionDescriptorAtIndex:(NSInteger)index;
- (void)removeSectionDescriptorsAtIndexes:(NSIndexSet *)indexSet;

- (BOOL)waitUntilDate:(NSDate *)date forContentInSectionAtIndex:(NSInteger)sectionIndex;

@end

extern const NSString *ALSectionDescriptorTitleKey;
Expand Down
38 changes: 36 additions & 2 deletions ALApplicationTableDataSource.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ @interface ALApplicationTableDataSourceSection : NSObject {
BOOL isStaticSection;
BOOL isLoading;
CFTimeInterval loadStartTime;
NSCondition *loadCondition;
}

@property (nonatomic, readonly) NSDictionary *descriptor;
Expand Down Expand Up @@ -149,6 +150,7 @@ - (id)initWithDescriptor:(NSDictionary *)descriptor dataSource:(ALApplicationTab
isLoading = YES;
loadStartTime = CACurrentMediaTime();
[self performSelectorInBackground:@selector(loadContent) withObject:nil];
loadCondition = [[NSCondition alloc] init];
} else {
[self loadContent];
}
Expand All @@ -159,6 +161,7 @@ - (id)initWithDescriptor:(NSDictionary *)descriptor dataSource:(ALApplicationTab

- (void)dealloc
{
[loadCondition release];
[_displayIdentifiers release];
[_displayNames release];
[_descriptor release];
Expand All @@ -185,19 +188,44 @@ - (void)loadContent
NSMutableArray *displayNames = [[NSMutableArray alloc] init];
for (NSString *displayId in displayIdentifiers)
[displayNames addObject:[applications objectForKey:displayId]];
[loadCondition lock];
_displayIdentifiers = displayIdentifiers;
_displayNames = displayNames;
iconSize = [[descriptor objectForKey:ALSectionDescriptorIconSizeKey] floatValue];
if (![NSThread isMainThread]) {
[self performSelectorOnMainThread:@selector(completedLoading) withObject:nil waitUntilDone:NO];
}
[loadCondition signal];
[loadCondition unlock];
[pool drain];
}

- (void)completedLoading
{
isLoading = NO;
[_dataSource sectionRequestedSectionReload:self animated:CACurrentMediaTime() - loadStartTime > 0.1];
if (isLoading) {
isLoading = NO;
[_dataSource sectionRequestedSectionReload:self animated:CACurrentMediaTime() - loadStartTime > 0.1];
}
}

- (BOOL)waitForContentUntilDate:(NSDate *)date
{
if (isLoading) {
[loadCondition lock];
BOOL result;
if (date)
result = [loadCondition waitUntilDate:date];
else {
[loadCondition wait];
result = YES;
}
[loadCondition unlock];
if (isLoading) {
[self completedLoading];
}
return result;
}
return YES;
}

@synthesize descriptor = _descriptor;
Expand Down Expand Up @@ -479,6 +507,12 @@ - (void)sectionRequestedSectionReload:(ALApplicationTableDataSourceSection *)sec
}
}

- (BOOL)waitUntilDate:(NSDate *)date forContentInSectionAtIndex:(NSInteger)sectionIndex
{
ALApplicationTableDataSourceSection *section = [_sectionDescriptors objectAtIndex:sectionIndex];
return [section waitForContentUntilDate:date];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (!_tableView) {
Expand Down

0 comments on commit e8c8022

Please sign in to comment.