commit 6b1f57d6d0d0656fbfd7f33540a84cca4502a448
parent 77f52683366c438139e575bc62817d323891a5a0
Author: OlegAba <mail@olegaba.com>
Date: Tue, 28 Feb 2023 23:53:48 -0500
Truncate long notes (#715)
Changelog-Added: Truncate large posts and add a show more button
Diffstat:
4 files changed, 40 insertions(+), 7 deletions(-)
diff --git a/damus/Views/ChatView.swift b/damus/Views/ChatView.swift
@@ -113,7 +113,8 @@ struct ChatView: View {
event: event,
show_images: show_images,
size: .normal,
- artifacts: .just_content(event.content))
+ artifacts: .just_content(event.content),
+ truncate: false)
if is_active || next_ev == nil || next_ev!.pubkey != event.pubkey {
let bar = make_actionbar_model(ev: event.id, damus: damus_state)
diff --git a/damus/Views/DMView.swift b/damus/Views/DMView.swift
@@ -23,7 +23,7 @@ struct DMView: View {
let should_show_img = should_show_images(contacts: damus_state.contacts, ev: event, our_pubkey: damus_state.pubkey)
- NoteContentView(damus_state: damus_state, event: event, show_images: should_show_img, size: .normal, artifacts: .just_content(event.get_content(damus_state.keypair.privkey)))
+ NoteContentView(damus_state: damus_state, event: event, show_images: should_show_img, size: .normal, artifacts: .just_content(event.get_content(damus_state.keypair.privkey)), truncate: false)
.foregroundColor(is_ours ? Color.white : Color.primary)
.padding(10)
.background(is_ours ? Color.accentColor : Color.secondary.opacity(0.15))
diff --git a/damus/Views/Events/EventBody.swift b/damus/Views/Events/EventBody.swift
@@ -29,7 +29,7 @@ struct EventBody: View {
ReplyDescription(event: event, profiles: damus_state.profiles)
}
- NoteContentView(damus_state: damus_state, event: event, show_images: should_show_img, size: size, artifacts: .just_content(content))
+ NoteContentView(damus_state: damus_state, event: event, show_images: should_show_img, size: size, artifacts: .just_content(content), truncate: true)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift
@@ -28,11 +28,12 @@ struct NoteContentView: View {
let show_images: Bool
let size: EventViewKind
let preview_height: CGFloat?
+ let truncate: Bool
@State var artifacts: NoteArtifacts
@State var preview: LinkViewRepresentable?
- init(damus_state: DamusState, event: NostrEvent, show_images: Bool, size: EventViewKind, artifacts: NoteArtifacts) {
+ init(damus_state: DamusState, event: NostrEvent, show_images: Bool, size: EventViewKind, artifacts: NoteArtifacts, truncate: Bool) {
self.damus_state = damus_state
self.event = event
self.show_images = show_images
@@ -41,6 +42,7 @@ struct NoteContentView: View {
self.preview_height = lookup_cached_preview_size(previews: damus_state.previews, evid: event.id)
self._preview = State(initialValue: load_cached_preview(previews: damus_state.previews, evid: event.id))
self._artifacts = State(initialValue: render_note_content(ev: event, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey))
+ self.truncate = truncate
}
func MainContent() -> some View {
@@ -50,9 +52,8 @@ struct NoteContentView: View {
SelectableText(attributedString: artifacts.content)
TranslateView(damus_state: damus_state, event: event)
} else {
- Text(artifacts.content)
+ TruncatedText(text: artifacts.content, maxChars: (truncate ? 280 : nil))
.font(eventviewsize_to_font(size))
- .fixedSize(horizontal: false, vertical: true)
}
if show_images && artifacts.images.count > 0 {
@@ -65,6 +66,7 @@ struct NoteContentView: View {
}
.cornerRadius(10)
}
+
if artifacts.invoices.count > 0 {
InvoicesView(our_pubkey: damus_state.keypair.pubkey, invoices: artifacts.invoices)
}
@@ -175,7 +177,7 @@ struct NoteContentView_Previews: PreviewProvider {
let state = test_damus_state()
let content = "hi there ¯\\_(ツ)_/¯ https://jb55.com/s/Oct12-150217.png 5739a762ef6124dd.jpg"
let artifacts = NoteArtifacts(content: AttributedString(stringLiteral: content), images: [], invoices: [], links: [])
- NoteContentView(damus_state: state, event: NostrEvent(content: content, pubkey: "pk"), show_images: true, size: .normal, artifacts: artifacts)
+ NoteContentView(damus_state: state, event: NostrEvent(content: content, pubkey: "pk"), show_images: true, size: .normal, artifacts: artifacts, truncate: false)
}
}
@@ -261,3 +263,33 @@ func load_cached_preview(previews: PreviewCache, evid: String) -> LinkViewRepres
return LinkViewRepresentable(meta: .linkmeta(meta))
}
+
+struct TruncatedText: View {
+
+ let text: AttributedString
+ let maxChars: Int?
+
+ var body: some View {
+ let truncatedAttributedString: AttributedString? = getTruncatedString()
+
+ Text(truncatedAttributedString ?? text)
+ .fixedSize(horizontal: false, vertical: true)
+
+ if truncatedAttributedString != nil {
+ Spacer()
+ Button(NSLocalizedString("Show more", comment: "Button to show entire note.")) { }
+ .allowsHitTesting(false)
+ }
+ }
+
+ func getTruncatedString() -> AttributedString? {
+ guard let maxChars = maxChars else { return nil }
+ let nsAttributedString = NSAttributedString(text)
+ if nsAttributedString.length < maxChars { return nil }
+
+ let range = NSRange(location: 0, length: maxChars)
+ let truncatedAttributedString = nsAttributedString.attributedSubstring(from: range)
+
+ return AttributedString(truncatedAttributedString) + "..."
+ }
+}