diff --git a/Evergreen/MainWindow/AddFeed/AddFeedController.swift b/Evergreen/MainWindow/AddFeed/AddFeedController.swift index 8f0d9bd01..f1cf64f74 100644 --- a/Evergreen/MainWindow/AddFeed/AddFeedController.swift +++ b/Evergreen/MainWindow/AddFeed/AddFeedController.swift @@ -163,9 +163,9 @@ private extension AddFeedController { return } - if let feed = folder.createFeedWithName(titleFromFeed, editedName: userEnteredTitle, urlString: feedURLString) { + if let feed = account.createFeed(with: titleFromFeed, editedName: userEnteredTitle, url: feedURLString) { print(feedURLString) - if folder.addItem(feed) { + if account.addFeed(feed, to: folder) { NotificationCenter.default.post(name: UserDidAddFeedNotification, object: self, userInfo: [UserDidAddFeedKey: feed]) } } diff --git a/Frameworks/Account/Account.swift b/Frameworks/Account/Account.swift index 01dba74dc..40d338adb 100644 --- a/Frameworks/Account/Account.swift +++ b/Frameworks/Account/Account.swift @@ -103,22 +103,36 @@ public final class Account: DisplayNameProvider, Hashable { // Return false if it couldn’t be added. // If it already existed in that folder, return true. - return true // TODO + var didAddFeed = false + let uniquedFeed = existingFeed(with: feed.feedID) ?? feed + + if let folder = folder { + didAddFeed = folder.addFeed(uniquedFeed) + } + else { + if !topLevelObjectsContainsFeed(uniquedFeed) { + topLevelObjects += [uniquedFeed] + } + didAddFeed = true + } + + updateFeedIDDictionary() + return didAddFeed // TODO } - public func createFeed(with name: String, userEnteredName: String, url: String) -> Feed { + public func createFeed(with name: String, editedName: String, url: String) -> Feed { // For syncing, this may need to be an async method with a callback, // since it will likely need to call the server. if let feed = existingFeed(withURL: url) { - feed.editedName = userEnteredName + feed.editedName = editedName return feed } let feed = Feed(accountID: accountID, url: url, feedID: url) feed.name = name - feed.editedName = userEnteredName + feed.editedName = editedName return feed } @@ -181,7 +195,7 @@ extension Account { } } -// Mark: - FeedIDDictionary +// MARK: - Private private extension Account { @@ -193,6 +207,18 @@ private extension Account { } feedIDDictionary = d } + + func topLevelObjectsContainsFeed(_ feed: Feed) -> Bool { + + return topLevelObjects.contains(where: { (object) -> Bool in + if let oneFeed = object as? Feed { + if oneFeed.feedID == feed.feedID { + return true + } + } + return false + }) + } } // MARK: - OPMLRepresentable diff --git a/Frameworks/Account/Folder.swift b/Frameworks/Account/Folder.swift index e2272e245..8de1bb461 100644 --- a/Frameworks/Account/Folder.swift +++ b/Frameworks/Account/Folder.swift @@ -95,6 +95,37 @@ public final class Folder: DisplayNameProvider, UnreadCountProvider { return d } } + + // MARK: Feeds + + func addFeed(_ feed: Feed) -> Bool { + + // The feed has been uniqued at this point. + // Return true in the case where the feed is already a child. + + if childrenContainsFeed(feed) { + return true + } + children += [feed] + return true + } +} + +// MARK: - Private + +private extension Folder { + + func childrenContainsFeed(_ feed: Feed) -> Bool { + + return children.contains(where: { (object) -> Bool in + if let oneFeed = object as? Feed { + if oneFeed.feedID == feed.feedID { + return true + } + } + return false + }) + } } // MARK: - Disk