Skip to content

Commit

Permalink
Create seperate file for the view model
Browse files Browse the repository at this point in the history
  • Loading branch information
nuno-vieira committed Jan 13, 2025
1 parent d83eaac commit e46eadd
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,6 @@ struct ReactionsUsersView: View {
}
}

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
}
}

extension ChatMessageReaction: Identifiable {

public var id: String {
Expand Down
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
}
}
4 changes: 4 additions & 0 deletions StreamChatSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@
AD3AB65C2CB730090014D4D7 /* Shimmer.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65B2CB730090014D4D7 /* Shimmer.swift */; };
AD3AB65E2CB731360014D4D7 /* ChatThreadListLoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65D2CB731360014D4D7 /* ChatThreadListLoadingView.swift */; };
AD3AB6602CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD3AB65F2CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift */; };
AD6B7E052D356E8800ADEF39 /* ReactionsUsersViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = AD6B7E042D356E8800ADEF39 /* ReactionsUsersViewModel.swift */; };
ADE0F55E2CB838420053B8B9 /* ChatThreadListErrorBannerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F55D2CB838420053B8B9 /* ChatThreadListErrorBannerView.swift */; };
ADE0F5602CB846EC0053B8B9 /* FloatingBannerViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F55F2CB846EC0053B8B9 /* FloatingBannerViewModifier.swift */; };
ADE0F5622CB8556F0053B8B9 /* ChatThreadListFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ADE0F5612CB8556F0053B8B9 /* ChatThreadListFooterView.swift */; };
Expand Down Expand Up @@ -1107,6 +1108,7 @@
AD3AB65B2CB730090014D4D7 /* Shimmer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Shimmer.swift; sourceTree = "<group>"; };
AD3AB65D2CB731360014D4D7 /* ChatThreadListLoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListLoadingView.swift; sourceTree = "<group>"; };
AD3AB65F2CB7403C0014D4D7 /* ChatThreadListHeaderViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListHeaderViewModifier.swift; sourceTree = "<group>"; };
AD6B7E042D356E8800ADEF39 /* ReactionsUsersViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionsUsersViewModel.swift; sourceTree = "<group>"; };
ADE0F55D2CB838420053B8B9 /* ChatThreadListErrorBannerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListErrorBannerView.swift; sourceTree = "<group>"; };
ADE0F55F2CB846EC0053B8B9 /* FloatingBannerViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FloatingBannerViewModifier.swift; sourceTree = "<group>"; };
ADE0F5612CB8556F0053B8B9 /* ChatThreadListFooterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChatThreadListFooterView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1824,6 +1826,7 @@
8465FD212746A95600AF091E /* ReactionsOverlayViewModel.swift */,
8465FD252746A95600AF091E /* ReactionsHelperViews.swift */,
84E6EC26279B0C930017207B /* ReactionsUsersView.swift */,
AD6B7E042D356E8800ADEF39 /* ReactionsUsersViewModel.swift */,
846D6563279FF0800094B36E /* ReactionUserView.swift */,
);
path = Reactions;
Expand Down Expand Up @@ -2717,6 +2720,7 @@
842383E427678A4D00888CFC /* QuotedMessageView.swift in Sources */,
84289BE328071C7200282ABE /* ChatChannelInfoViewModel.swift in Sources */,
4F6D83512C1079A00098C298 /* AlertBannerViewModifier.swift in Sources */,
AD6B7E052D356E8800ADEF39 /* ReactionsUsersViewModel.swift in Sources */,
8465FD932746A95700AF091E /* PhotoAttachmentPickerView.swift in Sources */,
841B64C82774BA770016FF3B /* CommandsHandler.swift in Sources */,
8465FDC42746A95700AF091E /* ChatChannelListScreen.swift in Sources */,
Expand Down

0 comments on commit e46eadd

Please sign in to comment.