Use new hash-into over hashValue in Articles.framework. WIP on #402.

This commit is contained in:
Brent Simmons 2018-08-25 12:05:47 -07:00
parent cfb3bd706e
commit a3a9e9c9bd
4 changed files with 55 additions and 33 deletions

View File

@ -27,7 +27,6 @@ public struct Article: Hashable {
public let authors: Set<Author>?
public let attachments: Set<Attachment>?
public let status: ArticleStatus
public let hashValue: Int
public init(accountID: String, articleID: String?, feedID: String, uniqueID: String, title: String?, contentHTML: String?, contentText: String?, url: String?, externalURL: String?, summary: String?, imageURL: String?, bannerImageURL: String?, datePublished: Date?, dateModified: Date?, authors: Set<Author>?, attachments: Set<Attachment>?, status: ArticleStatus) {
@ -54,8 +53,6 @@ public struct Article: Hashable {
else {
self.articleID = Article.calculatedArticleID(feedID: feedID, uniqueID: uniqueID)
}
self.hashValue = (accountID + self.articleID).hashValue
}
public static func calculatedArticleID(feedID: String, uniqueID: String) -> String {
@ -63,9 +60,18 @@ public struct Article: Hashable {
return databaseIDWithString("\(feedID) \(uniqueID)")
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(accountID)
hasher.combine(articleID)
}
// MARK: - Equatable
public static func ==(lhs: Article, rhs: Article) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.articleID == rhs.articleID && lhs.accountID == rhs.accountID && lhs.feedID == rhs.feedID && lhs.uniqueID == rhs.uniqueID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.url == rhs.url && lhs.externalURL == rhs.externalURL && lhs.summary == rhs.summary && lhs.imageURL == rhs.imageURL && lhs.bannerImageURL == rhs.bannerImageURL && lhs.datePublished == rhs.datePublished && lhs.authors == rhs.authors && lhs.attachments == rhs.attachments
return lhs.articleID == rhs.articleID && lhs.accountID == rhs.accountID && lhs.feedID == rhs.feedID && lhs.uniqueID == rhs.uniqueID && lhs.title == rhs.title && lhs.contentHTML == rhs.contentHTML && lhs.url == rhs.url && lhs.externalURL == rhs.externalURL && lhs.summary == rhs.summary && lhs.imageURL == rhs.imageURL && lhs.bannerImageURL == rhs.bannerImageURL && lhs.datePublished == rhs.datePublished && lhs.authors == rhs.authors && lhs.attachments == rhs.attachments
}
}

View File

@ -24,7 +24,6 @@ public final class ArticleStatus: Hashable {
public let articleID: String
public let dateArrived: Date
public let hashValue: Int
public var read = false
public var starred = false
@ -37,7 +36,6 @@ public final class ArticleStatus: Hashable {
self.starred = starred
self.userDeleted = userDeleted
self.dateArrived = dateArrived
self.hashValue = articleID.hashValue
}
public convenience init(articleID: String, dateArrived: Date) {
@ -69,9 +67,17 @@ public final class ArticleStatus: Hashable {
}
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(articleID)
}
// MARK: - Equatable
public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred && lhs.userDeleted == rhs.userDeleted
return lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred && lhs.userDeleted == rhs.userDeleted
}
}

View File

@ -16,7 +16,6 @@ public struct Attachment: Hashable {
public let title: String?
public let sizeInBytes: Int?
public let durationInSeconds: Int?
public let hashValue: Int
public init(attachmentID: String?, url: String, mimeType: String?, title: String?, sizeInBytes: Int?, durationInSeconds: Int?) {
@ -36,27 +35,33 @@ public struct Attachment: Hashable {
self.durationInSeconds = nil
}
var s = url
s += mimeType ?? ""
s += title ?? ""
if let sizeInBytes = sizeInBytes {
s += "\(sizeInBytes)"
}
if let durationInSeconds = durationInSeconds {
s += "\(durationInSeconds)"
}
self.hashValue = s.hashValue
if let attachmentID = attachmentID {
self.attachmentID = attachmentID
}
else {
var s = url
s += mimeType ?? ""
s += title ?? ""
if let sizeInBytes = sizeInBytes {
s += "\(sizeInBytes)"
}
if let durationInSeconds = durationInSeconds {
s += "\(durationInSeconds)"
}
self.attachmentID = databaseIDWithString(s)
}
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(attachmentID)
}
// MARK: - Equatable
public static func ==(lhs: Attachment, rhs: Attachment) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.attachmentID == rhs.attachmentID
return lhs.attachmentID == rhs.attachmentID
}
}

View File

@ -15,7 +15,6 @@ public struct Author: Hashable {
public let url: String?
public let avatarURL: String?
public let emailAddress: String?
public let hashValue: Int
public init?(authorID: String?, name: String?, url: String?, avatarURL: String?, emailAddress: String?) {
@ -27,16 +26,14 @@ public struct Author: Hashable {
self.avatarURL = avatarURL
self.emailAddress = emailAddress
var s = name ?? ""
s += url ?? ""
s += avatarURL ?? ""
s += emailAddress ?? ""
self.hashValue = s.hashValue
if let authorID = authorID {
self.authorID = authorID
}
else {
var s = name ?? ""
s += url ?? ""
s += avatarURL ?? ""
s += emailAddress ?? ""
self.authorID = databaseIDWithString(s)
}
}
@ -76,16 +73,24 @@ public struct Author: Hashable {
return d
}
public static func ==(lhs: Author, rhs: Author) -> Bool {
return lhs.hashValue == rhs.hashValue && lhs.authorID == rhs.authorID
}
public static func authorsWithDiskArray(_ diskArray: [[String: Any]]) -> Set<Author>? {
let authors = diskArray.compactMap { Author(dictionary: $0) }
return authors.isEmpty ? nil : Set(authors)
}
// MARK: - Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(authorID)
}
// MARK: - Equatable
public static func ==(lhs: Author, rhs: Author) -> Bool {
return lhs.authorID == rhs.authorID
}
}
extension Set where Element == Author {