Skip to content

Commit 41d67a9

Browse files
committed
IOS-5351 Show multichat previews and sum counters across all chats in Space Hub
1 parent 38b12a9 commit 41d67a9

File tree

4 files changed

+48
-26
lines changed

4 files changed

+48
-26
lines changed

Anytype/Sources/PresentationLayer/Modules/SpaceHub/Helpers/ParticipantSpaceViewDataWithPreview.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@ import StoredHashMacro
44
@StoredHash
55
struct ParticipantSpaceViewDataWithPreview: Equatable, Identifiable, Hashable {
66
let space: ParticipantSpaceViewData
7-
let preview: ChatMessagePreview
8-
7+
let latestPreview: ChatMessagePreview
8+
let totalUnreadCounter: Int
9+
let totalMentionCounter: Int
10+
911
var id: String { space.id }
10-
12+
1113
var spaceView: SpaceView { space.spaceView }
12-
13-
var hasCounters: Bool { preview.unreadCounter > 0 || preview.mentionCounter > 0 }
14-
15-
func updated(preview: ChatMessagePreview) -> Self {
16-
ParticipantSpaceViewDataWithPreview(space: space, preview: preview)
14+
15+
var hasCounters: Bool { totalUnreadCounter > 0 || totalMentionCounter > 0 }
16+
17+
func updated(latestPreview: ChatMessagePreview, totalUnread: Int, totalMentions: Int) -> Self {
18+
ParticipantSpaceViewDataWithPreview(
19+
space: space,
20+
latestPreview: latestPreview,
21+
totalUnreadCounter: totalUnread,
22+
totalMentionCounter: totalMentions
23+
)
1724
}
1825
}
1926

2027
extension ParticipantSpaceViewDataWithPreview {
2128
init(space: ParticipantSpaceViewData) {
22-
self.init(space: space, preview: ChatMessagePreview(spaceId: space.id, chatId: space.spaceView.chatId))
29+
self.init(
30+
space: space,
31+
latestPreview: ChatMessagePreview(spaceId: space.id, chatId: space.spaceView.chatId),
32+
totalUnreadCounter: 0,
33+
totalMentionCounter: 0
34+
)
2335
}
2436
}

Anytype/Sources/PresentationLayer/Modules/SpaceHub/Helpers/SpaceHubSpacesStorage.swift

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,29 @@ actor SpaceHubSpacesStorage: SpaceHubSpacesStorageProtocol {
2626
).throttle(milliseconds: 300)
2727

2828
return combineStream.map { (spaces, previews) in
29-
var spaces = spaces.map { ParticipantSpaceViewDataWithPreview(space: $0) }
30-
31-
for preview in previews {
32-
if let spaceIndex = spaces.firstIndex(where: { $0.spaceView.targetSpaceId == preview.spaceId }) {
33-
let participantSpace = spaces[spaceIndex]
34-
35-
if participantSpace.spaceView.chatId == preview.chatId {
36-
spaces[spaceIndex] = spaces[spaceIndex].updated(preview: preview)
29+
spaces.map { space in
30+
let spacePreviews = previews.filter { $0.spaceId == space.spaceView.targetSpaceId }
31+
32+
guard let latestPreview = spacePreviews.max(by: { preview1, preview2 in
33+
guard let date1 = preview1.lastMessage?.createdAt,
34+
let date2 = preview2.lastMessage?.createdAt else {
35+
return preview1.lastMessage == nil
3736
}
37+
return date1 < date2
38+
}) else {
39+
return ParticipantSpaceViewDataWithPreview(space: space)
3840
}
41+
42+
let totalUnread = spacePreviews.reduce(0) { $0 + $1.unreadCounter }
43+
let totalMentions = spacePreviews.reduce(0) { $0 + $1.mentionCounter }
44+
45+
return ParticipantSpaceViewDataWithPreview(
46+
space: space,
47+
latestPreview: latestPreview,
48+
totalUnreadCounter: totalUnread,
49+
totalMentionCounter: totalMentions
50+
)
3951
}
40-
41-
return spaces
4252
}
4353
.removeDuplicates()
4454
.eraseToAnyAsyncSequence()

Anytype/Sources/PresentationLayer/Modules/SpaceHub/SpaceHubViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ final class SpaceHubViewModel {
148148
case (false, true):
149149
return false
150150
case (false, false):
151-
let lhsMessageDate = lhs.preview.lastMessage?.createdAt
152-
let rhsMessageDate = rhs.preview.lastMessage?.createdAt
151+
let lhsMessageDate = lhs.latestPreview.lastMessage?.createdAt
152+
let rhsMessageDate = rhs.latestPreview.lastMessage?.createdAt
153153
let lhsJoinDate = lhs.spaceView.joinDate
154154
let rhsJoinDate = rhs.spaceView.joinDate
155155

Anytype/Sources/PresentationLayer/Modules/SpaceHub/Subviews/SpaceCard/SpaceCardModelBuilder.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ final class SpaceCardModelBuilder: SpaceCardModelBuilderProtocol, Sendable {
3030
wallpapers: [String: SpaceWallpaperType]
3131
) -> SpaceCardModel {
3232
let spaceView = spaceData.spaceView
33-
let preview = spaceData.preview
33+
let latestPreview = spaceData.latestPreview
3434

35-
let lastMessage = preview.lastMessage.map { lastMessagePreview in
35+
let lastMessage = latestPreview.lastMessage.map { lastMessagePreview in
3636
let attachments = lastMessagePreview.attachments.prefix(3).map { objectDetails in
3737
SpaceCardLastMessageModel.Attachment(
3838
id: objectDetails.id,
@@ -62,9 +62,9 @@ final class SpaceCardModelBuilder: SpaceCardModelBuilderProtocol, Sendable {
6262
uxTypeName: spaceView.uxType.name,
6363
allNotificationsUnmuted: spaceView.pushNotificationMode.isUnmutedAll,
6464
lastMessage: lastMessage,
65-
unreadCounter: preview.unreadCounter,
66-
mentionCounter: preview.mentionCounter,
67-
hasCounters: preview.hasCounters,
65+
unreadCounter: spaceData.totalUnreadCounter,
66+
mentionCounter: spaceData.totalMentionCounter,
67+
hasCounters: spaceData.hasCounters,
6868
wallpaper: wallpapers[spaceView.targetSpaceId] ?? .default
6969
)
7070
}

0 commit comments

Comments
 (0)