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.
This commit is contained in:
Shadowfacts 2019-11-28 22:26:37 -05:00
parent 06442b5629
commit 681cdb8bb5
Signed by untrusted user: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 11 additions and 6 deletions

View File

@ -16,10 +16,12 @@ class DraftsManager: Codable {
private static var archiveURL = DraftsManager.documentsDirectory.appendingPathComponent("drafts").appendingPathExtension("plist") private static var archiveURL = DraftsManager.documentsDirectory.appendingPathComponent("drafts").appendingPathExtension("plist")
static func save() { static func save() {
DispatchQueue.global(qos: .userInitiated).async {
let encoder = PropertyListEncoder() let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared) let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection) try? data?.write(to: archiveURL, options: .noFileProtection)
} }
}
static func load() -> DraftsManager { static func load() -> DraftsManager {
let decoder = PropertyListDecoder() let decoder = PropertyListDecoder()
@ -37,8 +39,10 @@ class DraftsManager: Codable {
return drafts.sorted(by: { $0.lastModified > $1.lastModified }) return drafts.sorted(by: { $0.lastModified > $1.lastModified })
} }
func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) { func create(text: String, contentWarning: String?, attachments: [DraftAttachment]) -> Draft {
drafts.append(Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments)) let draft = Draft(text: text, contentWarning: contentWarning, lastModified: Date(), attachments: attachments)
drafts.append(draft)
return draft
} }
func remove(_ draft: Draft) { func remove(_ draft: Draft) {

View File

@ -338,8 +338,9 @@ class ComposeViewController: UIViewController {
if let currentDraft = self.currentDraft { if let currentDraft = self.currentDraft {
currentDraft.update(text: self.statusTextView.text, contentWarning: cw, attachments: attachments) currentDraft.update(text: self.statusTextView.text, contentWarning: cw, attachments: attachments)
} else { } 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() { @objc func close() {