From 86e9ee16a0706296a3a3ecbab5bb260b31b2c417 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Tue, 30 Jan 2024 13:35:25 -0500 Subject: [PATCH] ui: truncate visible media URL The URL shown to the user before they click the 'Load Media' button can take up a large portion of the screen, and doesn't offer any value to the user. This patch features a naive approach to truncate the URL to be a certain number of characters if it is greater than the specified value. Right now the maximum number of characters is set to 80. Closes: https://github.com/damus-io/damus/issues/1950 Lightning-address: kernelkind@getalby.com Signed-off-by: kernelkind Link: 20240130183525.50446-1-kernelkind@gmail.com Signed-off-by: William Casarin --- damus/Util/URIParsing.swift | 11 +++++++++++ damus/Views/NoteContentView.swift | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/damus/Util/URIParsing.swift b/damus/Util/URIParsing.swift index d63681ef..6c7df5d9 100644 --- a/damus/Util/URIParsing.swift +++ b/damus/Util/URIParsing.swift @@ -7,6 +7,8 @@ import Foundation +fileprivate let MAX_CHAR_URL = 80 + private func remove_damus_uri_prefix(_ s: String) -> String { var uri = s.replacingOccurrences(of: "https://damus.io/r/", with: "") uri = uri.replacingOccurrences(of: "https://damus.io/", with: "") @@ -32,3 +34,12 @@ func remove_nostr_uri_prefix(_ s: String) -> String { return uri } + +func abbreviateURL(_ url: URL) -> String { + let urlString = url.absoluteString + + if urlString.count > MAX_CHAR_URL { + return String(urlString.prefix(MAX_CHAR_URL)) + "..." + } + return urlString +} diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift index 9687ef04..ef2253be 100644 --- a/damus/Views/NoteContentView.swift +++ b/damus/Views/NoteContentView.swift @@ -188,7 +188,7 @@ struct NoteContentView: View { .frame(height: 1) switch artifacts.media[index] { case .image(let url), .video(let url): - Text(url.absoluteString) + Text(abbreviateURL(url)) .font(eventviewsize_to_font(size, font_size: damus_state.settings.font_size)) .foregroundStyle(DamusColors.neutral6) .multilineTextAlignment(.leading)