@@ -289,65 +289,72 @@ public struct LinkDetectionTextView: View {
289
289
. font ( fonts. body)
290
290
. tint ( tintColor)
291
291
. onAppear {
292
- guard utils. messageListConfig. localLinkDetectionEnabled else { return }
293
- var attributes : [ NSAttributedString . Key : Any ] = [
294
- . foregroundColor: textColor ( for: message) ,
295
- . font: fonts. body
296
- ]
297
-
298
- let additional = utils. messageListConfig. messageDisplayOptions. messageLinkDisplayResolver ( message)
299
- for (key, value) in additional {
300
- if key == . foregroundColor, let value = value as? UIColor {
301
- tintColor = Color ( value)
302
- } else {
303
- attributes [ key] = value
304
- }
292
+ detectLinks ( for: message)
293
+ }
294
+ . onChange ( of: message, perform: { updated in
295
+ detectLinks ( for: updated)
296
+ } )
297
+ }
298
+
299
+ func detectLinks( for message: ChatMessage ) {
300
+ guard utils. messageListConfig. localLinkDetectionEnabled else { return }
301
+ var attributes : [ NSAttributedString . Key : Any ] = [
302
+ . foregroundColor: textColor ( for: message) ,
303
+ . font: fonts. body
304
+ ]
305
+
306
+ let additional = utils. messageListConfig. messageDisplayOptions. messageLinkDisplayResolver ( message)
307
+ for (key, value) in additional {
308
+ if key == . foregroundColor, let value = value as? UIColor {
309
+ tintColor = Color ( value)
310
+ } else {
311
+ attributes [ key] = value
305
312
}
306
-
307
- let attributedText = NSMutableAttributedString (
308
- string: message. adjustedText,
309
- attributes: attributes
310
- )
311
- let attributedTextString = attributedText. string
312
- var containsLinks = false
313
+ }
314
+
315
+ let attributedText = NSMutableAttributedString (
316
+ string: message. adjustedText,
317
+ attributes: attributes
318
+ )
319
+ let attributedTextString = attributedText. string
320
+ var containsLinks = false
313
321
314
- message. mentionedUsers. forEach { user in
315
- containsLinks = true
316
- let mention = " @ \( user. name ?? user. id) "
317
- attributedTextString
318
- . ranges ( of: mention, options: [ . caseInsensitive] )
319
- . map { NSRange ( $0, in: attributedTextString) }
320
- . forEach {
321
- let messageId = message. messageId. addingPercentEncoding ( withAllowedCharacters: . urlPathAllowed)
322
- if let messageId {
323
- attributedText. addAttribute ( . link, value: " getstream://mention/ \( messageId) / \( user. id) " , range: $0)
324
- }
322
+ message. mentionedUsers. forEach { user in
323
+ containsLinks = true
324
+ let mention = " @ \( user. name ?? user. id) "
325
+ attributedTextString
326
+ . ranges ( of: mention, options: [ . caseInsensitive] )
327
+ . map { NSRange ( $0, in: attributedTextString) }
328
+ . forEach {
329
+ let messageId = message. messageId. addingPercentEncoding ( withAllowedCharacters: . urlPathAllowed)
330
+ if let messageId {
331
+ attributedText. addAttribute ( . link, value: " getstream://mention/ \( messageId) / \( user. id) " , range: $0)
325
332
}
326
- }
327
-
328
- let range = NSRange ( location: 0 , length: message. adjustedText. utf16. count)
329
- linkDetector. links ( in: message. adjustedText) . forEach { textLink in
330
- let escapedOriginalText = NSRegularExpression . escapedPattern ( for: textLink. originalText)
331
- let pattern = " \\ [([^ \\ ]]+) \\ ] \\ ( \( escapedOriginalText) \\ ) "
332
- if let regex = try ? NSRegularExpression ( pattern: pattern) {
333
- containsLinks = ( regex. firstMatch (
334
- in: message. adjustedText,
335
- options: [ ] ,
336
- range: range
337
- ) == nil ) || !markdownEnabled
338
- } else {
339
- containsLinks = true
340
- }
341
-
342
- if !message. adjustedText. contains ( " ]( \( textLink. originalText) ) " ) {
343
- containsLinks = true
344
333
}
345
- attributedText. addAttribute ( . link, value: textLink. url, range: textLink. range)
334
+ }
335
+
336
+ let range = NSRange ( location: 0 , length: message. adjustedText. utf16. count)
337
+ linkDetector. links ( in: message. adjustedText) . forEach { textLink in
338
+ let escapedOriginalText = NSRegularExpression . escapedPattern ( for: textLink. originalText)
339
+ let pattern = " \\ [([^ \\ ]]+) \\ ] \\ ( \( escapedOriginalText) \\ ) "
340
+ if let regex = try ? NSRegularExpression ( pattern: pattern) {
341
+ containsLinks = ( regex. firstMatch (
342
+ in: message. adjustedText,
343
+ options: [ ] ,
344
+ range: range
345
+ ) == nil ) || !markdownEnabled
346
+ } else {
347
+ containsLinks = true
346
348
}
347
-
348
- if containsLinks {
349
- self . displayedText = AttributedString ( attributedText )
349
+
350
+ if !message . adjustedText . contains ( " ]( \( textLink . originalText ) ) " ) {
351
+ containsLinks = true
350
352
}
353
+ attributedText. addAttribute ( . link, value: textLink. url, range: textLink. range)
354
+ }
355
+
356
+ if containsLinks {
357
+ displayedText = AttributedString ( attributedText)
351
358
}
352
359
}
353
360
}
0 commit comments