Adding preferences for offline story limit and image download concurrency.

This commit is contained in:
Samuel Clay 2013-07-11 18:36:09 -07:00
parent f6bb6502f8
commit 2d7171ed45
5 changed files with 142 additions and 11 deletions

View file

@ -346,6 +346,10 @@
- (void)storeAllUnreadStories:(ASIHTTPRequest *)request;
- (void)flushQueuedReadStories:(BOOL)forceCheck withCallback:(void(^)())callback;
- (void)syncQueuedReadStories:(FMDatabase *)db withStories:(NSDictionary *)hashes withCallback:(void(^)())callback;
- (void)deleteAllCachedImages;
- (NSArray *)uncachedImageUrls;
- (void)fetchAllUncachedImages;
- (void)storeCachedImage:(ASIHTTPRequest *)request;
- (void)cachedImageQueueFinished:(ASINetworkQueue *)queue;
- (void)flushOldCachedImages;
- (void)prepareActiveCachedImages:(FMDatabase *)db;

View file

@ -368,7 +368,13 @@
preferencesViewController.showCreditsFooter = NO;
preferencesViewController.title = @"Preferences";
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"offline_allowed"];
preferencesViewController.hiddenKeys = enabled ? nil : [NSSet setWithObjects:@"offline_image_download", @"offline_download_connection", nil];
preferencesViewController.hiddenKeys = enabled ? nil :
[NSSet setWithObjects:@"offline_image_download",
@"offline_download_connection",
@"offline_store_limit",
@"offline_image_concurrency",
nil];
[[NSUserDefaults standardUserDefaults] setObject:@"Delete offline stories..." forKey:@"offline_cache_empty_stories"];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:preferencesViewController];
self.modalNavigationController = navController;
@ -2129,7 +2135,7 @@
[db executeUpdate:@"drop table if exists `accounts`"];
[db executeUpdate:@"drop table if exists `unread_counts`"];
[db executeUpdate:@"drop table if exists `cached_images`"];
// [db executeUpdate:@"drop table if exists `queued_read_hashes`"];
// [db executeUpdate:@"drop table if exists `queued_read_hashes`"]; // Nope, don't clear this.
NSLog(@"Dropped db: %@", [db lastErrorMessage]);
sqlite3_exec(db.sqliteHandle, [[NSString stringWithFormat:@"PRAGMA user_version = %d", CURRENT_DB_VERSION] UTF8String], NULL, NULL, NULL);
}
@ -2191,6 +2197,7 @@
" story_hash varchar(24),"
" image_url varchar(1024),"
" image_cached boolean"
" failed boolean"
")"];
[db executeUpdate:createImagesTable];
NSString *indexImagesFeedId = @"CREATE INDEX IF NOT EXISTS cached_images_story_feed_id ON cached_images (story_feed_id)";
@ -2477,7 +2484,7 @@
self.remainingUncachedImagesCount = count;
}
int limit = 12;
int limit = 96;
NSString *order;
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"default_order"] isEqualToString:@"oldest"]) {
order = @"ASC";
@ -2510,10 +2517,33 @@
return urls;
}
- (void)deleteAllCachedImages {
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"story_images"];
NSArray *directoryContents = [fileManager contentsOfDirectoryAtPath:cacheDirectory error:&error];
int removed = 0;
if (error == nil) {
for (NSString *path in directoryContents) {
NSString *fullPath = [cacheDirectory stringByAppendingPathComponent:path];
BOOL removeSuccess = [fileManager removeItemAtPath:fullPath error:&error];
removed++;
if (!removeSuccess) {
continue;
}
}
}
NSLog(@"Deleted %d images.", removed);
}
- (void)fetchAllUncachedImages {
NSArray *urls = [self uncachedImageUrls];
operationQueue = [[ASINetworkQueue alloc] init];
operationQueue.maxConcurrentOperationCount = 4;
operationQueue.maxConcurrentOperationCount = [[NSUserDefaults standardUserDefaults]
integerForKey:@"offline_image_concurrency"];
operationQueue.delegate = self;
if ([urls count] == 0) {
@ -2553,6 +2583,9 @@
NSData *responseData = [request responseData];
NSString *md5Url = [Utilities md5:[[request originalURL] absoluteString]];
NSLog(@"Storing image: %@ (%d bytes - %d in queue)", storyHash, [responseData length], [operationQueue requestsCount]);
if ([responseData length] <= 43) {
NSLog(@" ---> Image url: %@", [request url]);
}
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);

View file

@ -896,20 +896,48 @@ static const CGFloat kFolderTitleHeight = 28;
}
}
#pragma mark -
#pragma mark Preferences
- (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[appDelegate.masterContainerViewController dismissViewControllerAnimated:YES completion:nil];
} else {
[appDelegate.navigationController dismissViewControllerAnimated:YES completion:nil];
}
// your code here to reconfigure the app for changed settings
}
- (void)settingDidChange:(NSNotification*)notification {
if ([notification.object isEqual:@"offline_allowed"]) {
BOOL enabled = (BOOL)[[notification.userInfo objectForKey:@"offline_allowed"] intValue];
[appDelegate.preferencesViewController setHiddenKeys:enabled ? nil : [NSSet setWithObjects:@"offline_image_download", @"offline_download_connection", nil] animated:YES];
[appDelegate.preferencesViewController setHiddenKeys:enabled ? nil :
[NSSet setWithObjects:@"offline_image_download",
@"offline_download_connection",
@"offline_store_limit",
@"offline_image_concurrency",
nil] animated:YES];
}
}
- (void)settingsViewController:(IASKAppSettingsViewController*)sender buttonTappedForSpecifier:(IASKSpecifier*)specifier {
if ([specifier.key isEqualToString:@"offline_cache_empty_stories"]) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
[[NSUserDefaults standardUserDefaults] setObject:@"Deleting..." forKey:specifier.key];
[appDelegate.database inTransaction:^(FMDatabase *db, BOOL *rollback) {
[db executeUpdate:@"DROP TABLE unread_hashes"];
[db executeUpdate:@"DROP TABLE unread_counts"];
[db executeUpdate:@"DROP TABLE accounts"];
[db executeUpdate:@"DROP TABLE stories"];
[db executeUpdate:@"DROP TABLE cached_images"];
[appDelegate setupDatabase:db];
}];
[appDelegate deleteAllCachedImages];
dispatch_sync(dispatch_get_main_queue(), ^{
[[NSUserDefaults standardUserDefaults] setObject:@"Cleared all stories and images!"
forKey:specifier.key];
});
});
}
}

