Add code for fetching articles for the timeline. Doesn’t actually work yet, though, for some reason.

This commit is contained in:
Brent Simmons 2017-10-08 18:58:15 -07:00
parent 4b662efe15
commit 8aa3746cb8
5 changed files with 71 additions and 59 deletions

View File

@ -367,64 +367,46 @@ class TimelineViewController: NSViewController, NSTableViewDelegate, NSTableView
private func articleComparator(_ article1: Article, article2: Article) -> Bool { private func articleComparator(_ article1: Article, article2: Article) -> Bool {
return article1.logicalDatePublished.compare(article2.logicalDatePublished) == .orderedDescending return article1.logicalDatePublished < article2.logicalDatePublished
} }
private func articlesSortedByDate(_ articles: Set<Article>) -> [Article] {
return Array(articles).sorted(by: articleComparator)
}
// MARK: Fetching Articles // MARK: Fetching Articles
private func emptyTheTimeline() {
if !articles.isEmpty {
articles = [Article]()
}
}
private func fetchArticles() { private func fetchArticles() {
// guard let representedObjects = representedObjects else { guard let representedObjects = representedObjects else {
// if !articles.isEmpty { emptyTheTimeline()
// articles = [Article]() return
// } }
// return
// } var fetchedArticles = Set<Article>()
//
// var accountsDictionary = [String: [AnyObject]]() for object in representedObjects {
//
// func addToAccountArray(accountID: String, object: AnyObject) { if let feed = object as? Feed {
// fetchedArticles.formUnion(feed.fetchArticles())
// if let accountArray = accountsDictionary[accountID] { }
// if !accountArray.contains(where: { $0 === object }) { else if let folder = object as? Folder {
// accountsDictionary[accountID] = accountArray + [object] fetchedArticles.formUnion(folder.fetchArticles())
// } }
// } }
// else {
// accountsDictionary[accountID] = [object] let sortedArticles = articlesSortedByDate(fetchedArticles)
// } if articles != sortedArticles {
// } articles = sortedArticles
// }
// for oneObject in representedObjects {
//
// if let oneFeed = oneObject as? Feed {
// addToAccountArray(accountID: oneFeed.account.accountID, object: oneFeed)
// }
// else if let oneFolder = oneObject as? Folder, let accountID = oneFolder.account?.accountID {
// addToAccountArray(accountID: accountID, object: oneFolder)
// }
// }
//
// var fetchedArticles = [Article]()
// for (accountID, objects) in accountsDictionary {
//
// guard let oneAccount = accountWithID(accountID) else {
// continue
// }
//
// let oneFetchedArticles = oneAccount.fetchArticles(for: objects)
// for oneFetchedArticle in oneFetchedArticles {
// if !fetchedArticles.contains(where: { $0 === oneFetchedArticle }) {
// fetchedArticles += [oneFetchedArticle]
// }
// }
// }
//
// fetchedArticles.sort(by: articleComparator)
//
// if articles != fetchedArticles {
// articles = fetchedArticles
// }
} }
// MARK: Cell Configuring // MARK: Cell Configuring

View File

@ -242,6 +242,16 @@ public final class Account: DisplayNameProvider, Container, Hashable {
} }
} }
public func fetchArticles(for feed: Feed) -> Set<Article> {
return database.fetchArticles(for: feed)
}
public func fetchArticles(folder: Folder) -> Set<Article> {
return database.fetchUnreadArticles(for: folder.flattenedFeeds())
}
// MARK: - Notifications // MARK: - Notifications
@objc func downloadProgressDidChange(_ note: Notification) { @objc func downloadProgressDidChange(_ note: Notification) {

View File

@ -208,16 +208,20 @@ private struct AccountSpecifier {
init?(folderPath: String) { init?(folderPath: String) {
self.folderPath = folderPath if !FileManager.default.rs_fileIsFolder(folderPath) {
self.folderName = NSString(string: folderPath).lastPathComponent return nil
}
let nameComponents = self.folderName.components(separatedBy: "-") let name = NSString(string: folderPath).lastPathComponent
let satisfyCompilerFolderName = self.folderName if name.hasPrefix(".") {
assert(nameComponents.count == 2, "Cant determine account info from \(satisfyCompilerFolderName)") return nil
}
let nameComponents = name.components(separatedBy: "-")
if nameComponents.count != 2 { if nameComponents.count != 2 {
return nil return nil
} }
self.folderPath = folderPath
self.folderName = name
self.type = nameComponents[0] self.type = nameComponents[0]
self.identifier = nameComponents[1] self.identifier = nameComponents[1]

View File

@ -11,11 +11,20 @@ import Data
public extension Feed { public extension Feed {
var account: Account? { public var account: Account? {
get { get {
return AccountManager.shared.existingAccount(with: accountID) return AccountManager.shared.existingAccount(with: accountID)
} }
} }
public func fetchArticles() -> Set<Article> {
guard let account = account else {
assertionFailure("Expected feed.account.")
return Set<Article>()
}
return account.fetchArticles(for: self)
}
} }
public extension Article { public extension Article {

View File

@ -16,6 +16,13 @@ public final class Folder: DisplayNameProvider, Container, UnreadCountProvider {
var name: String? var name: String?
static let untitledName = NSLocalizedString("Untitled ƒ", comment: "Folder name") static let untitledName = NSLocalizedString("Untitled ƒ", comment: "Folder name")
// MARK: - Fetching Articles
public func fetchArticles() -> Set<Article> {
return account.fetchArticles(folder: self)
}
// MARK: - DisplayNameProvider // MARK: - DisplayNameProvider
public var nameForDisplay: String { public var nameForDisplay: String {