Skip to content

Commit 9fd829f

Browse files
Added local link detection for text messages (#445)
1 parent 5ff3ce3 commit 9fd829f

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
33

44
# Upcoming
55

6-
### 🔄 Changed
6+
### ✅ Added
7+
- Link detection in the text views
78

89
# [4.49.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.49.0)
910
_February 28, 2024_

Sources/StreamChatSwiftUI/ChatChannel/MessageList/ImageAttachmentView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ public struct AttachmentTextView: View {
8686

8787
public var body: some View {
8888
HStack {
89-
Text(message.adjustedText)
90-
.font(fonts.body)
89+
StreamTextView(message: message)
9190
.standardPadding()
92-
.foregroundColor(textColor(for: message))
9391
.fixedSize(horizontal: false, vertical: true)
9492
Spacer()
9593
}

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageListConfig.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public struct MessageListConfig {
2727
showNewMessagesSeparator: Bool = true,
2828
handleTabBarVisibility: Bool = true,
2929
messageListAlignment: MessageListAlignment = .standard,
30-
uniqueReactionsEnabled: Bool = false
30+
uniqueReactionsEnabled: Bool = false,
31+
localLinkDetectionEnabled: Bool = true
3132
) {
3233
self.messageListType = messageListType
3334
self.typingIndicatorPlacement = typingIndicatorPlacement
@@ -48,6 +49,7 @@ public struct MessageListConfig {
4849
self.handleTabBarVisibility = handleTabBarVisibility
4950
self.messageListAlignment = messageListAlignment
5051
self.uniqueReactionsEnabled = uniqueReactionsEnabled
52+
self.localLinkDetectionEnabled = localLinkDetectionEnabled
5153
}
5254

5355
public let messageListType: MessageListType
@@ -69,6 +71,7 @@ public struct MessageListConfig {
6971
public let handleTabBarVisibility: Bool
7072
public let messageListAlignment: MessageListAlignment
7173
public let uniqueReactionsEnabled: Bool
74+
public let localLinkDetectionEnabled: Bool
7275
}
7376

7477
/// Contains information about the message paddings.

Sources/StreamChatSwiftUI/ChatChannel/MessageList/MessageView.swift

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,12 @@ public struct MessageTextView<Factory: ViewFactory>: View {
163163
)
164164
}
165165

166-
Text(message.adjustedText)
166+
StreamTextView(message: message)
167167
.padding(.leading, leadingPadding)
168168
.padding(.trailing, trailingPadding)
169169
.padding(.top, topPadding)
170170
.padding(.bottom, bottomPadding)
171171
.fixedSize(horizontal: false, vertical: true)
172-
.foregroundColor(textColor(for: message))
173-
.font(fonts.body)
174172
}
175173
.modifier(
176174
factory.makeMessageViewModifier(
@@ -223,3 +221,62 @@ public struct EmojiTextView<Factory: ViewFactory>: View {
223221
.accessibilityIdentifier("MessageTextView")
224222
}
225223
}
224+
225+
struct StreamTextView: View {
226+
227+
@Injected(\.utils) var utils
228+
@Injected(\.fonts) var fonts
229+
230+
var message: ChatMessage
231+
232+
var body: some View {
233+
if #available(iOS 15, *), utils.messageListConfig.localLinkDetectionEnabled {
234+
LinkDetectionTextView(message: message)
235+
} else {
236+
Text(message.adjustedText)
237+
.foregroundColor(textColor(for: message))
238+
.font(fonts.body)
239+
}
240+
}
241+
}
242+
243+
@available(iOS 15, *)
244+
struct LinkDetectionTextView: View {
245+
246+
@Injected(\.colors) var colors
247+
@Injected(\.fonts) var fonts
248+
249+
var message: ChatMessage
250+
251+
var text: String {
252+
message.adjustedText
253+
}
254+
255+
@State var displayedText: AttributedString
256+
257+
@State var linkDetector = TextLinkDetector()
258+
259+
init(message: ChatMessage) {
260+
self.message = message
261+
_displayedText = State(initialValue: AttributedString(message.adjustedText))
262+
}
263+
264+
var body: some View {
265+
Text(displayedText)
266+
.onAppear {
267+
let attributedText = NSMutableAttributedString(
268+
string: text,
269+
attributes: [
270+
.foregroundColor: textColor(for: message),
271+
.font: fonts.body
272+
]
273+
)
274+
275+
linkDetector.links(in: text).forEach { textLink in
276+
attributedText.addAttribute(.link, value: textLink.url, range: textLink.range)
277+
}
278+
279+
self.displayedText = AttributedString(attributedText)
280+
}
281+
}
282+
}

0 commit comments

Comments
 (0)