From b6d82329518ce8090cceef4b85987a433e637111 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 29 Oct 2022 10:41:00 -0400 Subject: [PATCH] Fix replies appearing multiple times in drafts --- Tusker/Models/Draft.swift | 2 +- Tusker/Models/DraftsManager.swift | 28 ++++++++++++++++++++++----- Tusker/TuskerNavigationDelegate.swift | 1 - 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Tusker/Models/Draft.swift b/Tusker/Models/Draft.swift index 71988be6..0af72b28 100644 --- a/Tusker/Models/Draft.swift +++ b/Tusker/Models/Draft.swift @@ -62,7 +62,7 @@ class Draft: Codable, ObservableObject { self.attachments = try container.decode([CompositionAttachment].self, forKey: .attachments) self.inReplyToID = try container.decode(String?.self, forKey: .inReplyToID) self.visibility = try container.decode(Status.Visibility.self, forKey: .visibility) - self.poll = try container.decode(Poll.self, forKey: .poll) + self.poll = try container.decode(Poll?.self, forKey: .poll) self.localOnly = try container.decodeIfPresent(Bool.self, forKey: .localOnly) ?? false self.initialText = try container.decode(String.self, forKey: .initialText) diff --git a/Tusker/Models/DraftsManager.swift b/Tusker/Models/DraftsManager.swift index 8ce79395..46940ba3 100644 --- a/Tusker/Models/DraftsManager.swift +++ b/Tusker/Models/DraftsManager.swift @@ -34,21 +34,39 @@ class DraftsManager: Codable { private init() {} - var drafts: [Draft] = [] + required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + if let dict = try? container.decode([UUID: Draft].self, forKey: .drafts) { + self.drafts = dict + } else if let array = try? container.decode([Draft].self, forKey: .drafts) { + self.drafts = array.reduce(into: [:], { partialResult, draft in + partialResult[draft.id] = draft + }) + } else { + throw DecodingError.dataCorruptedError(forKey: .drafts, in: container, debugDescription: "expected drafts to be a dict or array of drafts") + } + } + + private var drafts: [UUID: Draft] = [:] var sorted: [Draft] { - return drafts.sorted(by: { $0.lastModified > $1.lastModified }) + return drafts.values.sorted(by: { $0.lastModified > $1.lastModified }) } func add(_ draft: Draft) { - drafts.append(draft) + drafts[draft.id] = draft } func remove(_ draft: Draft) { - drafts.removeAll { $0 == draft } + drafts.removeValue(forKey: draft.id) } func getBy(id: UUID) -> Draft? { - return drafts.first { $0.id == id } + return drafts[id] + } + + enum CodingKeys: String, CodingKey { + case drafts } } diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 0fada9a0..d7c57c35 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -104,7 +104,6 @@ extension TuskerNavigationDelegate { func compose(inReplyToID: String? = nil, mentioningAcct: String? = nil) { let draft = apiController.createDraft(inReplyToID: inReplyToID, mentioningAcct: mentioningAcct) - DraftsManager.shared.add(draft) compose(editing: draft) }