Fix auto parse url if app is closed

This commit is contained in:
relikd
2019-02-11 20:28:03 +01:00
parent 9bb1b23d8c
commit 5ed99a6e51
3 changed files with 24 additions and 16 deletions

View File

@@ -60,12 +60,14 @@
url = [url substringFromIndex:5]; url = [url substringFromIndex:5];
if ([url hasPrefix:@"//"]) if ([url hasPrefix:@"//"])
url = [url substringFromIndex:2]; url = [url substringFromIndex:2];
[FeedDownload autoDownloadAndParseURL:url]; [FeedDownload autoDownloadAndParseURL:url successBlock:^{
[self reopenPreferencesIfOpen];
}];
} }
} }
#pragma mark - Handle Menu Interaction #pragma mark - App Preferences
/// Called whenever the user activates the preferences (either through menu click or hotkey). /// Called whenever the user activates the preferences (either through menu click or hotkey).
@@ -87,6 +89,16 @@
[FeedDownload scheduleUpdateForUpcomingFeeds]; [FeedDownload scheduleUpdateForUpcomingFeeds];
} }
/// Close previous preferences window and re-open at the same position (will drop undo manager stack!)
- (void)reopenPreferencesIfOpen {
if (self.prefWindow) {
CGPoint screenPoint = self.prefWindow.window.frame.origin;
[self.prefWindow close];
[self openPreferences];
[self.prefWindow.window setFrameOrigin:screenPoint];
}
}
#pragma mark - Core Data stack #pragma mark - Core Data stack
@@ -109,10 +121,6 @@
return _persistentContainer; return _persistentContainer;
} }
#pragma mark - Core Data Saving and Undo support
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
// Save changes in the application's managed object context before the application terminates. // Save changes in the application's managed object context before the application terminates.
NSManagedObjectContext *context = self.persistentContainer.viewContext; NSManagedObjectContext *context = self.persistentContainer.viewContext;

View File

@@ -39,7 +39,7 @@
+ (void)forceUpdateAllFeeds; + (void)forceUpdateAllFeeds;
// Downloading // Downloading
+ (void)newFeed:(NSString *)urlStr askUser:(nonnull NSString*(^)(RSHTMLMetadata *meta))askUser block:(nonnull void(^)(RSParsedFeed *parsed, NSError *error, NSHTTPURLResponse *response))block; + (void)newFeed:(NSString *)urlStr askUser:(nonnull NSString*(^)(RSHTMLMetadata *meta))askUser block:(nonnull void(^)(RSParsedFeed *parsed, NSError *error, NSHTTPURLResponse *response))block;
+ (void)autoDownloadAndParseURL:(NSString*)urlStr; + (void)autoDownloadAndParseURL:(NSString*)urlStr successBlock:(nullable os_block_t)block;
+ (void)batchDownloadFeeds:(NSArray<Feed*> *)list favicons:(BOOL)fav showErrorAlert:(BOOL)alert finally:(nullable os_block_t)block; + (void)batchDownloadFeeds:(NSArray<Feed*> *)list favicons:(BOOL)fav showErrorAlert:(BOOL)alert finally:(nullable os_block_t)block;
// Favicon image download // Favicon image download
+ (void)downloadFavicon:(NSString*)urlStr finished:(void(^)(NSImage * _Nullable img))block; + (void)downloadFavicon:(NSString*)urlStr finished:(void(^)(NSImage * _Nullable img))block;

View File

@@ -123,17 +123,17 @@ static BOOL _nextUpdateIsForced = NO;
Called when schedule timer runs out (earliest @c .schedule date). Or if forced by user request. Called when schedule timer runs out (earliest @c .schedule date). Or if forced by user request.
*/ */
+ (void)updateTimerCallback { + (void)updateTimerCallback {
if (![self allowNetworkConnection])
return;
NSLog(@"fired"); NSLog(@"fired");
BOOL updateAll = _nextUpdateIsForced; BOOL updateAll = _nextUpdateIsForced;
_nextUpdateIsForced = NO; _nextUpdateIsForced = NO;
NSManagedObjectContext *moc = [StoreCoordinator createChildContext]; NSManagedObjectContext *moc = [StoreCoordinator createChildContext];
NSArray<Feed*> *list = [StoreCoordinator getListOfFeedsThatNeedUpdate:updateAll inContext:moc]; NSArray<Feed*> *list = [StoreCoordinator getListOfFeedsThatNeedUpdate:updateAll inContext:moc];
//NSAssert(list.count > 0, @"ERROR: Something went wrong, timer fired too early."); //NSAssert(list.count > 0, @"ERROR: Something went wrong, timer fired too early.");
if (![self allowNetworkConnection]) {
[moc reset];
return;
}
[self batchDownloadFeeds:list favicons:updateAll showErrorAlert:NO finally:^{ [self batchDownloadFeeds:list favicons:updateAll showErrorAlert:NO finally:^{
[StoreCoordinator saveContext:moc andParent:YES]; // save parents too ... [StoreCoordinator saveContext:moc andParent:YES]; // save parents too ...
[moc reset]; [moc reset];
@@ -279,10 +279,6 @@ static BOOL _nextUpdateIsForced = NO;
@param block Parameter @c success is only @c YES if download was successful or if status code is 304 (not modified). @param block Parameter @c success is only @c YES if download was successful or if status code is 304 (not modified).
*/ */
+ (void)backgroundUpdateFeed:(Feed*)feed showErrorAlert:(BOOL)alert finally:(nullable void(^)(BOOL success))block { + (void)backgroundUpdateFeed:(Feed*)feed showErrorAlert:(BOOL)alert finally:(nullable void(^)(BOOL success))block {
if (![self allowNetworkConnection]) {
if (block) block(NO);
return;
}
NSManagedObjectID *oid = feed.objectID; NSManagedObjectID *oid = feed.objectID;
NSManagedObjectContext *moc = feed.managedObjectContext; NSManagedObjectContext *moc = feed.managedObjectContext;
NSURLRequest *req = [self newRequest:feed.meta ignoreCache:(feed.articles.count == 0)]; NSURLRequest *req = [self newRequest:feed.meta ignoreCache:(feed.articles.count == 0)];
@@ -320,7 +316,7 @@ static BOOL _nextUpdateIsForced = NO;
Creates new @c FeedGroup, @c Feed, @c FeedMeta and @c FeedArticle instances and saves them to the persistent store. Creates new @c FeedGroup, @c Feed, @c FeedMeta and @c FeedArticle instances and saves them to the persistent store.
Update duration is set to the default of 30 minutes. Update duration is set to the default of 30 minutes.
*/ */
+ (void)autoDownloadAndParseURL:(NSString*)url { + (void)autoDownloadAndParseURL:(NSString*)url successBlock:(nullable os_block_t)block {
NSManagedObjectContext *moc = [StoreCoordinator createChildContext]; NSManagedObjectContext *moc = [StoreCoordinator createChildContext];
Feed *f = [Feed appendToRootWithDefaultIntervalInContext:moc]; Feed *f = [Feed appendToRootWithDefaultIntervalInContext:moc];
f.meta.url = url; f.meta.url = url;
@@ -330,6 +326,10 @@ static BOOL _nextUpdateIsForced = NO;
} }
[StoreCoordinator saveContext:moc andParent:YES]; [StoreCoordinator saveContext:moc andParent:YES];
[moc reset]; [moc reset];
if (successful) {
[self scheduleUpdateForUpcomingFeeds];
if (block) block();
}
}]; }];
} }