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) { func cancel(deleteDraft: Bool) {
if deleteDraft { if deleteDraft {
DraftsManager.shared.remove(draft) DraftsManager.shared.remove(draft)
} else {
DraftsManager.save()
} }
config.dismiss(.cancel) config.dismiss(.cancel)
} }

View File

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

View File

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