Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added local link detection for text messages #445

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
Loading