Make progress on fixing adding feeds.
This commit is contained in:
parent
4118e4a2d5
commit
71e8a9131d
|
@ -15,6 +15,8 @@ extension Notification.Name {
|
|||
static let TimelineSelectionDidChange = Notification.Name("TimelineSelectionDidChangeNotification")
|
||||
|
||||
static let AppNavigationKeyPressed = Notification.Name("AppNavigationKeyPressedNotification")
|
||||
|
||||
static let UserDidAddFeed = Notification.Name("UserDidAddFeedNotification")
|
||||
}
|
||||
|
||||
extension Notification {
|
||||
|
@ -39,7 +41,8 @@ final class AppInfo {
|
|||
var articles: Set<Article>?
|
||||
var navigationKey: Int?
|
||||
var objects: [AnyObject]?
|
||||
|
||||
var feed: Feed?
|
||||
|
||||
static let appInfoKey = "appInfo"
|
||||
|
||||
var userInfo: UserInfoDictionary {
|
||||
|
|
|
@ -21,21 +21,19 @@ import Account
|
|||
// Else,
|
||||
// display error sheet.
|
||||
|
||||
let UserDidAddFeedNotification = Notification.Name("UserDidAddFeedNotification")
|
||||
let UserDidAddFeedKey = "feed"
|
||||
|
||||
class AddFeedController: AddFeedWindowControllerDelegate, FeedFinderDelegate {
|
||||
|
||||
fileprivate let hostWindow: NSWindow
|
||||
fileprivate var addFeedWindowController: AddFeedWindowController?
|
||||
fileprivate var userEnteredURL: URL?
|
||||
fileprivate var userEnteredFolder: Folder?
|
||||
fileprivate var userEnteredTitle: String?
|
||||
fileprivate var foundFeedURLString: String?
|
||||
fileprivate var titleFromFeed: String?
|
||||
fileprivate var feedFinder: FeedFinder?
|
||||
fileprivate var isFindingFeed = false
|
||||
fileprivate var bestFeedSpecifier: FeedSpecifier?
|
||||
private let hostWindow: NSWindow
|
||||
private var addFeedWindowController: AddFeedWindowController?
|
||||
private var userEnteredURL: URL?
|
||||
private var userEnteredFolder: Folder?
|
||||
private var userEnteredTitle: String?
|
||||
private var userEnteredAccount: Account?
|
||||
private var foundFeedURLString: String?
|
||||
private var titleFromFeed: String?
|
||||
private var feedFinder: FeedFinder?
|
||||
private var isFindingFeed = false
|
||||
private var bestFeedSpecifier: FeedSpecifier?
|
||||
|
||||
init(hostWindow: NSWindow) {
|
||||
|
||||
|
@ -59,16 +57,22 @@ class AddFeedController: AddFeedWindowControllerDelegate, FeedFinderDelegate {
|
|||
func addFeedWindowController(_: AddFeedWindowController, userEnteredURL url: URL, userEnteredTitle title: String?, container: Container) {
|
||||
|
||||
closeAddFeedSheet(NSApplication.ModalResponse.OK)
|
||||
//
|
||||
// let account = folder.account
|
||||
// if account.hasFeed(withURL: url.absoluteString) {
|
||||
// showAlreadySubscribedError(url.absoluteString, folder)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// userEnteredURL = url
|
||||
// userEnteredFolder = folder
|
||||
// userEnteredTitle = title
|
||||
|
||||
guard let accountAndFolderSpecifier = accountAndFolderFromContainer(container) else {
|
||||
return
|
||||
}
|
||||
let account = accountAndFolderSpecifier.account
|
||||
let folder = accountAndFolderSpecifier.folder
|
||||
|
||||
if account.hasFeed(withURL: url.absoluteString) {
|
||||
showAlreadySubscribedError(url.absoluteString)
|
||||
return
|
||||
}
|
||||
|
||||
userEnteredAccount = account
|
||||
userEnteredURL = url
|
||||
userEnteredFolder = folder
|
||||
userEnteredTitle = title
|
||||
|
||||
findFeed()
|
||||
}
|
||||
|
@ -133,6 +137,22 @@ private extension AddFeedController {
|
|||
}
|
||||
}
|
||||
|
||||
struct AccountAndFolderSpecifier {
|
||||
let account: Account
|
||||
let folder: Folder?
|
||||
}
|
||||
|
||||
func accountAndFolderFromContainer(_ container: Container) -> AccountAndFolderSpecifier? {
|
||||
|
||||
if let account = container as? Account {
|
||||
return AccountAndFolderSpecifier(account: account, folder: nil)
|
||||
}
|
||||
if let folder = container as? Folder {
|
||||
return AccountAndFolderSpecifier(account: folder.account, folder: folder)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func closeAddFeedSheet(_ returnCode: NSApplication.ModalResponse) {
|
||||
|
||||
if let sheetWindow = addFeedWindowController?.window {
|
||||
|
@ -145,29 +165,28 @@ private extension AddFeedController {
|
|||
|
||||
// Add feed if not already subscribed-to.
|
||||
|
||||
guard let folder = userEnteredFolder else {
|
||||
assertionFailure("Folder must not be nil here.")
|
||||
return
|
||||
}
|
||||
guard let account = userEnteredFolder?.account else {
|
||||
assertionFailure("Folder must have an account.")
|
||||
guard let account = userEnteredAccount else {
|
||||
assertionFailure("Expected account.")
|
||||
return
|
||||
}
|
||||
guard let feedURLString = foundFeedURLString else {
|
||||
assertionFailure("urlString must not be nil here.")
|
||||
assertionFailure("Expected feedURLString.")
|
||||
return
|
||||
}
|
||||
|
||||
if account.hasFeed(withURL: feedURLString) {
|
||||
showAlreadySubscribedError(feedURLString, folder)
|
||||
showAlreadySubscribedError(feedURLString)
|
||||
return
|
||||
}
|
||||
|
||||
if let feed = account.createFeed(with: titleFromFeed, editedName: userEnteredTitle, url: feedURLString) {
|
||||
print(feedURLString)
|
||||
if account.addFeed(feed, to: folder) {
|
||||
NotificationCenter.default.post(name: UserDidAddFeedNotification, object: self, userInfo: [UserDidAddFeedKey: feed])
|
||||
}
|
||||
guard let feed = account.createFeed(with: titleFromFeed, editedName: userEnteredTitle, url: feedURLString) else {
|
||||
return
|
||||
}
|
||||
|
||||
if account.addFeed(feed, to: userEnteredFolder) {
|
||||
let appInfo = AppInfo()
|
||||
appInfo.feed = feed
|
||||
NotificationCenter.default.post(name: .UserDidAddFeed, object: self, userInfo: appInfo.userInfo)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,7 +195,7 @@ private extension AddFeedController {
|
|||
func findFeed() {
|
||||
|
||||
guard let url = userEnteredURL else {
|
||||
assertionFailure("userEnteredURL must not be nil.")
|
||||
assertionFailure("Expected userEnteredURL.")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -188,7 +207,7 @@ private extension AddFeedController {
|
|||
|
||||
// MARK: Errors
|
||||
|
||||
func showAlreadySubscribedError(_ urlString: String, _ folder: Folder) {
|
||||
func showAlreadySubscribedError(_ urlString: String) {
|
||||
|
||||
let alert = NSAlert()
|
||||
alert.alertStyle = .informational
|
||||
|
|
|
@ -94,19 +94,19 @@ class AddFeedWindowController : NSWindowController {
|
|||
|
||||
@IBAction func addFeed(_ sender: AnyObject) {
|
||||
|
||||
// let urlString = urlTextField.stringValue
|
||||
// let normalizedURLString = (urlString as NSString).rs_normalizedURL()
|
||||
//
|
||||
// if normalizedURLString.isEmpty {
|
||||
// cancelSheet()
|
||||
// return;
|
||||
// }
|
||||
// guard let url = URL(string: normalizedURLString) else {
|
||||
// cancelSheet()
|
||||
// return
|
||||
// }
|
||||
let urlString = urlTextField.stringValue
|
||||
let normalizedURLString = (urlString as NSString).rs_normalizedURL()
|
||||
|
||||
// delegate?.addFeedWindowController(self, userEnteredURL: url, userEnteredTitle: userEnteredTitle, container: selectedContainer()!)
|
||||
if normalizedURLString.isEmpty {
|
||||
cancelSheet()
|
||||
return;
|
||||
}
|
||||
guard let url = URL(string: normalizedURLString) else {
|
||||
cancelSheet()
|
||||
return
|
||||
}
|
||||
|
||||
delegate?.addFeedWindowController(self, userEnteredURL: url, userEnteredTitle: userEnteredTitle, container: selectedContainer()!)
|
||||
}
|
||||
|
||||
@IBAction func localShowFeedList(_ sender: AnyObject) {
|
||||
|
@ -136,9 +136,9 @@ private extension AddFeedWindowController {
|
|||
}
|
||||
|
||||
|
||||
func selectedFolder() -> Folder? {
|
||||
func selectedContainer() -> Container? {
|
||||
|
||||
return folderPopupButton.selectedItem?.representedObject as? Folder
|
||||
return folderPopupButton.selectedItem?.representedObject as? Container
|
||||
}
|
||||
|
||||
func createFolderPopupMenu() -> NSMenu {
|
||||
|
|
|
@ -27,7 +27,7 @@ import Account
|
|||
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(unreadCountDidChange(_:)), name: .UnreadCountDidChange, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(containerChildrenDidChange(_:)), name: .ChildrenDidChange, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: UserDidAddFeedNotification, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(userDidAddFeed(_:)), name: .UserDidAddFeed, object: nil)
|
||||
NotificationCenter.default.addObserver(self, selector: #selector(batchUpdateDidFinish(_:)), name: .BatchUpdateDidFinish, object: nil)
|
||||
|
||||
outlineView.reloadData()
|
||||
|
@ -55,9 +55,7 @@ import Account
|
|||
|
||||
@objc dynamic func userDidAddFeed(_ note: Notification) {
|
||||
|
||||
// Find the feed and select it.
|
||||
|
||||
guard let feed = note.userInfo?[UserDidAddFeedKey] as? Feed else {
|
||||
guard let appInfo = note.appInfo, let feed = appInfo.feed else {
|
||||
return
|
||||
}
|
||||
revealAndSelectRepresentedObject(feed)
|
||||
|
|
Loading…
Reference in New Issue