Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
13 changes: 13 additions & 0 deletions Sources/StreamVideo/Call.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<CallParticipant>]
) {
state.sortComparators = sortComparators
}

// MARK: - Internal

Expand Down
4 changes: 3 additions & 1 deletion Sources/StreamVideo/CallState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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] = []
Expand Down
21 changes: 21 additions & 0 deletions StreamVideoTests/Call/Call_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,27 @@ final class Call_Tests: StreamVideoTestCase {

XCTAssertEqual(mockCallController.timesJoinWasCalled, 1)
}

func test_call_customSorting() async throws {
// Given
let nameComparator: StreamSortComparator<CallParticipant> = {
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

Expand Down
14 changes: 14 additions & 0 deletions docusaurus/docs/iOS/03-guides/03-call-and-participant-state.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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<CallParticipant> = {
comparison($0, $1, keyPath: \.name)
}
let call = streamVideo.call(callType: callType, callId: callId)
call.updateParticipantsSorting(with: [nameComparator])
```

### Client State

```swift
Expand Down