Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TrackReference #3

Merged
merged 4 commits into from
Feb 29, 2024
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
2 changes: 1 addition & 1 deletion Sources/LiveKitComponents/Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public let liveKitComponentsVersion = "0.0.1"

public typealias ComponentBuilder<Content: View> = () -> Content
public typealias ParticipantComponentBuilder<Content: View> = (_: Participant) -> Content
public typealias TrackPublicationComponentBuilder<Content: View> = (_: TrackPublication) -> Content
public typealias TrackReferenceComponentBuilder<Content: View> = (_: TrackReference) -> Content

public typealias ParticipantLayoutBuilder<Content: View> = (_ participant: Participant,
_ geometry: GeometryProxy) -> Content
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SwiftUI
/// - filter: Type of track to loop through, defaults to `.video`.
///
/// > Note: References `Participant` environment object.
public struct ForEachTrackPublication<Content: View>: View {
public struct ForEachTrack<Content: View>: View {
public enum Filter {
case all
case video
Expand All @@ -33,10 +33,10 @@ public struct ForEachTrackPublication<Content: View>: View {
@EnvironmentObject var participant: Participant

let filter: Filter
let content: TrackPublicationComponentBuilder<Content>
let content: TrackReferenceComponentBuilder<Content>

public init(filter: Filter = .video,
@ViewBuilder content: @escaping TrackPublicationComponentBuilder<Content>)
@ViewBuilder content: @escaping TrackReferenceComponentBuilder<Content>)
{
self.filter = filter
self.content = content
Expand All @@ -53,8 +53,10 @@ public struct ForEachTrackPublication<Content: View>: View {

public var body: some View {
ForEach(computedTrackPublications()) { trackPublication in
content(trackPublication)
.environmentObject(trackPublication)
let trackReference = TrackReference(participant: participant,
publication: trackPublication)
content(trackReference)
.environmentObject(trackReference)
}
}
}
51 changes: 51 additions & 0 deletions Sources/LiveKitComponents/TrackReference.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import LiveKit
import SwiftUI

open class TrackReference: ObservableObject {
public let participant: Participant
public let publication: TrackPublication?
public let name: String?
public let source: Track.Source?

public init(participant: Participant, publication: TrackPublication? = nil, name: String? = nil, source: Track.Source? = nil) {
self.participant = participant
self.publication = publication
self.name = name
self.source = source
}

/// Attempts to reseolve ``TrackPublication`` in order: publication, name, source.
public func resolve() -> TrackPublication? {
if let publication {
return publication
} else if let name, let source, let publication = participant.trackPublications.first(where: { $0.value.name == name && $0.value.source == source })?.value {
return publication
} else if let name, let publication = participant.trackPublications.first(where: { $0.value.name == name })?.value {
return publication
} else if let source, let publication = participant.trackPublications.first(where: { $0.value.source == source })?.value {
return publication
}

return nil
}

public var isResolvable: Bool {
resolve() != nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public struct ParticipantView: View {
public var body: some View {
GeometryReader { geometry in
ZStack(alignment: .topLeading) {
if let trackPublication = participant.firstCameraPublication {
VideoTrackPublicationView()
.environmentObject(trackPublication)
let cameraReference = TrackReference(participant: participant, source: .camera)

if cameraReference.isResolvable {
VideoTrackView()
.environmentObject(cameraReference)
} else {
ui.videoDisabledView(geometry: geometry)
}

if showInformation {
ParticipantInformationView()
.padding(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import LiveKit
import SwiftUI

public struct VideoTrackPublicationView: View {
@EnvironmentObject var trackPublication: TrackPublication
public struct VideoTrackView: View {
@EnvironmentObject var trackReference: TrackReference
@EnvironmentObject var ui: UIPreference

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

if let track = trackPublication.track as? VideoTrack,
if let trackPublication = trackReference.resolve(),
let track = trackPublication.track as? VideoTrack,
trackPublication.isSubscribed,
!trackPublication.isMuted
{
Expand Down