2013-07-15 18:25:09 -07:00
//
// OfflineSyncUnreads . m
// NewsBlur
//
// Created by Samuel Clay on 7 / 15 / 13.
// Copyright ( c ) 2013 NewsBlur . All rights reserved .
//
# import "OfflineSyncUnreads.h"
# import "NewsBlurAppDelegate.h"
# import "NewsBlurViewController.h"
# import "FMResultSet.h"
# import "FMDatabase.h"
# import "AFJSONRequestOperation.h"
@ implementation OfflineSyncUnreads
@ synthesize appDelegate ;
2013-07-16 18:06:36 -07:00
@ synthesize request ;
2013-07-15 18:25:09 -07:00
- ( void ) main {
appDelegate = [ NewsBlurAppDelegate sharedAppDelegate ] ;
NSLog ( @ "Syncing Unreads..." ) ;
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ appDelegate . feedsViewController showSyncingNotifier ] ;
} ) ;
2013-07-16 18:06:36 -07:00
NSURL * url = [ NSURL URLWithString : [ NSString
stringWithFormat : @ "%@/reader/unread_story_hashes?include_timestamps=true" ,
2013-07-15 18:25:09 -07:00
NEWSBLUR_URL ] ] ;
2013-07-16 18:06:36 -07:00
request = [ AFJSONRequestOperation
JSONRequestOperationWithRequest : [ NSURLRequest requestWithURL : url ]
success : ^ ( NSURLRequest * request , NSHTTPURLResponse * response , id JSON ) {
[ self storeUnreadHashes : JSON ] ;
} failure : ^ ( NSURLRequest * request , NSHTTPURLResponse * response , NSError * error , id JSON ) {
NSLog ( @ "Failed fetch all story hashes." ) ;
} ] ;
2013-07-15 18:25:09 -07:00
request . successCallbackQueue = dispatch_get _global _queue ( DISPATCH_QUEUE _PRIORITY _DEFAULT ,
( unsigned long ) NULL ) ;
[ request start ] ;
[ request waitUntilFinished ] ;
}
- ( void ) storeUnreadHashes : ( NSDictionary * ) results {
2013-07-16 18:06:36 -07:00
if ( self . isCancelled ) {
NSLog ( @ "Canceled storing unread hashes" ) ;
[ request cancel ] ;
return ;
}
2013-07-15 18:25:09 -07:00
[ appDelegate . database inTransaction : ^ ( FMDatabase * db , BOOL * rollback ) {
NSLog ( @ "Storing unread story hashes..." ) ;
[ db executeUpdate : @ "DROP TABLE unread_hashes" ] ;
[ appDelegate setupDatabase : db ] ;
NSDictionary * hashes = [ results objectForKey : @ "unread_feed_story_hashes" ] ;
for ( NSString * feed in [ hashes allKeys ] ) {
NSArray * story_hashes = [ hashes objectForKey : feed ] ;
for ( NSArray * story_hash _tuple in story_hashes ) {
[ db executeUpdate : @ "INSERT into unread_hashes"
"(story_feed_id, story_hash, story_timestamp) VALUES "
"(?, ?, ?)" ,
feed ,
[ story_hash _tuple objectAtIndex : 0 ] ,
[ story_hash _tuple objectAtIndex : 1 ]
] ;
}
}
} ] ;
[ appDelegate . database inTransaction : ^ ( FMDatabase * db , BOOL * rollback ) {
// Once all unread hashes are in , only keep under preference for offline limit
NSInteger offlineLimit = [ [ NSUserDefaults standardUserDefaults ] integerForKey : @ "offline_store_limit" ] ;
NSString * order ;
NSString * orderComp ;
if ( [ [ [ NSUserDefaults standardUserDefaults ] objectForKey : @ "default_order" ] isEqualToString : @ "oldest" ] ) {
order = @ "ASC" ;
orderComp = @ ">" ;
} else {
order = @ "DESC" ;
orderComp = @ "<" ;
}
2013-09-25 17:43:00 -07:00
FMResultSet * cursor = [ db executeQuery : [ NSString stringWithFormat : @ "SELECT story_timestamp FROM unread_hashes ORDER BY story_timestamp %@ LIMIT 1 OFFSET %ld" , order , ( long ) offlineLimit ] ] ;
2013-07-15 18:25:09 -07:00
int offlineLimitTimestamp = 0 ;
while ( [ cursor next ] ) {
offlineLimitTimestamp = [ cursor intForColumn : @ "story_timestamp" ] ;
break ;
}
2013-09-25 17:43:00 -07:00
NSLog ( @ "Deleting stories over limit: %ld - %d" , ( long ) offlineLimit , offlineLimitTimestamp ) ;
2013-07-15 18:25:09 -07:00
[ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM unread_hashes WHERE story_timestamp %@ %d" , orderComp , offlineLimitTimestamp ] ] ;
2013-07-31 15:23:00 -07:00
[ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM stories WHERE story_timestamp %@ %d" , orderComp , offlineLimitTimestamp ] ] ;
2013-07-15 18:25:09 -07:00
} ] ;
appDelegate . totalUnfetchedStoryCount = 0 ;
appDelegate . remainingUnfetchedStoryCount = 0 ;
appDelegate . latestFetchedStoryDate = 0 ;
appDelegate . totalUncachedImagesCount = 0 ;
appDelegate . remainingUncachedImagesCount = 0 ;
NSLog ( @ "Done syncing Unreads..." ) ;
2013-07-16 18:06:36 -07:00
[ appDelegate startOfflineFetchStories ] ;
2013-07-15 18:25:09 -07:00
}
@ end