View file

@ -13,10 +13,10 @@
// #define BACKGROUND_REFRESH_SECONDS -5
#define BACKGROUND_REFRESH_SECONDS -10*60
// #define NEWSBLUR_URL [NSString stringWithFormat:@"http://nb.local.com"]
// #define NEWSBLUR_HOST [NSString stringWithFormat:@"nb.local.com"]
#define NEWSBLUR_URL [NSString stringWithFormat:@"https://www.newsblur.com"]
#define NEWSBLUR_HOST [NSString stringWithFormat:@"www.newsblur.com"]
#define NEWSBLUR_URL [NSString stringWithFormat:@"http://nb.local.com"]
#define NEWSBLUR_HOST [NSString stringWithFormat:@"nb.local.com"]
// #define NEWSBLUR_URL [NSString stringWithFormat:@"https://www.newsblur.com"]
// #define NEWSBLUR_HOST [NSString stringWithFormat:@"www.newsblur.com"]
#define NEWSBLUR_LINK_COLOR 0x405BA8
#define NEWSBLUR_HIGHLIGHT_COLOR 0xd2e6fd

View file

@ -116,6 +116,72 @@
<key>Key</key>
<string>offline_download_connection</string>
</dict>
<dict>
<key>FooterText</key>
<string>More stories take more disk space, but otherwise storing more stories has no noticable effect on performance.</string>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Store</string>
<key>Titles</key>
<array>
<string>500 stories</string>
<string>1,000 stories</string>
<string>2,000 stories</string>
<string>5,000 stories</string>
<string>10,000 stories</string>
</array>
<key>DefaultValue</key>
<string>1000</string>
<key>Values</key>
<array>
<string>500</string>
<string>1000</string>
<string>2000</string>
<string>5000</string>
<string>10000</string>
</array>
<key>Key</key>
<string>offline_store_limit</string>
</dict>
<dict>
<key>FooterText</key>
<string>More images at once means faster caching, but a less responsive app. Keep this to 1 image at a time if you read while downloading. Bump it up if you want to quickly cache as many images at once and won&apos;t be using the app during the caching process.</string>
<key>Type</key>
<string>PSMultiValueSpecifier</string>
<key>Title</key>
<string>Download</string>
<key>Titles</key>
<array>
<string>1 image at a time</string>
<string>2 images at a time</string>
<string>3 images at a time</string>
<string>4 images at a time</string>
<string>5 images at a time</string>
<string>6 images at a time</string>
</array>
<key>DefaultValue</key>
<integer>1</integer>
<key>Values</key>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
<integer>4</integer>
<integer>5</integer>
<integer>6</integer>
</array>
<key>Key</key>
<string>offline_image_concurrency</string>
</dict>
<dict>
<key>Type</key>
<string>IASKButtonSpecifier</string>
<key>Title</key>
<string>Delete offline stories...</string>
<key>Key</key>
<string>offline_cache_empty_stories</string>
</dict>
</array>
<key>StringsTable</key>
<string>Root</string>