|
| 1 | +// |
| 2 | +// Copyright © 2023 Stream.io Inc. All rights reserved. |
| 3 | +// |
| 4 | + |
| 5 | +import SnapshotTesting |
| 6 | +@testable import StreamChat |
| 7 | +@testable import StreamChatSwiftUI |
| 8 | +import SwiftUI |
| 9 | +import XCTest |
| 10 | + |
| 11 | +final class MessageViewMultiRowReactions_Tests: StreamChatTestCase { |
| 12 | + |
| 13 | + override public func setUp() { |
| 14 | + super.setUp() |
| 15 | + let reactionsTopPadding: (ChatMessage) -> CGFloat = { message in |
| 16 | + let padding: CGFloat = 8 |
| 17 | + var paddingOffset: CGFloat = 0 |
| 18 | + let rowSize: CGFloat = 24 |
| 19 | + let chunkSize: CGFloat = 2 |
| 20 | + let reactionsCount = CGFloat(message.reactionScores.count) |
| 21 | + let numberOfRows = ceil(reactionsCount / chunkSize) |
| 22 | + if numberOfRows > 1 { |
| 23 | + paddingOffset = (numberOfRows - 1) * padding |
| 24 | + } |
| 25 | + return numberOfRows * rowSize + paddingOffset |
| 26 | + } |
| 27 | + let messageDisplayOptions = MessageDisplayOptions(reactionsTopPadding: reactionsTopPadding) |
| 28 | + |
| 29 | + let utils = Utils( |
| 30 | + messageListConfig: MessageListConfig(messageDisplayOptions: messageDisplayOptions) |
| 31 | + ) |
| 32 | + streamChat = StreamChat(chatClient: chatClient, utils: utils) |
| 33 | + } |
| 34 | + |
| 35 | + func test_messageViewMultiRowReactions_snapshot() { |
| 36 | + // Given |
| 37 | + let viewFactory = TestViewFactory.shared |
| 38 | + let message = ChatMessage.mock( |
| 39 | + text: "Test message", |
| 40 | + reactionScores: [ |
| 41 | + .init(rawValue: "love"): 1, |
| 42 | + .init(rawValue: "like"): 1, |
| 43 | + .init(rawValue: "haha"): 1 |
| 44 | + ] |
| 45 | + ) |
| 46 | + let channel = ChatChannel.mockDMChannel() |
| 47 | + |
| 48 | + // When |
| 49 | + let view = MessageContainerView( |
| 50 | + factory: viewFactory, |
| 51 | + channel: channel, |
| 52 | + message: message, |
| 53 | + showsAllInfo: true, |
| 54 | + isInThread: false, |
| 55 | + isLast: true, |
| 56 | + scrolledId: .constant(nil), |
| 57 | + quotedMessage: .constant(nil), |
| 58 | + onLongPress: { _ in } |
| 59 | + ) |
| 60 | + .applyDefaultSize() |
| 61 | + |
| 62 | + // Then |
| 63 | + assertSnapshot(matching: view, as: .image) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +class TestViewFactory: ViewFactory { |
| 68 | + |
| 69 | + @Injected(\.chatClient) public var chatClient |
| 70 | + |
| 71 | + private init() {} |
| 72 | + |
| 73 | + public static let shared = TestViewFactory() |
| 74 | + |
| 75 | + func makeMessageReactionView( |
| 76 | + message: ChatMessage, |
| 77 | + onTapGesture: @escaping () -> Void, |
| 78 | + onLongPressGesture: @escaping () -> Void |
| 79 | + ) -> some View { |
| 80 | + CustomReactionsContainer(message: message, onTapGesture: onTapGesture, onLongPressGesture: onLongPressGesture) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +struct CustomReactionsContainer: View { |
| 85 | + |
| 86 | + @Injected(\.utils) var utils |
| 87 | + |
| 88 | + let chunkSize = 2 |
| 89 | + |
| 90 | + let message: ChatMessage |
| 91 | + var useLargeIcons = false |
| 92 | + var onTapGesture: () -> Void |
| 93 | + var onLongPressGesture: () -> Void |
| 94 | + |
| 95 | + var messageDisplayOptions: MessageDisplayOptions { |
| 96 | + utils.messageListConfig.messageDisplayOptions |
| 97 | + } |
| 98 | + |
| 99 | + var body: some View { |
| 100 | + GeometryReader { reader in |
| 101 | + Color.clear |
| 102 | + .overlay( |
| 103 | + ReactionsHStack(message: message) { |
| 104 | + CustomMessageReactionView( |
| 105 | + message: message, |
| 106 | + chunkSize: chunkSize, |
| 107 | + useLargeIcons: useLargeIcons, |
| 108 | + reactions: reactions, |
| 109 | + onReactionTap: { _ in } |
| 110 | + ) |
| 111 | + .onTapGesture { |
| 112 | + onTapGesture() |
| 113 | + } |
| 114 | + .onLongPressGesture { |
| 115 | + onLongPressGesture() |
| 116 | + } |
| 117 | + } |
| 118 | + .offset( |
| 119 | + x: offsetX, |
| 120 | + y: (-reader.size.height - offsetY) / 2 |
| 121 | + ) |
| 122 | + ) |
| 123 | + } |
| 124 | + .accessibilityElement(children: .contain) |
| 125 | + .accessibilityIdentifier("ReactionsContainer") |
| 126 | + } |
| 127 | + |
| 128 | + private var reactions: [MessageReactionType] { |
| 129 | + message.reactionScores.keys.filter { reactionType in |
| 130 | + (message.reactionScores[reactionType] ?? 0) > 0 |
| 131 | + } |
| 132 | + .sorted(by: { lhs, rhs in |
| 133 | + lhs.rawValue < rhs.rawValue |
| 134 | + }) |
| 135 | + } |
| 136 | + |
| 137 | + private var offsetY: CGFloat { |
| 138 | + let topPadding = utils.messageListConfig.messageDisplayOptions.reactionsTopPadding(message) |
| 139 | + let extraPadding: CGFloat = 10 |
| 140 | + return topPadding - extraPadding |
| 141 | + } |
| 142 | + |
| 143 | + private var reactionsSize: CGFloat { |
| 144 | + let entrySize = 32 |
| 145 | + var count = message.reactionScores.count |
| 146 | + if count > chunkSize { |
| 147 | + count = chunkSize |
| 148 | + } |
| 149 | + return CGFloat(count * entrySize) |
| 150 | + } |
| 151 | + |
| 152 | + private var offsetX: CGFloat { |
| 153 | + var offset = reactionsSize / 3 |
| 154 | + if message.reactionScores.count == 1 { |
| 155 | + offset = 16 |
| 156 | + } |
| 157 | + return message.isSentByCurrentUser ? -offset : offset |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +struct ReactionsRow: Identifiable { |
| 162 | + let id: Int // the row index |
| 163 | + let reactions: [MessageReactionType] |
| 164 | +} |
| 165 | + |
| 166 | +struct CustomMessageReactionView: View { |
| 167 | + |
| 168 | + @Injected(\.colors) private var colors |
| 169 | + @Injected(\.images) private var images |
| 170 | + |
| 171 | + let message: ChatMessage |
| 172 | + let chunkSize: Int |
| 173 | + var useLargeIcons = false |
| 174 | + var reactionsRows: [ReactionsRow] = [] |
| 175 | + var onReactionTap: (MessageReactionType) -> Void |
| 176 | + |
| 177 | + public init( |
| 178 | + message: ChatMessage, |
| 179 | + chunkSize: Int, |
| 180 | + useLargeIcons: Bool = false, |
| 181 | + reactions: [MessageReactionType], |
| 182 | + onReactionTap: @escaping (MessageReactionType) -> Void |
| 183 | + ) { |
| 184 | + self.message = message |
| 185 | + self.useLargeIcons = useLargeIcons |
| 186 | + self.chunkSize = chunkSize |
| 187 | + // self.reactionsRows = reactions.sorted().gridByRows().enumerated().map(ReactionsRow.init(id:reactions:)) |
| 188 | + self.onReactionTap = onReactionTap |
| 189 | + let chunks = reactions.chunks(chunkSize: chunkSize) |
| 190 | + for i in 0..<chunks.count { |
| 191 | + reactionsRows.append(ReactionsRow(id: i, reactions: chunks[i])) |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + var body: some View { |
| 196 | + |
| 197 | + VStack { |
| 198 | + ForEach(reactionsRows) { reactionsRow in |
| 199 | + HStack { |
| 200 | + ForEach(reactionsRow.reactions) { reaction in |
| 201 | + if let image = iconProvider(for: reaction) { |
| 202 | + Image(uiImage: image) |
| 203 | + .resizable() |
| 204 | + .scaledToFit() |
| 205 | + .foregroundColor(color(for: reaction)) |
| 206 | + .frame(width: useLargeIcons ? 25 : 20, height: useLargeIcons ? 27 : 20) |
| 207 | + .gesture( |
| 208 | + useLargeIcons ? |
| 209 | + TapGesture().onEnded { |
| 210 | + onReactionTap(reaction) |
| 211 | + } : nil |
| 212 | + ) |
| 213 | + .accessibilityIdentifier("reaction-\(reaction.id)") |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | + .padding(.all, 6) |
| 220 | + .padding(.horizontal, 4) |
| 221 | + .reactionsBubble(for: message) |
| 222 | + } |
| 223 | + |
| 224 | + private func iconProvider(for reaction: MessageReactionType) -> UIImage? { |
| 225 | + if useLargeIcons { |
| 226 | + return images.availableReactions[reaction]?.largeIcon |
| 227 | + } else { |
| 228 | + return images.availableReactions[reaction]?.smallIcon |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + private func color(for reaction: MessageReactionType) -> Color? { |
| 233 | + var colors = colors |
| 234 | + let containsUserReaction = userReactionIDs.contains(reaction) |
| 235 | + let color = containsUserReaction ? colors.reactionCurrentUserColor : colors.reactionOtherUserColor |
| 236 | + |
| 237 | + if let color = color { |
| 238 | + return Color(color) |
| 239 | + } else { |
| 240 | + return nil |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + private var userReactionIDs: Set<MessageReactionType> { |
| 245 | + Set(message.currentUserReactions.map(\.type)) |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +extension Array { |
| 250 | + |
| 251 | + func chunks(chunkSize: Int) -> [[Element]] { |
| 252 | + stride(from: 0, to: count, by: chunkSize).map { |
| 253 | + Array(self[$0..<Swift.min($0 + chunkSize, self.count)]) |
| 254 | + } |
| 255 | + } |
| 256 | +} |
0 commit comments