Skip to content

Commit d071cc2

Browse files
committed
update
1 parent 020eac7 commit d071cc2

File tree

5 files changed

+45
-37
lines changed

5 files changed

+45
-37
lines changed

Sources/LiveKitComponents/Components.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public let liveKitComponentsVersion = "0.0.1"
2121

2222
public typealias ComponentBuilder<Content: View> = () -> Content
2323
public typealias ParticipantComponentBuilder<Content: View> = (_: Participant) -> Content
24-
public typealias TrackPublicationComponentBuilder<Content: View> = (_: TrackPublication) -> Content
24+
public typealias TrackReferenceComponentBuilder<Content: View> = (_: TrackReference) -> Content
2525

2626
public typealias ParticipantLayoutBuilder<Content: View> = (_ participant: Participant,
2727
_ geometry: GeometryProxy) -> Content

Sources/LiveKitComponents/ForEach/ForEachTrackPublication.swift renamed to Sources/LiveKitComponents/ForEach/ForEachTrack.swift

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftUI
2323
/// - filter: Type of track to loop through, defaults to `.video`.
2424
///
2525
/// > Note: References `Participant` environment object.
26-
public struct ForEachTrackPublication<Content: View>: View {
26+
public struct ForEachTrack<Content: View>: View {
2727
public enum Filter {
2828
case all
2929
case video
@@ -33,10 +33,10 @@ public struct ForEachTrackPublication<Content: View>: View {
3333
@EnvironmentObject var participant: Participant
3434

3535
let filter: Filter
36-
let content: TrackPublicationComponentBuilder<Content>
36+
let content: TrackReferenceComponentBuilder<Content>
3737

3838
public init(filter: Filter = .video,
39-
@ViewBuilder content: @escaping TrackPublicationComponentBuilder<Content>)
39+
@ViewBuilder content: @escaping TrackReferenceComponentBuilder<Content>)
4040
{
4141
self.filter = filter
4242
self.content = content
@@ -53,8 +53,10 @@ public struct ForEachTrackPublication<Content: View>: View {
5353

5454
public var body: some View {
5555
ForEach(computedTrackPublications()) { trackPublication in
56-
content(trackPublication)
57-
.environmentObject(trackPublication)
56+
let trackReference = TrackReference(participant: participant,
57+
publication: trackPublication)
58+
content(trackReference)
59+
.environmentObject(trackReference)
5860
}
5961
}
6062
}
+29-27
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 LiveKit
2+
* Copyright 2024 LiveKit
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,36 +14,38 @@
1414
* limitations under the License.
1515
*/
1616

17-
import SwiftUI
1817
import LiveKit
18+
import SwiftUI
1919

20-
public struct TrackReference {
21-
let participantSid: Sid?
22-
let publicationSid: Sid
23-
let source: Track.Source
24-
}
25-
26-
public struct TrackFinder<FoundView: View, NotFoundView: View>: View {
27-
28-
@EnvironmentObject var room: Room
29-
30-
let reference: TrackReference
31-
let foundBuilder: ComponentBuilder<FoundView>
32-
let notFoundBuilder: ComponentBuilder<NotFoundView>
33-
34-
public init(_ reference: TrackReference,
35-
@ViewBuilder found: @escaping ComponentBuilder<FoundView>,
36-
@ViewBuilder notFound: @escaping ComponentBuilder<NotFoundView>) {
37-
38-
self.reference = reference
39-
self.foundBuilder = found
40-
self.notFoundBuilder = notFound
20+
open class TrackReference: ObservableObject {
21+
public let participant: Participant
22+
public let publication: TrackPublication?
23+
public let name: String?
24+
public let source: Track.Source?
25+
26+
public init(participant: Participant, publication: TrackPublication? = nil, name: String? = nil, source: Track.Source? = nil) {
27+
self.participant = participant
28+
self.publication = publication
29+
self.name = name
30+
self.source = source
4131
}
4232

43-
public var body: some View {
44-
45-
// TODO: Implement logic...
33+
/// Attempts to reseolve ``TrackPublication`` in order: publication, name, source.
34+
public func resolve() -> TrackPublication? {
35+
if let publication {
36+
return publication
37+
} else if let name, let source, let publication = participant.trackPublications.first(where: { $0.value.name == name && $0.value.source == source })?.value {
38+
return publication
39+
} else if let name, let publication = participant.trackPublications.first(where: { $0.value.name == name })?.value {
40+
return publication
41+
} else if let source, let publication = participant.trackPublications.first(where: { $0.value.source == source })?.value {
42+
return publication
43+
}
44+
45+
return nil
46+
}
4647

47-
AnyView(foundBuilder())
48+
public var isResolvable: Bool {
49+
resolve() != nil
4850
}
4951
}

Sources/LiveKitComponents/Views/Participant/ParticipantView.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ public struct ParticipantView: View {
3030
public var body: some View {
3131
GeometryReader { geometry in
3232
ZStack(alignment: .topLeading) {
33-
if let trackPublication = participant.firstCameraPublication {
33+
let cameraReference = TrackReference(participant: participant, source: .camera)
34+
35+
if cameraReference.isResolvable {
3436
VideoTrackPublicationView()
35-
.environmentObject(trackPublication)
37+
.environmentObject(cameraReference)
3638
} else {
3739
ui.videoDisabledView(geometry: geometry)
3840
}
41+
3942
if showInformation {
4043
ParticipantInformationView()
4144
.padding(5)

Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import LiveKit
1818
import SwiftUI
1919

2020
public struct VideoTrackPublicationView: View {
21-
@EnvironmentObject var trackPublication: TrackPublication
21+
@EnvironmentObject var trackReference: TrackReference
2222
@EnvironmentObject var ui: UIPreference
2323

2424
var layoutMode: VideoView.LayoutMode = .fill
@@ -29,7 +29,8 @@ public struct VideoTrackPublicationView: View {
2929
ZStack {
3030
ui.videoDisabledView(geometry: geometry)
3131

32-
if let track = trackPublication.track as? VideoTrack,
32+
if let trackPublication = trackReference.resolve(),
33+
let track = trackPublication.track as? VideoTrack,
3334
trackPublication.isSubscribed,
3435
!trackPublication.isMuted
3536
{

0 commit comments

Comments
 (0)