2020-04-29 12:24:35 +02:00
|
|
|
//
|
|
|
|
// CloudKitArticleStatusUpdate.swift
|
|
|
|
// Account
|
|
|
|
//
|
|
|
|
// Created by Maurice Parker on 4/29/20.
|
|
|
|
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SyncDatabase
|
|
|
|
import Articles
|
|
|
|
|
|
|
|
struct CloudKitArticleStatusUpdate {
|
|
|
|
|
|
|
|
enum Record {
|
|
|
|
case all
|
2020-05-10 02:36:03 +02:00
|
|
|
case new
|
2020-04-29 12:24:35 +02:00
|
|
|
case statusOnly
|
|
|
|
case delete
|
|
|
|
}
|
|
|
|
|
|
|
|
var articleID: String
|
|
|
|
var statuses: [SyncStatus]
|
|
|
|
var article: Article?
|
|
|
|
|
2020-06-16 11:00:58 +02:00
|
|
|
init?(articleID: String, statuses: [SyncStatus], article: Article?) {
|
|
|
|
self.articleID = articleID
|
|
|
|
self.statuses = statuses
|
|
|
|
self.article = article
|
|
|
|
|
|
|
|
let rec = record
|
|
|
|
// This is an invalid status update. The article is required for new and all
|
|
|
|
if article == nil && (rec == .all || rec == .new) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
var record: Record {
|
|
|
|
if statuses.contains(where: { $0.key == .deleted }) {
|
|
|
|
return .delete
|
|
|
|
}
|
|
|
|
|
2020-05-10 02:36:03 +02:00
|
|
|
if statuses.count == 1, statuses.first!.key == .new {
|
|
|
|
return .new
|
|
|
|
}
|
|
|
|
|
2020-04-29 12:24:35 +02:00
|
|
|
if let article = article {
|
|
|
|
if article.status.read == false || article.status.starred == true {
|
|
|
|
return .all
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return .statusOnly
|
|
|
|
}
|
|
|
|
|
|
|
|
var isRead: Bool {
|
|
|
|
if let article = article {
|
|
|
|
return article.status.read
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
var isStarred: Bool {
|
|
|
|
if let article = article {
|
|
|
|
return article.status.starred
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|