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,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) {
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue