2017-07-03 19:29:44 +02: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 21:59:42 +02: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 19:29:44 +02:00
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
|
public enum Key: String {
|
|
|
|
|
case read = "read"
|
|
|
|
|
case starred = "starred"
|
|
|
|
|
case userDeleted = "userDeleted"
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
|
public let articleID: String
|
2017-09-05 02:10:02 +02:00
|
|
|
|
public let dateArrived: Date
|
2017-09-08 05:51:51 +02:00
|
|
|
|
|
|
|
|
|
public var read = false
|
|
|
|
|
public var starred = false
|
|
|
|
|
public var userDeleted = false
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-09-22 17:06:06 +02:00
|
|
|
|
public init(articleID: String, read: Bool, starred: Bool, userDeleted: Bool, dateArrived: Date) {
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
|
self.articleID = articleID
|
2017-07-03 19:29:44 +02:00
|
|
|
|
self.read = read
|
|
|
|
|
self.starred = starred
|
|
|
|
|
self.userDeleted = userDeleted
|
|
|
|
|
self.dateArrived = dateArrived
|
|
|
|
|
}
|
2017-08-01 03:39:42 +02:00
|
|
|
|
|
2017-09-18 22:01:36 +02:00
|
|
|
|
public convenience init(articleID: String, dateArrived: Date) {
|
2017-08-01 03:39:42 +02:00
|
|
|
|
|
2017-09-22 17:06:06 +02:00
|
|
|
|
self.init(articleID: articleID, read: false, starred: false, userDeleted: false, dateArrived: dateArrived)
|
2017-08-01 03:39:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
|
public func boolStatus(forKey key: ArticleStatus.Key) -> Bool {
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
|
switch key {
|
|
|
|
|
case .read:
|
|
|
|
|
return read
|
|
|
|
|
case .starred:
|
|
|
|
|
return starred
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
return userDeleted
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
|
public func setBoolStatus(_ status: Bool, forKey key: ArticleStatus.Key) {
|
2017-09-08 05:51:51 +02:00
|
|
|
|
|
2017-10-09 06:06:25 +02:00
|
|
|
|
switch key {
|
|
|
|
|
case .read:
|
|
|
|
|
read = status
|
|
|
|
|
case .starred:
|
|
|
|
|
starred = status
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
userDeleted = status
|
2017-09-08 05:51:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
|
2018-08-25 21:05:47 +02:00
|
|
|
|
// MARK: - Hashable
|
|
|
|
|
|
|
|
|
|
public func hash(into hasher: inout Hasher) {
|
|
|
|
|
hasher.combine(articleID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Equatable
|
|
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
|
public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool {
|
2018-08-26 01:29:11 +02:00
|
|
|
|
|
2018-08-25 21:05:47 +02: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 20:20:14 +02:00
|
|
|
|
}
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
2017-10-09 06:06:25 +02:00
|
|
|
|
|
|
|
|
|
public extension Set where Element == ArticleStatus {
|
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
|
func articleIDs() -> Set<String> {
|
2017-10-09 06:06:25 +02:00
|
|
|
|
|
|
|
|
|
return Set<String>(map { $0.articleID })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public extension Array where Element == ArticleStatus {
|
|
|
|
|
|
2019-02-12 16:04:18 +01:00
|
|
|
|
func articleIDs() -> [String] {
|
2017-10-09 06:06:25 +02:00
|
|
|
|
|
|
|
|
|
return map { $0.articleID }
|
|
|
|
|
}
|
|
|
|
|
}
|