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
This commit is contained in:
Shadowfacts 2024-03-09 14:15:14 -05:00
parent 3caa419659
commit d481ef6c9f
2 changed files with 12 additions and 2 deletions

View File

@ -34,11 +34,16 @@ class PollController: ViewController {
} }
private func moveOptions(indices: IndexSet, newIndex: Int) { 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) { 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 { private var canAddOption: Bool {

View File

@ -52,6 +52,11 @@ struct EmojiTextField: UIViewRepresentable {
if text != uiView.text { if text != uiView.text {
uiView.text = text uiView.text = text
} }
if placeholder != uiView.attributedPlaceholder?.string {
uiView.attributedPlaceholder = NSAttributedString(string: placeholder, attributes: [
.foregroundColor: UIColor.secondaryLabel,
])
}
context.coordinator.text = $text context.coordinator.text = $text
context.coordinator.maxLength = maxLength context.coordinator.maxLength = maxLength