2017-07-03 10:29:44 -07:00
|
|
|
|
//
|
|
|
|
|
// ArticleStatus.swift
|
|
|
|
|
// DataModel
|
|
|
|
|
//
|
|
|
|
|
// Created by Brent Simmons on 7/1/17.
|
|
|
|
|
// Copyright © 2017 Ranchero Software, LLC. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
2017-09-18 12:59:42 -07:00
|
|
|
|
// Threading rules:
|
|
|
|
|
// * Main-thread only
|
|
|
|
|
// * Except: may be created on background thread by StatusesTable.
|
|
|
|
|
// Which is safe, because at creation time it’t not yet shared,
|
|
|
|
|
// and it won’t be mutated ever on a background thread.
|
|
|
|
|
|
|
|
|
|
public final class ArticleStatus: Hashable {
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2017-10-08 21:06:25 -07:00
|
|
|
|
public enum Key: String {
|
|
|
|
|
case read = "read"
|
|
|
|
|
case starred = "starred"
|
|
|
|
|
case userDeleted = "userDeleted"
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-03 15:04:31 -07:00
|
|
|
|
public let articleID: String
|
2017-09-04 17:10:02 -07:00
|
|
|
|
public let dateArrived: Date
|
2017-09-07 20:51:51 -07:00
|
|
|
|
|
|
|
|
|
public var read = false
|
|
|
|
|
public var starred = false
|
|
|
|
|
public var userDeleted = false
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2017-09-22 08:06:06 -07:00
|
|
|
|
public init(articleID: String, read: Bool, starred: Bool, userDeleted: Bool, dateArrived: Date) {
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2017-07-03 15:04:31 -07:00
|
|
|
|
self.articleID = articleID
|
2017-07-03 10:29:44 -07:00
|
|
|
|
self.read = read
|
|
|
|
|
self.starred = starred
|
|
|
|
|
self.userDeleted = userDeleted
|
|
|
|
|
self.dateArrived = dateArrived
|
|
|
|
|
}
|
2017-07-31 18:39:42 -07:00
|
|
|
|
|
2017-09-18 13:01:36 -07:00
|
|
|
|
public convenience init(articleID: String, dateArrived: Date) {
|
2017-07-31 18:39:42 -07:00
|
|
|
|
|
2017-09-22 08:06:06 -07:00
|
|
|
|
self.init(articleID: articleID, read: false, starred: false, userDeleted: false, dateArrived: dateArrived)
|
2017-07-31 18:39:42 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 21:06:25 -07:00
|
|
|
|
public func boolStatus(forKey key: ArticleStatus.Key) -> Bool {
|
2017-07-03 10:29:44 -07:00
|
|
|
|
|
2017-10-08 21:06:25 -07:00
|
|
|
|
switch key {
|
|
|
|
|
case .read:
|
|
|
|
|
return read
|
|
|
|
|
case .starred:
|
|
|
|
|
return starred
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
return userDeleted
|
2017-07-03 10:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-08 21:06:25 -07:00
|
|
|
|
public func setBoolStatus(_ status: Bool, forKey key: ArticleStatus.Key) {
|
2017-09-07 20:51:51 -07:00
|
|
|
|
|
2017-10-08 21:06:25 -07:00
|
|
|
|
switch key {
|
|
|
|
|
case .read:
|
|
|
|
|
read = status
|
|
|
|
|
case .starred:
|
|
|
|
|
starred = status
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
userDeleted = status
|
2017-09-07 20:51:51 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-04 17:10:02 -07:00
|
|
|
|
|
2018-08-25 12:05:47 -07:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(articleID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2017-09-07 20:51:51 -07:00
|
|
|
|
public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool {
|
2018-08-25 16:29:11 -07:00
|
|
|
|
|
2018-08-25 12:05:47 -07:00
|
|
|
|
return lhs.articleID == rhs.articleID && lhs.dateArrived == rhs.dateArrived && lhs.read == rhs.read && lhs.starred == rhs.starred && lhs.userDeleted == rhs.userDeleted
|
2017-07-03 11:20:14 -07:00
|
|
|
|
}
|
2017-07-03 10:29:44 -07:00
|
|
|
|
}
|
2017-10-08 21:06:25 -07:00
|
|
|
|
|
|
|
|
|
public extension Set where Element == ArticleStatus {
|
|
|
|
|
|
|
|
|
|
public func articleIDs() -> Set<String> {
|
|
|
|
|
|
|
|
|
|
return Set<String>(map { $0.articleID })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public extension Array where Element == ArticleStatus {
|
|
|
|
|
|
|
|
|
|
public func articleIDs() -> [String] {
|
|
|
|
|
|
|
|
|
|
return map { $0.articleID }
|
|
|
|
|
}
|
|
|
|
|
}
|