forked from shadowfacts/Tusker
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:
parent
06442b5629
commit
681cdb8bb5
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue