From 681cdb8bb52bc7c3ea0c9bf932df46f2fe2ea4e0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 28 Nov 2019 22:26:37 -0500 Subject: [PATCH] Fix automatically created drafts not being deleted after successful post The newly created draft needs to be set to the compose VC's currentDraft so that it gets removed after the status is successfully created. Also, save the drafts to disk after saving a draft so that crashes don't cause draft loss. --- Tusker/DraftsManager.swift | 14 +++++++++----- Tusker/Screens/Compose/ComposeViewController.swift | 3 ++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Tusker/DraftsManager.swift b/Tusker/DraftsManager.swift index 65ca778d..95878030 100644 --- a/Tusker/DraftsManager.swift +++ b/Tusker/DraftsManager.swift @@ -16,9 +16,11 @@ class DraftsManager: Codable { private static var archiveURL = DraftsManager.documentsDirectory.appendingPathComponent("drafts").appendingPathExtension("plist") static func save() { - let encoder = PropertyListEncoder() - let data = try? encoder.encode(shared) - try? data?.write(to: archiveURL, options: .noFileProtection) + DispatchQueue.global(qos: .userInitiated).async { + let encoder = PropertyListEncoder() + let data = try? encoder.encode(shared) + try? data?.write(to: archiveURL, options: .noFileProtection) + } } static func load() -> DraftsManager { @@ -37,8 +39,10 @@ class DraftsManager: Codable { return drafts.sorted(by: { $0.lastModified > $1.lastModified }) } - func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) { - drafts.append(Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments)) + func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) -> Draft { + let draft = Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments) + drafts.append(draft) + return draft } func remove(_ draft: Draft) { diff --git a/Tusker/Screens/Compose/ComposeViewController.swift b/Tusker/Screens/Compose/ComposeViewController.swift index 5fdf3762..b7694407 100644 --- a/Tusker/Screens/Compose/ComposeViewController.swift +++ b/Tusker/Screens/Compose/ComposeViewController.swift @@ -338,8 +338,9 @@ class ComposeViewController: UIViewController { if let currentDraft = self.currentDraft { currentDraft.update(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) } else { - DraftsManager.shared.create(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) + self.currentDraft = DraftsManager.shared.create(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) } + DraftsManager.save() } @objc func close() {