Handle OPML file import without drag & drop

This commit is contained in:
relikd
2019-07-28 22:43:18 +02:00
parent cd68febd88
commit bdc6d45a54
6 changed files with 41 additions and 7 deletions

View File

@@ -25,4 +25,5 @@
@interface SettingsFeeds (DragDrop) <NSOutlineViewDataSource, NSFilePromiseProviderDelegate, NSPasteboardTypeOwner, OpmlFileImportDelegate, OpmlFileExportDelegate>
- (void)prepareOutlineViewForDragDrop:(NSOutlineView*)outline;
- (void)importOpmlFiles:(NSArray<NSURL*>*)files;
@end

View File

@@ -99,7 +99,7 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder";
} else {
// File import
NSArray<NSURL*> *files = [info.draggingPasteboard readObjectsForClasses:@[NSURL.class] options:@{ NSPasteboardURLReadingContentsConformToTypesKey: @[UTI_OPML] }];
[[OpmlFileImport withDelegate:self] importFiles:files];
[self importOpmlFiles:files];
}
return YES;
}
@@ -108,6 +108,11 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder";
#pragma mark - OPML File Import
/// Helper method is also called from Application Delegate
- (void)importOpmlFiles:(NSArray<NSURL*>*)files {
[[OpmlFileImport withDelegate:self] importFiles:files];
}
/// Filter out file urls that are not opml files
- (void)outlineView:(NSOutlineView *)outlineView updateDraggingItemsForDrag:(id <NSDraggingInfo>)info {
if ([info.draggingPasteboard canReadItemWithDataConformingToTypes:@[(NSPasteboardType)kUTTypeFileURL]]) {
@@ -134,7 +139,7 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder";
/// OPML import (did end). Save changes, select newly inserted, and perform web request.
- (void)opmlFileImportDidEnd:(NSManagedObjectContext*)moc {
if (!moc.hasChanges) { // exit early, dont need to create empty arrays
if (moc.undoManager.groupingLevel == 1 && !moc.hasChanges) { // exit early, dont need to create empty arrays
[self endCoreDataChangeUndoEmpty:YES forceUndo:YES];
return;
}
@@ -153,7 +158,8 @@ const NSPasteboardType dragReorder = @"de.relikd.baRSS.drag-reorder";
}
// Persist state, because on crash we have at least inserted items (without articles & icons)
[StoreCoordinator saveContext:moc andParent:YES];
[self.dataStore setSelectionIndexPaths:selection];
if (selection.count > 0)
[self.dataStore setSelectionIndexPaths:selection];
[FeedDownload batchDownloadFeeds:feedsList favicons:YES showErrorAlert:YES finally:^{
[self endCoreDataChangeUndoEmpty:NO forceUndo:NO];
[self someDatesChangedScheduleUpdateTimer];

View File

@@ -22,6 +22,9 @@
#import <Cocoa/Cocoa.h>
@class SettingsFeeds;
@interface Preferences : NSWindow <NSWindowDelegate>
+ (instancetype)window;
- (SettingsFeeds*)selectFeedsTab;
@end

View File

@@ -49,9 +49,7 @@
TabItem(NSImageNameInfo, NSLocalizedString(@"About", nil), [SettingsAbout class]),
];
NSInteger index = [[NSUserDefaults standardUserDefaults] integerForKey:@"preferencesTab"];
if (index > 0 || (NSUInteger)index < self.tabViewItems.count)
self.selectedTabViewItemIndex = index;
[self switchToTab:[[NSUserDefaults standardUserDefaults] integerForKey:@"preferencesTab"]];
}
return self;
}
@@ -64,6 +62,12 @@ NS_INLINE NSTabViewItem* TabItem(NSImageName imageName, NSString *text, Class cl
return item;
}
/// Safely set selected index without out of bounds exception
- (void)switchToTab:(NSInteger)index {
if (index > 0 || (NSUInteger)index < self.tabViewItems.count)
self.selectedTabViewItemIndex = index;
}
/// Delegate method, store last selected tab to user preferences
- (void)tabView:(NSTabView*)tabView didSelectTabViewItem:(nullable NSTabViewItem*)tabViewItem {
[super tabView:tabView didSelectTabViewItem:tabViewItem];
@@ -96,6 +100,12 @@ NS_INLINE NSTabViewItem* TabItem(NSImageName imageName, NSString *text, Class cl
return w;
}
- (SettingsFeeds*)selectFeedsTab {
PrefTabs *pref = (PrefTabs*)self.contentViewController;
[pref switchToTab:1];
return (SettingsFeeds*)[pref.tabViewItems[1] viewController];
}
- (void)windowWillClose:(NSNotification *)notification {
[[NSUserDefaults standardUserDefaults] setObject:self.stringWithSavedFrame forKey:@"prefWindow"];
}