From 074a296a68bf69f0348f121fb38291b1b6a7a10e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 21 Apr 2023 18:02:30 -0400 Subject: [PATCH] Fix Post button always being disabled when require attachment descriptions is enabled Also fix post button state not updating when description edited Closes #371 --- .../Controllers/AttachmentsListController.swift | 17 +++++++++++++---- .../Controllers/ComposeController.swift | 3 +++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift index 215506d0..51a4f0aa 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift @@ -17,18 +17,20 @@ class AttachmentsListController: ViewController { var isValid: Bool { !requiresAttachmentDescriptions && validAttachmentCombination } - + private var requiresAttachmentDescriptions: Bool { if parent.config.requireAttachmentDescriptions { - return draft.attachments.allSatisfy { - !$0.attachmentDescription.isEmpty + if draft.attachments.isEmpty { + return false + } else { + return !parent.attachmentsMissingDescriptions.isEmpty } } else { return false } } - private var validAttachmentCombination: Bool { + var validAttachmentCombination: Bool { if !parent.mastodonController.instanceFeatures.mastodonAttachmentRestrictions { return true } else if draft.attachments.contains(where: { $0.data.type == .video }) && @@ -139,6 +141,13 @@ class AttachmentsListController: ViewController { .onDrag { NSItemProvider(object: attachment) } + .onReceive(attachment.$attachmentDescription.map(\.isEmpty).removeDuplicates()) { isEmpty in + if isEmpty { + controller.parent.attachmentsMissingDescriptions.insert(attachment.id) + } else { + controller.parent.attachmentsMissingDescriptions.remove(attachment.id) + } + } } .onMove(perform: controller.moveAttachments) .onDelete(perform: controller.deleteAttachments) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift index df2db563..00a946d2 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift @@ -32,6 +32,9 @@ public final class ComposeController: ViewController { @Published var toolbarController: ToolbarController! @Published var attachmentsListController: AttachmentsListController! + // this property is here rather than on the AttachmentsListController so that the ComposeView + // updates when it changes, because changes to it may alter postButtonEnabled + @Published var attachmentsMissingDescriptions = Set() @Published var contentWarningBecomeFirstResponder = false @Published var mainComposeTextViewBecomeFirstResponder = false @Published var currentInput: (any ComposeInput)? = nil