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"
2015-09-18 15:02:15 -07:00
# import "AFHTTPRequestOperation.h"
2013-07-15 18:25:09 -07:00
@ 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 ] ;
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Syncing Unreads..." ) ;
2013-07-15 18:25:09 -07:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
[ appDelegate . feedsViewController showSyncingNotifier ] ;
} ) ;
2015-09-18 15:02:15 -07:00
__block NSCondition * lock = [ NSCondition new ] ;
__weak __typeof ( & * self ) weakSelf = self ;
[ lock lock ] ;
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 ] ] ;
2015-09-18 15:02:15 -07:00
request = [ [ AFHTTPRequestOperation alloc ] initWithRequest : [ NSURLRequest requestWithURL : url ] ] ;
[ request setResponseSerializer : [ AFJSONResponseSerializer serializer ] ] ;
[ request setCompletionBlockWithSuccess : ^ ( AFHTTPRequestOperation * _Nonnull operation , id _Nonnull responseObject ) {
__strong __typeof ( & * weakSelf ) strongSelf = weakSelf ;
NSLog ( @ "Syncing stories success: %@-%@" , weakSelf , strongSelf ) ;
if ( ! strongSelf ) return ;
[ strongSelf storeUnreadHashes : responseObject ] ;
[ lock signal ] ;
} failure : ^ ( AFHTTPRequestOperation * _Nonnull operation , NSError * _Nonnull error ) {
NSLog ( @ "Failed fetch all story hashes: %@" , error ) ;
[ lock signal ] ;
} ] ;
[ request setCompletionQueue : dispatch_get _global _queue ( DISPATCH_QUEUE _PRIORITY _DEFAULT , ( unsigned long ) NULL ) ] ;
2013-07-15 18:25:09 -07:00
[ request start ] ;
2015-09-18 15:02:15 -07:00
[ lock waitUntilDate : [ NSDate dateWithTimeIntervalSinceNow : 30 ] ] ;
[ lock unlock ] ;
NSLog ( @ "Finished syncing stories" ) ;
2013-07-15 18:25:09 -07:00
}
- ( void ) storeUnreadHashes : ( NSDictionary * ) results {
2013-07-16 18:06:36 -07:00
if ( self . isCancelled ) {
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Canceled storing unread hashes" ) ;
2013-07-16 18:06:36 -07:00
[ request cancel ] ;
return ;
}
2013-07-15 18:25:09 -07:00
[ appDelegate . database inTransaction : ^ ( FMDatabase * db , BOOL * rollback ) {
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Storing unread story hashes..." ) ;
2013-07-15 18:25:09 -07:00
[ 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
2013-10-03 18:07:39 -07:00
NSInteger offlineLimit = [ [ NSUserDefaults standardUserDefaults ]
integerForKey : @ "offline_store_limit" ] ;
2013-07-15 18:25:09 -07:00
NSString * order ;
NSString * orderComp ;
2013-10-03 18:07:39 -07:00
if ( [ [ [ NSUserDefaults standardUserDefaults ] objectForKey : @ "default_order" ]
isEqualToString : @ "oldest" ] ) {
2013-07-15 18:25:09 -07:00
order = @ "ASC" ;
orderComp = @ ">" ;
} else {
order = @ "DESC" ;
orderComp = @ "<" ;
}
2013-10-03 18:07:39 -07:00
NSString * lastStorySql = [ NSString stringWithFormat :
@ "SELECT story_timestamp FROM unread_hashes "
"ORDER BY story_timestamp %@ LIMIT 1 OFFSET %ld" ,
order , ( long ) offlineLimit ] ;
FMResultSet * cursor = [ db executeQuery : lastStorySql ] ;
2013-07-15 18:25:09 -07:00
int offlineLimitTimestamp = 0 ;
while ( [ cursor next ] ) {
offlineLimitTimestamp = [ cursor intForColumn : @ "story_timestamp" ] ;
break ;
}
2013-10-03 18:07:39 -07:00
[ cursor close ] ;
if ( offlineLimitTimestamp ) {
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Deleting stories over limit: %ld - %d" , ( long ) offlineLimit , offlineLimitTimestamp ) ;
2013-10-03 18:07:39 -07:00
[ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM unread_hashes WHERE story_timestamp %@ %d" , orderComp , offlineLimitTimestamp ] ] ;
[ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM stories WHERE story_timestamp %@ %d" , orderComp , offlineLimitTimestamp ] ] ;
2015-09-17 13:15:10 -07:00
// [ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM story_scrolls WHERE story_timestamp %@ %d" , orderComp , offlineLimitTimestamp ] ] ; // Don ' t cleanup story scrolls just yet
2013-10-03 18:07:39 -07:00
}
2013-07-15 18:25:09 -07:00
} ] ;
appDelegate . totalUnfetchedStoryCount = 0 ;
appDelegate . remainingUnfetchedStoryCount = 0 ;
appDelegate . latestFetchedStoryDate = 0 ;
appDelegate . totalUncachedImagesCount = 0 ;
appDelegate . remainingUncachedImagesCount = 0 ;
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Done syncing Unreads..." ) ;
2013-07-16 18:06:36 -07:00
[ appDelegate startOfflineFetchStories ] ;
2013-07-15 18:25:09 -07:00
}
@ end