From 4f10f1b5fea01fbc4d2465b659570fec7eec5c48 Mon Sep 17 00:00:00 2001 From: martinmitrevski Date: Tue, 18 Jun 2024 10:29:46 +0200 Subject: [PATCH 1/2] Added support for custom participant sorting in the Call object --- Sources/StreamVideo/Call.swift | 13 ++++++++++++ Sources/StreamVideo/CallState.swift | 4 +++- StreamVideoTests/Call/Call_Tests.swift | 21 +++++++++++++++++++ .../03-call-and-participant-state.mdx | 14 +++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Sources/StreamVideo/Call.swift b/Sources/StreamVideo/Call.swift index 2e4d242b0..f015caef8 100644 --- a/Sources/StreamVideo/Call.swift +++ b/Sources/StreamVideo/Call.swift @@ -1084,6 +1084,19 @@ public class Call: @unchecked Sendable, WSEventsSubscriber { reason: reason ) } + + // MARK: - Sorting + + /// Updates the sorting of call participants with the provided sort comparators. + /// + /// - Parameters: + /// - sortComparators: An array of `StreamSortComparator` objects for `CallParticipant`. + @MainActor + public func updateParticipantsSorting( + with sortComparators: [StreamSortComparator] + ) { + state.sortComparators = sortComparators + } // MARK: - Internal diff --git a/Sources/StreamVideo/CallState.swift b/Sources/StreamVideo/CallState.swift index 657f00471..9ae4eeeb1 100644 --- a/Sources/StreamVideo/CallState.swift +++ b/Sources/StreamVideo/CallState.swift @@ -98,6 +98,8 @@ public class CallState: ObservableObject { @Published public internal(set) var duration: TimeInterval = 0 @Published public internal(set) var statsReport: CallStatsReport? + var sortComparators = defaultComparators + private var localCallSettingsUpdate = false private var durationTimer: Foundation.Timer? @@ -349,7 +351,7 @@ public class CallState: ObservableObject { participants .compactMap { participantsMap[$0.id] } + newlyAddedParticipants ) - .sorted(by: defaultComparators) + .sorted(by: sortComparators) // Variables to hold segregated participants. var remoteParticipants: [CallParticipant] = [] diff --git a/StreamVideoTests/Call/Call_Tests.swift b/StreamVideoTests/Call/Call_Tests.swift index f979a3731..35a31d244 100644 --- a/StreamVideoTests/Call/Call_Tests.swift +++ b/StreamVideoTests/Call/Call_Tests.swift @@ -330,6 +330,27 @@ final class Call_Tests: StreamVideoTestCase { XCTAssertEqual(mockCallController.timesJoinWasCalled, 1) } + + func test_call_customSorting() async throws { + // Given + let nameComparator: StreamSortComparator = { + comparison($0, $1, keyPath: \.name) + } + let call = streamVideo?.call(callType: callType, callId: callId) + call?.updateParticipantsSorting(with: [nameComparator]) + + // When + call?.state.participantsMap = [ + "martin": .dummy(id: "martin", name: "Martin", isSpeaking: true), + "ilias": .dummy(id: "ilias", name: "Ilias", pin: PinInfo(isLocal: false, pinnedAt: Date())), + "alexey": .dummy(id: "alexey", name: "Alexey") + ] + + // Then + let participants = call?.state.participants + XCTAssertEqual(participants?[0].name, "Alexey") + XCTAssertEqual(participants?[1].name, "Ilias") + } // MARK: - Private helpers diff --git a/docusaurus/docs/iOS/03-guides/03-call-and-participant-state.mdx b/docusaurus/docs/iOS/03-guides/03-call-and-participant-state.mdx index 3770ccf97..d68640e05 100644 --- a/docusaurus/docs/iOS/03-guides/03-call-and-participant-state.mdx +++ b/docusaurus/docs/iOS/03-guides/03-call-and-participant-state.mdx @@ -99,6 +99,20 @@ The following fields are available on the participant: | `audioLevel` | The audio level for the user. | | `audioLevels` | A list of the last 10 audio levels. Convenient for audio visualizations. | +### Participants Sorting + +If you want to change the default sorting of the participants, you can use the `Call` object's method `updateParticipantsSorting`. + +Here's an example that will sort participants alphabetically, by their name: + +```swift +let nameComparator: StreamSortComparator = { + comparison($0, $1, keyPath: \.name) +} +let call = streamVideo.call(callType: callType, callId: callId) +call.updateParticipantsSorting(with: [nameComparator]) +``` + ### Client State ```swift From 168e8d6d89c2dba8e4a771fc687137573c8a9237 Mon Sep 17 00:00:00 2001 From: martinmitrevski Date: Tue, 18 Jun 2024 10:41:26 +0200 Subject: [PATCH 2/2] Updated the CHANGELOG --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb780e377..d89efcd82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming -### 🔄 Changed +### ✅ Added +- Support for custom participant sorting in the Call object. [#438](https://github.com/GetStream/stream-video-swift/pull/438) # [1.0.8](https://github.com/GetStream/stream-video-swift/releases/tag/1.0.8) _June 17, 2024_