Tusker/Tusker/DraftsManager.swift

93 lines
3.1 KiB
Swift
Raw Normal View History

2018-10-23 02:09:11 +00:00
//
// DraftsManager.swift
// Tusker
//
// Created by Shadowfacts on 10/22/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
class DraftsManager: Codable {
private(set) static var shared: DraftsManager = load()
private static var documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
private static var archiveURL = DraftsManager.documentsDirectory.appendingPathComponent("drafts").appendingPathExtension("plist")
static func save() {
DispatchQueue.global(qos: .userInitiated).async {
let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection)
}
2018-10-23 02:09:11 +00:00
}
static func load() -> DraftsManager {
let decoder = PropertyListDecoder()
if let data = try? Data(contentsOf: archiveURL),
let draftsManager = try? decoder.decode(DraftsManager.self, from: data) {
return draftsManager
}
return DraftsManager()
}
private init() {}
var drafts: [Draft] = []
var sorted: [Draft] {
return drafts.sorted(by: { $0.lastModified > $1.lastModified })
}
func create(accountID: String, text: String, contentWarning: String?, inReplyToID: String?, attachments: [DraftAttachment]) -> Draft {
let draft = Draft(accountID: accountID, text: text, contentWarning: contentWarning, inReplyToID: inReplyToID, attachments: attachments)
drafts.append(draft)
return draft
2018-10-23 02:09:11 +00:00
}
func remove(_ draft: Draft) {
let index = drafts.firstIndex(of: draft)!
drafts.remove(at: index)
}
}
extension DraftsManager {
class Draft: Codable, Equatable {
let id: UUID
private(set) var accountID: String
2018-10-23 02:09:11 +00:00
private(set) var text: String
2019-09-08 21:45:33 +00:00
private(set) var contentWarning: String?
2019-02-22 18:53:38 +00:00
private(set) var attachments: [DraftAttachment]
2019-12-14 16:30:35 +00:00
private(set) var inReplyToID: String?
private(set) var lastModified: Date
2018-10-23 02:09:11 +00:00
init(accountID: String, text: String, contentWarning: String?, inReplyToID: String?, attachments: [DraftAttachment], lastModified: Date = Date()) {
2018-10-23 02:09:11 +00:00
self.id = UUID()
self.accountID = accountID
2018-10-23 02:09:11 +00:00
self.text = text
2019-09-08 21:45:33 +00:00
self.contentWarning = contentWarning
2019-12-14 16:30:35 +00:00
self.inReplyToID = inReplyToID
2019-02-22 18:53:38 +00:00
self.attachments = attachments
2019-12-14 16:30:35 +00:00
self.lastModified = lastModified
2018-10-23 02:09:11 +00:00
}
func update(accountID: String, text: String, contentWarning: String?, attachments: [DraftAttachment]) {
self.accountID = accountID
2018-10-23 02:09:11 +00:00
self.text = text
2019-09-08 21:45:33 +00:00
self.contentWarning = contentWarning
2018-10-23 02:09:11 +00:00
self.lastModified = Date()
2019-02-22 18:53:38 +00:00
self.attachments = attachments
2018-10-23 02:09:11 +00:00
}
static func ==(lhs: Draft, rhs: Draft) -> Bool {
return lhs.id == rhs.id
}
}
2019-02-22 18:53:38 +00:00
struct DraftAttachment: Codable {
let attachment: CompositionAttachment
2019-02-22 18:53:38 +00:00
let description: String
}
2018-10-23 02:09:11 +00:00
}