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 "FMResultSet.h"
# import "FMDatabase.h"
2020-08-27 21:26:12 -07:00
# import "NewsBlur-Swift.h"
2013-07-15 18:25:09 -07:00
@ implementation OfflineSyncUnreads
@ synthesize appDelegate ;
- ( void ) main {
2017-09-26 10:48:02 -07:00
dispatch_sync ( dispatch_get _main _queue ( ) , ^ {
self . appDelegate = [ NewsBlurAppDelegate sharedAppDelegate ] ;
} ) ;
2013-07-15 18:25:09 -07:00
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 ( ) , ^ {
2020-08-27 15:08:46 -07:00
[ self . appDelegate . feedsViewController showSyncingNotifier ] ;
2013-07-15 18:25:09 -07:00
} ) ;
2015-09-18 15:02:15 -07:00
__block NSCondition * lock = [ NSCondition new ] ;
[ lock lock ] ;
2017-04-03 16:07:01 -07:00
NSString * urlString = [ NSString stringWithFormat : @ "%@/reader/unread_story_hashes?include_timestamps=true" ,
self . appDelegate . url ] ;
AFHTTPSessionManager * manager = [ AFHTTPSessionManager manager ] ;
manager . responseSerializer = [ AFJSONResponseSerializer serializer ] ;
2017-04-03 18:52:23 -07:00
manager . completionQueue = dispatch_get _global _queue ( DISPATCH_QUEUE _PRIORITY _BACKGROUND , 0 ) ;
2017-04-03 16:07:01 -07:00
[ manager GET : urlString parameters : nil progress : nil success : ^ ( NSURLSessionDataTask * _Nonnull task , id _Nullable responseObject ) {
NSLog ( @ "Syncing stories success" ) ;
[ self storeUnreadHashes : responseObject ] ;
2015-09-18 15:02:15 -07:00
[ lock signal ] ;
2017-04-03 16:07:01 -07:00
} failure : ^ ( NSURLSessionDataTask * _Nullable task , NSError * _Nonnull error ) {
2015-09-18 15:02:15 -07:00
NSLog ( @ "Failed fetch all story hashes: %@" , error ) ;
[ lock signal ] ;
} ] ;
2017-04-03 16:07:01 -07:00
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" ) ;
2017-04-03 16:07:01 -07:00
// [ request cancel ] ;
2013-07-16 18:06:36 -07:00
return ;
}
2013-07-15 18:25:09 -07:00
2020-08-27 15:08:46 -07:00
[ self . 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" ] ;
2020-08-27 15:08:46 -07:00
[ self . appDelegate setupDatabase : db force : NO ] ;
2013-07-15 18:25:09 -07:00
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 ]
] ;
}
}
} ] ;
2020-08-27 15:08:46 -07:00
[ self . appDelegate . database inTransaction : ^ ( FMDatabase * db , BOOL * rollback ) {
2013-07-15 18:25:09 -07:00
// 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 ] ] ;
2019-10-25 20:52:51 -07:00
[ db executeUpdate : [ NSString stringWithFormat : @ "DELETE FROM text 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
} ] ;
2020-08-27 15:08:46 -07:00
self . appDelegate . totalUnfetchedStoryCount = 0 ;
self . appDelegate . remainingUnfetchedStoryCount = 0 ;
self . appDelegate . latestFetchedStoryDate = 0 ;
self . appDelegate . totalUncachedImagesCount = 0 ;
self . appDelegate . remainingUncachedImagesCount = 0 ;
2013-07-15 18:25:09 -07:00
2014-02-11 11:52:32 -08:00
// NSLog ( @ "Done syncing Unreads..." ) ;
2020-08-27 15:08:46 -07:00
[ self . appDelegate startOfflineFetchStories ] ;
2013-07-15 18:25:09 -07:00
}
@ end