|
| 1 | +import StreamVideo |
| 2 | +import StreamVideoSwiftUI |
| 3 | +import StreamVideoUIKit |
| 4 | +import SwiftUI |
| 5 | +import Combine |
| 6 | + |
| 7 | +@MainActor |
| 8 | +fileprivate func content() { |
| 9 | + container { |
| 10 | + struct LivestreamApp: App { |
| 11 | + @State var streamVideo: StreamVideo |
| 12 | + @State var call: Call |
| 13 | + |
| 14 | + init() { |
| 15 | + let streamVideo = StreamVideo( |
| 16 | + apiKey: apiKey, |
| 17 | + user: .init(id: "martin"), |
| 18 | + token: .empty |
| 19 | + ) |
| 20 | + let call = streamVideo.call(callType: "livestream", callId: "123") |
| 21 | + self.call = call |
| 22 | + self.streamVideo = streamVideo |
| 23 | + } |
| 24 | + |
| 25 | + var body: some Scene { |
| 26 | + WindowGroup { |
| 27 | + LivestreamView(state: call.state) |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + struct LivestreamView: View { |
| 33 | + |
| 34 | + @StateObject var state: CallState |
| 35 | + |
| 36 | + var body: some View { |
| 37 | + VStack { |
| 38 | + if state.backstage { |
| 39 | + backstageView |
| 40 | + } else if state.endedAt != nil { |
| 41 | + callEndedView |
| 42 | + } else { |
| 43 | + livestreamInfoView |
| 44 | + videoRendererView |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @ViewBuilder |
| 50 | + var backstageView: some View { |
| 51 | + if let startedAt = state.startsAt { |
| 52 | + Text("Livestream starting at \(startedAt.formatted())") |
| 53 | + } else { |
| 54 | + Text("Livestream starting soon") |
| 55 | + } |
| 56 | + if let session = state.session { |
| 57 | + let waitingCount = session.participants.filter({ $0.role != "host" }).count |
| 58 | + if waitingCount > 0 { |
| 59 | + Text("\(waitingCount) participants waiting") |
| 60 | + .font(.headline) |
| 61 | + .padding(.horizontal) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @State var recordings: [CallRecording]? |
| 67 | + |
| 68 | + @ViewBuilder |
| 69 | + var callEndedView: some View { |
| 70 | + Text("Call ended") |
| 71 | + .onAppear { |
| 72 | + if recordings == nil { |
| 73 | + Task { |
| 74 | + do { |
| 75 | + recordings = try await call.listRecordings() |
| 76 | + } catch { |
| 77 | + print("Error fetching recordings: \(error)") |
| 78 | + recordings = [] |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if let recordings, recordings.count > 0 { |
| 85 | + Text("Watch recordings:") |
| 86 | + ForEach(recordings, id: \.self) { recording in |
| 87 | + Button { |
| 88 | + if let url = URL(string: recording.url), UIApplication.shared.canOpenURL(url) { |
| 89 | + UIApplication.shared.open(url) |
| 90 | + } |
| 91 | + } label: { |
| 92 | + Text(recording.url) |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + @Injected(\.formatters.mediaDuration) private var formatter: MediaDurationFormatter |
| 99 | + |
| 100 | + @ViewBuilder |
| 101 | + var livestreamInfoView: some View { |
| 102 | + HStack { |
| 103 | + if let duration = formatter.format(state.duration) { |
| 104 | + Text("Live for \(duration)") |
| 105 | + .font(.headline) |
| 106 | + .padding(.horizontal) |
| 107 | + } |
| 108 | + |
| 109 | + Spacer() |
| 110 | + |
| 111 | + Text("Live \(state.participantCount)") |
| 112 | + .bold() |
| 113 | + .padding(.all, 4) |
| 114 | + .foregroundColor(.white) |
| 115 | + .background(Color.blue) |
| 116 | + .cornerRadius(8) |
| 117 | + .opacity(state.backstage ? 0 : 1) |
| 118 | + .padding(.horizontal) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @ViewBuilder |
| 123 | + var videoRendererView: some View { |
| 124 | + GeometryReader { reader in |
| 125 | + if let first = state.participants.first(where: { hostIds.contains($0.userId) }) { |
| 126 | + VideoRendererView(id: first.id, size: reader.size) { renderer in |
| 127 | + renderer.handleViewRendering(for: first) { size, participant in } |
| 128 | + } |
| 129 | + } else { |
| 130 | + Text("The host's video is not available") |
| 131 | + } |
| 132 | + } |
| 133 | + .padding() |
| 134 | + } |
| 135 | + |
| 136 | + var hostIds: [String] { |
| 137 | + state.members.filter { $0.role == "host" }.map(\.id) |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments