Skip to content

Commit

Permalink
Added local link detection for text messages (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinmitrevski authored Mar 6, 2024
1 parent 5ff3ce3 commit 9fd829f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 🔄 Changed
### ✅ Added
- Link detection in the text views

# [4.49.0](https://github.com/GetStream/stream-chat-swiftui/releases/tag/4.49.0)
_February 28, 2024_
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ public struct AttachmentTextView: View {

public var body: some View {
HStack {
Text(message.adjustedText)
.font(fonts.body)
StreamTextView(message: message)
.standardPadding()
.foregroundColor(textColor(for: message))
.fixedSize(horizontal: false, vertical: true)
Spacer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public struct MessageListConfig {
showNewMessagesSeparator: Bool = true,
handleTabBarVisibility: Bool = true,
messageListAlignment: MessageListAlignment = .standard,
uniqueReactionsEnabled: Bool = false
uniqueReactionsEnabled: Bool = false,
localLinkDetectionEnabled: Bool = true
) {
self.messageListType = messageListType
self.typingIndicatorPlacement = typingIndicatorPlacement
Expand All @@ -48,6 +49,7 @@ public struct MessageListConfig {
self.handleTabBarVisibility = handleTabBarVisibility
self.messageListAlignment = messageListAlignment
self.uniqueReactionsEnabled = uniqueReactionsEnabled
self.localLinkDetectionEnabled = localLinkDetectionEnabled
}

public let messageListType: MessageListType
Expand All @@ -69,6 +71,7 @@ public struct MessageListConfig {
public let handleTabBarVisibility: Bool
public let messageListAlignment: MessageListAlignment
public let uniqueReactionsEnabled: Bool
public let localLinkDetectionEnabled: Bool
}

/// Contains information about the message paddings.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,12 @@ public struct MessageTextView<Factory: ViewFactory>: View {
)
}

Text(message.adjustedText)
StreamTextView(message: message)
.padding(.leading, leadingPadding)
.padding(.trailing, trailingPadding)
.padding(.top, topPadding)
.padding(.bottom, bottomPadding)
.fixedSize(horizontal: false, vertical: true)
.foregroundColor(textColor(for: message))
.font(fonts.body)
}
.modifier(
factory.makeMessageViewModifier(
Expand Down Expand Up @@ -223,3 +221,62 @@ public struct EmojiTextView<Factory: ViewFactory>: View {
.accessibilityIdentifier("MessageTextView")
}
}

struct StreamTextView: View {

@Injected(\.utils) var utils
@Injected(\.fonts) var fonts

var message: ChatMessage

var body: some View {
if #available(iOS 15, *), utils.messageListConfig.localLinkDetectionEnabled {
LinkDetectionTextView(message: message)
} else {
Text(message.adjustedText)
.foregroundColor(textColor(for: message))
.font(fonts.body)
}
}
}

@available(iOS 15, *)
struct LinkDetectionTextView: View {

@Injected(\.colors) var colors
@Injected(\.fonts) var fonts

var message: ChatMessage

var text: String {
message.adjustedText
}

@State var displayedText: AttributedString

@State var linkDetector = TextLinkDetector()

init(message: ChatMessage) {
self.message = message
_displayedText = State(initialValue: AttributedString(message.adjustedText))
}

var body: some View {
Text(displayedText)
.onAppear {
let attributedText = NSMutableAttributedString(
string: text,
attributes: [
.foregroundColor: textColor(for: message),
.font: fonts.body
]
)

linkDetector.links(in: text).forEach { textLink in
attributedText.addAttribute(.link, value: textLink.url, range: textLink.range)
}

self.displayedText = AttributedString(attributedText)
}
}
}

0 comments on commit 9fd829f

Please sign in to comment.