From 25dbf2c94bc5c5d2d8935ed173f389fd34613c3a Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Tue, 24 Oct 2023 16:27:02 +0900 Subject: [PATCH 1/3] wip --- .../LiveKitComponents/TrackReference.swift | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Sources/LiveKitComponents/TrackReference.swift diff --git a/Sources/LiveKitComponents/TrackReference.swift b/Sources/LiveKitComponents/TrackReference.swift new file mode 100644 index 0000000..98d290e --- /dev/null +++ b/Sources/LiveKitComponents/TrackReference.swift @@ -0,0 +1,49 @@ +/* + * Copyright 2023 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 SwiftUI +import LiveKit + +public struct TrackReference { + let participantSid: Sid? + let publicationSid: Sid + let source: Track.Source +} + +public struct TrackFinder: View { + + @EnvironmentObject var room: Room + + let reference: TrackReference + let foundBuilder: ComponentBuilder + let notFoundBuilder: ComponentBuilder + + public init(_ reference: TrackReference, + @ViewBuilder found: @escaping ComponentBuilder, + @ViewBuilder notFound: @escaping ComponentBuilder) { + + self.reference = reference + self.foundBuilder = found + self.notFoundBuilder = notFound + } + + public var body: some View { + + // TODO: Implement logic... + + AnyView(foundBuilder()) + } +} From d071cc2468aebff1bd04c0cadd8b2dcfc17fffbe Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Tue, 27 Feb 2024 02:42:09 +0900 Subject: [PATCH 2/3] update --- Sources/LiveKitComponents/Components.swift | 2 +- ...ckPublication.swift => ForEachTrack.swift} | 12 ++-- .../LiveKitComponents/TrackReference.swift | 56 ++++++++++--------- .../Views/Participant/ParticipantView.swift | 7 ++- .../VideoTrackPublicationView.swift | 5 +- 5 files changed, 45 insertions(+), 37 deletions(-) rename Sources/LiveKitComponents/ForEach/{ForEachTrackPublication.swift => ForEachTrack.swift} (78%) diff --git a/Sources/LiveKitComponents/Components.swift b/Sources/LiveKitComponents/Components.swift index 6e8af2b..4dfe160 100644 --- a/Sources/LiveKitComponents/Components.swift +++ b/Sources/LiveKitComponents/Components.swift @@ -21,7 +21,7 @@ public let liveKitComponentsVersion = "0.0.1" public typealias ComponentBuilder = () -> Content public typealias ParticipantComponentBuilder = (_: Participant) -> Content -public typealias TrackPublicationComponentBuilder = (_: TrackPublication) -> Content +public typealias TrackReferenceComponentBuilder = (_: TrackReference) -> Content public typealias ParticipantLayoutBuilder = (_ participant: Participant, _ geometry: GeometryProxy) -> Content diff --git a/Sources/LiveKitComponents/ForEach/ForEachTrackPublication.swift b/Sources/LiveKitComponents/ForEach/ForEachTrack.swift similarity index 78% rename from Sources/LiveKitComponents/ForEach/ForEachTrackPublication.swift rename to Sources/LiveKitComponents/ForEach/ForEachTrack.swift index fca0888..24e59f1 100644 --- a/Sources/LiveKitComponents/ForEach/ForEachTrackPublication.swift +++ b/Sources/LiveKitComponents/ForEach/ForEachTrack.swift @@ -23,7 +23,7 @@ import SwiftUI /// - filter: Type of track to loop through, defaults to `.video`. /// /// > Note: References `Participant` environment object. -public struct ForEachTrackPublication: View { +public struct ForEachTrack: View { public enum Filter { case all case video @@ -33,10 +33,10 @@ public struct ForEachTrackPublication: View { @EnvironmentObject var participant: Participant let filter: Filter - let content: TrackPublicationComponentBuilder + let content: TrackReferenceComponentBuilder public init(filter: Filter = .video, - @ViewBuilder content: @escaping TrackPublicationComponentBuilder) + @ViewBuilder content: @escaping TrackReferenceComponentBuilder) { self.filter = filter self.content = content @@ -53,8 +53,10 @@ public struct ForEachTrackPublication: 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) } } } diff --git a/Sources/LiveKitComponents/TrackReference.swift b/Sources/LiveKitComponents/TrackReference.swift index 98d290e..a27ad73 100644 --- a/Sources/LiveKitComponents/TrackReference.swift +++ b/Sources/LiveKitComponents/TrackReference.swift @@ -1,5 +1,5 @@ /* - * Copyright 2023 LiveKit + * 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. @@ -14,36 +14,38 @@ * limitations under the License. */ -import SwiftUI import LiveKit +import SwiftUI -public struct TrackReference { - let participantSid: Sid? - let publicationSid: Sid - let source: Track.Source -} - -public struct TrackFinder: View { - - @EnvironmentObject var room: Room - - let reference: TrackReference - let foundBuilder: ComponentBuilder - let notFoundBuilder: ComponentBuilder - - public init(_ reference: TrackReference, - @ViewBuilder found: @escaping ComponentBuilder, - @ViewBuilder notFound: @escaping ComponentBuilder) { - - self.reference = reference - self.foundBuilder = found - self.notFoundBuilder = notFound +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 } - public var body: some View { - - // TODO: Implement logic... + /// 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 + } - AnyView(foundBuilder()) + public var isResolvable: Bool { + resolve() != nil } } diff --git a/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift b/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift index 3cc76f4..4f708fa 100644 --- a/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift +++ b/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift @@ -30,12 +30,15 @@ public struct ParticipantView: View { public var body: some View { GeometryReader { geometry in ZStack(alignment: .topLeading) { - if let trackPublication = participant.firstCameraPublication { + let cameraReference = TrackReference(participant: participant, source: .camera) + + if cameraReference.isResolvable { VideoTrackPublicationView() - .environmentObject(trackPublication) + .environmentObject(cameraReference) } else { ui.videoDisabledView(geometry: geometry) } + if showInformation { ParticipantInformationView() .padding(5) diff --git a/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift b/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift index 520ec2b..1916214 100644 --- a/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift +++ b/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift @@ -18,7 +18,7 @@ import LiveKit import SwiftUI public struct VideoTrackPublicationView: View { - @EnvironmentObject var trackPublication: TrackPublication + @EnvironmentObject var trackReference: TrackReference @EnvironmentObject var ui: UIPreference var layoutMode: VideoView.LayoutMode = .fill @@ -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 { From 33ad1fc106f09562da4b0433d9b2d9f23b5fed37 Mon Sep 17 00:00:00 2001 From: Hiroshi Horie <548776+hiroshihorie@users.noreply.github.com> Date: Thu, 29 Feb 2024 19:50:03 +0900 Subject: [PATCH 3/3] rename to VideoTrackView --- .../LiveKitComponents/Views/Participant/ParticipantView.swift | 2 +- .../{VideoTrackPublicationView.swift => VideoTrackView.swift} | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename Sources/LiveKitComponents/Views/TrackPublication/{VideoTrackPublicationView.swift => VideoTrackView.swift} (96%) diff --git a/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift b/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift index 4f708fa..82e8fdf 100644 --- a/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift +++ b/Sources/LiveKitComponents/Views/Participant/ParticipantView.swift @@ -33,7 +33,7 @@ public struct ParticipantView: View { let cameraReference = TrackReference(participant: participant, source: .camera) if cameraReference.isResolvable { - VideoTrackPublicationView() + VideoTrackView() .environmentObject(cameraReference) } else { ui.videoDisabledView(geometry: geometry) diff --git a/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift b/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackView.swift similarity index 96% rename from Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift rename to Sources/LiveKitComponents/Views/TrackPublication/VideoTrackView.swift index 1916214..e1ac06c 100644 --- a/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackPublicationView.swift +++ b/Sources/LiveKitComponents/Views/TrackPublication/VideoTrackView.swift @@ -17,7 +17,7 @@ import LiveKit import SwiftUI -public struct VideoTrackPublicationView: View { +public struct VideoTrackView: View { @EnvironmentObject var trackReference: TrackReference @EnvironmentObject var ui: UIPreference