Start working of moving from stubArticles to article dictionaries.
This commit is contained in:
parent
8767aa3178
commit
f0eea49179
|
@ -162,28 +162,89 @@ private extension ArticlesTable {
|
||||||
// Then fetch the related objects, given the set of articleIDs.
|
// Then fetch the related objects, given the set of articleIDs.
|
||||||
// Then create set of Articles *with* related objects and return it.
|
// Then create set of Articles *with* related objects and return it.
|
||||||
|
|
||||||
let stubArticles = makeStubArticles(with: resultSet)
|
let articleDictionaries = makeArticleDictionaries(with: resultSet)
|
||||||
if stubArticles.isEmpty {
|
if articleDictionaries.isEmpty {
|
||||||
return stubArticles
|
return Set<Article>()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch related objects.
|
// Fetch related objects.
|
||||||
|
|
||||||
let articleIDs = stubArticles.articleIDs()
|
let articleIDs = articleDictionaries.map { $0[DatabaseKey.articleID] }
|
||||||
let authorsMap = authorsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
let authorsMap = authorsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
||||||
let attachmentsMap = attachmentsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
let attachmentsMap = attachmentsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
||||||
let tagsMap = tagsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
let tagsMap = tagsLookupTable.fetchRelatedObjects(for: articleIDs, in: database)
|
||||||
|
|
||||||
if authorsMap == nil && attachmentsMap == nil && tagsMap == nil {
|
// TODO: get statusesDictionary from statusesTable
|
||||||
return stubArticles
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create articles with related objects.
|
// Create articles with related objects.
|
||||||
|
|
||||||
let articles = Set(stubArticles.map { articleWithAttachedRelatedObjects($0, authorsMap, attachmentsMap, tagsMap) })
|
let articles = Set(articleDictionaries.flatMap { articleWithDictionary($0, authorsMap, attachmentsMap, tagsMap) })
|
||||||
return articles
|
return articles
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func articleWithDictionary(_ articleDictionary: [String: Any], _ authorsMap: RelatedObjectsMap?, _ attachmentsMap: RelatedObjectsMap?, _ tagsMap: RelatedObjectsMap?) -> Article? {
|
||||||
|
|
||||||
|
let articleID = articleDictionary[DatabaseKey.articleID]!
|
||||||
|
let status = articleDictionary[statusKey]!
|
||||||
|
let authors = authorsMap?.authors(for: articleID)
|
||||||
|
let attachments = attachmentsMap?.attachments(for: articleID)
|
||||||
|
let tags = tagsMap?.tags(for: articleID)
|
||||||
|
|
||||||
|
return Author(dictionary: articleDictionary, status: status, accountID: accountID, authors: authors, attachments: attachments, tags: tags)
|
||||||
|
}
|
||||||
|
|
||||||
|
func makeArticleDictionaries(with resultSet: FMResultSet) -> Set<[String: Any]> {
|
||||||
|
|
||||||
|
let dictionaries = resultSet.mapToSet{ (row) -> [String: Any]? in
|
||||||
|
|
||||||
|
// The resultSet is a result of a JOIN query with the statuses table,
|
||||||
|
// so we can get the statuses at the same time and avoid additional database lookups.
|
||||||
|
|
||||||
|
guard let status = statusesTable.statusWithRow(resultSet) else {
|
||||||
|
assertionFailure("Expected status.")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let let articleID = row.string(forColumn: DatabaseKey.articleID) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard let feedID = row.string(forColumn: DatabaseKey.feedID) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
guard let uniqueID = row.string(forColumn: DatabaseKey.uniqueID) else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var d = [String: Any]()
|
||||||
|
|
||||||
|
d[statusKey] = status
|
||||||
|
d[DatabaseKey.articleID] = articleID
|
||||||
|
d[DatabaseKey.feedID] = feedID
|
||||||
|
d[DatabaseKey.uniqueID] = uniqueID
|
||||||
|
|
||||||
|
if let title = row.string(forColumn: DatabaseKey.title) {
|
||||||
|
d[DatabaseKey.title] = title
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
// let contentHTML = row.string(forColumn: DatabaseKey.contentHTML)
|
||||||
|
// let contentText = row.string(forColumn: DatabaseKey.contentText)
|
||||||
|
// let url = row.string(forColumn: DatabaseKey.url)
|
||||||
|
// let externalURL = row.string(forColumn: DatabaseKey.externalURL)
|
||||||
|
// let summary = row.string(forColumn: DatabaseKey.summary)
|
||||||
|
// let imageURL = row.string(forColumn: DatabaseKey.imageURL)
|
||||||
|
// let bannerImageURL = row.string(forColumn: DatabaseKey.bannerImageURL)
|
||||||
|
// let datePublished = row.date(forColumn: DatabaseKey.datePublished)
|
||||||
|
// let dateModified = row.date(forColumn: DatabaseKey.dateModified)
|
||||||
|
// let accountInfo: AccountInfo? = nil // TODO
|
||||||
|
//
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
resultSet.close()
|
||||||
|
|
||||||
|
return dictionaries
|
||||||
|
}
|
||||||
|
|
||||||
func makeStubArticles(with resultSet: FMResultSet) -> Set<Article> {
|
func makeStubArticles(with resultSet: FMResultSet) -> Set<Article> {
|
||||||
|
|
||||||
var stubArticles = Set<Article>()
|
var stubArticles = Set<Article>()
|
||||||
|
|
|
@ -38,6 +38,27 @@ extension Article {
|
||||||
self.init(accountID: accountID, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo, status: status)
|
self.init(accountID: accountID, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo, status: status)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init?(dictionary: [String: Any], accountID: String, status: ArticleStatus, authors: Set<Author>?, attachments: Set<Attachment>?, tags: Set<String>?) {
|
||||||
|
|
||||||
|
guard let articleID = dictionary[DatabaseKey.articleID], let feedID = dictionary[DatabaseKey.feedID], let uniqueID = dictionary[DatabaseKey.uniqueID] else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
let title = dictionary[DatabaseKey.title] as? String
|
||||||
|
let contentHTML = dictionary[DatabaseKey.contentHTML] as? String
|
||||||
|
let contentText = dictionary[DatabaseKey.contentText] as? String
|
||||||
|
let url = dictionary[DatabaseKey.url] as? String
|
||||||
|
let externalURL = dictionary[DatabaseKey.externalURL] as? String
|
||||||
|
let summary = dictionary[DatabaseKey.summary] as? String
|
||||||
|
let imageURL = dictionary[DatabaseKey.imageURL] as? String
|
||||||
|
let bannerImageURL = dictionary[DatabaseKey.bannerImageURL] as? String
|
||||||
|
let datePublished = dictionary[DatabaseKey.datePublished] as? Date
|
||||||
|
let dateModified = dictionary[DatabaseKey.dateModified]? as? Date
|
||||||
|
let accountInfo: AccountInfo? = nil // TODO
|
||||||
|
|
||||||
|
self.init(accountID: accountID, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo, status: status)
|
||||||
|
}
|
||||||
|
|
||||||
init(parsedItem: ParsedItem, accountID: String, feedID: String, status: ArticleStatus) {
|
init(parsedItem: ParsedItem, accountID: String, feedID: String, status: ArticleStatus) {
|
||||||
|
|
||||||
let authors = Author.authorsWithParsedAuthors(parsedItem.authors)
|
let authors = Author.authorsWithParsedAuthors(parsedItem.authors)
|
||||||
|
@ -46,15 +67,15 @@ extension Article {
|
||||||
self.init(accountID: accountID, articleID: parsedItem.syncServiceID, feedID: feedID, uniqueID: parsedItem.uniqueID, title: parsedItem.title, contentHTML: parsedItem.contentHTML, contentText: parsedItem.contentText, url: parsedItem.url, externalURL: parsedItem.externalURL, summary: parsedItem.summary, imageURL: parsedItem.imageURL, bannerImageURL: parsedItem.bannerImageURL, datePublished: parsedItem.datePublished, dateModified: parsedItem.dateModified, authors: authors, tags: parsedItem.tags, attachments: attachments, accountInfo: nil, status: status)
|
self.init(accountID: accountID, articleID: parsedItem.syncServiceID, feedID: feedID, uniqueID: parsedItem.uniqueID, title: parsedItem.title, contentHTML: parsedItem.contentHTML, contentText: parsedItem.contentText, url: parsedItem.url, externalURL: parsedItem.externalURL, summary: parsedItem.summary, imageURL: parsedItem.imageURL, bannerImageURL: parsedItem.bannerImageURL, datePublished: parsedItem.datePublished, dateModified: parsedItem.dateModified, authors: authors, tags: parsedItem.tags, attachments: attachments, accountInfo: nil, status: status)
|
||||||
}
|
}
|
||||||
|
|
||||||
func articleByAttaching(_ authors: Set<Author>?, _ attachments: Set<Attachment>?, _ tags: Set<String>?) -> Article {
|
// func articleByAttaching(_ authors: Set<Author>?, _ attachments: Set<Attachment>?, _ tags: Set<String>?) -> Article {
|
||||||
|
//
|
||||||
if authors == nil && attachments == nil && tags == nil {
|
// if authors == nil && attachments == nil && tags == nil {
|
||||||
return self
|
// return self
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
return Article(accountID: accountID, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo, status: status)
|
// return Article(accountID: accountID, articleID: articleID, feedID: feedID, uniqueID: uniqueID, title: title, contentHTML: contentHTML, contentText: contentText, url: url, externalURL: externalURL, summary: summary, imageURL: imageURL, bannerImageURL: bannerImageURL, datePublished: datePublished, dateModified: dateModified, authors: authors, tags: tags, attachments: attachments, accountInfo: accountInfo, status: status)
|
||||||
}
|
// }
|
||||||
|
|
||||||
private func addPossibleStringChangeWithKeyPath(_ comparisonKeyPath: KeyPath<Article,String?>, _ otherArticle: Article, _ key: String, _ dictionary: NSMutableDictionary) {
|
private func addPossibleStringChangeWithKeyPath(_ comparisonKeyPath: KeyPath<Article,String?>, _ otherArticle: Article, _ key: String, _ dictionary: NSMutableDictionary) {
|
||||||
|
|
||||||
if self[keyPath: comparisonKeyPath] != otherArticle[keyPath: comparisonKeyPath] {
|
if self[keyPath: comparisonKeyPath] != otherArticle[keyPath: comparisonKeyPath] {
|
||||||
|
|
Loading…
Reference in New Issue