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.
|
|
|
|
|
|
2017-07-03 19:29:44 +02:00
|
|
|
|
public enum ArticleStatusKey: String {
|
|
|
|
|
|
|
|
|
|
case read = "read"
|
|
|
|
|
case starred = "starred"
|
|
|
|
|
case userDeleted = "userDeleted"
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 21:59:42 +02:00
|
|
|
|
public final class ArticleStatus: Hashable {
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
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-07-03 20:20:14 +02:00
|
|
|
|
public let hashValue: Int
|
2017-09-08 05:51:51 +02:00
|
|
|
|
|
|
|
|
|
public var read = false
|
|
|
|
|
public var starred = false
|
|
|
|
|
public var userDeleted = false
|
|
|
|
|
public var accountInfo: AccountInfo?
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
|
public init(articleID: String, read: Bool, starred: Bool, userDeleted: Bool, dateArrived: Date, accountInfo: AccountInfo?) {
|
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
|
|
|
|
|
self.accountInfo = accountInfo
|
2017-07-04 00:04:31 +02:00
|
|
|
|
self.hashValue = articleID.hashValue
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|
2017-08-01 03:39:42 +02:00
|
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
|
public init(articleID: String, dateArrived: Date) {
|
2017-08-01 03:39:42 +02:00
|
|
|
|
|
2017-08-04 04:22:54 +02:00
|
|
|
|
self.init(articleID: articleID, read: false, starred: false, userDeleted: false, dateArrived: dateArrived, accountInfo: nil)
|
2017-08-01 03:39:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-04 00:04:31 +02:00
|
|
|
|
public func boolStatus(forKey key: String) -> Bool {
|
2017-07-03 19:29:44 +02:00
|
|
|
|
|
|
|
|
|
if let articleStatusKey = ArticleStatusKey(rawValue: key) {
|
|
|
|
|
switch articleStatusKey {
|
|
|
|
|
case .read:
|
|
|
|
|
return read
|
|
|
|
|
case .starred:
|
|
|
|
|
return starred
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
return userDeleted
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-08 06:00:30 +02:00
|
|
|
|
// else if let flag = accountInfo?[key] as? Bool {
|
|
|
|
|
// return flag
|
|
|
|
|
// }
|
2017-07-03 19:29:44 +02:00
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-18 21:59:42 +02:00
|
|
|
|
public func setBoolStatus(_ status: Bool, forKey key: String) {
|
2017-09-08 05:51:51 +02:00
|
|
|
|
|
|
|
|
|
if let articleStatusKey = ArticleStatusKey(rawValue: key) {
|
|
|
|
|
switch articleStatusKey {
|
|
|
|
|
case .read:
|
|
|
|
|
read = status
|
|
|
|
|
case .starred:
|
|
|
|
|
starred = status
|
|
|
|
|
case .userDeleted:
|
|
|
|
|
userDeleted = status
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
// else {
|
|
|
|
|
// if accountInfo == nil {
|
|
|
|
|
// accountInfo = AccountInfo()
|
|
|
|
|
// }
|
|
|
|
|
// accountInfo![key] = status
|
|
|
|
|
// }
|
2017-09-08 05:51:51 +02:00
|
|
|
|
}
|
2017-09-05 02:10:02 +02:00
|
|
|
|
|
2017-09-08 05:51:51 +02:00
|
|
|
|
public static func ==(lhs: ArticleStatus, rhs: ArticleStatus) -> Bool {
|
2017-07-03 20:20:14 +02:00
|
|
|
|
|
2017-09-05 02:10:02 +02:00
|
|
|
|
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 && lhs.accountInfo == rhs.accountInfo
|
2017-07-03 20:20:14 +02:00
|
|
|
|
}
|
2017-07-03 19:29:44 +02:00
|
|
|
|
}
|