-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create seperate file for the view model
- Loading branch information
1 parent
d83eaac
commit e46eadd
Showing
3 changed files
with
56 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Sources/StreamChatSwiftUI/ChatChannel/Reactions/ReactionsUsersViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Copyright © 2025 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import StreamChat | ||
import SwiftUI | ||
|
||
class ReactionsUsersViewModel: ObservableObject, ChatMessageControllerDelegate { | ||
@Published var reactions: [ChatMessageReaction] = [] | ||
|
||
var totalReactionsCount: Int { | ||
messageController?.message?.totalReactionsCount ?? 0 | ||
} | ||
|
||
var isRightAligned: Bool { | ||
messageController?.message?.isRightAligned == true | ||
} | ||
|
||
private var isLoading = false | ||
private let messageController: ChatMessageController? | ||
|
||
init(message: ChatMessage) { | ||
if let cid = message.cid { | ||
messageController = InjectedValues[\.chatClient].messageController( | ||
cid: cid, | ||
messageId: message.id | ||
) | ||
} else { | ||
messageController = nil | ||
} | ||
messageController?.delegate = self | ||
loadMoreReactions() | ||
} | ||
|
||
func loadMoreReactions() { | ||
guard let messageController = self.messageController else { | ||
return | ||
} | ||
guard !isLoading && messageController.hasLoadedAllReactions == false else { | ||
return | ||
} | ||
|
||
isLoading = true | ||
messageController.loadNextReactions { [weak self] _ in | ||
self?.isLoading = false | ||
} | ||
} | ||
|
||
func messageController(_ controller: ChatMessageController, didChangeReactions reactions: [ChatMessageReaction]) { | ||
self.reactions = reactions | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters