damus

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

commit abcff3b92851cb25f2f3a41e99e749abea0cb329
parent c8f18958a2d4d2ceab095ddcefaa78460cdd9945
Author: William Casarin <jb55@jb55.com>
Date:   Wed, 28 Jun 2023 17:40:27 +0200

profile: allow post button on every profile and prefill user tag

Changelog-Added: Add post button to profile pages

Diffstat:
Mdamus/ContentView.swift | 2+-
Mdamus/Views/PostView.swift | 47++++++++++++++++++++++++++++++++++++-----------
Mdamus/Views/Profile/ProfileView.swift | 4++--
3 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/damus/ContentView.swift b/damus/ContentView.swift @@ -128,7 +128,7 @@ struct ContentView: View { if privkey != nil { PostButtonContainer(is_left_handed: damus_state?.settings.left_handed ?? false) { - self.active_sheet = .post(.posting) + self.active_sheet = .post(.posting(.none)) } } } diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift @@ -19,10 +19,15 @@ class TagModel: ObservableObject { var diff = 0 } +enum PostTarget { + case none + case user(String) +} + enum PostAction { case replying_to(NostrEvent) case quoting(NostrEvent) - case posting + case posting(PostTarget) var ev: NostrEvent? { switch self { @@ -155,9 +160,20 @@ struct PostView: View { .clipShape(Capsule()) } - var isEmpty: Bool { - self.uploadedMedias.count == 0 && - self.post.mutableString.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + func isEmpty() -> Bool { + return self.uploadedMedias.count == 0 && + self.post.mutableString.trimmingCharacters(in: .whitespacesAndNewlines) == + initialString().mutableString.trimmingCharacters(in: .whitespacesAndNewlines) + } + + func initialString() -> NSMutableAttributedString { + guard case .posting(let target) = action, + case .user(let pubkey) = target else { + return .init(string: "") + } + + let profile = damus_state.profiles.lookup(id: pubkey) + return user_tag_attr_string(profile: profile, pubkey: pubkey) } func clear_draft() { @@ -172,15 +188,17 @@ struct PostView: View { } - func load_draft() { + func load_draft() -> Bool { guard let draft = load_draft_for_post(drafts: self.damus_state.drafts, action: self.action) else { self.post = NSMutableAttributedString("") self.uploadedMedias = [] - return + + return false } self.uploadedMedias = draft.media self.post = draft.content + return true } func post_changed(post: NSMutableAttributedString, media: [UploadedMedia]) { @@ -321,6 +339,11 @@ struct PostView: View { .padding(.horizontal) } + func fill_target_content(target: PostTarget) { + self.post = initialString() + self.tagModel.diff = post.string.count + } + var body: some View { GeometryReader { (deviceSize: GeometryProxy) in VStack(alignment: .leading, spacing: 0) { @@ -390,7 +413,7 @@ struct PostView: View { } } .onAppear() { - load_draft() + let loaded_draft = load_draft() switch action { case .replying_to(let replying_to): @@ -399,8 +422,10 @@ struct PostView: View { case .quoting(let quoting): references = gather_quote_ids(our_pubkey: damus_state.pubkey, from: quoting) originalReferences = references - case .posting: - break + case .posting(let target): + guard !loaded_draft else { break } + + fill_target_content(target: target) } DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { @@ -408,7 +433,7 @@ struct PostView: View { } } .onDisappear { - if isEmpty { + if isEmpty() { clear_draft() } } @@ -448,7 +473,7 @@ func get_searching_string(_ word: String?) -> String? { struct PostView_Previews: PreviewProvider { static var previews: some View { - PostView(action: .posting, damus_state: test_damus_state()) + PostView(action: .posting(.none), damus_state: test_damus_state()) } } diff --git a/damus/Views/Profile/ProfileView.swift b/damus/Views/Profile/ProfileView.swift @@ -523,9 +523,9 @@ struct ProfileView: View { QRCodeView(damus_state: damus_state, pubkey: profile.pubkey) } - if profile.pubkey == damus_state.pubkey && damus_state.is_privkey_user { + if damus_state.is_privkey_user { PostButtonContainer(is_left_handed: damus_state.settings.left_handed) { - notify(.compose, PostAction.posting) + notify(.compose, PostAction.posting(.user(profile.pubkey))) } } }