Fix drafts from share sheet not being saved

This commit is contained in:
Shadowfacts 2023-04-19 22:27:25 -04:00
parent 3d3fc3f515
commit 74a157d26c
3 changed files with 17 additions and 9 deletions

View File

@ -143,8 +143,6 @@ public final class ComposeController: ViewController {
func cancel(deleteDraft: Bool) {
if deleteDraft {
DraftsManager.shared.remove(draft)
} else {
DraftsManager.save()
}
config.dismiss(.cancel)
}

View File

@ -8,6 +8,9 @@
import Foundation
import Combine
import OSLog
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "DraftsManager")
public class DraftsManager: Codable, ObservableObject {
@ -21,18 +24,25 @@ public class DraftsManager: Codable, ObservableObject {
public static func save() {
saveQueue.async {
let encoder = PropertyListEncoder()
let data = try? encoder.encode(shared)
try? data?.write(to: archiveURL, options: .noFileProtection)
do {
let data = try encoder.encode(shared)
try data.write(to: archiveURL, options: .noFileProtection)
} catch {
logger.error("Save failed: \(String(describing: error))")
}
}
}
static func load() -> DraftsManager {
let decoder = PropertyListDecoder()
if let data = try? Data(contentsOf: archiveURL),
let draftsManager = try? decoder.decode(DraftsManager.self, from: data) {
do {
let data = try Data(contentsOf: archiveURL)
let draftsManager = try decoder.decode(DraftsManager.self, from: data)
return draftsManager
} catch {
logger.error("Load failed: \(String(describing: error))")
return DraftsManager()
}
return DraftsManager()
}
public static func migrate(from url: URL) -> Result<Void, any Error> {

View File

@ -57,11 +57,11 @@ class ShareViewController: UIViewController {
text: text,
contentWarning: "",
inReplyToID: nil,
// TODO: get the default visibility from preferences
visibility: .public,
visibility: Preferences.shared.defaultPostVisibility,
localOnly: false
)
draft.attachments = attachments
DraftsManager.shared.add(draft)
return draft
}