From 74a157d26ca171d7469468716eba01cf131fa576 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 19 Apr 2023 22:27:25 -0400 Subject: [PATCH] Fix drafts from share sheet not being saved --- .../Controllers/ComposeController.swift | 2 -- .../ComposeUI/Model/DraftsManager.swift | 20 ++++++++++++++----- ShareExtension/ShareViewController.swift | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift index 86c6abd3..153f1365 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift @@ -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) } diff --git a/Packages/ComposeUI/Sources/ComposeUI/Model/DraftsManager.swift b/Packages/ComposeUI/Sources/ComposeUI/Model/DraftsManager.swift index 5670f530..e37cdff7 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Model/DraftsManager.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Model/DraftsManager.swift @@ -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 { diff --git a/ShareExtension/ShareViewController.swift b/ShareExtension/ShareViewController.swift index 4270ad67..a2b46750 100644 --- a/ShareExtension/ShareViewController.swift +++ b/ShareExtension/ShareViewController.swift @@ -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 }