From d481ef6c9f0e265969a44701ed77a5b3b3012076 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 9 Mar 2024 14:15:14 -0500 Subject: [PATCH] Fix crash when removing the same poll option multiple times SwiftUI doesn't detect updates to CoreData objects when directly mutating the NSMutableOrderedSet of a relationship Closes #458 --- .../Sources/ComposeUI/Controllers/PollController.swift | 9 +++++++-- .../Sources/ComposeUI/Views/EmojiTextField.swift | 5 +++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift index 3914e51b..7a1e2cb9 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift @@ -34,11 +34,16 @@ class PollController: ViewController { } private func moveOptions(indices: IndexSet, newIndex: Int) { - poll.options.moveObjects(at: indices, to: newIndex) + // see AttachmentsListController.moveAttachments + var array = poll.pollOptions + array.move(fromOffsets: indices, toOffset: newIndex) + poll.options = NSMutableOrderedSet(array: array) } private func removeOption(_ option: PollOption) { - poll.options.remove(option) + var array = poll.pollOptions + array.remove(at: poll.options.index(of: option)) + poll.options = NSMutableOrderedSet(array: array) } private var canAddOption: Bool { diff --git a/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift b/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift index cbb9906c..ef856abe 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Views/EmojiTextField.swift @@ -52,6 +52,11 @@ struct EmojiTextField: UIViewRepresentable { if text != uiView.text { uiView.text = text } + if placeholder != uiView.attributedPlaceholder?.string { + uiView.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [ + .foregroundColor: UIColor.secondaryLabel, + ]) + } context.coordinator.text = $text context.coordinator.maxLength = maxLength