Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DemoShare/DemoShareViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class DemoShareViewModel: ObservableObject, ChatChannelControllerDelegate {
chatClient.currentUserId
}

@Published var channels = LazyCachedMapCollection<ChatChannel>()
@Published var channels: [ChatChannel] = []
@Published var text = ""
@Published var images = [UIImage]()
@Published var selectedChannel: ChatChannel?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension ChatChannelController {
@Published public private(set) var channel: ChatChannel?

/// The messages related to the channel.
@Published public private(set) var messages: LazyCachedMapCollection<ChatMessage> = []
@Published public private(set) var messages: [ChatMessage] = []

/// The current state of the Controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
/// To observe changes of the messages, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
///
public var messages: LazyCachedMapCollection<ChatMessage> {
public var messages: [ChatMessage] {
if state == .initialized {
setLocalStateBasedOnError(startDatabaseObservers())
}
Expand Down Expand Up @@ -1697,7 +1697,7 @@ public class ChatChannelController: DataController, DelegateCallable, DataStoreP
public func getFirstUnreadMessageId(for channel: ChatChannel) -> MessageId? {
UnreadMessageLookup.firstUnreadMessageId(
in: channel,
messages: StreamCollection(messages),
messages: messages,
hasLoadedAllPreviousMessages: hasLoadedAllPreviousMessages,
currentUserId: client.currentUserId
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension ChatChannelListController {
public let controller: ChatChannelListController

/// The channels matching the query.
@Published public private(set) var channels: LazyCachedMapCollection<ChatChannel> = []
@Published public private(set) var channels: [ChatChannel] = []

/// The current state of the Controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class ChatChannelListController: DataController, DelegateCallable, DataSt
/// To observe changes of the channels, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
///
public var channels: LazyCachedMapCollection<ChatChannel> {
public var channels: [ChatChannel] {
startChannelListObserverIfNeeded()
return channelListObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension ChatChannelWatcherListController {
public let controller: ChatChannelWatcherListController

/// The channel members.
@Published public private(set) var watchers: LazyCachedMapCollection<ChatUser> = []
@Published public private(set) var watchers: [ChatUser] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class ChatChannelWatcherListController: DataController, DelegateCallable,
/// The channel watchers matching the query.
/// To observe the watcher list changes, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var watchers: LazyCachedMapCollection<ChatUser> {
public var watchers: [ChatUser] {
startObservingIfNeeded()
return watchersObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import CoreData
import Foundation

class BackgroundListDatabaseObserver<Item: Sendable, DTO: NSManagedObject>: BackgroundDatabaseObserver<Item, DTO>, @unchecked Sendable {
var items: LazyCachedMapCollection<Item> {
LazyCachedMapCollection(elements: rawItems)
var items: [Item] {
rawItems
}

init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension ChatChannelMemberListController {
public let controller: ChatChannelMemberListController

/// The channel members.
@Published public private(set) var members: LazyCachedMapCollection<ChatChannelMember> = []
@Published public private(set) var members: [ChatChannelMember] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ChatChannelMemberListController: DataController, DelegateCallable,
/// The channel members matching the query.
/// To observe the member list changes, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var members: LazyCachedMapCollection<ChatChannelMember> {
public var members: [ChatChannelMember] {
startObservingIfNeeded()
return memberListObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension ChatMessageController {
@Published public private(set) var message: ChatMessage?

/// The replies the message controller observes.
@Published public private(set) var replies: LazyCachedMapCollection<ChatMessage> = []
@Published public private(set) var replies: [ChatMessage] = []

/// The reactions the message controller observes.
@Published public private(set) var reactions: [ChatMessageReaction] = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class ChatMessageController: DataController, DelegateCallable, DataStoreP
/// To observe changes of the replies, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
///
public var replies: LazyCachedMapCollection<ChatMessage> {
public var replies: [ChatMessage] {
startObserversIfNeeded()
return repliesObserver?.items ?? []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class MessageReminderListController: DataController, DelegateCallable, Da
///
/// To observe changes of the reminders, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var reminders: LazyCachedMapCollection<MessageReminder> {
public var reminders: [MessageReminder] {
startMessageRemindersObserverIfNeeded()
return messageRemindersObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension PollController {
@Published public private(set) var poll: Poll?

/// The current user's votes.
@Published public private(set) var ownVotes: LazyCachedMapCollection<PollVote> = []
@Published public private(set) var ownVotes: [PollVote] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class PollController: DataController, DelegateCallable, DataStoreProvider
}

/// Returns the current user's votes.
public var ownVotes: LazyCachedMapCollection<PollVote> {
public var ownVotes: [PollVote] {
ownVotesObserver.items
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension PollVoteListController {
public let controller: PollVoteListController

/// The poll votes.
@Published public private(set) var votes: LazyCachedMapCollection<PollVote> = []
@Published public private(set) var votes: [PollVote] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class PollVoteListController: DataController, DelegateCallable, DataStore
///
/// To observe changes of the votes, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var votes: LazyCachedMapCollection<PollVote> {
public var votes: [PollVote] {
startPollVotesListObserverIfNeeded()
return pollVotesObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension ChatReactionListController {
public let controller: ChatReactionListController

/// The message reactions.
@Published public private(set) var reactions: LazyCachedMapCollection<ChatMessageReaction> = []
@Published public private(set) var reactions: [ChatMessageReaction] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ChatReactionListController: DataController, DelegateCallable, DataS
///
/// To observe changes of the reactions, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var reactions: LazyCachedMapCollection<ChatMessageReaction> {
public var reactions: [ChatMessageReaction] {
startReactionListObserverIfNeeded()
return reactionListObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension ChatMessageSearchController {
public let controller: ChatMessageSearchController

/// The current result of messages.
@Published public private(set) var messages: LazyCachedMapCollection<ChatMessage> = []
@Published public private(set) var messages: [ChatMessage] = []

/// The current state of the controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public class ChatMessageSearchController: DataController, DelegateCallable, Data
///
/// To observe changes of the messages, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
public var messages: LazyCachedMapCollection<ChatMessage> {
public var messages: [ChatMessage] {
startObserversIfNeeded()
return messagesObserver?.items ?? []
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension ChatThreadListController {
internal let controller: ChatThreadListController

/// The threads.
@Published internal private(set) var threads: LazyCachedMapCollection<ChatThread> = []
@Published internal private(set) var threads: [ChatThread] = []

/// The current state of the controller.
@Published internal private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class ChatThreadListController: DataController, DelegateCallable, DataSto
///
/// To observe changes of the threads, set your class as a delegate of this controller
/// or use the provided combine publishers.
public var threads: LazyCachedMapCollection<ChatThread> {
public var threads: [ChatThread] {
startThreadListObserverIfNeeded()
return threadListObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension ChatUserListController {
public let controller: ChatUserListController

/// The users matching the query.
@Published public private(set) var users: LazyCachedMapCollection<ChatUser> = []
@Published public private(set) var users: [ChatUser] = []

/// The current state of the Controller.
@Published public private(set) var state: DataController.State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ChatUserListController: DataController, DelegateCallable, DataStore
/// To observe changes of the users, set your class as a delegate of this controller or use the provided
/// `Combine` publishers.
///
public var users: LazyCachedMapCollection<ChatUser> {
public var users: [ChatUser] {
startUserListObserverIfNeeded()
return userListObserver.items
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ extension ChannelListState {
}

struct Handlers {
let channelsDidChange: @Sendable @MainActor (StreamCollection<ChatChannel>) async -> Void
let channelsDidChange: @Sendable @MainActor ([ChatChannel]) async -> Void
}

func start(with handlers: Handlers) -> StreamCollection<ChatChannel> {
func start(with handlers: Handlers) -> [ChatChannel] {
do {
channelListLinker.start(with: eventNotificationCenter)
return try channelListObserver.startObserving(didChange: handlers.channelsDidChange)
} catch {
log.error("Failed to start the channel list observer for query: \(query)")
return StreamCollection([])
return []
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StreamChat/StateLayer/ChannelListState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ import Foundation
public let query: ChannelListQuery

/// An array of channels for the specified ``ChannelListQuery``.
@Published public internal(set) var channels = StreamCollection<ChatChannel>([])
@Published public internal(set) var channels: [ChatChannel] = []
}
14 changes: 7 additions & 7 deletions Sources/StreamChat/StateLayer/ChatState+Observer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ extension ChatState {

struct Handlers {
let channelDidChange: @Sendable @MainActor (ChatChannel?) async -> Void
let membersDidChange: @Sendable @MainActor (StreamCollection<ChatChannelMember>) async -> Void
let messagesDidChange: @Sendable @MainActor (StreamCollection<ChatMessage>) async -> Void
let watchersDidChange: @Sendable @MainActor (StreamCollection<ChatUser>) async -> Void
let membersDidChange: @Sendable @MainActor ([ChatChannelMember]) async -> Void
let messagesDidChange: @Sendable @MainActor ([ChatMessage]) async -> Void
let watchersDidChange: @Sendable @MainActor ([ChatUser]) async -> Void
}

@MainActor func start(
with handlers: Handlers
) -> (
channel: ChatChannel?,
members: StreamCollection<ChatChannelMember>,
messages: StreamCollection<ChatMessage>,
watchers: StreamCollection<ChatUser>
members: [ChatChannelMember],
messages: [ChatMessage],
watchers: [ChatUser]
) {
memberListObserver = memberListState.$members
.dropFirst()
Expand All @@ -81,7 +81,7 @@ extension ChatState {
return (channel, memberListState.members, messages, watchers)
} catch {
log.error("Failed to start the observers for cid: \(cid) with error \(error)")
return (nil, StreamCollection([]), StreamCollection([]), StreamCollection([]))
return (nil, [], [], [])
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/StreamChat/StateLayer/ChatState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import Foundation
/// An array of loaded channel members.
///
/// Use load members in ``Chat`` for loading more members.
@Published public private(set) var members = StreamCollection<ChatChannelMember>([])
@Published public private(set) var members: [ChatChannelMember] = []

/// The sorting order for channel members (the default sorting is by created at in ascending order).
public let memberSorting: [Sorting<ChannelMemberListSortingKey>]
Expand All @@ -74,7 +74,7 @@ import Foundation
/// Messages are ordered by timestamp and ``messageOrder`` (In case of ``MessageOrdering/bottomToTop`` the list is sorted in ascending order).
///
/// Use load messages in ``Chat`` for loading more messages.
@Published public internal(set) var messages = StreamCollection<ChatMessage>([])
@Published public internal(set) var messages: [ChatMessage] = []

/// A Boolean value that returns whether the oldest messages have all been loaded or not.
public var hasLoadedAllOldestMessages: Bool {
Expand Down Expand Up @@ -161,7 +161,7 @@ import Foundation
/// An array of users who are currently watching the channel.
///
/// Use load watchers method in ``Chat`` for populating this array.
@Published public internal(set) var watchers = StreamCollection<ChatUser>([])
@Published public internal(set) var watchers: [ChatUser] = []
}

// MARK: - Internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ extension StateLayerDatabaseObserver where ResultType == ListResult {
)
}

var items: StreamCollection<Item> {
var collection: StreamCollection<Item>!
var items: [Item] {
var collection: [Item]!
context.performAndWait {
// When we already have loaded items, reuse them, otherwise refetch all
let items = reuseItems ?? updateItems(nil)
collection = StreamCollection(items)
collection = items
}
return collection
}
Expand All @@ -168,7 +168,7 @@ extension StateLayerDatabaseObserver where ResultType == ListResult {
/// - Parameter didChange: The callback which is triggered when the observed item changes. Runs on the ``MainActor``.
///
/// - Returns: Returns the current state of items in the local database.
func startObserving(didChange: @escaping @Sendable @MainActor (StreamCollection<Item>) async -> Void) throws -> StreamCollection<Item> where Item: Sendable {
func startObserving(didChange: @escaping @Sendable @MainActor ([Item]) async -> Void) throws -> [Item] where Item: Sendable {
try startObserving(onContextDidChange: { items, _ in
Task.mainActor { await didChange(items) }
})
Expand All @@ -181,12 +181,12 @@ extension StateLayerDatabaseObserver where ResultType == ListResult {
/// - Note: Use it if you need to do additional processing on the context's queue.
///
/// - Returns: Returns the current state of items in the local database.
@discardableResult func startObserving(onContextDidChange: @escaping (StreamCollection<Item>, [ListChange<Item>]) -> Void) throws -> StreamCollection<Item> {
@discardableResult func startObserving(onContextDidChange: @escaping ([Item], [ListChange<Item>]) -> Void) throws -> [Item] {
changeAggregator.onDidChange = { [weak self] changes in
guard let self else { return }
// Runs on the NSManagedObjectContext's queue, therefore skip performAndWait
let items = self.updateItems(changes)
onContextDidChange(StreamCollection(items), changes)
onContextDidChange(items, changes)
}
frc.delegate = changeAggregator
try frc.performFetch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ extension MemberListState {
}

struct Handlers {
let membersDidChange: @Sendable @MainActor (StreamCollection<ChatChannelMember>) async -> Void
let membersDidChange: @Sendable @MainActor ([ChatChannelMember]) async -> Void
}

func start(with handlers: Handlers) -> StreamCollection<ChatChannelMember> {
func start(with handlers: Handlers) -> [ChatChannelMember] {
do {
return try memberListObserver.startObserving(didChange: handlers.membersDidChange)
} catch {
log.error("Failed to start the member list observer with error \(error)")
return StreamCollection([])
return []
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StreamChat/StateLayer/MemberListState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import Foundation
public let query: ChannelMemberListQuery

/// An array of members for the specified ``ChannelMemberListQuery``.
@Published public private(set) var members = StreamCollection<ChatChannelMember>([])
@Published public private(set) var members: [ChatChannelMember] = []
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extension MessageSearchState {
}

struct Handlers {
let messagesDidChange: @Sendable @MainActor (StreamCollection<ChatMessage>) async -> Void
let messagesDidChange: @Sendable @MainActor ([ChatMessage]) async -> Void
}

private var handlers: Handlers?
Expand Down
Loading
Loading