Fix replies appearing multiple times in drafts

This commit is contained in:
Shadowfacts 2022-10-29 10:41:00 -04:00
parent bb9cef55ea
commit b6d8232951
3 changed files with 24 additions and 7 deletions

View File

@ -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)

View File

@ -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
}
}

View File

@ -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)
}