mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 16:10:20 +00:00
Refactor the RoomMessageEventStringBuilder to take a Style instead of a Destination
This make its usage clearer and makes different styles reusable
This commit is contained in:
committed by
Stefan Ceriu
parent
df7914d052
commit
823e2afa97
@@ -1332,7 +1332,8 @@ private struct ClientProxyServices {
|
|||||||
let roomListService = syncService.roomListService()
|
let roomListService = syncService.roomListService()
|
||||||
|
|
||||||
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(cacheKey: "roomList",
|
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(cacheKey: "roomList",
|
||||||
mentionBuilder: PlainMentionBuilder()), destination: .roomList)
|
mentionBuilder: PlainMentionBuilder()),
|
||||||
|
style: .senderPrefixed)
|
||||||
let eventStringBuilder = try RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: client.userId(), shouldDisambiguateDisplayNames: false),
|
let eventStringBuilder = try RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: client.userId(), shouldDisambiguateDisplayNames: false),
|
||||||
messageEventStringBuilder: roomMessageEventStringBuilder,
|
messageEventStringBuilder: roomMessageEventStringBuilder,
|
||||||
shouldDisambiguateDisplayNames: false,
|
shouldDisambiguateDisplayNames: false,
|
||||||
|
|||||||
@@ -34,14 +34,14 @@ struct RoomEventStringBuilder {
|
|||||||
case .message(let messageContent):
|
case .message(let messageContent):
|
||||||
return messageEventStringBuilder.buildAttributedString(for: messageContent.msgType, senderDisplayName: displayName, isOutgoing: isOutgoing)
|
return messageEventStringBuilder.buildAttributedString(for: messageContent.msgType, senderDisplayName: displayName, isOutgoing: isOutgoing)
|
||||||
case .sticker:
|
case .sticker:
|
||||||
if messageEventStringBuilder.destination == .pinnedEvent {
|
if messageEventStringBuilder.style == .typeBolded {
|
||||||
var string = AttributedString(L10n.commonSticker)
|
var string = AttributedString(L10n.commonSticker)
|
||||||
string.bold()
|
string.bold()
|
||||||
return string
|
return string
|
||||||
}
|
}
|
||||||
return prefix(L10n.commonSticker, with: displayName, isOutgoing: isOutgoing)
|
return prefix(L10n.commonSticker, with: displayName, isOutgoing: isOutgoing)
|
||||||
case .poll(let question, _, _, _, _, _, _):
|
case .poll(let question, _, _, _, _, _, _):
|
||||||
if messageEventStringBuilder.destination == .pinnedEvent {
|
if messageEventStringBuilder.style == .typeBolded {
|
||||||
let questionPlaceholder = "{question}"
|
let questionPlaceholder = "{question}"
|
||||||
var finalString = AttributedString(L10n.commonPollSummary(questionPlaceholder))
|
var finalString = AttributedString(L10n.commonPollSummary(questionPlaceholder))
|
||||||
finalString.bold()
|
finalString.bold()
|
||||||
@@ -106,10 +106,19 @@ struct RoomEventStringBuilder {
|
|||||||
|
|
||||||
static func pinnedEventStringBuilder(userID: String) -> Self {
|
static func pinnedEventStringBuilder(userID: String) -> Self {
|
||||||
RoomEventStringBuilder(stateEventStringBuilder: .init(userID: userID,
|
RoomEventStringBuilder(stateEventStringBuilder: .init(userID: userID,
|
||||||
shouldDisambiguateDisplayNames: false),
|
shouldDisambiguateDisplayNames: true),
|
||||||
messageEventStringBuilder: .init(attributedStringBuilder: AttributedStringBuilder(cacheKey: "pinnedEvents", mentionBuilder: PlainMentionBuilder()),
|
messageEventStringBuilder: .init(attributedStringBuilder: AttributedStringBuilder(cacheKey: "pinnedEvents", mentionBuilder: PlainMentionBuilder()),
|
||||||
destination: .pinnedEvent),
|
style: .typeBolded),
|
||||||
shouldDisambiguateDisplayNames: false,
|
shouldDisambiguateDisplayNames: true,
|
||||||
|
shouldPrefixSenderName: false)
|
||||||
|
}
|
||||||
|
|
||||||
|
static func threadListEventStringBuilder(userID: String) -> Self {
|
||||||
|
RoomEventStringBuilder(stateEventStringBuilder: .init(userID: userID,
|
||||||
|
shouldDisambiguateDisplayNames: true),
|
||||||
|
messageEventStringBuilder: .init(attributedStringBuilder: AttributedStringBuilder(cacheKey: "threadList", mentionBuilder: PlainMentionBuilder()),
|
||||||
|
style: .plain),
|
||||||
|
shouldDisambiguateDisplayNames: true,
|
||||||
shouldPrefixSenderName: false)
|
shouldPrefixSenderName: false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,20 +10,20 @@ import Foundation
|
|||||||
import MatrixRustSDK
|
import MatrixRustSDK
|
||||||
|
|
||||||
struct RoomMessageEventStringBuilder {
|
struct RoomMessageEventStringBuilder {
|
||||||
enum Destination {
|
enum Style {
|
||||||
|
/// Plain: no prefix, no special text treatment
|
||||||
|
/// Shown in push notifications and thread lists
|
||||||
|
case plain
|
||||||
/// Strings show on the room list as the last message
|
/// Strings show on the room list as the last message
|
||||||
/// The sender will be prefixed in bold
|
/// The sender will be prefixed in bold
|
||||||
case roomList
|
case senderPrefixed
|
||||||
/// Events pinned to the banner on the top of the timeline
|
/// Events pinned to the banner on the top of the timeline
|
||||||
/// The message type will be prefixed in bold
|
/// The message type will be prefixed in bold
|
||||||
case pinnedEvent
|
case typeBolded
|
||||||
/// Shown in push notifications
|
|
||||||
/// No prefix
|
|
||||||
case notification
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let attributedStringBuilder: AttributedStringBuilderProtocol
|
let attributedStringBuilder: AttributedStringBuilderProtocol
|
||||||
let destination: Destination
|
let style: Style
|
||||||
|
|
||||||
func buildAttributedString(for messageType: MessageType, senderDisplayName: String, isOutgoing: Bool) -> AttributedString {
|
func buildAttributedString(for messageType: MessageType, senderDisplayName: String, isOutgoing: Bool) -> AttributedString {
|
||||||
let message: AttributedString
|
let message: AttributedString
|
||||||
@@ -37,19 +37,19 @@ struct RoomMessageEventStringBuilder {
|
|||||||
case .audio(content: let content):
|
case .audio(content: let content):
|
||||||
let isVoiceMessage = content.voice != nil
|
let isVoiceMessage = content.voice != nil
|
||||||
var content = AttributedString(isVoiceMessage ? L10n.commonVoiceMessage : L10n.commonAudio)
|
var content = AttributedString(isVoiceMessage ? L10n.commonVoiceMessage : L10n.commonAudio)
|
||||||
if destination == .pinnedEvent {
|
if style == .typeBolded {
|
||||||
content.bold()
|
content.bold()
|
||||||
}
|
}
|
||||||
message = content
|
message = content
|
||||||
case .image(let content):
|
case .image(let content):
|
||||||
message = buildMessage(for: destination, caption: content.caption, type: L10n.commonImage)
|
message = buildMessage(for: style, caption: content.caption, type: L10n.commonImage)
|
||||||
case .video(let content):
|
case .video(let content):
|
||||||
message = buildMessage(for: destination, caption: content.caption, type: L10n.commonVideo)
|
message = buildMessage(for: style, caption: content.caption, type: L10n.commonVideo)
|
||||||
case .file(let content):
|
case .file(let content):
|
||||||
message = buildMessage(for: destination, caption: content.caption, type: L10n.commonFile)
|
message = buildMessage(for: style, caption: content.caption, type: L10n.commonFile)
|
||||||
case .location:
|
case .location:
|
||||||
var content = AttributedString(L10n.commonSharedLocation)
|
var content = AttributedString(L10n.commonSharedLocation)
|
||||||
if destination == .pinnedEvent {
|
if style == .typeBolded {
|
||||||
content.bold()
|
content.bold()
|
||||||
}
|
}
|
||||||
message = content
|
message = content
|
||||||
@@ -71,7 +71,7 @@ struct RoomMessageEventStringBuilder {
|
|||||||
message = AttributedString(body)
|
message = AttributedString(body)
|
||||||
}
|
}
|
||||||
|
|
||||||
if destination == .roomList {
|
if style == .senderPrefixed {
|
||||||
return prefix(message, with: isOutgoing ? L10n.commonYou : senderDisplayName)
|
return prefix(message, with: isOutgoing ? L10n.commonYou : senderDisplayName)
|
||||||
} else {
|
} else {
|
||||||
return message
|
return message
|
||||||
@@ -80,23 +80,23 @@ struct RoomMessageEventStringBuilder {
|
|||||||
|
|
||||||
func buildAttributedStringForLiveLocation(senderDisplayName: String, isOutgoing: Bool) -> AttributedString {
|
func buildAttributedStringForLiveLocation(senderDisplayName: String, isOutgoing: Bool) -> AttributedString {
|
||||||
var message = AttributedString(L10n.commonSharedLiveLocation)
|
var message = AttributedString(L10n.commonSharedLiveLocation)
|
||||||
if destination == .pinnedEvent {
|
if style == .typeBolded {
|
||||||
message.bold()
|
message.bold()
|
||||||
}
|
}
|
||||||
|
|
||||||
if destination == .roomList {
|
if style == .senderPrefixed {
|
||||||
return prefix(message, with: isOutgoing ? L10n.commonYou : senderDisplayName)
|
return prefix(message, with: isOutgoing ? L10n.commonYou : senderDisplayName)
|
||||||
} else {
|
} else {
|
||||||
return message
|
return message
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func buildMessage(for destination: Destination, caption: String?, type: String) -> AttributedString {
|
private func buildMessage(for style: Style, caption: String?, type: String) -> AttributedString {
|
||||||
guard let caption else {
|
guard let caption else {
|
||||||
return AttributedString(type)
|
return AttributedString(type)
|
||||||
}
|
}
|
||||||
|
|
||||||
if destination == .pinnedEvent {
|
if style == .typeBolded {
|
||||||
return prefix(AttributedString(caption), with: type)
|
return prefix(AttributedString(caption), with: type)
|
||||||
} else {
|
} else {
|
||||||
return AttributedString("\(type) - \(caption)")
|
return AttributedString("\(type) - \(caption)")
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class NotificationHandler {
|
|||||||
self.tag = tag
|
self.tag = tag
|
||||||
|
|
||||||
let eventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()),
|
let eventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()),
|
||||||
destination: .notification)
|
style: .plain)
|
||||||
|
|
||||||
notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: eventStringBuilder,
|
notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: eventStringBuilder,
|
||||||
notificationSoundName: settings.notificationSoundName.publisher.value,
|
notificationSoundName: settings.notificationSoundName.publisher.value,
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ struct NotificationContentBuilderTests {
|
|||||||
init() {
|
init() {
|
||||||
notificationContent = .init()
|
notificationContent = .init()
|
||||||
let stringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()),
|
let stringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(mentionBuilder: PlainMentionBuilder()),
|
||||||
destination: .notification)
|
style: .plain)
|
||||||
mediaProvider = MediaProviderMock(configuration: .init())
|
mediaProvider = MediaProviderMock(configuration: .init())
|
||||||
notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: stringBuilder,
|
notificationContentBuilder = NotificationContentBuilder(messageEventStringBuilder: stringBuilder,
|
||||||
notificationSoundName: UNNotificationSoundName("message.caf"),
|
notificationSoundName: UNNotificationSoundName("message.caf"),
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ struct RoomEventStringBuilderTests {
|
|||||||
|
|
||||||
stringBuilder = RoomEventStringBuilder(stateEventStringBuilder: stateEventStringBuilder,
|
stringBuilder = RoomEventStringBuilder(stateEventStringBuilder: stateEventStringBuilder,
|
||||||
messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: attributedStringBuilder,
|
messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: attributedStringBuilder,
|
||||||
destination: .roomList),
|
style: .senderPrefixed),
|
||||||
shouldDisambiguateDisplayNames: true,
|
shouldDisambiguateDisplayNames: true,
|
||||||
shouldPrefixSenderName: true)
|
shouldPrefixSenderName: true)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ final class RoomSummaryProviderTests {
|
|||||||
let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder())
|
let attributedStringBuilder = AttributedStringBuilder(mentionBuilder: MentionBuilder())
|
||||||
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: stateEventStringBuilder,
|
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: stateEventStringBuilder,
|
||||||
messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: attributedStringBuilder,
|
messageEventStringBuilder: RoomMessageEventStringBuilder(attributedStringBuilder: attributedStringBuilder,
|
||||||
destination: .roomList),
|
style: .senderPrefixed),
|
||||||
shouldDisambiguateDisplayNames: true,
|
shouldDisambiguateDisplayNames: true,
|
||||||
shouldPrefixSenderName: true)
|
shouldPrefixSenderName: true)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user