damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 9db81fd6b89bf0c8de051761e563f09d2fc4b80e
parent f08efd7e30543f49fc073511ed7082757a810da5
Author: William Casarin <jb55@jb55.com>
Date:   Fri, 14 Jul 2023 15:54:17 -0700

views: refactor post_changed in PostView

Use some helper functions instead of the full switch

Diffstat:
Mdamus/Views/PostView.swift | 41++++++++++++++++++-----------------------
1 file changed, 18 insertions(+), 23 deletions(-)

diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -183,30 +183,14 @@ struct PostView: View { self.post = draft.content return true } - + func post_changed(post: NSMutableAttributedString, media: [UploadedMedia]) { - switch action { - case .replying_to(let ev): - if let draft = damus_state.drafts.replies[ev] { - draft.content = post - draft.media = media - } else { - damus_state.drafts.replies[ev] = DraftArtifacts(content: post, media: media) - } - case .quoting(let ev): - if let draft = damus_state.drafts.quotes[ev] { - draft.content = post - draft.media = media - } else { - damus_state.drafts.quotes[ev] = DraftArtifacts(content: post, media: media) - } - case .posting: - if let draft = damus_state.drafts.post { - draft.content = post - draft.media = media - } else { - damus_state.drafts.post = DraftArtifacts(content: post, media: media) - } + if let draft = load_draft_for_post(drafts: damus_state.drafts, action: action) { + draft.content = post + draft.media = media + } else { + let artifacts = DraftArtifacts(content: post, media: media) + set_draft_for_post(drafts: damus_state.drafts, action: action, artifacts: artifacts) } } @@ -544,6 +528,17 @@ struct UploadedMedia: Equatable { } +func set_draft_for_post(drafts: Drafts, action: PostAction, artifacts: DraftArtifacts) { + switch action { + case .replying_to(let ev): + drafts.replies[ev] = artifacts + case .quoting(let ev): + drafts.quotes[ev] = artifacts + case .posting: + drafts.post = artifacts + } +} + func load_draft_for_post(drafts: Drafts, action: PostAction) -> DraftArtifacts? { switch action { case .replying_to(let ev):